From b9451ccf0ed90c674c6f714de10a71a33a6c7b16 Mon Sep 17 00:00:00 2001 From: Ludovic Pouzenc Date: Sun, 22 Jul 2018 22:33:53 +0200 Subject: bake: allow default order by through associated tables --- fai_gestion/config/bake_extra.php | 21 ++++++++++++++++++++ .../CustomTheme/src/Template/Bake/Model/table.twig | 8 ++++++-- .../src/View/Helper/BakeExtraHelper.php | 23 ++++++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/fai_gestion/config/bake_extra.php b/fai_gestion/config/bake_extra.php index e8788c8..5bd5e89 100644 --- a/fai_gestion/config/bake_extra.php +++ b/fai_gestion/config/bake_extra.php @@ -134,6 +134,27 @@ EOT ], ], ], + 'EquipementInterfaces' => [ + 'order' => [ + 'Equipements.hostname' => 'ASC', + 'EquipementInterfaces.title' => 'ASC', + ], + 'filters' => [ + 'q' => [ + 'mode' => 'like', + 'before' => true, + 'after' => true, + 'field' => ['Equipements.hostname', 'Equipements.ip_management', 'EquipementInterfaces.title', 'mac'], + 'colspan' => 3, + 'hint' => 'Hostname, IP, MAC...', + ], + 'lien_radio_id' => [ + 'mode' => 'value', + 'colspan' => 1, + 'hint' => 'LienRadios', + ], + ], + ], 'EquipementModeles' => [ 'order' => [ 'EquipementModeles.constructeur' => 'ASC', 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 788e48f..c4a158c 100644 --- a/fai_gestion/plugins/CustomTheme/src/Template/Bake/Model/table.twig +++ b/fai_gestion/plugins/CustomTheme/src/Template/Bake/Model/table.twig @@ -185,10 +185,14 @@ class {{ name }}Table extends Table */ public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary) { -{%- if BakeExtra.hasOrderOpts(name) %} - +{% if BakeExtra.hasOrderOpts(name) %} $order = $query->clause('order'); if ($order === null || !count($order)) { +{% if BakeExtra.hasOrderContains(name) %} + $query->contain([ + {{- Bake.stringifyList(BakeExtra.getOrderContains(name), {'indent': 4})|raw -}} + ]); +{% endif %} $query->order([ {{- Bake.stringifyList(BakeExtra.getOrderOpts(name), {'indent': 4})|raw -}} ]); diff --git a/fai_gestion/plugins/CustomTheme/src/View/Helper/BakeExtraHelper.php b/fai_gestion/plugins/CustomTheme/src/View/Helper/BakeExtraHelper.php index eb4b1df..44b28d4 100644 --- a/fai_gestion/plugins/CustomTheme/src/View/Helper/BakeExtraHelper.php +++ b/fai_gestion/plugins/CustomTheme/src/View/Helper/BakeExtraHelper.php @@ -120,6 +120,29 @@ class BakeExtraHelper extends Helper return Hash::get($this->_config, "templateExtra.$controllerName.order"); } + public function hasOrderContains($controllerName) { + if ( ! is_string($controllerName) ) return FALSE; + $orderContains = $this->getOrderContains($controllerName); + return (is_array($orderContains) && count($orderContains) > 0); + } + + public function getOrderContains($controllerName) { + if ( ! is_string($controllerName) ) return FALSE; + $orderOpts = $this->getOrderOpts($controllerName); + $contains = collection(array_keys($orderOpts)) + ->map( + function ($item, $key) use ($controllerName) { + $alias = substr($item, 0, strpos($item, '.')); + return ($alias===$controllerName)?FALSE:$alias; + } + )->filter( + function ($item, $key) { + return $item !== FALSE; + } + )->toArray(); + return $contains; + } + public function hasOrderHooks($controllerName) { if ( ! is_string($controllerName) ) return FALSE; $orderHooks = $this->getOrderHooks($controllerName); -- cgit v1.1