From 0dd134b71539f38d84db3b023ba42d2650df278a Mon Sep 17 00:00:00 2001 From: Ludovic Pouzenc Date: Thu, 19 Jul 2018 00:20:03 +0200 Subject: bake: Add configurability, generate less actions --- app-from-scratch.sh | 21 +++++ fai_gestion/config/bake_extra.php | 95 ++++++++++++++++++++++ fai_gestion/config/bootstrap_cli.php | 4 +- .../plugins/CustomTheme/config/bootstrap.php | 47 +++++++++++ .../Template/Bake/Element/Controller/_empty.twig | 34 ++++++++ .../src/View/Helper/BakeExtraHelper.php | 63 ++++++++++++++ 6 files changed, 263 insertions(+), 1 deletion(-) create mode 100644 fai_gestion/config/bake_extra.php create mode 100644 fai_gestion/plugins/CustomTheme/config/bootstrap.php create mode 100644 fai_gestion/plugins/CustomTheme/src/Template/Bake/Element/Controller/_empty.twig create mode 100644 fai_gestion/plugins/CustomTheme/src/View/Helper/BakeExtraHelper.php diff --git a/app-from-scratch.sh b/app-from-scratch.sh index bacf80e..a168168 100644 --- a/app-from-scratch.sh +++ b/app-from-scratch.sh @@ -253,3 +253,24 @@ cp -a vendor/cakephp/bake/src/Shell/BakeShell.php plugins/CustomTheme/src/Shell/ editor plugins/CustomTheme/src/Shell/BakeShell.php git add plugins/CustomTheme/src/Shell/BakeShell.php git commit -m "bake : Commit imported&derived files from bake to keep the diffs" +# 1eeb6c1ee3b538e2a3f5b940c32725ec068f21ba + +# bake: Add config, generate less actions +# https://book.cakephp.org/3.0/fr/bake/development.html#creating-a-bake-theme +editor config/bake_extra.php +git add config/bake_extra.php + +editor config/bootstrap_cli.php plugins/CustomTheme/config/bootstrap.php +git add config/bootstrap_cli.php plugins/CustomTheme/config/bootstrap.php + +editor plugins/CustomTheme/src/Template/Bake/Element/Controller/_empty.twig +git add plugins/CustomTheme/src/Template/Bake/Element/Controller/_empty.twig + +editor plugins/CustomTheme/src/Shell/BakeShell.php +git add plugins/CustomTheme/src/Shell/BakeShell.php + +editor plugins/CustomTheme/src/View/Helper/BakeExtraHelper.php +git add plugins/CustomTheme/src/View/Helper/BakeExtraHelper.php + +git add ../app-from-scratch.sh +git commit -m "bake: Add configurability, generate less actions" diff --git a/fai_gestion/config/bake_extra.php b/fai_gestion/config/bake_extra.php new file mode 100644 index 0000000..e33b2a0 --- /dev/null +++ b/fai_gestion/config/bake_extra.php @@ -0,0 +1,95 @@ + + * Copyright 2016 Nicolas Goaziou + * + * 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 . +**/ +/* + * This config file is used twice when "cake bake all" is run : + * + * -> config/bootstrap_cli.php : Plugin::load('CustomTheme',['bootstrap' => true,...]) + * (1)->plugins/CustomTheme/src/Shell/BakeShell.php + * startup() include this file and set Model->skipTables + * all() set $this->{$task}->params at each task run + * + * So "all" subcommand is limited/tweaked by $skipTables and $taskParams + * + * (2)->plugins/CustomTheme/config/bootstrap.php: + * EventManager::instance()->on('Bake.initialize',...) + * $extra = include(); + * $view->loadHelper('CustomTheme.BakeExtra', $extra); + * EventManager::instance()->on('Bake.beforeRender.Controller.controller',...) + * set $view->viewVars['actions'] with $extra + * + * So the whole config it goes into CustomTheme\BakeExtraHelper $config + * and becomes available in all .twig via BakeExtraHelper methods. + */ + +$skipTables = [ 'network_edge_list_tree' ]; +$taskParams = [ + 'default' => [ + 'theme' => 'CustomTheme', + 'no-fixture' => true, + 'no-test' => true, + 'no-rules' => true, + 'index-columns' => 6 + #'components' => 'hello,world', + ], + 'Adherents' => [ 'index-columns' => 7 ], +]; +$controllerActions = [ + 'default' => [ 'index', 'view', 'add', 'edit'], + 'AdherentRoles' => [ 'index', 'add', 'delete' ], + 'AdherentRoleTypes' => [ ], +]; +$templateExtra = [ + 'Adherents' => [ + 'title' => [ + 'custom_code' => <<<'EOT' + "ADT" . $this->_properties['id'] . " - " . ( $this->_properties['raison']?($this->_properties['raison'] . " (" . $this->_properties['nom'] . ")" ):($this->_properties['nom'] . " " . $this->_properties['prenom']) ) +EOT + ], + 'filters' => [ + 'q' => [ + 'mode' => 'like', + 'before' => 'true', + 'after' => 'true', + 'columns' => ['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', + ], + ], + ], +]; + +return compact('skipTables', 'taskParams', 'controllerActions', 'templateExtra'); diff --git a/fai_gestion/config/bootstrap_cli.php b/fai_gestion/config/bootstrap_cli.php index 43b569a..f645560 100644 --- a/fai_gestion/config/bootstrap_cli.php +++ b/fai_gestion/config/bootstrap_cli.php @@ -29,4 +29,6 @@ use Cake\Core\Plugin; Configure::write('Log.debug.file', 'cli-debug'); Configure::write('Log.error.file', 'cli-error'); -Plugin::load('CustomTheme', ['bootstrap' => false, 'routes' => true]); +// Bake tweaking +// https://book.cakephp.org/3.0/en/bake/development.html +Plugin::load('CustomTheme', ['bootstrap' => true, 'routes' => true]); diff --git a/fai_gestion/plugins/CustomTheme/config/bootstrap.php b/fai_gestion/plugins/CustomTheme/config/bootstrap.php new file mode 100644 index 0000000..f90c873 --- /dev/null +++ b/fai_gestion/plugins/CustomTheme/config/bootstrap.php @@ -0,0 +1,47 @@ + + * Copyright 2016 Nicolas Goaziou + * + * 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 . +**/ +use Cake\Event\Event; +use Cake\Event\EventManager; + +// https://book.cakephp.org/3.0/en/bake/development.html + +EventManager::instance()->on('Bake.initialize', function (Event $event) { + $view = $event->getSubject(); + // Initialize BakeExtraHelper with bake_extra.php config array + $extra = include(ROOT . DS . 'config' . DS . 'bake_extra.php'); + $view->loadHelper('CustomTheme.BakeExtra', $extra); +}); + +EventManager::instance()->on('Bake.beforeRender.Controller.controller', function (Event $event) { + $view = $event->getSubject(); + // Override controller's default action list with the configured one in bake_extra.php + $controllerName = $view->viewVars['name']; + $actions = $view->BakeExtra->getActions($controllerName); + if ( is_array($actions) ) { + $view->viewVars['actions'] = count($actions)?$actions:['_empty']; + } +}); + +/* + * Note : "cake bake all" behavior is customized in + * plugins/CustomTheme/src/Shell/BakeShell.php + * and use the same bake_extra.php config file. + */ diff --git a/fai_gestion/plugins/CustomTheme/src/Template/Bake/Element/Controller/_empty.twig b/fai_gestion/plugins/CustomTheme/src/Template/Bake/Element/Controller/_empty.twig new file mode 100644 index 0000000..8175902 --- /dev/null +++ b/fai_gestion/plugins/CustomTheme/src/Template/Bake/Element/Controller/_empty.twig @@ -0,0 +1,34 @@ +{# +/* + * Copyright 2016-2018 Ludovic Pouzenc + * Copyright 2016 Nicolas Goaziou + * + * 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 . + * + */ +#} +{% set compact = ["'#{singularName}'"] %} + + /** + * _empty method + * Generated when no other actions requested + * Prevents bake all to make all $scaffoldActions when unwanted + * + * @return void + */ + public function _empty() + { + } diff --git a/fai_gestion/plugins/CustomTheme/src/View/Helper/BakeExtraHelper.php b/fai_gestion/plugins/CustomTheme/src/View/Helper/BakeExtraHelper.php new file mode 100644 index 0000000..0e94b55 --- /dev/null +++ b/fai_gestion/plugins/CustomTheme/src/View/Helper/BakeExtraHelper.php @@ -0,0 +1,63 @@ + + * Copyright 2016 Nicolas Goaziou + * + * 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 . + * +**/ +namespace CustomTheme\View\Helper; + +use Cake\View\Helper; +use Cake\Utility\Hash; + +class BakeExtraHelper extends Helper +{ + public function hello() + { + return "\n"; + } + + public function hasFilters($controllerName) { + if ( ! is_string($controllerName) ) return FALSE; + $filters = $this->getFilters($controllerName); + return (is_array($filters) && count($filters) > 0); + } + + public function getFilters($controllerName) { + if ( ! is_string($controllerName) ) return FALSE; + return Hash::get($this->_config, "templateExtra.$controllerName.filters"); + } + + public function hasAction($action, $controllerName) { + if ( ! is_string($controllerName) ) return FALSE; + return in_array($action, $this->getActions($controllerName)); + } + + public function getActions($controllerName) { + if ( ! is_string($controllerName) ) return FALSE; + $actions = Hash::get($this->_config, "controllerActions.$controllerName"); + if ( ! is_array($actions) ) { + $actions = Hash::get($this->_config, "controllerActions.default"); + } + return $actions; + } + + public function getTitleOpts($controllerName) { + if ( ! is_string($controllerName) ) return FALSE; + return Hash::get($this->_config, "templateExtra.$controllerName.title"); + } +} -- cgit v1.1