diff --git a/ProcessMaker/Events/ScreenCreated.php b/ProcessMaker/Events/ScreenCreated.php index 314bca13bd..dc20dbbdb6 100644 --- a/ProcessMaker/Events/ScreenCreated.php +++ b/ProcessMaker/Events/ScreenCreated.php @@ -21,9 +21,9 @@ public function __construct(array $newScreen) { $this->newScreen = $newScreen; - if (isset($newScreen['screen_category_id'])) { - $this->newScreen['screen_category'] = ScreenCategory::getNamesByIds($newScreen['screen_category_id']); - unset($this->newScreen['screen_category_id']); + if (isset($newScreen['tmp_screen_category_id'])) { + $this->newScreen['screen_category'] = ScreenCategory::getNamesByIds($newScreen['tmp_screen_category_id']); + unset($newScreen['tmp_screen_category_id']); } } @@ -36,9 +36,10 @@ public function getData(): array { return [ 'name' => $this->newScreen['title'] ?? '', + 'created_at' => $this->newScreen['created_at'] ?? '', 'description' => $this->newScreen['description'] ?? '', 'type' => $this->newScreen['type'] ?? '', - 'screen_category_id' => $this->newScreen['screen_category_id'] ?? '', + 'screen_category' => $this->newScreen['screen_category'] ?? '', ]; } diff --git a/ProcessMaker/Events/ScreenUpdated.php b/ProcessMaker/Events/ScreenUpdated.php index 808f530215..3657fcfd65 100644 --- a/ProcessMaker/Events/ScreenUpdated.php +++ b/ProcessMaker/Events/ScreenUpdated.php @@ -4,6 +4,7 @@ use Illuminate\Foundation\Events\Dispatchable; use ProcessMaker\Contracts\SecurityLogEventInterface; +use ProcessMaker\Helpers\ArrayHelper; use ProcessMaker\Models\Screen; use ProcessMaker\Models\ScreenCategory; use ProcessMaker\Traits\FormatSecurityLogChanges; @@ -19,6 +20,11 @@ class ScreenUpdated implements SecurityLogEventInterface private array $original; + public const REMOVE_KEYS = [ + 'screen_category_id', + 'tmp_screen_category_id' + ]; + /** * Create a new event instance. * @@ -31,10 +37,14 @@ public function __construct(Screen $screen, array $changes, array $original) $this->original = $original; // Get category name - $this->original['screen_category'] = isset($original['screen_category_id']) ? ScreenCategory::getNamesByIds($this->original['screen_category_id']) : ''; + $this->original['screen_category'] = isset($original['screen_category_id']) + ? ScreenCategory::getNamesByIds($this->original['screen_category_id']) + : ''; unset($this->original['screen_category_id']); - $this->changes['screen_category'] = isset($changes['screen_category_id']) ? ScreenCategory::getNamesByIds($this->changes['screen_category_id']) : ''; - unset($this->changes['screen_category_id']); + $this->changes['screen_category'] = isset($changes['tmp_screen_category_id']) + ? ScreenCategory::getNamesByIds($this->changes['tmp_screen_category_id']) + : ''; + $this->changes = array_diff_key($this->changes, array_flip($this::REMOVE_KEYS)); } /** @@ -51,7 +61,7 @@ public function getData(): array } else { return array_merge([ 'last_modified' => $this->screen->getAttribute('updated_at'), - ], $this->formatChanges($this->changes, $this->original)); + ], ArrayHelper::getArrayDifferencesWithFormat($this->changes, $this->original)); } } diff --git a/ProcessMaker/Events/ScriptCreated.php b/ProcessMaker/Events/ScriptCreated.php index 4cc96df059..b4ba029ac2 100644 --- a/ProcessMaker/Events/ScriptCreated.php +++ b/ProcessMaker/Events/ScriptCreated.php @@ -31,6 +31,10 @@ public function __construct(Script $script, array $changes) { $this->script = $script; $this->changes = $changes; + if (isset($this->changes['tmp_script_category_id'])) { + $this->categoryName = ScriptCategory::getNamesByIds($this->changes['tmp_script_category_id']); + unset($this->changes['tmp_script_category_id']); + } } /** @@ -52,18 +56,14 @@ public function getChanges(): array */ public function getData(): array { - $categoryId = $this->script['script_category_id'] ?? ''; - if (!empty($categoryId)) { - $categoryName = ScriptCategory::where('id', $categoryId)->value('name'); - } - $basic = isset($this->changes['code']) ? [ 'name' => $this->script->getAttribute('title'), 'created_at' => $this->script->getAttribute('created_at'), ] : [ 'name' => $this->script->getAttribute('title'), + 'created_at' => $this->script->getAttribute('created_at'), 'description' => $this->script->getAttribute('description'), - 'category' => $categoryName, + 'category' => $this->categoryName, 'language' => $this->script->getAttribute('language'), ]; unset($this->changes['code']); diff --git a/ProcessMaker/Events/ScriptUpdated.php b/ProcessMaker/Events/ScriptUpdated.php index 35c40bc34d..f2bcc7c121 100644 --- a/ProcessMaker/Events/ScriptUpdated.php +++ b/ProcessMaker/Events/ScriptUpdated.php @@ -4,6 +4,7 @@ use Illuminate\Foundation\Events\Dispatchable; use ProcessMaker\Contracts\SecurityLogEventInterface; +use ProcessMaker\Helpers\ArrayHelper; use ProcessMaker\Models\Script; use ProcessMaker\Models\ScriptCategory; use ProcessMaker\Traits\FormatSecurityLogChanges; @@ -19,6 +20,11 @@ class ScriptUpdated implements SecurityLogEventInterface private Script $script; + public const REMOVE_KEYS = [ + 'script_category_id', + 'tmp_script_category_id' + ]; + /** * Create a new event instance. * @@ -33,10 +39,14 @@ public function __construct(Script $script, array $changes, array $original) $this->original = $original; // Get category name - $this->original['script_category'] = isset($original['script_category_id']) ? ScriptCategory::getNamesByIds($this->original['script_category_id']) : ''; - unset($this->original['script_category_id']); - $this->changes['script_category'] = isset($changes['script_category_id']) ? ScriptCategory::getNamesByIds($this->changes['script_category_id']) : ''; - unset($this->changes['script_category_id']); + $this->original['script_category'] = isset($original['tmp_script_category_id']) + ? ScriptCategory::getNamesByIds($this->original['tmp_script_category_id']) + : ''; + $this->changes['script_category'] = isset($changes['tmp_script_category_id']) + ? ScriptCategory::getNamesByIds($this->changes['tmp_script_category_id']) + : ''; + $this->changes = array_diff_key($this->changes, array_flip($this::REMOVE_KEYS)); + $this->original = array_diff_key($this->original, array_flip($this::REMOVE_KEYS)); } /** @@ -70,7 +80,7 @@ public function getData(): array unset($changes['code']); unset($original['code']); - return array_merge($basic, $this->formatChanges($changes, $original)); + return array_merge($basic, ArrayHelper::getArrayDifferencesWithFormat($changes, $original)); } public function getEventName(): string diff --git a/ProcessMaker/Http/Controllers/Api/ScreenController.php b/ProcessMaker/Http/Controllers/Api/ScreenController.php index 133ace90d9..90b3a0309e 100644 --- a/ProcessMaker/Http/Controllers/Api/ScreenController.php +++ b/ProcessMaker/Http/Controllers/Api/ScreenController.php @@ -215,8 +215,9 @@ public function store(Request $request) $screen = new Screen(); $screen->fill($request->input()); $newScreen = $screen->fill($request->input()); - $screen->saveOrFail(); + //Creating temporary Key to store multiple id categories + $newScreen['tmp_screen_category_id'] = $request->input('screen_category_id'); //Call event to store New Screen data in LOG ScreenCreated::dispatch($newScreen->getAttributes()); @@ -265,6 +266,8 @@ public function update(Screen $screen, Request $request) //Call event to store Screen Changes into Log $request->validate(Screen::rules($screen)); $changes = $screen->getChanges(); + //Creating temporary Key to store multiple id categories + $changes['tmp_screen_category_id'] = $request->input('screen_category_id'); ScreenUpdated::dispatch($screen, $changes, $original); return response([], 204); diff --git a/ProcessMaker/Http/Controllers/Api/ScriptController.php b/ProcessMaker/Http/Controllers/Api/ScriptController.php index 0dd0f1f647..dd6fb22b48 100644 --- a/ProcessMaker/Http/Controllers/Api/ScriptController.php +++ b/ProcessMaker/Http/Controllers/Api/ScriptController.php @@ -332,7 +332,10 @@ public function store(Request $request) $script->fill($request->input()); $script->saveOrFail(); - ScriptCreated::dispatch($script, $script->getChanges()); + $changes = $script->getChanges(); + //Creating temporary Key to store multiple id categories + $changes['tmp_script_category_id'] = $request->input('script_category_id'); + ScriptCreated::dispatch($script, $changes); return new ScriptResource($script); } @@ -373,11 +376,15 @@ public function store(Request $request) public function update(Script $script, Request $request) { $request->validate(Script::rules($script)); - $script->fill($request->input()); $original = array_intersect_key($script->getOriginal(), $script->getDirty()); + //Creating temporary Key to store multiple id categories + $original['tmp_script_category_id'] = $script->script_category_id; $script->saveOrFail(); - ScriptUpdated::dispatch($script, $script->getChanges(), $original); + $changes = $script->getChanges(); + //Creating temporary Key to store multiple id categories + $changes['tmp_script_category_id'] = $request->input('script_category_id'); + ScriptUpdated::dispatch($script, $changes, $original); return response($request, 204); } diff --git a/tests/unit/ProcessMaker/Helpers/ArrayHelperTest.php b/tests/unit/ProcessMaker/Helpers/ArrayHelperTest.php index e0a9ce0880..b477188b29 100644 --- a/tests/unit/ProcessMaker/Helpers/ArrayHelperTest.php +++ b/tests/unit/ProcessMaker/Helpers/ArrayHelperTest.php @@ -1,6 +1,6 @@