From b59164b0b7451905adcca244e6395281eb0633ca Mon Sep 17 00:00:00 2001 From: Ludovic Pouzenc Date: Fri, 20 Jul 2018 00:41:45 +0200 Subject: bake add Search filter support --- app-from-scratch.sh | 23 +++++++++ fai_gestion/composer.json | 1 + fai_gestion/composer.lock | 58 +++++++++++++++++++++- fai_gestion/config/bake_extra.php | 8 +-- fai_gestion/config/bootstrap.php | 11 ++++ .../plugins/CustomTheme/config/bootstrap.php | 13 +++++ .../Template/Bake/Element/Controller/index.twig | 12 ++++- .../CustomTheme/src/Template/Bake/Model/table.twig | 18 +++++++ .../src/Template/Bake/Template/index.twig | 28 +++++++++++ .../src/View/Helper/BakeExtraHelper.php | 35 +++++++++++++ fai_gestion/src/Application.php | 2 + fai_gestion/src/Template/Layout/default.ctp | 1 + fai_gestion/webroot/css/local.css | 28 +++++++++++ 13 files changed, 229 insertions(+), 9 deletions(-) create mode 100644 fai_gestion/webroot/css/local.css diff --git a/app-from-scratch.sh b/app-from-scratch.sh index 6b9ebb3..c84fa0a 100644 --- a/app-from-scratch.sh +++ b/app-from-scratch.sh @@ -293,3 +293,26 @@ git commit -m "Date picker in DD/MM/YYYY order" editor plugins/CustomTheme/src/Shell/BakeShell.php git add ../app-from-scratch.sh plugins/CustomTheme/src/Shell/BakeShell.php git commit -m "bake all : skip some models and actions, via configuration" +# 76ab63096a25f5bf7b9d076cdc9ab3fc5218d216 + +# bake: add Search filter support + +# https://github.com/FriendsOfCake/search +composer.phar require friendsofcake/search +cake plugin load Search +git add src/Application.php config/bootstrap.php composer.json composer.lock + +editor plugins/CustomTheme/src/View/Helper/BakeExtraHelper.php $p/Model/table.twig +git add plugins/CustomTheme/src/View/Helper/BakeExtraHelper.php $p/Model/table.twig + +editor plugins/CustomTheme/config/bootstrap.php +git add plugins/CustomTheme/config/bootstrap.php + +editor $p/Model/table.twig $p/Element/Controller/index.twig $p/Template/index.twig +git add $p/Model/table.twig $p/Element/Controller/index.twig $p/Template/index.twig + +editor src/Template/Layout/default.ctp webroot/css/local.css +git add src/Template/Layout/default.ctp webroot/css/local.css + +git add ../app-from-scratch.sh +git commit -m "bake add Search filter support" diff --git a/fai_gestion/composer.json b/fai_gestion/composer.json index d370f55..a398a3a 100644 --- a/fai_gestion/composer.json +++ b/fai_gestion/composer.json @@ -9,6 +9,7 @@ "cakephp/cakephp": "3.6.*", "cakephp/migrations": "^2.0.0", "cakephp/plugin-installer": "^1.0", + "friendsofcake/search": "^4.4", "josegonzalez/dotenv": "3.*", "mobiledetect/mobiledetectlib": "2.*" }, diff --git a/fai_gestion/composer.lock b/fai_gestion/composer.lock index b1a1098..efb20eb 100644 --- a/fai_gestion/composer.lock +++ b/fai_gestion/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "bdfe56ce06a95062ff683ed4dad28676", + "content-hash": "3d0caf03156fd7056c3407c2b807c8ac", "packages": [ { "name": "aura/intl", @@ -290,6 +290,62 @@ "time": "2017-12-24T21:09:29+00:00" }, { + "name": "friendsofcake/search", + "version": "4.4.0", + "source": { + "type": "git", + "url": "https://github.com/FriendsOfCake/search.git", + "reference": "f928b2b594697fb65c8e9c1404f7cd89a31e7a88" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/FriendsOfCake/search/zipball/f928b2b594697fb65c8e9c1404f7cd89a31e7a88", + "reference": "f928b2b594697fb65c8e9c1404f7cd89a31e7a88", + "shasum": "" + }, + "require": { + "cakephp/cakephp": "^3.5" + }, + "require-dev": { + "cakephp/cakephp-codesniffer": "^3.0", + "cakephp/chronos": "^1.1", + "friendsofcake/cakephp-test-utilities": "^1.0", + "muffin/webservice": "^1.3", + "phpunit/phpunit": "^5.7.14|^6.0" + }, + "type": "cakephp-plugin", + "autoload": { + "psr-4": { + "Search\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Christian Winther", + "homepage": "http://cakephp.nu/", + "role": "Author" + }, + { + "name": "ADmad", + "homepage": "https://github.com/admad", + "role": "Contributor" + } + ], + "description": "CakePHP Search plugin using PRG pattern", + "homepage": "https://github.com/FriendsOfCake/search", + "keywords": [ + "cake3", + "cakephp", + "cakephp3", + "search" + ], + "time": "2018-04-16T21:29:06+00:00" + }, + { "name": "josegonzalez/dotenv", "version": "3.2.0", "source": { diff --git a/fai_gestion/config/bake_extra.php b/fai_gestion/config/bake_extra.php index e33b2a0..559b340 100644 --- a/fai_gestion/config/bake_extra.php +++ b/fai_gestion/config/bake_extra.php @@ -68,23 +68,17 @@ EOT 'mode' => 'like', 'before' => 'true', 'after' => 'true', - 'columns' => ['id','nom','nom2','prenom','prenom2','raison','proprio','tel_mobile1','tel_mobile2'], + 'field' => ['id','nom','nom2','prenom','prenom2','raison','proprio','tel_mobile1','tel_mobile2'], 'colspan' => 3, 'hint' => 'Find...', ], 'adherent_type_id' => [ 'mode' => 'value', - 'before' => 'false', - 'after' => 'false', - 'model' => 'AdherentTypes', 'colspan' => 2, 'hint' => 'AdherentTypes', ], 'ville_id' => [ 'mode' => 'value', - 'before' => 'false', - 'after' => 'false', - 'model' => 'Villes', 'colspan' => 2, 'hint' => 'Villes', ], diff --git a/fai_gestion/config/bootstrap.php b/fai_gestion/config/bootstrap.php index 24f6908..9a77fca 100644 --- a/fai_gestion/config/bootstrap.php +++ b/fai_gestion/config/bootstrap.php @@ -205,3 +205,14 @@ FrozenDate::setToStringFormat('yyyy-MM-dd'); // For any immutable Date //Inflector::rules('transliteration', ['/å/' => 'aa']); Inflector::rules('uninflected', ['relais']); Inflector::rules('irregular', ['interesse' => 'interesses']); + +/* + * Note : Cake 3.6 introduce new way of loading plugins + * src/Application.php: + * public function bootstrap() + * { + * #[...] + * $this->addPlugin('Search'); + * } + * Old-style was adding Plugin::load('Search'); in this file. + */ diff --git a/fai_gestion/plugins/CustomTheme/config/bootstrap.php b/fai_gestion/plugins/CustomTheme/config/bootstrap.php index f90c873..7215ce7 100644 --- a/fai_gestion/plugins/CustomTheme/config/bootstrap.php +++ b/fai_gestion/plugins/CustomTheme/config/bootstrap.php @@ -38,8 +38,21 @@ EventManager::instance()->on('Bake.beforeRender.Controller.controller', function if ( is_array($actions) ) { $view->viewVars['actions'] = count($actions)?$actions:['_empty']; } + // Add Search.Prg component if we have configured filters in bake_extra.php + $controllerName = $view->viewVars['name']; + if ( $view->BakeExtra->hasFilters($controllerName) ) { + $view->viewVars['components'] += ['Search.Prg']; + } }); +EventManager::instance()->on('Bake.beforeRender.Model.table', function (Event $event) { + $view = $event->getSubject(); + // Add Search.Search behavior if we have configured filters in bake_extra.php + $controllerName = $view->viewVars['name']; + if ( $view->BakeExtra->hasFilters($controllerName) ) { + $view->viewVars['behaviors'] += ['Search.Search' => [] ]; + } +}); /* * Note : "cake bake all" behavior is customized in * plugins/CustomTheme/src/Shell/BakeShell.php diff --git a/fai_gestion/plugins/CustomTheme/src/Template/Bake/Element/Controller/index.twig b/fai_gestion/plugins/CustomTheme/src/Template/Bake/Element/Controller/index.twig index 181e830..8938f6a 100644 --- a/fai_gestion/plugins/CustomTheme/src/Template/Bake/Element/Controller/index.twig +++ b/fai_gestion/plugins/CustomTheme/src/Template/Bake/Element/Controller/index.twig @@ -27,7 +27,17 @@ 'contain' => [{{ Bake.stringifyList(belongsTo, {'indent': false})|raw }}] ]; {% endif %} +{%- if BakeExtra.hasFilters(name) %} + $query = $this->{{ currentModelName }} + ->find('search', ['search' => $this->request->getQueryParams()]); + ${{ pluralName }} = $this->paginate($query); +{% else %} ${{ pluralName }} = $this->paginate($this->{{ currentModelName }}); +{% endif %} + $this->set('{{ pluralName }}', ${{ pluralName }}); +{% for model in BakeExtra.getFiltersExtraModels(name, modelObj) %} - $this->set(compact('{{ pluralName }}')); + $this->loadModel('{{ model.getAlias }}'); + $this->set('{{ model.getAlias|variable }}', $this->{{ model.getAlias }}->find('list')->toArray()); +{% endfor %} } diff --git a/fai_gestion/plugins/CustomTheme/src/Template/Bake/Model/table.twig b/fai_gestion/plugins/CustomTheme/src/Template/Bake/Model/table.twig index 2ef02e9..fb3a417 100644 --- a/fai_gestion/plugins/CustomTheme/src/Template/Bake/Model/table.twig +++ b/fai_gestion/plugins/CustomTheme/src/Template/Bake/Model/table.twig @@ -156,4 +156,22 @@ class {{ name }}Table extends Table return '{{ connection }}'; } {% endif %} + +{%- if BakeExtra.hasFilters(name) %} + + /** + * @return \Search\Manager + */ + public function searchManager() + { + $searchManager = $this->behaviors()->Search->searchManager(); +{% for k, filter in BakeExtra.getFilters(name) %} + $searchManager->{{ filter.mode }}('{{ k }}', [ + {{- BakeExtra.stringifySearchManagerConfig(filter)|raw -}} + ]); +{% endfor %} + + return $searchManager; + } +{% endif %} } diff --git a/fai_gestion/plugins/CustomTheme/src/Template/Bake/Template/index.twig b/fai_gestion/plugins/CustomTheme/src/Template/Bake/Template/index.twig index 074a30b..15bfe45 100644 --- a/fai_gestion/plugins/CustomTheme/src/Template/Bake/Template/index.twig +++ b/fai_gestion/plugins/CustomTheme/src/Template/Bake/Template/index.twig @@ -39,6 +39,9 @@ * @var \{{ namespace }}\View\AppView $this * @var \{{ entityClass }}[]|\Cake\Collection\CollectionInterface ${{ pluralVar }} */ + +// index could contain table head filters and labels in aren't useful +$this->Form->setTemplates(['label' => '']); ?> {% set fields = Bake.filterFields(fields, schema, modelObject, indexColumns, ['binary', 'text']) %}