From 748b7bf7c5c4df2d546ae375d9c13b5657e431df Mon Sep 17 00:00:00 2001 From: Ludovic Pouzenc Date: Sat, 21 Jul 2018 23:52:44 +0200 Subject: bake: implement virtual entity titles --- app-from-scratch.sh | 15 ++++++++--- fai_gestion/config/bake_extra.php | 20 +++++++++----- .../plugins/CustomTheme/config/bootstrap.php | 31 ++++++++++++++++++++++ .../src/Template/Bake/Model/entity.twig | 22 ++++++++++++++- 4 files changed, 77 insertions(+), 11 deletions(-) diff --git a/app-from-scratch.sh b/app-from-scratch.sh index e06be6c..c0d9811 100644 --- a/app-from-scratch.sh +++ b/app-from-scratch.sh @@ -42,7 +42,7 @@ cake server -p 8080 # -> everything green except database connection ^C -# prepare for apache2 +# prepare for apache2 ln -s ~intarnet/chd_gestion/fai_gestion /var/www/intarnet.fr/gestion sudo chgrp -R www-data tmp/ logs/ sudo chmod -R ug=rwX,o=rX tmp/ logs/ @@ -200,7 +200,7 @@ cat ~/chd_gestion/schema/enum-tables-data-mysql.sql | sudo mariadb fai_gestion do [ $i -ne 254 ] && sep=',' || sep=';' printf "('185.131.43.%i', '2a03:a0a1:1:%x::', '1')$sep\n" $i $i - done + done ) | sudo mariadb fai_gestion # Convert equipements.uplink tree to (equipement_interfaces, lien_cable, lien_radio, lien_tunnel) @@ -346,8 +346,17 @@ git add ../app-from-scratch.sh git commit -m "rename ippubliques -> ip_publiques, .gitignore autogenerated code for now" # 51f0aacb4d4f2514433139552ac6ab8ab5506653 -# no 200 item limit on drop-down lists, index : better codegen +# no 200 item limit on drop-down lists, index : better codegen editor plugins/CustomTheme/src/Template/Bake/Element/Controller/* git add plugins/CustomTheme/src/Template/Bake/Element/Controller/* git add ../app-from-scratch.sh git commit -m "no 200 item limit on drop-down lists, index : better codegen" +# 61cad7c5f1680e3c91da16f2d5deb38ceb73fffb + +# bake: implement virtual entity titles +editor plugins/CustomTheme/config/bootstrap.php plugins/CustomTheme/src/Template/Bake/Model/entity.twig +editor config/bake_extra.php +git add plugins/CustomTheme/config/bootstrap.php plugins/CustomTheme/src/Template/Bake/Model/entity.twig +git add config/bake_extra.php +git add ../app-from-scratch.sh +git commit -m "bake: implement virtual entity titles" diff --git a/fai_gestion/config/bake_extra.php b/fai_gestion/config/bake_extra.php index 788c073..67030bf 100644 --- a/fai_gestion/config/bake_extra.php +++ b/fai_gestion/config/bake_extra.php @@ -74,8 +74,12 @@ $controllerActions = [ $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']) ) + 'customCode' => <<<'EOT' + return "ADT" . $this->id . " - " + . ($this->raison + ?($this->raison . " (" . $this->nom . ")" ) + :($this->nom . " " . $this->prenom) + ); EOT ], 'filters' => [ @@ -101,9 +105,7 @@ EOT ], 'Equipements' => [ 'title' => [ - 'custom_code' => <<<'EOT' - $this->_properties['ip_management'] . ( $this->_properties['hostname']?(" (" . $this->_properties['hostname'] . ")"):"") -EOT + 'pieces' => ['hostname', 'ip_management'], ], 'filters' => [ 'q' => [ @@ -128,7 +130,7 @@ EOT ], 'EquipementModeles' => [ 'title' => [ - 'glue' => ' ', + 'glue' => ' - ', 'pieces' => ['constructeur', 'modele'], ], ], @@ -170,9 +172,13 @@ EOT ], ], ], + 'LienRadios' => [ + 'title' => [ + 'pieces' => ['ssid'], + ], + ], 'Services' => [ 'title' => [ - 'glue' => '-', 'prefix' => 'SER', 'pieces' => ['id'], ], diff --git a/fai_gestion/plugins/CustomTheme/config/bootstrap.php b/fai_gestion/plugins/CustomTheme/config/bootstrap.php index 7215ce7..351e582 100644 --- a/fai_gestion/plugins/CustomTheme/config/bootstrap.php +++ b/fai_gestion/plugins/CustomTheme/config/bootstrap.php @@ -20,6 +20,7 @@ **/ use Cake\Event\Event; use Cake\Event\EventManager; +use Cake\Utility\Inflector; // https://book.cakephp.org/3.0/en/bake/development.html @@ -52,6 +53,36 @@ EventManager::instance()->on('Bake.beforeRender.Model.table', function (Event $e if ( $view->BakeExtra->hasFilters($controllerName) ) { $view->viewVars['behaviors'] += ['Search.Search' => [] ]; } + $titleOpts = $view->BakeExtra->getTitleOpts($controllerName); + // Force displayField to be the custom title if any defined in bake_extra.php + if ( is_array($titleOpts) ) { + $view->viewVars['displayField'] = 'title'; + } +}); + +EventManager::instance()->on('Bake.beforeRender.Model.entity', function (Event $event) { + $view = $event->getSubject(); + // Set template variables for virtual title if any defined in bake_extra.php + $controllerName = Inflector::pluralize($view->viewVars['name']); + $titleOpts = $view->BakeExtra->getTitleOpts($controllerName); + + // viewVars are kept from one entity to another (Cake bug ?) + unset($view->viewVars['virtualTitleGlue']); + unset($view->viewVars['virtualTitlePieces']); + unset($view->viewVars['virtualTitleCustomCode']); + + if ( is_array($titleOpts) ) { + foreach ($titleOpts as $k => $v) { + // For pieces array, prefix each item to have '$this->col_name' + if ( $k === 'pieces' && is_array($v) ) { + $v = preg_filter('/^/', '$this->', $v); + } + // Prefix each key (less view namespace problems and get more explcit in .twig + $k = 'virtualTitle'.Inflector::camelize($k); + // Export options to the view + $view->viewVars[$k] = $v; + } + } }); /* * Note : "cake bake all" behavior is customized in diff --git a/fai_gestion/plugins/CustomTheme/src/Template/Bake/Model/entity.twig b/fai_gestion/plugins/CustomTheme/src/Template/Bake/Model/entity.twig index aaa15e5..527a30b 100644 --- a/fai_gestion/plugins/CustomTheme/src/Template/Bake/Model/entity.twig +++ b/fai_gestion/plugins/CustomTheme/src/Template/Bake/Model/entity.twig @@ -76,7 +76,27 @@ class {{ name }} extends Entity protected $_hidden = [{{ Bake.stringifyList(hidden)|raw }}]; {% endif %} -{%- if not accessible and not hidden %} +{%- if virtualTitleCustomCode or virtualTitlePieces %} + /** + * Virtual fields to be exposed (serialized) + */ + protected $_virtual = ['title']; + + /** + * Virtual field for pretty print in related table's views + * + * @return String + */ + protected function _getTitle() + { +{%- if virtualTitleCustomCode %} + +{{ virtualTitleCustomCode|raw }} +{% else %} + + return "{{ virtualTitlePrefix }}" . implode("{{ virtualTitleGlue??' ' }}", [{{ Bake.stringifyList(virtualTitlePieces, { indent:3, quotes: false } )|raw }}]); +{% endif %} + } {% endif %} } -- cgit v1.1