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

170 lines
6.4 KiB
PHP
Executable File

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Requests\TicketRequest;
use App\Models\Status;
use Backpack\CRUD\app\Http\Controllers\CrudController;
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
/**
* Class TicketCrudController
* @package App\Http\Controllers\Admin
* @property-read \Backpack\CRUD\app\Library\CrudPanel\CrudPanel $crud
*/
class TicketCrudController 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()
{
if(!(backpack_user()->hasPermissionTo('tickets'))){
$this->crud->denyAccess(['delete', 'update']);
}
CRUD::setModel(\App\Models\Ticket::class);
CRUD::setRoute(config('backpack.base.route_prefix') . '/ticket');
CRUD::setEntityNameStrings(trans('app.ticket.title'), trans('app.ticket.list_title'));
// select2 filter
$this->crud->addFilter([
'name' => 'status',
'type' => 'select2',
'label' => trans('app.ticket.status'),
], function () {
return Status::get()->pluck('name', 'id')->toArray();
}, function ($value) { // if the filter is active
$this->crud->addClause('where', 'status_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' => 'content',
'type' => 'text',
'label' => trans('app.ticket.content')
],
[
'name' => 'title',
'type' => 'text',
'label' => trans('app.ticket.ticket_title')
],
[
'name' => 'client_id',
'type' => 'account_profile_name',
'label' => trans('app.ticket.account'),
],
[
'name' => 'status',
'type' => 'status',
'label' => trans('app.ticket.status'),
],
[
'name' => 'application_id',
'type' => 'text',
'label' => trans('app.ticket.application_id')
]
]);
$this->crud->addButtonFromModelFunction('line', 'messages', 'messagesOfTicket', 'beginning');
}
/**
* Define what happens when the Create operation is loaded.
*
* @see https://backpackforlaravel.com/docs/crud-operation-create
* @return void
*/
protected function setupCreateOperation()
{
CRUD::setValidation(TicketRequest::class);
$this->crud->addFields([
[
'name' => 'title',
'type' => 'text',
'label' => trans('app.ticket.ticket_title')
],
[
'name' => 'content',
'type' => 'textarea',
'label' => trans('app.ticket.content')
],
// [ // SelectMultiple = n-n relationship (with pivot table)
// 'label' => trans('app.ticket.account'),
// 'type' => 'custom_select_account',
// 'name' => 'client_id', // the method that defines the relationship in your Model
// 'entity' => 'account', // the method that defines the relationship in your Model
// 'model' => "App\Models\Account", // foreign key model
// 'attribute_1' => 'name', // foreign key attribute that is shown to user
// 'attribute_2' => 'surname',
// ],
[
'label' => trans('app.ticket.account'), // Table column heading
'type' => "select2_from_ajax",
'name' => 'client_id', // the column that contains the ID of that connected entity
'entity' => 'account', // the method that defines the relationship in your Model
'attribute' => "name", // foreign key attribute that is shown to user
'data_source' => backpack_url('account/fetch'),
// OPTIONAL
// 'delay' => 500, // the minimum amount of time between ajax requests when searching in the field
'placeholder' => "Select a client", // placeholder for the select
'minimum_input_length' => 2, // minimum characters to type before querying results
'model' => "App\Models\Account", // foreign key model
// 'dependencies' => ['category'], // when a dependency changes, this select2 is reset to null
'method' => 'POST', // optional - HTTP method to use for the AJAX call (GET, POST)
// 'include_all_form_fields' => false, // optional - only send the current field through AJAX (for a smaller payload if you're not using multiple chained select2s)
],
[
'name' => 'category_id',
'entity' => 'category',
'type' => 'select',
'label' => trans('app.ticket.category'),
'attribute' => 'name',
'model' => 'App\Models\Category'
],
[
'name' => 'status_id',
'entity' => 'status',
'type' => 'select',
'label' =>trans('app.ticket.status'),
'attribute' => 'name',
'model' => 'App\Models\Status'
],
[
'name' => 'last_sender',
'type' => 'hidden',
'value' => 'admin',
]
]);
}
/**
* Define what happens when the Update operation is loaded.
*
* @see https://backpackforlaravel.com/docs/crud-operation-update
* @return void
*/
protected function setupUpdateOperation()
{
$this->setupCreateOperation();
}
}