From db99658f51f8e9d3ec417eb636bbe7af5a08b57b Mon Sep 17 00:00:00 2001 From: pbarroca <pbarroca@afi-sa.fr> Date: Thu, 27 Apr 2017 12:14:06 +0200 Subject: [PATCH] dev #59717 : handling articles configuration --- .../Form/Configuration/Widget/Articles.php | 36 ++++--- .../Form/Configuration/Widget/Base.php | 2 +- .../Form/Configuration/Widget/Calendar.php | 8 +- .../Form/Decorator/ActivitiesSelector.php | 19 ++-- .../VersionCompare/ActivitiesSelector.php | 2 +- .../AlbumsCategoriesSelector.php | 2 +- .../Decorator/VersionCompare/DomainSelect.php | 2 +- .../Decorator/VersionCompare/TreeSelect.php | 68 ++++++++------ .../VersionCompare/TreeSelectSingle.php | 94 +++++++++++++++++++ library/ZendAfi/Form/Element/TreeSelect.php | 1 + 10 files changed, 174 insertions(+), 60 deletions(-) create mode 100644 library/ZendAfi/Form/Decorator/VersionCompare/TreeSelectSingle.php diff --git a/library/ZendAfi/Form/Configuration/Widget/Articles.php b/library/ZendAfi/Form/Configuration/Widget/Articles.php index 64e329955f5..2db30375227 100644 --- a/library/ZendAfi/Form/Configuration/Widget/Articles.php +++ b/library/ZendAfi/Form/Configuration/Widget/Articles.php @@ -22,15 +22,6 @@ class ZendAfi_Form_Configuration_Widget_Articles extends ZendAfi_Form_Configuration_Widget_Base { public function init() { - Class_ScriptLoader::getInstance() - ->addJQueryReady( - 'formSelectToggleVisibilityForElement("input[name=\'display_order\']", "#fieldset-datas tr:nth-child(2)", ["DateCreationDesc", "DebutPublicationDesc", "EventDebut", "Random", "CommentCount"]);' - - . 'formSelectToggleVisibilityForElement("input[name=\'display_order\']", "#fieldset-datas tr:nth-child(3)", "Random");' - - . 'checkBoxToggleVisibilityForElement("#allow_link_on_title", "#fieldset-affichage tr:nth-child(4)", true);'); - - parent::init(); $this @@ -65,6 +56,14 @@ class ZendAfi_Form_Configuration_Widget_Articles extends ZendAfi_Form_Configurat ->addElement('imagesViewer', 'style_liste', ['label' => $this->_('Style de représentation')]); + + Class_ScriptLoader::getInstance() + ->addJQueryReady( + 'formSelectToggleVisibilityForElement("input[name=\'display_order\']", "#fieldset-datas tr:nth-child(2)", ["DateCreationDesc", "DebutPublicationDesc", "EventDebut", "Random", "CommentCount"]);' + + . 'formSelectToggleVisibilityForElement("input[name=\'display_order\']", "#fieldset-datas tr:nth-child(3)", "Random");' + + . 'checkBoxToggleVisibilityForElement("#allow_link_on_title", "#fieldset-affichage tr:nth-child(4)", true);'); } @@ -75,7 +74,17 @@ class ZendAfi_Form_Configuration_Widget_Articles extends ZendAfi_Form_Configurat $this->addElement('treeSelect', 'articles_selector', ['IdItems' => isset($datas['id_items']) ? $datas['id_items'] : '', + 'ItemRenderer' => function($id) + { + if ($model = Class_Article::find($id)) + return $model->getTitre(); + }, 'IdCategories' => isset($datas['id_categorie']) ? $datas['id_categorie'] : '', + 'CategoryRenderer' => function($id) + { + if ($model = Class_ArticleCategorie::find($id)) + return implode(' > ', $model->getPathParts()); + }, 'UrlDataSource' => Class_Url::assemble(['module' => 'admin', 'controller' => 'bib', 'action' => 'articles', @@ -87,11 +96,10 @@ class ZendAfi_Form_Configuration_Widget_Articles extends ZendAfi_Form_Configurat 'nb_aff', 'nb_analyse']); - parent::populate($datas); - - return $this->addToStyleGroup(['display_titles_only', - 'allow_link_on_title', - 'anchor']); + $this->addToStyleGroup(['display_titles_only', + 'allow_link_on_title', + 'anchor']); + return parent::populate($datas); } } \ No newline at end of file diff --git a/library/ZendAfi/Form/Configuration/Widget/Base.php b/library/ZendAfi/Form/Configuration/Widget/Base.php index 42ea2b41da2..5441df3111c 100644 --- a/library/ZendAfi/Form/Configuration/Widget/Base.php +++ b/library/ZendAfi/Form/Configuration/Widget/Base.php @@ -43,7 +43,7 @@ class ZendAfi_Form_Configuration_Widget_Base extends ZendAfi_Form { } - public function populate($datas) { + public function populate(array $datas) { $this ->addToHeadGroup(['titre', 'style_liste']) diff --git a/library/ZendAfi/Form/Configuration/Widget/Calendar.php b/library/ZendAfi/Form/Configuration/Widget/Calendar.php index bc8daed7fcc..6bd960e83fe 100644 --- a/library/ZendAfi/Form/Configuration/Widget/Calendar.php +++ b/library/ZendAfi/Form/Configuration/Widget/Calendar.php @@ -104,9 +104,13 @@ class ZendAfi_Form_Configuration_Widget_Calendar extends ZendAfi_Form_Configurat 'action' => 'articles', 'id_bib' => isset($datas['id_bib']) ? $datas['id_bib'] : '', 'categories_only' => 1]), - 'InputIdCategoriesName' => 'id_categorie', 'IdItems' => '', - 'IdCategories' => isset($datas['id_categorie']) ? $datas['id_categorie'] : '']); + 'IdCategories' => isset($datas['id_categorie']) ? $datas['id_categorie'] : '', + 'CategoryRenderer' => function($id) + { + if ($model = Class_ArticleCategorie::find($id)) + return implode(' > ', $model->getPathParts()); + },]); $this ->addToSelectionGroup(['data_source']) diff --git a/library/ZendAfi/Form/Decorator/ActivitiesSelector.php b/library/ZendAfi/Form/Decorator/ActivitiesSelector.php index 3456c28f03c..36879dd7a3f 100644 --- a/library/ZendAfi/Form/Decorator/ActivitiesSelector.php +++ b/library/ZendAfi/Form/Decorator/ActivitiesSelector.php @@ -27,14 +27,15 @@ class ZendAfi_Form_Decorator_ActivitiesSelector extends Zend_Form_Decorator_Abst $helper = $view->getHelper('TreeSelect'); $helper->categoriesNotSelectable(); - return $helper->treeSelect($this->_element->getValue(), - '', - true, - $view->url(['module' => 'admin', - 'controller' => 'activity', - 'action' => 'get']), - 'form', - null, - $this->_element->getName()); + return $helper + ->treeSelect($this->_element->getValue(), + '', + true, + $view->url(['module' => 'admin', + 'controller' => 'activity', + 'action' => 'get']), + 'form', + null, + $this->_element->getName()); } } \ No newline at end of file diff --git a/library/ZendAfi/Form/Decorator/VersionCompare/ActivitiesSelector.php b/library/ZendAfi/Form/Decorator/VersionCompare/ActivitiesSelector.php index dc5af860a58..edcf70c9a9f 100644 --- a/library/ZendAfi/Form/Decorator/VersionCompare/ActivitiesSelector.php +++ b/library/ZendAfi/Form/Decorator/VersionCompare/ActivitiesSelector.php @@ -21,7 +21,7 @@ class ZendAfi_Form_Decorator_VersionCompare_ActivitiesSelector - extends ZendAfi_Form_Decorator_VersionCompare_TreeSelect { + extends ZendAfi_Form_Decorator_VersionCompare_TreeSelectSingle { protected $_current_separator = '-'; diff --git a/library/ZendAfi/Form/Decorator/VersionCompare/AlbumsCategoriesSelector.php b/library/ZendAfi/Form/Decorator/VersionCompare/AlbumsCategoriesSelector.php index 32687d18fb7..388bcfdd31c 100644 --- a/library/ZendAfi/Form/Decorator/VersionCompare/AlbumsCategoriesSelector.php +++ b/library/ZendAfi/Form/Decorator/VersionCompare/AlbumsCategoriesSelector.php @@ -21,7 +21,7 @@ class ZendAfi_Form_Decorator_VersionCompare_AlbumsCategoriesSelector - extends ZendAfi_Form_Decorator_VersionCompare_TreeSelect { + extends ZendAfi_Form_Decorator_VersionCompare_TreeSelectSingle { protected $_current_separator = '-'; diff --git a/library/ZendAfi/Form/Decorator/VersionCompare/DomainSelect.php b/library/ZendAfi/Form/Decorator/VersionCompare/DomainSelect.php index a5f246b5e5e..7778108eddd 100644 --- a/library/ZendAfi/Form/Decorator/VersionCompare/DomainSelect.php +++ b/library/ZendAfi/Form/Decorator/VersionCompare/DomainSelect.php @@ -21,7 +21,7 @@ class ZendAfi_Form_Decorator_VersionCompare_DomainSelect - extends ZendAfi_Form_Decorator_VersionCompare_TreeSelect { + extends ZendAfi_Form_Decorator_VersionCompare_TreeSelectSingle { protected function _find($id) { return Class_Catalogue::find($id); diff --git a/library/ZendAfi/Form/Decorator/VersionCompare/TreeSelect.php b/library/ZendAfi/Form/Decorator/VersionCompare/TreeSelect.php index 7700312e5c2..3a1807015be 100644 --- a/library/ZendAfi/Form/Decorator/VersionCompare/TreeSelect.php +++ b/library/ZendAfi/Form/Decorator/VersionCompare/TreeSelect.php @@ -21,35 +21,41 @@ class ZendAfi_Form_Decorator_VersionCompare_TreeSelect - extends ZendAfi_Form_Decorator_VersionCompare{ + extends ZendAfi_Form_Decorator_VersionCompare_TreeSelectSingle { protected $_version_separator = '-', - $_current_separator = ';'; + $_current_separator = '-'; - protected function _renderCurrentValue() { - return $this->_renderValue($this->_getCurrentIds()); + protected function _getCurrentIds() { + return [$this->_idsFromAttrib('IdItems'), + $this->_idsFromAttrib('IdCategories')]; } - protected function _getCurrentIds() { - return $this->_getIds($this->getElement()->getValue(), + protected function _idsFromAttrib($name) { + return $this->_getIds($this->_element->getAttrib($name), $this->_current_separator); } - protected function _renderVersionValue() { - return $this->_renderValue($this->_getVersionIds()); + protected function _getVersionIds() { + if (!$datas = $this->getOption('datas')) + return []; + + return [$this->_idsFromDatas('id_items'), + $this->_idsFromDatas('id_categorie')]; } - protected function _getVersionIds() { - $datas = $this->getOption('datas'); + protected function _idsFromDatas($name) { + if (!$datas = $this->getOption('datas')) + return []; - return $datas && array_key_exists($name = $this->getElement()->getName(), $datas) - ? $this->_getIds($datas[$name], $this->_version_separator) - : []; + return (array_key_exists($name, $datas) + ? $this->_getIds($datas[$name], $this->_version_separator) + : []); } @@ -57,18 +63,28 @@ class ZendAfi_Form_Decorator_VersionCompare_TreeSelect if (!$ids) return $this->_('Non renseigné'); + return + $this->_formatValueType($this->_('Éléments'), + $ids[0], + $this->_element->getAttrib('ItemRenderer')) + . $this->_formatValueType($this->_('Catégories'), $ids[1], + $this->_element->getAttrib('CategoryRenderer')); + } + + + protected function _formatValueType($name, $ids, $label_renderer) { + $head = $this->_tag('h3', $name); + if (!$ids) + return $head . $this->_('Non renseigné'); + $html = []; foreach($ids as $id) - $html[] = $this->_tag('li', ($model = $this->_find($id)) - ? $this->_labelOf($model) + $html[] = $this->_tag('li', + ($label_renderer && ($label = $label_renderer($id))) + ? $label : $this->_('Inconnu ou Supprimé')); - sort($html); - return $this->_tag('ul', implode('', $html)); - } - - protected function _getIds($value, $separator) { - return $value ? explode($separator, $value) : []; + return $head . $this->_tag('ul', implode('', $html)); } @@ -80,14 +96,4 @@ class ZendAfi_Form_Decorator_VersionCompare_TreeSelect return $current != $version; } - - - protected function _find($id) { - return null; - } - - - protected function _labelOf($model) { - return ''; - } } diff --git a/library/ZendAfi/Form/Decorator/VersionCompare/TreeSelectSingle.php b/library/ZendAfi/Form/Decorator/VersionCompare/TreeSelectSingle.php new file mode 100644 index 00000000000..909956069fc --- /dev/null +++ b/library/ZendAfi/Form/Decorator/VersionCompare/TreeSelectSingle.php @@ -0,0 +1,94 @@ +<?php +/** + * Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved. + * + * BOKEH is free software; you can redistribute it and/or modify + * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by + * the Free Software Foundation. + * + * There are special exceptions to the terms and conditions of the AGPL as it + * is applied to this software (see README file). + * + * BOKEH 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 AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE + * along with BOKEH; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + + +class ZendAfi_Form_Decorator_VersionCompare_TreeSelectSingle + extends ZendAfi_Form_Decorator_VersionCompare { + + protected + $_version_separator = '-', + $_current_separator = ';'; + + + protected function _renderCurrentValue() { + return $this->_renderValue($this->_getCurrentIds()); + } + + + protected function _getCurrentIds() { + return $this->_getIds($this->_element->getValue(), + $this->_current_separator); + } + + + protected function _renderVersionValue() { + return $this->_renderValue($this->_getVersionIds()); + } + + + protected function _getVersionIds() { + $datas = $this->getOption('datas'); + + return $datas && (array_key_exists($name = $this->_element->getName(), $datas) + ? $this->_getIds($datas[$name], $this->_version_separator) + : []); + } + + + protected function _formatValue($ids) { + if (!$ids) + return $this->_('Non renseigné'); + + $html = []; + foreach($ids as $id) + $html[] = $this->_tag('li', ($model = $this->_find($id)) + ? $this->_labelOf($model) + : $this->_('Inconnu ou Supprimé')); + sort($html); + + return $this->_tag('ul', implode('', $html)); + } + + + protected function _getIds($value, $separator) { + return $value ? explode($separator, $value) : []; + } + + + protected function _isModified() { + $current = $this->_getCurrentIds(); + $version = $this->_getVersionIds(); + sort($current); + sort($version); + + return $current != $version; + } + + + protected function _find($id) { + return null; + } + + + protected function _labelOf($model) { + return ''; + } +} diff --git a/library/ZendAfi/Form/Element/TreeSelect.php b/library/ZendAfi/Form/Element/TreeSelect.php index 03ae0744e32..baca233dd99 100644 --- a/library/ZendAfi/Form/Element/TreeSelect.php +++ b/library/ZendAfi/Form/Element/TreeSelect.php @@ -28,6 +28,7 @@ class ZendAfi_Form_Element_TreeSelect extends Zend_Form_Element { foreach ($decorators as $name => $value) $this->_decorators[$name] = $value; + $this->removeDecorator('ViewHelper'); } } \ No newline at end of file -- GitLab