Some More Refinement To Datagrid
This commit is contained in:
parent
21225766fe
commit
9f55877f9a
|
|
@ -6,10 +6,11 @@ use Illuminate\Support\Str;
|
|||
use Illuminate\Support\Facades\Event;
|
||||
use Webkul\Ui\DataGrid\Traits\ProvideBouncer;
|
||||
use Webkul\Ui\DataGrid\Traits\ProvideCollection;
|
||||
use Webkul\Ui\DataGrid\Traits\ProvideExceptionHandler;
|
||||
|
||||
abstract class DataGrid
|
||||
{
|
||||
use ProvideBouncer, ProvideCollection;
|
||||
use ProvideBouncer, ProvideCollection, ProvideExceptionHandler;
|
||||
|
||||
/**
|
||||
* Set index columns, ex: id.
|
||||
|
|
@ -237,6 +238,8 @@ abstract class DataGrid
|
|||
*/
|
||||
public function addColumn($column)
|
||||
{
|
||||
$this->checkRequiredColumnKeys($column);
|
||||
|
||||
$this->fireEvent('add.column.before.' . $column['index']);
|
||||
|
||||
$this->columns[] = $column;
|
||||
|
|
@ -281,6 +284,8 @@ abstract class DataGrid
|
|||
*/
|
||||
public function addAction($action, $specialPermission = false)
|
||||
{
|
||||
$this->checkRequiredActionKeys($action);
|
||||
|
||||
$this->checkPermissions($action, $specialPermission, function ($action, $eventName) {
|
||||
$this->fireEvent('action.before.' . $eventName);
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
|
||||
namespace Webkul\Ui\DataGrid\Traits;
|
||||
|
||||
trait ProvideExceptionHandler
|
||||
{
|
||||
/**
|
||||
* Required column keys.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $requiredColumnKeys = ['index', 'label'];
|
||||
|
||||
/**
|
||||
* Required action keys.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $requiredActionKeys = ['title', 'method', 'route', 'icon'];
|
||||
|
||||
/**
|
||||
* This will check the keys which are needed for column.
|
||||
*
|
||||
* @param array $column
|
||||
* @return void|\Webkul\Ui\Exceptions\ColumnKeyException
|
||||
*/
|
||||
public function checkRequiredColumnKeys($column)
|
||||
{
|
||||
$this->checkRequiredKeys($this->requiredColumnKeys, $column, function ($missingKeys) {
|
||||
$message = 'Missing Keys: ' . implode(', ', $missingKeys);
|
||||
|
||||
throw new \Webkul\Ui\Exceptions\ColumnKeyException($message);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* This will check the keys which are needed for action.
|
||||
*
|
||||
* @param array $action
|
||||
* @return void|\Webkul\Ui\Exceptions\ActionKeyException
|
||||
*/
|
||||
public function checkRequiredActionKeys($action)
|
||||
{
|
||||
$this->checkRequiredKeys($this->requiredActionKeys, $action, function ($missingKeys) {
|
||||
$message = 'Missing Keys: ' . implode(', ', $missingKeys);
|
||||
|
||||
throw new \Webkul\Ui\Exceptions\ActionKeyException($message);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Check required keys.
|
||||
*
|
||||
* @param array $requiredKeys
|
||||
* @return void|\Closure
|
||||
*/
|
||||
public function checkRequiredKeys($requiredKeys, $actualKeys, $operation)
|
||||
{
|
||||
$requiredKeys = array_flip($requiredKeys);
|
||||
|
||||
$missingKeys = array_flip(array_diff_key($requiredKeys, $actualKeys));
|
||||
|
||||
return ! empty($missingKeys) ? $operation($missingKeys) : null;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
namespace Webkul\Ui\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class ActionKeyException extends Exception
|
||||
{
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
namespace Webkul\Ui\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class ColumnKeyException extends Exception
|
||||
{
|
||||
}
|
||||
Loading…
Reference in New Issue