Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ProcessMaker/Exception/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

use Exception;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Support\Facades\App;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
* Our general exception handler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public function show(Process $process, Dynaform $dynaform)
public function store(Process $process, Request $request)
{
$data = [
'DYN_TITLE' => $request->input('dyn_title', ''),
'DYN_DESCRIPTION' => $request->input('dyn_description', '')
'title' => $request->input('dyn_title', ''),
'description' => $request->input('dyn_description', '')
];
$data = array_merge($data, $this->formatData($request, ['dyn_content']));

Expand Down Expand Up @@ -98,7 +98,7 @@ public function remove(Process $process, Dynaform $dynaform)
{
$this->belongsToProcess($process, $dynaform);
DynaformManager::remove($dynaform);
return response([], 200);
return response([], 204);
}

/**
Expand All @@ -111,7 +111,7 @@ public function remove(Process $process, Dynaform $dynaform)
*/
private function belongsToProcess(Process $process, Dynaform $dynaform): void
{
if ($process->PRO_ID !== $dynaform->PRO_ID) {
if ($process->id !== $dynaform->process_id) {
Throw new DoesNotBelongToProcessException(__('The Dynaform does not belong to this process.'));
}
}
Expand All @@ -124,12 +124,12 @@ private function belongsToProcess(Process $process, Dynaform $dynaform): void
*
* @return array
*/
private function formatData(Request $request, array $fields)
private function formatData(Request $request, array $fields): array
{
$data = [];
foreach ($fields as $field) {
if ($request->has($field)) {
$data[strtoupper($field)] = $request->input($field);
$data[substr($field, 4)] = $request->input($field);
}
}
return $data;
Expand Down
32 changes: 15 additions & 17 deletions ProcessMaker/Managers/DynaformManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class DynaformManager
*/
public function index(Process $process): Paginator
{
return Dynaform::where('PRO_UID', $process->PRO_UID)->simplePaginate(20);
return Dynaform::where('process_id', $process->id)->simplePaginate(20);
}

/**
Expand All @@ -39,12 +39,11 @@ public function save(Process $process, $data): Dynaform
{
$this->validate($data);

$data['DYN_UID'] = str_replace('-', '', Uuid::uuid4());
$data['PRO_UID'] = $process->PRO_UID;
$data['PRO_ID'] = $process->PRO_ID;
$data['uid'] = Uuid::uuid4();
$data['process_id'] = $process->id;

if (!isset($data['DYN_CONTENT']) || empty($data['DYN_CONTENT'])) {
$data['DYN_CONTENT'] = $this->generateContent($data['DYN_UID'], $data['DYN_TITLE'], $data['DYN_DESCRIPTION']);
if (!isset($data['content']) || empty($data['content'])) {
$data['content'] = $this->generateContent($data['uid'], $data['title'], $data['description']);
}

$dynaform = new Dynaform();
Expand All @@ -66,23 +65,23 @@ public function save(Process $process, $data): Dynaform
public function copyImport(Process $process, $data): Dynaform
{
$this->validate($data);
$oldProcess = Process::where('PRO_UID', $data['COPY_IMPORT']['pro_uid'])->get();
$oldProcess = Process::where('uid', $data['COPY_IMPORT']['pro_uid'])->get();
if ($oldProcess->isEmpty()) {
throw new ModelNotFoundException(__('The process not exists.'));
}

$copyDynaform = Dynaform::where('DYN_UID', $data['COPY_IMPORT']['dyn_uid'])->get();
$copyDynaform = Dynaform::where('uid', $data['COPY_IMPORT']['dyn_uid'])->get();

if ($copyDynaform->isEmpty()) {
throw new ModelNotFoundException(__('The Dynaform not exists'));
}

if ($oldProcess->first()->PRO_ID !== $copyDynaform->first()->PRO_ID) {
if ($oldProcess->first()->id !== $copyDynaform->first()->process_id) {
Throw new DoesNotBelongToProcessException(__('The Dynaform does not belong to this process.'));
}

if (!isset($data['DYN_CONTENT'])) {
$data['DYN_CONTENT'] = $copyDynaform->first()->DYN_CONTENT;
if (!isset($data['content'])) {
$data['content'] = $copyDynaform->first()->content;
}

unset($data['COPY_IMPORT']);
Expand All @@ -102,12 +101,11 @@ public function copyImport(Process $process, $data): Dynaform
*/
public function update(Process $process, Dynaform $dynaform, $data): Dynaform
{
$data['PRO_UID'] = $process->PRO_UID;
$data['PRO_ID'] = $process->PRO_ID;
$data['process_id'] = $process->id;
$dynaform->fill($data);
$this->validate($dynaform->toArray());
if (empty($dynaform->DYN_CONTENT)) {
$dynaform->DYN_CONTENT = $this->generateContent($dynaform->DYN_UID, $dynaform->DYN_TITLE, $dynaform->DYN_DESCRIPTION);
if (empty($dynaform->content)) {
$dynaform->content = $this->generateContent($dynaform->uid, $dynaform->title, $dynaform->description);
}
$dynaform->saveOrFail();
return $dynaform;
Expand Down Expand Up @@ -147,8 +145,8 @@ private function validate($data): void
$data,
[
'COPY_IMPORT' => 'required|array',
'COPY_IMPORT.pro_uid' => 'required|string|max:32',
'COPY_IMPORT.dyn_uid' => 'required|string|max:32'
'COPY_IMPORT.pro_uid' => 'required|string|max:36',
'COPY_IMPORT.dyn_uid' => 'required|string|max:36'
]
);
if ($validator->fails()) {
Expand Down
100 changes: 46 additions & 54 deletions ProcessMaker/Model/Dynaform.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,113 +4,105 @@

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use ProcessMaker\Model\Traits\Uuid;
use Watson\Validating\ValidatingTrait;

/**
* Class dynaform
*
* @package ProcessMaker\Model
*
* @property int DYN_ID
* @property string DYN_UID
* @property int PRO_ID
* @property string PRO_UID
* @property string DYN_TITLE
* @property string DYN_DESCRIPTION
* @property array DYN_CONTENT
* @property string DYN_LABEL
* @property Carbon DYN_UPDATE_DATE
* @property int id
* @property string uid
* @property int process_id
* @property string title
* @property string description
* @property array content
* @property string label
* @property Carbon type
*
*/
class Dynaform extends Model
{
use ValidatingTrait;
use Uuid;

protected $table = 'DYNAFORM';
protected $primaryKey = 'DYN_ID';

const CREATED_AT = null;

/**
* The name of the "updated at" column.
*/
const UPDATED_AT = 'DYN_UPDATE_DATE';
protected $table = 'dynaform';

protected $fillable = [
'DYN_UID',
'PRO_ID',
'PRO_UID',
'DYN_TITLE',
'DYN_DESCRIPTION',
'DYN_CONTENT',
'DYN_LABEL',
'DYN_UPDATE_DATE'
'uid',
'title',
'description',
'content',
'label',
'type',
'created_at',
'updated_at',
'process_id',
];

protected $attributes = [
'DYN_UID' => null,
'PRO_ID' => '',
'PRO_UID' => null,
'DYN_TITLE' => null,
'DYN_DESCRIPTION' => null,
'DYN_CONTENT' => null,
'DYN_LABEL' => null,
'DYN_UPDATE_DATE' => null,
'uid' => null,
'process_id' => '',
'title' => null,
'description' => null,
'content' => null,
'label' => null,
'type' => 'form',
];

protected $casts = [
'DYN_UID' => 'string',
'PRO_ID' => 'int',
'PRO_UID' => 'string',
'DYN_TITLE' => 'string',
'DYN_DESCRIPTION' => 'string',
'DYN_CONTENT' => 'array',
'DYN_LABEL' => 'string',
'DYN_UPDATE_DATE' => 'string',
'uid' => 'string',
'process_id' => 'int',
'title' => 'string',
'description' => 'string',
'content' => 'array',
'label' => 'string',
'type' => 'string',
];

protected $rules = [
'DYN_UID' => 'required|max:32',
'PRO_ID' => 'required',
'PRO_UID' => 'required|max:32',
'DYN_TITLE' => 'required|unique:DYNAFORM,DYN_TITLE',
'uid' => 'required|max:36',
'process_id' => 'exists:processes,id',
'title' => 'required|unique:dynaform,title',
];

protected $validationMessages = [
'DYN_TITLE.unique' => 'A Dynaform with the same name already exists in this process.'
'title.unique' => 'A Dynaform with the same name already exists in this process.'
];

/**
* Get the route key for the model.
*
* @return string
*/
public function getRouteKeyName()
public function getRouteKeyName(): string
{
return 'DYN_UID';
return 'uid';
}

/**
* Accessor DYN_CONTENT to json
* Accessor content to json
*
* @param $value
*
* @return array|null
*/
public function getDynContentAttribute($value): ?array
public function getContentAttribute($value): ?array
{
return json_decode($value, true);
}

/**
* Mutator DYN_CONTENT json decode
* Mutator content json decode
*
* @param $value
*
* @return void
*/
public function setDynContentAttribute($value): void
public function setContentAttribute($value): void
{
$this->attributes['DYN_CONTENT'] = empty($value) ? null : json_encode($value);
$this->attributes['content'] = empty($value) ? null : json_encode($value);
}

}
14 changes: 7 additions & 7 deletions database/factories/DynaformFactory.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<?php

use Ramsey\Uuid\Uuid;
use Faker\Generator as Faker;
use ProcessMaker\Model\Dynaform;
use ProcessMaker\Model\Process;
use Ramsey\Uuid\Uuid;

/**
* Model factory for a Dynaform.
*/
$factory->define(Dynaform::class, function (Faker $faker) {
$pro = factory(Process::class)->create();
return [
'DYN_UID' => str_replace('-', '', Uuid::uuid4()),
'PRO_ID' => $pro->PRO_ID,
'PRO_UID' => $pro->PRO_UID,
'DYN_TITLE' => $faker->sentence(3),
'DYN_DESCRIPTION' => $faker->sentence(5)
'uid' => Uuid::uuid4(),
'process_id' => function () {
return factory(Process::class)->create()->id;
},
'title' => $faker->sentence(3),
'description' => $faker->sentence(5)
];
});
60 changes: 29 additions & 31 deletions database/migrations/2018_04_11_183825_create_DYNAFORM_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,37 @@
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateDYNAFORMTable extends Migration {
class CreateDYNAFORMTable extends Migration
{

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('DYNAFORM', function(Blueprint $table)
{
$table->string('DYN_UID', 32)->default('')->primary();
$table->text('DYN_TITLE', 16777215);
$table->text('DYN_DESCRIPTION', 16777215)->nullable();
$table->string('PRO_UID', 32)->default('0');
$table->string('DYN_TYPE', 20)->default('xmlform');
$table->string('DYN_FILENAME', 100)->default('');
$table->text('DYN_CONTENT', 16777215)->nullable();
$table->text('DYN_LABEL', 16777215)->nullable();
$table->integer('DYN_VERSION');
$table->dateTime('DYN_UPDATE_DATE')->nullable();
});
}
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('dynaform', function (Blueprint $table) {
$table->increments('id');
$table->uuid('uid')->unique();
$table->text('title', 16777215);
$table->text('description', 16777215)->nullable();
$table->string('type', 20)->default('form');
$table->text('content', 16777215)->nullable();
$table->text('label', 16777215)->nullable();
$table->timestamps();
});
}


/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('DYNAFORM');
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('dynaform');
}

}
Loading