A free open source IT asset/license management system
<?php
namespace App\Http\Transformers;
use App\Helpers\Helper;
use App\Helpers\StorageHelper;
use App\Models\Actionlog;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Storage;
class UploadedFilesTransformer
{
public function transformFiles(Collection $files, $total)
{
$array = [];
foreach ($files as $file) {
$array[] = self::transformFile($file);
}
return (new DatatablesTransformer)->transformDatatables($array, $total);
}
public function transformFile(Actionlog $file)
{
$snipeModel = $file->item_type;
$item = null;
if (class_exists($snipeModel)) {
$itemQuery = $snipeModel::query();
if (in_array(SoftDeletes::class, class_uses_recursive($snipeModel), true)) {
$itemQuery->withTrashed();
}
$item = $itemQuery->find($file->item_id);
}
$array = [
'id' => (int) $file->id,
'icon' => Helper::filetype_icon($file->filename),
'name' => e($file->filename),
'item' => ($file->item_type) ? [
'id' => (int) $file->item_id,
'type' => str_plural(strtolower(class_basename($file->item_type))),
] : null,
'filename' => e($file->filename),
'filetype' => StorageHelper::getFiletype($file->uploads_file_path()),
'mediatype' => StorageHelper::getMediaType($file->uploads_file_path()),
'url' => $file->uploads_file_url(),
// Notes are single-encoded here (e()), but the bootstrap-table
// gallery "custom view" concatenates the note into the
// data-footer attribute of the lightbox anchor and ekko-lightbox
// later rebuilds the modal footer as an HTML string from
// $el.data('footer'). Two decodes strip the entity encoding and
// leave live markup, so the field must be inert HTML by the time
// it leaves this transformer. strip_tags removes any tag markup
// placed in the note before it reaches any renderer.
'note' => ($file->note) ? e(strip_tags($file->note)) : null,
'created_by' => ($file->adminuser) ? [
'id' => (int) $file->adminuser->id,
'name' => e($file->adminuser->display_name),
] : null,
'created_at' => Helper::getFormattedDateObject($file->created_at, 'datetime'),
'deleted_at' => Helper::getFormattedDateObject($file->deleted_at, 'datetime'),
'inlineable' => StorageHelper::allowSafeInline($file->uploads_file_path()) ?? false,
'exists_on_disk' => (Storage::exists($file->uploads_file_path()) ? true : false),
];
$permissions_array['available_actions'] = [
'delete' => (Gate::allows('update', $item ?? $snipeModel) && ($file->deleted_at == '')),
];
$array += $permissions_array;
return $array;
}
}