]> _ Git - cubist_cms-back.git/commitdiff
wip #5682 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 23 Jan 2023 18:14:13 +0000 (19:14 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 23 Jan 2023 18:14:13 +0000 (19:14 +0100)
src/app/Magic/Controllers/CubistMagicController.php
src/app/Magic/Models/CubistMagicAbstractModel.php

index 6f0f12f43d0cbd85667d6f9175ec059ec906e23b..a65d336b1bf3e39a67ff721b576b00e8e9aecd51 100644 (file)
@@ -158,10 +158,18 @@ class CubistMagicController extends CubistCrudController
                     continue;
                 }
                 $this->addField($field);
-            }catch (\Error $e){
-                dd($field,$e);
+            } catch (\Error $e) {
+                dd($field, $e);
             }
         }
+
+
+        foreach ($model->getFilters() as $filter) {
+            $this->crud->addFilter(['name' => $filter['name'], 'type' => $filter['type'], 'label' => $filter['label']], $filter['options'], function ($value) use ($filter) {
+                $f = $filter['logic'];
+                return $f($value, $this->crud);
+            });
+        }
     }
 
 
index 27b4d121e7744dbed5685c8e9732d03b3e67884c..b963c6cf656739fdc8ea098d4b64f96eab5866df 100644 (file)
@@ -112,6 +112,8 @@ class CubistMagicAbstractModel extends Model implements HasMedia
      */
     protected $editActions = [];
 
+    protected $filters = [];
+
 
     /**
      * @var array
@@ -704,7 +706,7 @@ class CubistMagicAbstractModel extends Model implements HasMedia
         $res = $this->getMedia($c);
         if ($res->count() === 0 && $this->hasField($c)) {
             $c = $this->getAttribute($c);
-            if(null===$c){
+            if (null === $c) {
                 return [];
             }
             $res = $this->getMedia($c);
@@ -1103,7 +1105,7 @@ class CubistMagicAbstractModel extends Model implements HasMedia
     public function getFirstMediaInField(string $fieldName): ?Media
     {
         $collection = $this->getMediaInField($fieldName);
-        if(!$collection){
+        if (!$collection) {
             return null;
         }
         if ($collection->count() > 0) {
@@ -1270,4 +1272,17 @@ class CubistMagicAbstractModel extends Model implements HasMedia
     {
         return true;
     }
+
+    public function addFilter($name, $type, $label, $logic = null, $options = null)
+    {
+        $this->filters[$name] = ['name' => $name, 'type' => $type, 'label' => $label, 'logic' => $logic, 'options' => $options];
+    }
+
+    /**
+     * @return array[]
+     */
+    public function getFilters()
+    {
+        return $this->filters;
+    }
 }