From 847ff1cd2fba135e1947cc81269a4cf0480ebdb8 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Mon, 7 Dec 2020 12:15:31 +0100 Subject: [PATCH] wip #3753 @1 --- .../Controllers/CubistMagicController.php | 4 ++ src/app/Magic/Fields/Checkbox.php | 2 + src/app/Magic/Fields/Field.php | 53 ++++++++++++++++++- src/app/Magic/Fields/Model.php | 5 ++ src/app/Magic/Fields/ModelAttribute.php | 7 +++ src/app/Magic/Fields/SelectFromArray.php | 6 +++ src/app/Magic/Fields/SelectFromModel.php | 5 ++ 7 files changed, 81 insertions(+), 1 deletion(-) diff --git a/src/app/Magic/Controllers/CubistMagicController.php b/src/app/Magic/Controllers/CubistMagicController.php index c2b31fb..6684bf3 100644 --- a/src/app/Magic/Controllers/CubistMagicController.php +++ b/src/app/Magic/Controllers/CubistMagicController.php @@ -124,6 +124,10 @@ class CubistMagicController extends CubistCrudController $this->crud->afterColumn($moveAfter); } } + + if ($field->isFilter()) { + $field->addFilter($this->crud); + } $this->crud->addField($field->getDefinition(), $field->getCRUDForm()); $this->crud->orderFields(['id', 'variant']); diff --git a/src/app/Magic/Fields/Checkbox.php b/src/app/Magic/Fields/Checkbox.php index 7d13000..d67909b 100644 --- a/src/app/Magic/Fields/Checkbox.php +++ b/src/app/Magic/Fields/Checkbox.php @@ -11,4 +11,6 @@ class Checkbox extends Field protected $_columnType = 'check'; protected $_cast = 'boolean'; protected $_viewNamespace = 'toggle-field-for-backpack::fields'; + protected $_filterType = 'simple'; + } diff --git a/src/app/Magic/Fields/Field.php b/src/app/Magic/Fields/Field.php index b4bc028..b21541c 100644 --- a/src/app/Magic/Fields/Field.php +++ b/src/app/Magic/Fields/Field.php @@ -3,6 +3,7 @@ namespace Cubist\Backpack\Magic\Fields; +use Cubist\Backpack\CubistCrudPanel; use Cubist\Backpack\Magic\CubistMagicAttribute; use Cubist\Backpack\CubistBackpackServiceProvider; use Doctrine\DBAL\Schema\Table; @@ -24,12 +25,14 @@ class Field implements \ArrayAccess 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; @@ -111,6 +114,7 @@ class Field implements \ArrayAccess { 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, @@ -178,6 +182,52 @@ class Field implements \ArrayAccess 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'); @@ -240,6 +290,7 @@ class Field implements \ArrayAccess 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'); diff --git a/src/app/Magic/Fields/Model.php b/src/app/Magic/Fields/Model.php index ce87a43..75f14a9 100644 --- a/src/app/Magic/Fields/Model.php +++ b/src/app/Magic/Fields/Model.php @@ -45,4 +45,9 @@ class Model extends Field } return parent::getDatabaseType(); } + + public function getFilterValues() + { + return $this->_getOptions(); + } } diff --git a/src/app/Magic/Fields/ModelAttribute.php b/src/app/Magic/Fields/ModelAttribute.php index 0308080..6095ca9 100644 --- a/src/app/Magic/Fields/ModelAttribute.php +++ b/src/app/Magic/Fields/ModelAttribute.php @@ -8,6 +8,13 @@ use Cubist\Backpack\Magic\Models\CubistMagicAbstractModel; class ModelAttribute extends StaticValue { + protected $_attribute = null; + + public function getDefaultAttributes() + { + return array_merge(parent::getDefaultAttributes(), ['attribute' => $this->_attribute]); + } + public function filterValue($value) { $a = $this->getAttribute('attribute'); diff --git a/src/app/Magic/Fields/SelectFromArray.php b/src/app/Magic/Fields/SelectFromArray.php index 01edf84..1f8e0bd 100644 --- a/src/app/Magic/Fields/SelectFromArray.php +++ b/src/app/Magic/Fields/SelectFromArray.php @@ -17,6 +17,7 @@ class SelectFromArray extends Field protected $_allowNull = true; protected $_options = []; protected $_options_aliases = []; + protected $_filterType = 'dropdown'; public function getDefaultAttributes() { @@ -39,4 +40,9 @@ class SelectFromArray extends Field } return $value; } + + public function getFilterValues() + { + return $this->getColumnData()['options']; + } } diff --git a/src/app/Magic/Fields/SelectFromModel.php b/src/app/Magic/Fields/SelectFromModel.php index 8876779..c38bf97 100644 --- a/src/app/Magic/Fields/SelectFromModel.php +++ b/src/app/Magic/Fields/SelectFromModel.php @@ -66,4 +66,9 @@ class SelectFromModel extends Model return $this->pluck($this->getAttribute('column_attribute')); } + + public function getFilterValues() + { + return $this->getColumnData()['options']; + } } -- 2.39.5