birzha-legalizasia/app/Http/Controllers/Admin/DocumentCrudController.php

232 lines
7.4 KiB
PHP
Executable File

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Requests\DocumentRequest;
use App\Models\Country;
use Backpack\CRUD\app\Http\Controllers\CrudController;
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
/**
* Class DocumentCrudController
* @package App\Http\Controllers\Admin
* @property-read \Backpack\CRUD\app\Library\CrudPanel\CrudPanel $crud
*/
class DocumentCrudController extends CrudController
{
use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
/**
* Configure the CrudPanel object. Apply settings to all operations.
*
* @return void
*/
public function setup()
{
CRUD::setModel(\App\Models\Document::class);
CRUD::setRoute(config('backpack.base.route_prefix') . '/document');
CRUD::setEntityNameStrings(trans('app.resource.document'), trans('app.resource.documents'));
$this->crud->addFilter([
'name' => 'business',
'type' => 'dropdown',
'label' => trans('app.resource.business')
], [
1 => trans('app.resource.yes'),
0 => trans('app.resource.no')
], function ($value) { // if the filter is active
$this->crud->addClause('where', 'business', $value);
});
$this->crud->addFilter([
'name' => 'company',
'type' => 'dropdown',
'label' => trans('app.resource.company')
], [
1 => trans('app.resource.yes'),
0 => trans('app.resource.no')
], function ($value) { // if the filter is active
$this->crud->addClause('where', 'company', $value);
});
$this->crud->addFilter([
'name' => 'all_country',
'type' => 'dropdown',
'label' => trans('app.rescource.all_countries')
], [
1 => trans('app.resource.yes'),
0 => trans('app.resource.no')
], function ($value) {
$this->crud->addClause('where', 'all_country', $value);
});
// select2_multiple filter
$this->crud->addFilter([
'name' => 'countries',
'type' => 'select2_multiple',
'label' => trans('app.resource.countries')
], function() {
return Country::all()->pluck('name', 'id')->toArray();
}, function($values) {
foreach ($values as $key => $value) {
$this->crud->query = $this->crud->query->whereHas('countries', function ($query) use ($value) {
$query->where('country_id', $value);
});
}
});
}
/**
* Define what happens when the List operation is loaded.
*
* @see https://backpackforlaravel.com/docs/crud-operation-list-entries
* @return void
*/
protected function setupListOperation()
{
$this->crud->addColumns([
[
'name' => 'name',
'type' => 'text',
'label' => trans('app.resource.name')
],
[
'name' => 'max_size',
'type' => 'number',
'label' => trans('app.resource.max_size'),
'default' => 0
],
[
'name' => 'order',
'type' => 'number',
'label' => trans('app.resource.position'),
'default' => 0
],
[
'name' => 'business',
'type' => 'radio',
'label' => trans('app.resource.enterpreneurs'),
'options' => [
1 => trans('app.resource.yes'),
0 => trans('app.resource.no')
]
],
[
'name' => 'company',
'type' => 'radio',
'label' => trans('app.resource.companies'),
'options' => [
1 => trans('app.resource.yes'),
0 => trans('app.resource.no')
]
],
[
'name' => 'all_country',
'type' => 'radio',
'label' => trans('app.resource.all_countries'),
'options' => [
1 => trans('app.resource.yes'),
0 => trans('app.resource.no')
]
],
[
'label' => trans('app.resource.doc_countries'),
'type' => 'select_multiple',
'name' => 'countries',
'entity' => 'countries',
'model' => "App\Models\Country",
'attribute' => 'name',
'pivot' => true,
'options' => (function ($query) {
return $query->orderBy('name', 'ASC')->get();
}),
],
]);
}
/**
* Define what happens when the Create operation is loaded.
*
* @see https://backpackforlaravel.com/docs/crud-operation-create
* @return void
*/
protected function setupCreateOperation()
{
CRUD::setValidation(DocumentRequest::class);
$this->crud->addFields([
[
'name' => 'name',
'type' => 'text',
'label' => 'Name'
],
[
'name' => 'description',
'type' => 'textarea',
'label' => 'Description'
],
[
'name' => 'max_size',
'type' => 'number',
'label' => 'Max size (KBytes)',
'default' => 0
],
[
'name' => 'order',
'type' => 'number',
'label' => 'Position',
'default' => 0
],
[
'name' => 'business',
'label' => 'Enterpreneurs',
'type' => 'checkbox'
],
[
'name' => 'company',
'label' => 'Companies',
'type' => 'checkbox'
],
[
'name' => 'all_country',
'label' => 'All countries',
'type' => 'checkbox'
],
[
'label' => "Document Countries",
'type' => 'select2_multiple',
'name' => 'countries',
'entity' => 'countries',
'model' => "App\Models\Country",
'attribute' => 'name',
'pivot' => true,
'options' => (function ($query) {
return $query->orderBy('name', 'ASC')->get();
}),
],
[
'name' => 'max_size',
'type' => 'text',
'label' => 'Max size (KBytes)'
]
]);
}
/**
* Define what happens when the Update operation is loaded.
*
* @see https://backpackforlaravel.com/docs/crud-operation-update
* @return void
*/
protected function setupUpdateOperation()
{
$this->setupCreateOperation();
}
}