summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLudovic Pouzenc <ludovic@pouzenc.fr>2018-07-22 11:27:20 +0200
committerLudovic Pouzenc <ludovic@pouzenc.fr>2018-07-22 11:27:20 +0200
commit28e5cfbde499b7d08649596d0f675ad8bba0143a (patch)
treead4a9b224486d6d4148906710680fc3e64a87ace
parentb49f4d5ccd5a61e901c448ad94495750bb2d0269 (diff)
downloadchd_gestion-28e5cfbde499b7d08649596d0f675ad8bba0143a.zip
chd_gestion-28e5cfbde499b7d08649596d0f675ad8bba0143a.tar.gz
chd_gestion-28e5cfbde499b7d08649596d0f675ad8bba0143a.tar.bz2
bake: sort tables by default (with config) + allow ORDER BY hooks
-rw-r--r--app-from-scratch.sh6
-rw-r--r--fai_gestion/plugins/CustomTheme/src/Template/Bake/Model/table.twig37
2 files changed, 43 insertions, 0 deletions
diff --git a/app-from-scratch.sh b/app-from-scratch.sh
index 5d35c2b..fba441e 100644
--- a/app-from-scratch.sh
+++ b/app-from-scratch.sh
@@ -369,3 +369,9 @@ git add plugins/CustomTheme/src/View/Helper/BakeExtraHelper.php
git add config/bake_extra.php
git add ../app-from-scratch.sh
git commit -m "bake: fix Search filter support (bad stringifyList())"
+# b49f4d5ccd5a61e901c448ad94495750bb2d0269
+
+# bake: sort tables by default (with config) + allow ORDER BY hooks
+editor plugins/CustomTheme/src/Template/Bake/Model/table.twig
+git add ../app-from-scratch.sh plugins/CustomTheme/src/Template/Bake/Model/table.twig
+git commit -m "bake: sort tables by default (with config) + allow ORDER BY hooks"
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 fb3a417..788e48f 100644
--- a/fai_gestion/plugins/CustomTheme/src/Template/Bake/Model/table.twig
+++ b/fai_gestion/plugins/CustomTheme/src/Template/Bake/Model/table.twig
@@ -39,6 +39,9 @@
namespace {{ namespace }}\Model\Table;
{% set uses = ['use Cake\\ORM\\Query;', 'use Cake\\ORM\\RulesChecker;', 'use Cake\\ORM\\Table;', 'use Cake\\Validation\\Validator;'] %}
+{%- if BakeExtra.hasOrderOpts(name) or BakeExtra.hasOrderHooks(name) %}
+{% set uses = uses|merge(['use ArrayObject;', 'use Cake\\Event\\Event;']) %}
+{% endif %}
{{ uses|join('\n')|raw }}
{{ DocBlock.classDescription(name, 'Model', annotations)|raw }}
@@ -174,4 +177,38 @@ class {{ name }}Table extends Table
return $searchManager;
}
{% endif %}
+
+{%- if BakeExtra.hasOrderOpts(name) or BakeExtra.hasOrderHooks(name) %}
+
+ /**
+ * @return \Cake\ORM\Query
+ */
+ public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary)
+ {
+{%- if BakeExtra.hasOrderOpts(name) %}
+
+ $order = $query->clause('order');
+ if ($order === null || !count($order)) {
+ $query->order([
+ {{- Bake.stringifyList(BakeExtra.getOrderOpts(name), {'indent': 4})|raw -}}
+ ]);
+ }
+{% endif %}
+{%- if BakeExtra.hasOrderHooks(name) %}
+
+ $orderHooks = [
+ {{- Bake.stringifyList(BakeExtra.getOrderHooks(name), {'indent': 3})|raw -}}
+ ];
+ $order = $query->clause('order');
+ if ($order !== null && count($order)) {
+ $order->iterateParts(function ($rhs, &$lhs) use ($orderHooks) {
+ if ( array_key_exists($lhs, $orderHooks) ) {
+ $lhs = $orderHooks[$lhs];
+ }
+ return $rhs;
+ });
+ }
+{% endif %}
+ }
+{% endif %}
}