summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLudovic Pouzenc <ludovic@pouzenc.fr>2018-07-21 23:52:44 +0200
committerLudovic Pouzenc <ludovic@pouzenc.fr>2018-07-21 23:52:44 +0200
commit748b7bf7c5c4df2d546ae375d9c13b5657e431df (patch)
tree35ac11de286bdfe4b6e243e2eb05b52992b6f420
parent38fc194ababe7e1876a27697a9beb6d21623dcd6 (diff)
downloadchd_gestion-748b7bf7c5c4df2d546ae375d9c13b5657e431df.zip
chd_gestion-748b7bf7c5c4df2d546ae375d9c13b5657e431df.tar.gz
chd_gestion-748b7bf7c5c4df2d546ae375d9c13b5657e431df.tar.bz2
bake: implement virtual entity titles
-rw-r--r--app-from-scratch.sh15
-rw-r--r--fai_gestion/config/bake_extra.php20
-rw-r--r--fai_gestion/plugins/CustomTheme/config/bootstrap.php31
-rw-r--r--fai_gestion/plugins/CustomTheme/src/Template/Bake/Model/entity.twig22
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 %}
}