From 359c35ebbad3cc2a9d046988ad55ace994da1805 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Mon, 23 Jan 2023 19:14:13 +0100 Subject: [PATCH] wip #5682 @1 --- .../Controllers/CubistMagicController.php | 12 ++++++++++-- .../Magic/Models/CubistMagicAbstractModel.php | 19 +++++++++++++++++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/app/Magic/Controllers/CubistMagicController.php b/src/app/Magic/Controllers/CubistMagicController.php index 6f0f12f..a65d336 100644 --- a/src/app/Magic/Controllers/CubistMagicController.php +++ b/src/app/Magic/Controllers/CubistMagicController.php @@ -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); + }); + } } diff --git a/src/app/Magic/Models/CubistMagicAbstractModel.php b/src/app/Magic/Models/CubistMagicAbstractModel.php index 27b4d12..b963c6c 100644 --- a/src/app/Magic/Models/CubistMagicAbstractModel.php +++ b/src/app/Magic/Models/CubistMagicAbstractModel.php @@ -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; + } } -- 2.39.5