2017-07-07 17:10:06 -07:00
|
|
|
<?php
|
|
|
|
namespace App\Http\Transformers;
|
|
|
|
|
|
|
|
use App\Models\Company;
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
use Gate;
|
|
|
|
use App\Helpers\Helper;
|
2018-09-29 21:33:52 -07:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2017-07-07 17:10:06 -07:00
|
|
|
|
|
|
|
class CompaniesTransformer
|
|
|
|
{
|
|
|
|
|
|
|
|
public function transformCompanies (Collection $companies, $total)
|
|
|
|
{
|
|
|
|
$array = array();
|
|
|
|
foreach ($companies as $company) {
|
|
|
|
$array[] = self::transformCompany($company);
|
|
|
|
}
|
|
|
|
return (new DatatablesTransformer)->transformDatatables($array, $total);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function transformCompany (Company $company = null)
|
|
|
|
{
|
|
|
|
if ($company) {
|
|
|
|
|
|
|
|
$array = [
|
2017-08-22 14:53:48 -07:00
|
|
|
'id' => (int) $company->id,
|
2017-07-07 17:10:06 -07:00
|
|
|
'name' => e($company->name),
|
2018-09-29 21:33:52 -07:00
|
|
|
'image' => ($company->image) ? Storage::disk('public')->url('companies/'.e($company->image)) : null,
|
2017-07-07 17:10:06 -07:00
|
|
|
"created_at" => Helper::getFormattedDateObject($company->created_at, 'datetime'),
|
|
|
|
"updated_at" => Helper::getFormattedDateObject($company->updated_at, 'datetime'),
|
2017-07-25 23:40:30 -07:00
|
|
|
"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
|
2017-07-07 17:10:06 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
$permissions_array['available_actions'] = [
|
|
|
|
'update' => Gate::allows('update', Company::class) ? true : false,
|
2018-01-19 17:51:28 -08:00
|
|
|
'delete' => (Gate::allows('delete', Company::class) && ($company->assets_count == 0) && ($company->accessories_count == 0) && ($company->consumables_count == 0) && ($company->components_count == 0) && ($company->users_count == 0)) ? true : false,
|
2017-07-07 17:10:06 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
$array += $permissions_array;
|
|
|
|
|
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|