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 /fai_gestion/plugins | |
parent | 76ab63096a25f5bf7b9d076cdc9ab3fc5218d216 (diff) | |
download | chd_gestion-b59164b0b7451905adcca244e6395281eb0633ca.zip chd_gestion-b59164b0b7451905adcca244e6395281eb0633ca.tar.gz chd_gestion-b59164b0b7451905adcca244e6395281eb0633ca.tar.bz2 |
bake add Search filter support
Diffstat (limited to 'fai_gestion/plugins')
5 files changed, 105 insertions, 1 deletions
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"); } + } |