namespace Cubist\Backpack\Magic\Fields;
+use Cubist\Backpack\CubistCrudPanel;
use Cubist\Backpack\Magic\CubistMagicAttribute;
use Cubist\Backpack\CubistBackpackServiceProvider;
use Doctrine\DBAL\Schema\Table;
protected $_columnFormat = null;
protected $_preview = true;
+ protected $_filterType = 'simple';
+ protected $_filterValues = null;
+
protected $_adminType = 'text';
protected $_viewNamespace = 'crud::fields';
protected $_columnViewNamespace = 'crud::columns';
protected $_searchLogic = 'text';
-
protected $_databaseType = 'text';
protected $_databaseUnique = false;
protected $_databaseIndex = false;
{
return ['type' => $this->_adminType, 'view_namespace' => $this->_viewNamespace, 'column' => false, 'form' => 'both', 'rules' => '',
'fillable' => true, 'guarded' => false, 'hidden' => false,
+ 'filter' => false, 'filter_type' => $this->_filterType, 'filter_label' => null, 'filter_values' => $this->_filterValues,
'translatable' => $this->_translatable, 'migrateTranslatable' => $this->_migrateTranslatable,
'preview' => $this->_preview, 'column_type' => $this->_columnType, 'column_move_after' => $this->_columnMoveAfter, 'column_format' => $this->_columnFormat,
'default' => '', 'cast' => $this->_cast, 'column_view_namespace' => $this->_columnViewNamespace, 'searchLogic' => $this->_searchLogic,
return $res;
}
+ public function isFilter()
+ {
+ return !!$this->getAttribute('filter');
+ }
+
+ public function getFilterOptions()
+ {
+ return [
+ 'type' => $this->getAttribute('filter_type'),
+ 'name' => $this->getAttribute('name'),
+ 'label' => $this->getAttribute('filter_label') ?? $this->getAttribute('column_label') ?? $this->getAttribute('label'),
+ ];
+ }
+
+ public function getFilterValues()
+ {
+ return false;
+ }
+
+ /**
+ * @param $crud CubistCrudPanel
+ */
+ public function filterLogic($crud, $value)
+ {
+ $type = $this->getAttribute('filter_type');
+ $name = $this->getAttribute('name');
+ if ($type === 'simple') {
+ $crud->addClause('where', $name, '1');
+ } else if ($type === 'dropdown' || $type === 'select2') {
+ $crud->addClause('where', $name, $value);
+ } else if ($type === 'select2_multiple') {
+ $crud->addClause('whereIn', $name, json_decode($value));
+ }
+ }
+
+
+ /**
+ * @param $crud CubistCrudPanel
+ */
+ public function addFilter($crud)
+ {
+ $crud->addFilter($this->getFilterOptions(), $this->getAttribute('filter_values') ?? $this->getFilterValues(), function ($value) use ($crud) {
+ $this->filterLogic($crud, $value);
+ });
+ }
+
public function getCRUDForm()
{
return $this->getAttribute('form');
if (null !== $this->getAttribute('can', null)) {
if (!can($this->getAttribute('can'))) {
$this->setAttribute('preview', false);
+ $this->setAttribute('filter', false);
$this->setAttribute('column', false);
$this->setAttribute('auth', false);
$this->setAttribute('type', 'authhidden');