mirror of
https://github.com/snipe/snipe-it.git
synced 2026-04-02 05:52:33 +00:00
78 lines
2.3 KiB
PHP
78 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Observers;
|
|
|
|
use App\Models\Actionlog;
|
|
use App\Models\Component;
|
|
|
|
class ComponentObserver
|
|
{
|
|
/**
|
|
* Listen to the User created event.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function updated(Component $component)
|
|
{
|
|
|
|
foreach ($component->getRawOriginal() as $key => $value) {
|
|
// Check and see if the value changed
|
|
if ($component->getRawOriginal()[$key] != $component->getAttributes()[$key]) {
|
|
$changed[$key]['old'] = $component->getRawOriginal()[$key];
|
|
$changed[$key]['new'] = $component->getAttributes()[$key];
|
|
}
|
|
}
|
|
|
|
if (count($changed) > 0) {
|
|
$logAction = new Actionlog;
|
|
$logAction->item_type = Component::class;
|
|
$logAction->item_id = $component->id;
|
|
$logAction->created_at = date('Y-m-d H:i:s');
|
|
$logAction->action_date = date('Y-m-d H:i:s');
|
|
$logAction->created_by = auth()->id();
|
|
$logAction->log_meta = json_encode($changed);
|
|
if ($component->imported) {
|
|
$logAction->setActionSource('importer');
|
|
}
|
|
$logAction->logaction('update');
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Listen to the Component created event when
|
|
* a new component is created.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function created(Component $component)
|
|
{
|
|
$logAction = new Actionlog;
|
|
$logAction->item_type = Component::class;
|
|
$logAction->item_id = $component->id;
|
|
$logAction->created_at = date('Y-m-d H:i:s');
|
|
$logAction->action_date = date('Y-m-d H:i:s');
|
|
$logAction->created_by = auth()->id();
|
|
if ($component->imported) {
|
|
$logAction->setActionSource('importer');
|
|
}
|
|
$logAction->logaction('create');
|
|
}
|
|
|
|
/**
|
|
* Listen to the Component deleting event.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function deleting(Component $component)
|
|
{
|
|
$logAction = new Actionlog;
|
|
$logAction->item_type = Component::class;
|
|
$logAction->item_id = $component->id;
|
|
$logAction->created_at = date('Y-m-d H:i:s');
|
|
$logAction->action_date = date('Y-m-d H:i:s');
|
|
$logAction->created_by = auth()->id();
|
|
$logAction->logaction('delete');
|
|
}
|
|
}
|