Skip to content

A free open source IT asset/license management system

3.0 KB
Raw Blame
<?php

namespace App\Http\Transformers;

use App\Enums\FileStorage;
use App\Helpers\Helper;
use App\Models\Company;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Storage;

class CompaniesTransformer
{
    public function transformCompanies(Collection $companies, $total)
    {
        $array = [];
        foreach ($companies as $company) {
            $array[] = self::transformCompany($company);
        }

        return (new DatatablesTransformer)->transformDatatables($array, $total);
    }

    public function transformCompany(?Company $company = null)
    {
        if ($company) {
            $array = [
                'id' => (int) $company->id,
                'name' => e($company->name),
                'phone' => ($company->phone != '') ? e($company->phone) : null,
                'fax' => ($company->fax != '') ? e($company->fax) : null,
                'email' => ($company->email != '') ? e($company->email) : null,
                'image' => ($company->image) ? Storage::disk('public')->url(FileStorage::Companies->publicPath().e($company->image)) : null,
                'qr_code_url' => route('qr_code/common', ['object_type' => 'companies', 'id' => $company->id]),
                'parent' => ($company->parent) ? [
                    'id' => (int) $company->parent->id,
                    'name' => e($company->parent->name),
                ] : null,
                'children_count' => (int) ($company->children_count ?? $company->children()->count()),
                'assets_count' => (int) $company->assets_count,
                'licenses_count' => (int) $company->licenses_count,
                'accessories_count' => (int) $company->accessories_count,
                'consumables_count' => (int) $company->consumables_count,
                'components_count' => (int) $company->components_count,
                'users_count' => (int) $company->users_count,
                'created_by' => ($company->adminuser) ? [
                    'id' => (int) $company->adminuser->id,
                    'name' => e($company->adminuser->display_name),
                ] : null,
                'tag_color' => ($company->tag_color != '') ? e($company->tag_color) : null,
                'notes' => Helper::parseEscapedMarkedownInline($company->notes),
                'webhook_selected' => $company->webhook_selected,
                'created_at' => Helper::getFormattedDateObject($company->created_at, 'datetime'),
                'updated_at' => Helper::getFormattedDateObject($company->updated_at, 'datetime'),
            ];

            $permissions_array['available_actions'] = [
                'view' => Gate::allows('view', $company),
                'update' => Gate::allows('update', $company),
                'delete' => $company->isDeletable(),
                'bulk_selectable' => [
                    'delete' => $company->isDeletable(),
                ],
            ];

            $array += $permissions_array;

            return $array;
        }
    }
}