2017-06-15 20:54:14 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Observers;
|
|
|
|
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Models\Actionlog;
|
2017-06-15 20:54:14 -07:00
|
|
|
use App\Models\Asset;
|
|
|
|
use App\Models\Setting;
|
|
|
|
use Auth;
|
|
|
|
|
|
|
|
class AssetObserver
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Listen to the User created event.
|
|
|
|
*
|
|
|
|
* @param Asset $asset
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-07-08 03:38:39 -07:00
|
|
|
public function updating(Asset $asset)
|
2017-06-15 20:54:14 -07:00
|
|
|
{
|
2022-03-23 09:23:53 -07:00
|
|
|
$attributes = $asset->getAttributes();
|
2022-06-06 17:14:32 -07:00
|
|
|
$attributesOriginal = $asset->getRawOriginal();
|
|
|
|
|
2017-12-12 03:40:04 -08:00
|
|
|
// If the asset isn't being checked out or audited, log the update.
|
|
|
|
// (Those other actions already create log entries.)
|
2022-06-06 17:14:32 -07:00
|
|
|
if (($attributes['assigned_to'] == $attributesOriginal['assigned_to'])
|
|
|
|
&& ($attributes['checkout_counter'] == $attributesOriginal['checkout_counter'])
|
|
|
|
&& ($attributes['checkin_counter'] == $attributesOriginal['checkin_counter'])
|
2022-03-23 09:23:53 -07:00
|
|
|
&& ((isset( $attributes['next_audit_date']) ? $attributes['next_audit_date'] : null) == (isset($attributesOriginal['next_audit_date']) ? $attributesOriginal['next_audit_date']: null))
|
2022-06-06 20:00:05 -07:00
|
|
|
&& ($attributes['last_checkout'] == $attributesOriginal['last_checkout']))
|
2017-07-08 03:38:39 -07:00
|
|
|
{
|
2017-12-12 04:59:28 -08:00
|
|
|
$changed = [];
|
|
|
|
|
2022-06-06 17:14:32 -07:00
|
|
|
foreach ($asset->getRawOriginal() as $key => $value) {
|
|
|
|
if ($asset->getRawOriginal()[$key] != $asset->getAttributes()[$key]) {
|
|
|
|
$changed[$key]['old'] = $asset->getRawOriginal()[$key];
|
2017-12-12 04:59:28 -08:00
|
|
|
$changed[$key]['new'] = $asset->getAttributes()[$key];
|
|
|
|
}
|
2022-06-06 20:00:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($changed)){
|
|
|
|
return;
|
|
|
|
}
|
2017-12-12 04:59:28 -08:00
|
|
|
|
2017-07-08 03:38:39 -07:00
|
|
|
$logAction = new Actionlog();
|
|
|
|
$logAction->item_type = Asset::class;
|
|
|
|
$logAction->item_id = $asset->id;
|
2021-06-10 13:15:52 -07:00
|
|
|
$logAction->created_at = date('Y-m-d H:i:s');
|
2017-07-08 03:38:39 -07:00
|
|
|
$logAction->user_id = Auth::id();
|
2017-12-12 04:59:28 -08:00
|
|
|
$logAction->log_meta = json_encode($changed);
|
2017-07-08 03:38:39 -07:00
|
|
|
$logAction->logaction('update');
|
2021-06-10 13:15:52 -07:00
|
|
|
}
|
2017-06-15 20:54:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-10 13:15:52 -07:00
|
|
|
* Listen to the Asset created event, and increment
|
2017-06-15 20:54:14 -07:00
|
|
|
* the next_auto_tag_base value in the settings table when i
|
|
|
|
* a new asset is created.
|
|
|
|
*
|
|
|
|
* @param Asset $asset
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function created(Asset $asset)
|
|
|
|
{
|
2018-11-01 19:59:50 -07:00
|
|
|
if ($settings = Setting::getSettings()) {
|
2017-08-23 14:07:01 -07:00
|
|
|
$settings->increment('next_auto_tag_base');
|
2019-03-13 10:58:35 -07:00
|
|
|
$settings->save();
|
2017-08-23 14:07:01 -07:00
|
|
|
}
|
2017-06-15 20:54:14 -07:00
|
|
|
|
|
|
|
$logAction = new Actionlog();
|
|
|
|
$logAction->item_type = Asset::class;
|
|
|
|
$logAction->item_id = $asset->id;
|
2021-06-10 13:15:52 -07:00
|
|
|
$logAction->created_at = date('Y-m-d H:i:s');
|
2017-06-20 20:38:13 -07:00
|
|
|
$logAction->user_id = Auth::id();
|
2017-06-15 20:54:14 -07:00
|
|
|
$logAction->logaction('create');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Listen to the Asset deleting event.
|
|
|
|
*
|
|
|
|
* @param Asset $asset
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function deleting(Asset $asset)
|
|
|
|
{
|
|
|
|
$logAction = new Actionlog();
|
|
|
|
$logAction->item_type = Asset::class;
|
|
|
|
$logAction->item_id = $asset->id;
|
2021-06-10 13:15:52 -07:00
|
|
|
$logAction->created_at = date('Y-m-d H:i:s');
|
2017-06-20 20:38:13 -07:00
|
|
|
$logAction->user_id = Auth::id();
|
2017-06-15 20:54:14 -07:00
|
|
|
$logAction->logaction('delete');
|
|
|
|
}
|
|
|
|
}
|