diff options
author | Ludovic Pouzenc <ludovic@pouzenc.fr> | 2018-07-20 00:41:45 +0200 |
---|---|---|
committer | Ludovic Pouzenc <ludovic@pouzenc.fr> | 2018-07-20 00:41:45 +0200 |
commit | b59164b0b7451905adcca244e6395281eb0633ca (patch) | |
tree | 098111b525b796221a931aaae5c1688cf1a45d88 | |
parent | 76ab63096a25f5bf7b9d076cdc9ab3fc5218d216 (diff) | |
download | chd_gestion-b59164b0b7451905adcca244e6395281eb0633ca.zip chd_gestion-b59164b0b7451905adcca244e6395281eb0633ca.tar.gz chd_gestion-b59164b0b7451905adcca244e6395281eb0633ca.tar.bz2 |
bake add Search filter support
-rw-r--r-- | app-from-scratch.sh | 23 | ||||
-rw-r--r-- | fai_gestion/composer.json | 1 | ||||
-rw-r--r-- | fai_gestion/composer.lock | 58 | ||||
-rw-r--r-- | fai_gestion/config/bake_extra.php | 8 | ||||
-rw-r--r-- | fai_gestion/config/bootstrap.php | 11 | ||||
-rw-r--r-- | fai_gestion/plugins/CustomTheme/config/bootstrap.php | 13 | ||||
-rw-r--r-- | fai_gestion/plugins/CustomTheme/src/Template/Bake/Element/Controller/index.twig | 12 | ||||
-rw-r--r-- | fai_gestion/plugins/CustomTheme/src/Template/Bake/Model/table.twig | 18 | ||||
-rw-r--r-- | fai_gestion/plugins/CustomTheme/src/Template/Bake/Template/index.twig | 28 | ||||
-rw-r--r-- | fai_gestion/plugins/CustomTheme/src/View/Helper/BakeExtraHelper.php | 35 | ||||
-rw-r--r-- | fai_gestion/src/Application.php | 2 | ||||
-rw-r--r-- | fai_gestion/src/Template/Layout/default.ctp | 1 | ||||
-rw-r--r-- | fai_gestion/webroot/css/local.css | 28 |
13 files changed, 229 insertions, 9 deletions
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 <th> aren't useful +$this->Form->setTemplates(['label' => '']); ?> {% set fields = Bake.filterFields(fields, schema, modelObject, indexColumns, ['binary', 'text']) %} <nav class="large-3 medium-4 columns" id="actions-sidebar"> @@ -61,6 +64,31 @@ <h3><?= __('{{ pluralHumanName }}') ?></h3> <table cellpadding="0" cellspacing="0"> <thead> +{% if BakeExtra.hasFilters(modelClass) %} + <tr class="filter"> + <?= $this->Form->create(null, ['valueSources' => 'query']) . "\n"; ?> +{% for k, filter in BakeExtra.getFilters(modelClass) %} + <th colspan="{{ filter.colspan }}"> + <?= $this->Form->control('{{ k }}', [ + 'placeholder' => __('{{ filter.hint }}'), + 'empty' => __('{{ filter.hint }}') + ]) . "\n"; ?> + </th> +{% endfor %} + <th class="actions"> + <?php + echo $this->Form->button('Filter', [ 'type' => 'submit' ]); + if (!empty($_isSearch)) { + echo " " . $this->Html->link('Reset', [ 'action' => 'index' ]); + } + echo "\n"; + ?> + </th> +{% if BakeExtra.hasFilters(modelClass) %} + <?= $this->Form->end() . "\n"; ?> +{% endif %} + </tr> +{% endif %} <tr> {% for field in fields %} <th scope="col"><?= $this->Paginator->sort('{{ field }}') ?></th> diff --git a/fai_gestion/plugins/CustomTheme/src/View/Helper/BakeExtraHelper.php b/fai_gestion/plugins/CustomTheme/src/View/Helper/BakeExtraHelper.php index 0e94b55..f58de2a 100644 --- a/fai_gestion/plugins/CustomTheme/src/View/Helper/BakeExtraHelper.php +++ b/fai_gestion/plugins/CustomTheme/src/View/Helper/BakeExtraHelper.php @@ -26,6 +26,8 @@ use Cake\Utility\Hash; class BakeExtraHelper extends Helper { + public $helpers = ['Bake']; + public function hello() { return "<!-- BakeExtra::hello()\n" . print_r($this->_config, true) . "\n-->\n"; @@ -42,6 +44,38 @@ class BakeExtraHelper extends Helper return Hash::get($this->_config, "templateExtra.$controllerName.filters"); } + public function getFiltersExtraModels($controllerName, $modelObj) { + $extraModels=[]; + $filters = $this->getFilters($controllerName); + if ( is_array($filters) && is_object($modelObj) ) { + $searchFields = []; + foreach ($filters as $k => $filter) { + $searchFields += array_keys( $this->getFilters($controllerName) ); + } + foreach ($modelObj->associations()->getByType('BelongsTo') as $model) { + if ( in_array($model->getForeignKey(), $searchFields) ) { + $extraModels[] = $model; + } + } + } + return $extraModels; + } + public function stringifySearchManagerConfig($filterConfig) { + $field = array_key_exists('field', $filterConfig)?$filterConfig['field']:FALSE; + unset($filterConfig['field']); + unset($filterConfig['mode']); + unset($filterConfig['colspan']); + unset($filterConfig['hint']); + $s = ''; + if ($field) { + $s .= "\n 'field' => ["; + $s .= $this->Bake->stringifyList($field, ['indent'=>4]); + $s .= "],"; + } + $s .= $this->Bake->stringifyList($filterConfig, ['indent'=>3]); + return $s; + } + public function hasAction($action, $controllerName) { if ( ! is_string($controllerName) ) return FALSE; return in_array($action, $this->getActions($controllerName)); @@ -60,4 +94,5 @@ class BakeExtraHelper extends Helper if ( ! is_string($controllerName) ) return FALSE; return Hash::get($this->_config, "templateExtra.$controllerName.title"); } + } diff --git a/fai_gestion/src/Application.php b/fai_gestion/src/Application.php index 48484b4..10c630b 100644 --- a/fai_gestion/src/Application.php +++ b/fai_gestion/src/Application.php @@ -35,6 +35,8 @@ class Application extends BaseApplication */ public function bootstrap() { + $this->addPlugin('Search'); + // Call parent to load bootstrap from files. parent::bootstrap(); diff --git a/fai_gestion/src/Template/Layout/default.ctp b/fai_gestion/src/Template/Layout/default.ctp index caf014e..b412f64 100644 --- a/fai_gestion/src/Template/Layout/default.ctp +++ b/fai_gestion/src/Template/Layout/default.ctp @@ -28,6 +28,7 @@ $cakeDescription = 'CakePHP: the rapid development php framework'; <?= $this->Html->css('base.css') ?> <?= $this->Html->css('style.css') ?> + <?= $this->Html->css('local.css') ?> <?= $this->fetch('meta') ?> <?= $this->fetch('css') ?> diff --git a/fai_gestion/webroot/css/local.css b/fai_gestion/webroot/css/local.css new file mode 100644 index 0000000..b55ce54 --- /dev/null +++ b/fai_gestion/webroot/css/local.css @@ -0,0 +1,28 @@ +/** + * Copyright 2016-2018 Ludovic Pouzenc <ludovic@pouzenc.fr> + * Copyright 2016 Nicolas Goaziou <mail@nicolasgoaziou.fr> + * + * This file is part of FAI Gestion forked from CHD Gestion. + * + * FAI Gestion is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * FAI Gestion is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with FAI Gestion. If not, see <http://www.gnu.org/licenses/>. +**/ +/* index Template : add filter fields, make it skinny */ +.filter th { + padding-top:0; + padding-bottom:0; +} + +.filter input,button { + padding: 0.5rem; +} |