87 lines
1.9 KiB
PHP
87 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\MacroSchedule\Marker;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\View;
|
|
use Kwaadpepper\CrudPolicies\Traits\CrudController;
|
|
|
|
class MarkerController extends Controller
|
|
{
|
|
use CrudController;
|
|
|
|
/** @var \App\Models\MacroSchedule\Marker */
|
|
public static $modelClass = Marker::class;
|
|
|
|
/**
|
|
* Crud Layout
|
|
*
|
|
* @var string
|
|
*/
|
|
public static $layout = 'layout.app';
|
|
|
|
/**
|
|
* Crud index Hook
|
|
*
|
|
* @param Builder $query
|
|
* @return Builder
|
|
*/
|
|
public function indexQuery(Builder $query): Builder
|
|
{
|
|
$parent = request()->project;
|
|
if ($parent) {
|
|
$query->whereHas('project', function ($q) use ($parent) {
|
|
return $q->whereId($parent->id);
|
|
});
|
|
}
|
|
return $query;
|
|
}
|
|
|
|
/**
|
|
* Show create page middleware
|
|
*
|
|
* @return void
|
|
*/
|
|
public function createModel()
|
|
{
|
|
$this->shareLabelsToView();
|
|
}
|
|
|
|
/**
|
|
* Show edit model middleware
|
|
*
|
|
* @param Model $model
|
|
* @return void
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
|
|
*/
|
|
public function editModel(Model &$model)
|
|
{
|
|
$this->shareLabelsToView();
|
|
}
|
|
|
|
/**
|
|
* Before store model middleware
|
|
*
|
|
* @param \App\Models\MacroSchedule\Marker $marker
|
|
* @return void
|
|
*/
|
|
public function storeModel(Marker &$marker)
|
|
{
|
|
$marker->project()->associate(request()->project);
|
|
}
|
|
|
|
/**
|
|
* Share Marker labels to view
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function shareLabelsToView()
|
|
{
|
|
View::share('markerLabels', Cache::remember('markers_labels', 20, function () {
|
|
return Marker::pluck('label');
|
|
}));
|
|
}
|
|
}
|