2017-05-23 14:31:04 -07:00
|
|
|
<?php
|
|
|
|
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;
|
2023-08-22 12:34:45 -07:00
|
|
|
use App\Models\CustomField;
|
2017-08-25 18:40:20 -07:00
|
|
|
use App\Models\Setting;
|
2023-08-17 16:14:01 -07:00
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\Supplier;
|
|
|
|
use App\Models\Location;
|
|
|
|
use App\Models\AssetModel;
|
2017-05-23 14:31:04 -07:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2023-10-13 11:32:09 -07:00
|
|
|
use Illuminate\Contracts\Encryption\DecryptException;
|
|
|
|
use Illuminate\Support\Facades\Crypt;
|
2017-05-23 14:31:04 -07:00
|
|
|
|
|
|
|
class ActionlogsTransformer
|
|
|
|
{
|
|
|
|
|
|
|
|
public function transformActionlogs (Collection $actionlogs, $total)
|
|
|
|
{
|
|
|
|
$array = 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
|
|
|
}
|
|
|
|
return (new DatatablesTransformer)->transformDatatables($array, $total);
|
|
|
|
}
|
|
|
|
|
2022-02-15 18:29:45 -08:00
|
|
|
private function clean_field($value)
|
|
|
|
{
|
2022-03-03 15:48:04 -08:00
|
|
|
// 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
|
2022-02-15 18:29:45 -08:00
|
|
|
if(is_object($value) && isset($value->value)) {
|
2022-03-03 18:17:29 -08:00
|
|
|
return $this->clean_field($value->value);
|
2022-02-15 18:29:45 -08:00
|
|
|
}
|
|
|
|
return is_scalar($value) || is_null($value) ? e($value) : e(json_encode($value));
|
|
|
|
}
|
|
|
|
|
2017-08-25 18:40:20 -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();
|
2023-09-27 19:00:49 -07:00
|
|
|
$custom_fields = CustomField::all();
|
|
|
|
|
2018-05-02 14:13:06 -07:00
|
|
|
if ($actionlog->filename!='') {
|
2023-09-27 19:00:49 -07:00
|
|
|
$icon = 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
|
|
|
|
if (($actionlog->log_meta) && ($actionlog->log_meta!='')) {
|
|
|
|
$meta_array = json_decode($actionlog->log_meta);
|
2019-09-03 14:02:08 -07:00
|
|
|
|
2023-08-24 10:40:44 -07:00
|
|
|
$clean_meta = [];
|
|
|
|
|
2020-01-24 17:31:43 -08:00
|
|
|
if ($meta_array) {
|
2023-09-27 19:00:49 -07:00
|
|
|
|
2022-02-15 18:29:45 -08:00
|
|
|
foreach ($meta_array as $fieldname => $fieldata) {
|
2023-09-27 19:00:49 -07:00
|
|
|
|
|
|
|
$clean_meta[$fieldname]['old'] = $this->clean_field($fieldata->old);
|
|
|
|
$clean_meta[$fieldname]['new'] = $this->clean_field($fieldata->new);
|
|
|
|
|
|
|
|
// this is a custom field
|
|
|
|
if (str_starts_with($fieldname, '_snipeit_')) {
|
|
|
|
|
|
|
|
foreach ($custom_fields as $custom_field) {
|
|
|
|
|
|
|
|
if ($custom_field->db_column == $fieldname) {
|
|
|
|
|
2023-10-13 11:32:09 -07:00
|
|
|
if ($custom_field->field_encrypted == '1') {
|
|
|
|
|
|
|
|
// Unset these fields. We need to decrypt them, since even if the decrypted value
|
|
|
|
// didn't change, their value in the DB will, so we have to compare the unencrypted version
|
|
|
|
// to see if the values actually did change
|
|
|
|
unset($clean_meta[$fieldname]);
|
|
|
|
unset($clean_meta[$fieldname]);
|
|
|
|
|
|
|
|
$enc_old = '';
|
|
|
|
$enc_new = '';
|
|
|
|
|
|
|
|
try {
|
|
|
|
$enc_old = \Crypt::decryptString($this->clean_field($fieldata->old));
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
\Log::debug('Could not decrypt field - maybe the key changed?');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$enc_new = \Crypt::decryptString($this->clean_field($fieldata->new));
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
\Log::debug('Could not decrypt field - maybe the key changed?');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($enc_old != $enc_new) {
|
|
|
|
\Log::debug('custom fields do not match');
|
|
|
|
$clean_meta[$fieldname]['old'] = "************";
|
|
|
|
$clean_meta[$fieldname]['new'] = "************";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-27 19:00:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-08-22 12:34:45 -07:00
|
|
|
}
|
|
|
|
}
|
2023-09-27 19:00:49 -07:00
|
|
|
|
2019-03-18 20:49:32 -07:00
|
|
|
}
|
2023-08-24 08:15:07 -07:00
|
|
|
|
2023-08-17 16:14:01 -07:00
|
|
|
}
|
2023-08-17 16:14:01 -07:00
|
|
|
$clean_meta= $this->changedInfo($clean_meta);
|
2019-03-18 20:49:32 -07:00
|
|
|
}
|
|
|
|
|
2022-05-25 16:53:17 -07:00
|
|
|
$file_url = '';
|
2022-05-25 16:50:32 -07:00
|
|
|
if($actionlog->filename!='') {
|
2022-07-12 15:59:03 -07:00
|
|
|
if ($actionlog->action_type == 'accepted') {
|
2022-05-25 16:53:17 -07:00
|
|
|
$file_url = route('log.storedeula.download', ['filename' => $actionlog->filename]);
|
2022-05-25 16:50:32 -07:00
|
|
|
} else {
|
2023-02-07 09:26:49 -08:00
|
|
|
if ($actionlog->item) {
|
|
|
|
if ($actionlog->itemType() == 'asset') {
|
|
|
|
$file_url = route('show/assetfile', ['assetId' => $actionlog->item->id, 'fileId' => $actionlog->id]);
|
|
|
|
} elseif ($actionlog->itemType() == 'license') {
|
|
|
|
$file_url = route('show.licensefile', ['licenseId' => $actionlog->item->id, 'fileId' => $actionlog->id]);
|
|
|
|
} elseif ($actionlog->itemType() == 'user') {
|
|
|
|
$file_url = route('show/userfile', ['userId' => $actionlog->item->id, 'fileId' => $actionlog->id]);
|
|
|
|
}
|
2022-05-25 16:50:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-18 20:49:32 -07:00
|
|
|
|
2022-05-25 16:50:32 -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,
|
2022-05-17 07:01:33 -07:00
|
|
|
'file' => ($actionlog->filename!='')
|
|
|
|
?
|
2018-05-02 14:13:06 -07:00
|
|
|
[
|
2022-05-25 16:53:17 -07:00
|
|
|
'url' => $file_url,
|
2018-05-02 14:13:06 -07:00
|
|
|
'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,
|
2022-05-25 16:50:32 -07:00
|
|
|
'name' => ($actionlog->itemType()=='user') ? e($actionlog->item->getFullNameAttribute()) : e($actionlog->item->getDisplayNameAttribute()),
|
2017-05-23 14:31:04 -07:00
|
|
|
'type' => e($actionlog->itemType()),
|
2022-11-17 12:57:53 -08:00
|
|
|
'serial' =>e($actionlog->item->serial) ? e($actionlog->item->serial) : null
|
2017-05-23 14:31:04 -07:00
|
|
|
] : 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'),
|
2018-02-21 01:18:21 -08: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(),
|
2022-08-02 23:50:10 -07:00
|
|
|
'admin' => ($actionlog->admin) ? [
|
|
|
|
'id' => (int) $actionlog->admin->id,
|
|
|
|
'name' => e($actionlog->admin->getFullNameAttribute()),
|
|
|
|
'first_name'=> e($actionlog->admin->first_name),
|
|
|
|
'last_name'=> e($actionlog->admin->last_name)
|
2017-05-23 14:31:04 -07:00
|
|
|
] : null,
|
|
|
|
'target' => ($actionlog->target) ? [
|
|
|
|
'id' => (int) $actionlog->target->id,
|
|
|
|
'name' => ($actionlog->targetType()=='user') ? e($actionlog->target->getFullNameAttribute()) : e($actionlog->target->getDisplayNameAttribute()),
|
|
|
|
'type' => e($actionlog->targetType()),
|
|
|
|
] : null,
|
|
|
|
|
2023-07-11 03:41:58 -07:00
|
|
|
'note' => ($actionlog->note) ? Helper::parseEscapedMarkedownInline($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
|
|
|
];
|
|
|
|
|
2023-08-22 11:39:38 -07:00
|
|
|
// \Log::info("Clean Meta is: ".print_r($clean_meta,true));
|
2022-05-25 16:50:32 -07:00
|
|
|
//dd($array);
|
2023-08-28 10:14:41 -07:00
|
|
|
|
2017-05-23 14:31:04 -07:00
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-02 14:13:06 -07:00
|
|
|
|
2017-05-23 14:31:04 -07:00
|
|
|
public function transformCheckedoutActionlog (Collection $accessories_users, $total)
|
|
|
|
{
|
|
|
|
|
|
|
|
$array = array();
|
|
|
|
foreach ($accessories_users as $user) {
|
|
|
|
$array[] = (new UsersTransformer)->transformUser($user);
|
|
|
|
}
|
|
|
|
return (new DatatablesTransformer)->transformDatatables($array, $total);
|
|
|
|
}
|
2023-08-17 16:14:01 -07:00
|
|
|
/**
|
|
|
|
* This takes the ids of the changed attributes and returns the names instead for the history view of an Asset
|
|
|
|
*
|
|
|
|
* @param array $clean_meta
|
|
|
|
* @return array
|
|
|
|
*/
|
2023-08-23 00:40:59 -07:00
|
|
|
|
2023-08-17 16:14:01 -07:00
|
|
|
public function changedInfo(array $clean_meta)
|
2023-08-23 00:32:43 -07:00
|
|
|
{ $location = Location::withTrashed()->get();
|
|
|
|
$supplier = Supplier::withTrashed()->get();
|
|
|
|
$model = AssetModel::withTrashed()->get();
|
2023-09-05 08:10:20 -07:00
|
|
|
$company = Company::get();
|
2023-08-22 12:34:45 -07:00
|
|
|
|
2023-08-17 16:14:01 -07:00
|
|
|
|
|
|
|
if(array_key_exists('rtd_location_id',$clean_meta)) {
|
2023-10-13 11:32:09 -07:00
|
|
|
|
|
|
|
$oldRtd = $location->find($clean_meta['rtd_location_id']['old']);
|
|
|
|
$oldRtdName = $oldRtd ? e($oldRtd->name) : trans('general.deleted');
|
|
|
|
|
|
|
|
$newRtd = $location->find($clean_meta['rtd_location_id']['new']);
|
|
|
|
$newRtdName = $newRtd ? e($newRtd->name) : trans('general.deleted');
|
|
|
|
|
2023-10-13 11:49:03 -07:00
|
|
|
$clean_meta['rtd_location_id']['old'] = $clean_meta['rtd_location_id']['old'] ? "[id: ".$clean_meta['rtd_location_id']['old']."] ". $oldRtdName : '';
|
2023-10-13 11:32:09 -07:00
|
|
|
$clean_meta['rtd_location_id']['new'] = $clean_meta['rtd_location_id']['new'] ? "[id: ".$clean_meta['rtd_location_id']['new']."] ". $newRtdName : trans('general.unassigned');
|
2023-08-17 16:14:01 -07:00
|
|
|
$clean_meta['Default Location'] = $clean_meta['rtd_location_id'];
|
|
|
|
unset($clean_meta['rtd_location_id']);
|
|
|
|
}
|
2023-10-06 11:45:23 -07:00
|
|
|
|
2023-10-13 11:32:09 -07:00
|
|
|
|
2023-10-06 11:45:23 -07:00
|
|
|
if (array_key_exists('location_id', $clean_meta)) {
|
2023-10-13 11:32:09 -07:00
|
|
|
|
|
|
|
$oldLocation = $location->find($clean_meta['location_id']['old']);
|
|
|
|
$oldLocationName = $oldLocation ? e($oldLocation->name) : trans('general.deleted');
|
|
|
|
|
|
|
|
$newLocation = $location->find($clean_meta['location_id']['new']);
|
|
|
|
$newLocationName = $newLocation ? e($newLocation->name) : trans('general.deleted');
|
|
|
|
|
|
|
|
|
|
|
|
$clean_meta['location_id']['old'] = $clean_meta['location_id']['old'] ? "[id: ".$clean_meta['location_id']['old']."] ". $oldLocationName : trans('general.deleted');
|
|
|
|
$clean_meta['location_id']['new'] = $clean_meta['location_id']['new'] ? "[id: ".$clean_meta['location_id']['new']."] ". $newLocationName : trans('general.unassigned');
|
2023-08-17 16:14:01 -07:00
|
|
|
$clean_meta['Current Location'] = $clean_meta['location_id'];
|
|
|
|
unset($clean_meta['location_id']);
|
|
|
|
}
|
2023-10-06 11:45:23 -07:00
|
|
|
|
2023-08-17 16:14:01 -07:00
|
|
|
if(array_key_exists('model_id', $clean_meta)) {
|
2023-08-23 00:38:58 -07:00
|
|
|
|
2023-09-05 07:58:21 -07:00
|
|
|
$oldModel = $model->find($clean_meta['model_id']['old']);
|
2023-10-06 11:45:23 -07:00
|
|
|
$oldModelName = $oldModel ? e($oldModel->name) : trans('admin/models/message.deleted');
|
2023-09-05 07:58:21 -07:00
|
|
|
|
|
|
|
$newModel = $model->find($clean_meta['model_id']['new']);
|
2023-10-06 11:45:23 -07:00
|
|
|
$newModelName = $newModel ? e($newModel->name) : trans('admin/models/message.deleted');
|
2023-09-05 07:58:21 -07:00
|
|
|
|
|
|
|
$clean_meta['model_id']['old'] = "[id: ".$clean_meta['model_id']['old']."] ".$oldModelName;
|
|
|
|
$clean_meta['model_id']['new'] = "[id: ".$clean_meta['model_id']['new']."] ".$newModelName; /** model is required at asset creation */
|
2023-08-23 00:38:58 -07:00
|
|
|
|
2023-08-17 16:14:01 -07:00
|
|
|
$clean_meta['Model'] = $clean_meta['model_id'];
|
|
|
|
unset($clean_meta['model_id']);
|
|
|
|
}
|
|
|
|
if(array_key_exists('company_id', $clean_meta)) {
|
2023-09-05 07:58:21 -07:00
|
|
|
|
2023-09-05 08:02:28 -07:00
|
|
|
$oldCompany = $company->find($clean_meta['company_id']['old']);
|
2023-10-06 11:45:23 -07:00
|
|
|
$oldCompanyName = $oldCompany ? e($oldCompany->name) : trans('admin/company/message.deleted');
|
2023-08-28 19:35:46 -07:00
|
|
|
|
2023-09-05 08:02:28 -07:00
|
|
|
$newCompany = $company->find($clean_meta['company_id']['new']);
|
2023-10-06 11:45:23 -07:00
|
|
|
$newCompanyName = $newCompany ? e($newCompany->name) : trans('admin/company/message.deleted');
|
2023-08-28 19:35:46 -07:00
|
|
|
|
|
|
|
$clean_meta['company_id']['old'] = $clean_meta['company_id']['old'] ? "[id: ".$clean_meta['company_id']['old']."] ". $oldCompanyName : trans('general.unassigned');
|
|
|
|
$clean_meta['company_id']['new'] = $clean_meta['company_id']['new'] ? "[id: ".$clean_meta['company_id']['new']."] ". $newCompanyName : trans('general.unassigned');
|
2023-08-17 16:14:01 -07:00
|
|
|
$clean_meta['Company'] = $clean_meta['company_id'];
|
|
|
|
unset($clean_meta['company_id']);
|
|
|
|
}
|
|
|
|
if(array_key_exists('supplier_id', $clean_meta)) {
|
2023-09-05 07:58:21 -07:00
|
|
|
|
2023-09-05 08:02:28 -07:00
|
|
|
$oldSupplier = $supplier->find($clean_meta['supplier_id']['old']);
|
2023-10-06 11:45:23 -07:00
|
|
|
$oldSupplierName = $oldSupplier ? e($oldSupplier->name) : trans('admin/suppliers/message.deleted');
|
2023-09-05 07:58:21 -07:00
|
|
|
|
2023-09-05 08:02:28 -07:00
|
|
|
$newSupplier = $supplier->find($clean_meta['supplier_id']['new']);
|
2023-10-06 11:45:23 -07:00
|
|
|
$newSupplierName = $newSupplier ? e($newSupplier->name) : trans('admin/suppliers/message.deleted');
|
2023-09-05 07:58:21 -07:00
|
|
|
|
|
|
|
$clean_meta['supplier_id']['old'] = $clean_meta['supplier_id']['old'] ? "[id: ".$clean_meta['supplier_id']['old']."] ". $oldSupplierName : trans('general.unassigned');
|
|
|
|
$clean_meta['supplier_id']['new'] = $clean_meta['supplier_id']['new'] ? "[id: ".$clean_meta['supplier_id']['new']."] ". $newSupplierName : trans('general.unassigned');
|
2023-08-17 16:14:01 -07:00
|
|
|
$clean_meta['Supplier'] = $clean_meta['supplier_id'];
|
|
|
|
unset($clean_meta['supplier_id']);
|
|
|
|
}
|
2023-08-22 11:39:38 -07:00
|
|
|
if(array_key_exists('asset_eol_date', $clean_meta)) {
|
|
|
|
$clean_meta['EOL date'] = $clean_meta['asset_eol_date'];
|
|
|
|
unset($clean_meta['asset_eol_date']);
|
|
|
|
}
|
2023-08-17 16:14:01 -07:00
|
|
|
|
|
|
|
return $clean_meta;
|
|
|
|
|
|
|
|
}
|
2017-05-23 14:31:04 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-17 16:14:01 -07:00
|
|
|
}
|