]> _ Git - cubist_cms-back.git/commitdiff
wip #3753 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 7 Dec 2020 11:15:31 +0000 (12:15 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 7 Dec 2020 11:15:31 +0000 (12:15 +0100)
src/app/Magic/Controllers/CubistMagicController.php
src/app/Magic/Fields/Checkbox.php
src/app/Magic/Fields/Field.php
src/app/Magic/Fields/Model.php
src/app/Magic/Fields/ModelAttribute.php
src/app/Magic/Fields/SelectFromArray.php
src/app/Magic/Fields/SelectFromModel.php

index c2b31fbac9d94b97db643b5ba57aae079e4464bd..6684bf3581d4f720d76a3763bec5142255f225eb 100644 (file)
@@ -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']);
index 7d13000ac4ad31d8189934223a485128564901e9..d67909b8e94d1dde525b4f70bb8048a963ea3d63 100644 (file)
@@ -11,4 +11,6 @@ class Checkbox extends Field
     protected $_columnType = 'check';
     protected $_cast = 'boolean';
     protected $_viewNamespace = 'toggle-field-for-backpack::fields';
+    protected $_filterType = 'simple';
+
 }
index b4bc028b7d167e17ea927c56da6ee9b5dc89410d..b21541cc2f44671b88db6d96a6d122c4381d05e0 100644 (file)
@@ -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');
index ce87a4350b379a88c8a71e8e1b7691425872f102..75f14a927242fe1ffaf647c6d883448a57a76fb7 100644 (file)
@@ -45,4 +45,9 @@ class Model extends Field
         }
         return parent::getDatabaseType();
     }
+
+    public function getFilterValues()
+    {
+        return $this->_getOptions();
+    }
 }
index 0308080799ce1415a9aa5755d12c24fc00145a05..6095ca952f56bbff4757ead479ee34f674d16e3a 100644 (file)
@@ -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');
index 01edf84cef7a7c5b3f08c41d1807ad685bd568d3..1f8e0bdb7c662b18a02df7f6eae7022879c716ed 100644 (file)
@@ -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'];
+    }
 }
index 8876779869fe190697cd017f51bae0774aecbe6a..c38bf97c396ca5bfb1025fe44031ba4f96c6240a 100644 (file)
@@ -66,4 +66,9 @@ class SelectFromModel extends Model
 
         return $this->pluck($this->getAttribute('column_attribute'));
     }
+
+    public function getFilterValues()
+    {
+        return $this->getColumnData()['options'];
+    }
 }