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
9 changes: 5 additions & 4 deletions ProcessMaker/Events/ScreenCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}

Expand All @@ -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'] ?? '',
];
}

Expand Down
18 changes: 14 additions & 4 deletions ProcessMaker/Events/ScreenUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*
Expand All @@ -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));
}

/**
Expand All @@ -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));
}
}

Expand Down
12 changes: 6 additions & 6 deletions ProcessMaker/Events/ScriptCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}

/**
Expand All @@ -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']);
Expand Down
20 changes: 15 additions & 5 deletions ProcessMaker/Events/ScriptUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*
Expand All @@ -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));
}

/**
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion ProcessMaker/Http/Controllers/Api/ScreenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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);
Expand Down
13 changes: 10 additions & 3 deletions ProcessMaker/Http/Controllers/Api/ScriptController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/ProcessMaker/Helpers/ArrayHelperTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests\unit\ProcessMaker\Helpers;
namespace ProcessMaker\Helpers;

use ProcessMaker\Helpers\ArrayHelper;
use stdClass;
Expand Down