2018-10-31 06:06:38 -07:00
|
|
|
<?php
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2018-10-31 06:06:38 -07:00
|
|
|
namespace App\Http\Transformers;
|
|
|
|
|
|
|
|
use App\Models\PredefinedKit;
|
|
|
|
use App\Models\SnipeModel;
|
2023-03-18 11:58:09 -07:00
|
|
|
use Illuminate\Support\Facades\Gate;
|
2019-03-13 20:12:03 -07:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2018-10-31 06:06:38 -07:00
|
|
|
|
2019-02-23 11:44:03 -08:00
|
|
|
/**
|
|
|
|
* transforms collection of models to array with simple typres
|
2021-06-10 13:15:52 -07:00
|
|
|
*
|
2019-02-23 11:44:03 -08:00
|
|
|
* @author [D. Minaev] [<dmitriy.minaev.v@gmail.com>]
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-10-31 06:06:38 -07:00
|
|
|
class PredefinedKitsTransformer
|
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
public function transformPredefinedKits(Collection $kits, $total)
|
2018-10-31 06:06:38 -07:00
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
$array = [];
|
2018-10-31 06:06:38 -07:00
|
|
|
foreach ($kits as $kit) {
|
|
|
|
$array[] = self::transformPredefinedKit($kit);
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2018-10-31 06:06:38 -07:00
|
|
|
return (new DatatablesTransformer)->transformDatatables($array, $total);
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
public function transformPredefinedKit(PredefinedKit $kit)
|
2018-10-31 06:06:38 -07:00
|
|
|
{
|
|
|
|
$array = [
|
|
|
|
'id' => (int) $kit->id,
|
2021-06-10 13:15:52 -07:00
|
|
|
'name' => e($kit->name),
|
2018-10-31 06:06:38 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
$permissions_array['available_actions'] = [
|
|
|
|
'update' => Gate::allows('update', PredefinedKit::class),
|
|
|
|
'delete' => Gate::allows('delete', PredefinedKit::class),
|
2020-05-23 10:36:02 -07:00
|
|
|
'checkout' => Gate::allows('checkout', PredefinedKit::class),
|
2018-10-31 06:06:38 -07:00
|
|
|
// 'clone' => Gate::allows('create', PredefinedKit::class),
|
|
|
|
// 'restore' => Gate::allows('create', PredefinedKit::class),
|
|
|
|
];
|
2018-11-13 09:33:32 -08:00
|
|
|
$array['user_can_checkout'] = true;
|
2018-10-31 06:06:38 -07:00
|
|
|
$array += $permissions_array;
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2018-10-31 06:06:38 -07:00
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
2019-02-23 11:44:03 -08:00
|
|
|
/**
|
2021-06-10 13:15:52 -07:00
|
|
|
* transform collection of any elemets attached to kit
|
2019-02-23 11:44:03 -08:00
|
|
|
* @return array
|
|
|
|
*/
|
2021-06-10 13:15:52 -07:00
|
|
|
public function transformElements(Collection $elements, $total)
|
|
|
|
{
|
|
|
|
$array = [];
|
2018-10-31 06:06:38 -07:00
|
|
|
foreach ($elements as $element) {
|
|
|
|
$array[] = self::transformElement($element);
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2018-10-31 06:06:38 -07:00
|
|
|
return (new DatatablesTransformer)->transformDatatables($array, $total);
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
public function transformElement(SnipeModel $element)
|
|
|
|
{
|
2018-10-31 06:06:38 -07:00
|
|
|
$array = [
|
|
|
|
'id' => (int) $element->id,
|
|
|
|
'pivot_id' => (int) $element->pivot->id,
|
|
|
|
'owner_id' => (int) $element->pivot->kit_id,
|
|
|
|
'quantity' => (int) $element->pivot->quantity,
|
2021-06-10 13:15:52 -07:00
|
|
|
'name' => e($element->name),
|
2018-10-31 06:06:38 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
$permissions_array['available_actions'] = [
|
|
|
|
'update' => Gate::allows('update', PredefinedKit::class),
|
|
|
|
'delete' => Gate::allows('delete', PredefinedKit::class),
|
|
|
|
];
|
|
|
|
|
|
|
|
$array += $permissions_array;
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2018-10-31 06:06:38 -07:00
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
public function transformPredefinedKitsDatatable($kits)
|
|
|
|
{
|
2018-10-31 06:06:38 -07:00
|
|
|
return (new DatatablesTransformer)->transformDatatables($kits);
|
|
|
|
}
|
|
|
|
}
|