2017-05-23 14:31:04 -07:00
|
|
|
<?php
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-05-23 14:31:04 -07:00
|
|
|
namespace App\Http\Transformers;
|
|
|
|
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Helpers\Helper;
|
2017-05-23 14:31:04 -07:00
|
|
|
use App\Models\Actionlog;
|
2017-08-25 18:40:20 -07:00
|
|
|
use App\Models\Setting;
|
2017-05-23 14:31:04 -07:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
|
|
|
|
class ActionlogsTransformer
|
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
public function transformActionlogs(Collection $actionlogs, $total)
|
2017-05-23 14:31:04 -07:00
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
$array = [];
|
2017-08-25 18:40:20 -07:00
|
|
|
$settings = Setting::getSettings();
|
2017-05-23 14:31:04 -07:00
|
|
|
foreach ($actionlogs as $actionlog) {
|
2017-08-25 18:40:20 -07:00
|
|
|
$array[] = self::transformActionlog($actionlog, $settings);
|
2017-05-23 14:31:04 -07:00
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-05-23 14:31:04 -07:00
|
|
|
return (new DatatablesTransformer)->transformDatatables($array, $total);
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
public function transformActionlog(Actionlog $actionlog, $settings = null)
|
2017-05-23 14:31:04 -07:00
|
|
|
{
|
2018-05-02 14:13:06 -07:00
|
|
|
$icon = $actionlog->present()->icon();
|
2021-06-10 13:15:52 -07:00
|
|
|
if ($actionlog->filename != '') {
|
2021-09-28 19:44:55 -07:00
|
|
|
$icon = e(Helper::filetype_icon($actionlog->filename));
|
2018-05-02 14:13:06 -07:00
|
|
|
}
|
2019-03-18 20:49:32 -07:00
|
|
|
|
|
|
|
// This is necessary since we can't escape special characters within a JSON object
|
2021-06-10 13:15:52 -07:00
|
|
|
if (($actionlog->log_meta) && ($actionlog->log_meta != '')) {
|
2019-03-18 20:49:32 -07:00
|
|
|
$meta_array = json_decode($actionlog->log_meta);
|
2019-09-03 14:02:08 -07:00
|
|
|
|
2020-01-24 17:31:43 -08:00
|
|
|
if ($meta_array) {
|
|
|
|
foreach ($meta_array as $key => $value) {
|
|
|
|
foreach ($value as $meta_key => $meta_value) {
|
|
|
|
if (is_array($meta_value)) {
|
|
|
|
foreach ($meta_value as $meta_value_key => $meta_value_value) {
|
|
|
|
$clean_meta[$key][$meta_value_key] = e($meta_value_value);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// This object stuff is weird, and is used to make up for the fact that
|
|
|
|
// older data can get strangely formatted if an asset existed,
|
|
|
|
// then a new custom field is added, and the asset is saved again.
|
|
|
|
// It can result in funnily-formatted strings like:
|
|
|
|
//
|
|
|
|
// {"_snipeit_right_sized_fault_tolerant_localareanetwo_1":
|
|
|
|
// {"old":null,"new":{"value":"1579490695972","_snipeit_new_field_2":2,"_snipeit_new_field_3":"Monday, 20 January 2020 2:24:55 PM"}}
|
|
|
|
// so we have to walk down that next level
|
|
|
|
|
|
|
|
if (is_object($meta_value)) {
|
|
|
|
foreach ($meta_value as $meta_value_key => $meta_value_value) {
|
|
|
|
if ($meta_value_key == 'value') {
|
|
|
|
$clean_meta[$key]['old'] = null;
|
|
|
|
$clean_meta[$key]['new'] = e($meta_value->value);
|
|
|
|
} else {
|
|
|
|
$clean_meta[$meta_value_key]['old'] = null;
|
|
|
|
$clean_meta[$meta_value_key]['new'] = e($meta_value_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$clean_meta[$key][$meta_key] = e($meta_value);
|
|
|
|
}
|
2019-09-03 14:02:08 -07:00
|
|
|
}
|
2020-01-24 17:31:43 -08:00
|
|
|
}
|
2019-03-18 20:49:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-18 21:09:57 -07:00
|
|
|
$array = [
|
2017-08-25 18:40:20 -07:00
|
|
|
'id' => (int) $actionlog->id,
|
2018-05-02 14:13:06 -07:00
|
|
|
'icon' => $icon,
|
2021-06-10 13:15:52 -07:00
|
|
|
'file' => ($actionlog->filename != '') ?
|
2018-05-02 14:13:06 -07:00
|
|
|
[
|
|
|
|
'url' => route('show/assetfile', ['assetId' => $actionlog->item->id, 'fileId' => $actionlog->id]),
|
|
|
|
'filename' => $actionlog->filename,
|
2021-09-28 19:44:55 -07:00
|
|
|
'inlineable' => (bool) Helper::show_file_inline($actionlog->filename),
|
2018-05-02 14:13:06 -07:00
|
|
|
] : null,
|
|
|
|
|
2017-05-23 14:31:04 -07:00
|
|
|
'item' => ($actionlog->item) ? [
|
|
|
|
'id' => (int) $actionlog->item->id,
|
2021-06-10 13:15:52 -07:00
|
|
|
'name' => ($actionlog->itemType() == 'user') ? $actionlog->filename : e($actionlog->item->getDisplayNameAttribute()),
|
2017-05-23 14:31:04 -07:00
|
|
|
'type' => e($actionlog->itemType()),
|
|
|
|
] : null,
|
2017-08-25 18:40:20 -07:00
|
|
|
'location' => ($actionlog->location) ? [
|
|
|
|
'id' => (int) $actionlog->location->id,
|
2021-06-10 13:15:52 -07:00
|
|
|
'name' => e($actionlog->location->name),
|
2017-08-25 18:40:20 -07:00
|
|
|
] : null,
|
2021-09-01 11:58:17 -07:00
|
|
|
'created_at' => Helper::getFormattedDateObject($actionlog->created_at, 'datetime'),
|
2017-05-23 14:31:04 -07:00
|
|
|
'updated_at' => Helper::getFormattedDateObject($actionlog->updated_at, 'datetime'),
|
2021-06-10 13:15:52 -07:00
|
|
|
'next_audit_date' => ($actionlog->itemType() == 'asset') ? Helper::getFormattedDateObject($actionlog->calcNextAuditDate(null, $actionlog->item), 'date') : null,
|
2017-08-25 18:40:20 -07:00
|
|
|
'days_to_next_audit' => $actionlog->daysUntilNextAudit($settings->audit_interval, $actionlog->item),
|
2017-05-23 14:31:04 -07:00
|
|
|
'action_type' => $actionlog->present()->actionType(),
|
|
|
|
'admin' => ($actionlog->user) ? [
|
|
|
|
'id' => (int) $actionlog->user->id,
|
|
|
|
'name' => e($actionlog->user->getFullNameAttribute()),
|
|
|
|
'first_name'=> e($actionlog->user->first_name),
|
2021-06-10 13:15:52 -07:00
|
|
|
'last_name'=> e($actionlog->user->last_name),
|
2017-05-23 14:31:04 -07:00
|
|
|
] : null,
|
|
|
|
'target' => ($actionlog->target) ? [
|
|
|
|
'id' => (int) $actionlog->target->id,
|
2021-06-10 13:15:52 -07:00
|
|
|
'name' => ($actionlog->targetType() == 'user') ? e($actionlog->target->getFullNameAttribute()) : e($actionlog->target->getDisplayNameAttribute()),
|
2017-05-23 14:31:04 -07:00
|
|
|
'type' => e($actionlog->targetType()),
|
|
|
|
] : null,
|
|
|
|
|
2017-09-28 15:13:05 -07:00
|
|
|
'note' => ($actionlog->note) ? e($actionlog->note): null,
|
2020-10-22 23:20:55 -07:00
|
|
|
'signature_file' => ($actionlog->accept_signature) ? route('log.signature.view', ['filename' => $actionlog->accept_signature ]) : null,
|
2019-03-18 20:49:32 -07:00
|
|
|
'log_meta' => ((isset($clean_meta)) && (is_array($clean_meta))) ? $clean_meta: null,
|
2021-09-01 11:58:17 -07:00
|
|
|
'action_date' => ($actionlog->action_date) ? Helper::getFormattedDateObject($actionlog->action_date, 'datetime'): Helper::getFormattedDateObject($actionlog->created_at, 'datetime'),
|
2017-05-23 14:31:04 -07:00
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
public function transformCheckedoutActionlog(Collection $accessories_users, $total)
|
2017-05-23 14:31:04 -07:00
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
$array = [];
|
2017-05-23 14:31:04 -07:00
|
|
|
foreach ($accessories_users as $user) {
|
|
|
|
$array[] = (new UsersTransformer)->transformUser($user);
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-05-23 14:31:04 -07:00
|
|
|
return (new DatatablesTransformer)->transformDatatables($array, $total);
|
|
|
|
}
|
|
|
|
}
|