diff --git a/application/modules/admin/controllers/DeweyBrowserController.php b/application/modules/admin/controllers/DeweyBrowserController.php new file mode 100644 index 0000000000000000000000000000000000000000..17473b30cc3e92f40248cb12a9972a56c0f11017 --- /dev/null +++ b/application/modules/admin/controllers/DeweyBrowserController.php @@ -0,0 +1,27 @@ +<?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 Admin_DeweyBrowserController extends ZendAfi_Controller_Action { + public function getPlugins() { + return ['ZendAfi_Controller_Plugin_ResourceDefinition_Codification_Dewey']; + } +} \ No newline at end of file diff --git a/library/Class/CodifDewey.php b/library/Class/CodifDewey.php index c0e345541ff2d055ba8beba2442c398eda2fb14f..20f3c14209b74fa10dec344d448839959c4bea73 100644 --- a/library/Class/CodifDewey.php +++ b/library/Class/CodifDewey.php @@ -20,57 +20,119 @@ */ class Class_CodifDeweyLoader extends Storm_Model_Loader { -// ---------------------------------------------------------------- -// Rend le libelle ou le code si le libelle est vide -// ---------------------------------------------------------------- - public function getLibelle($indice) { - return (($dewey = Class_CodifDewey::find($indice)) && $dewey->getLibelle()) - ? $dewey->getLibelle() - : Class_CodifDewey::formatIndice($indice); + + public function root() { + return new Class_CodifDewey; } -} -class Class_CodifDewey extends Storm_Model_Abstract { + public function findNodesOfBy($instance, $params = []) { + $children = $instance->getChildren($params); - const CODE_FACETTE = 'D'; + if($instance->isNew()) + return $children; - protected $_table_name = 'codif_dewey'; - protected $_table_primary = 'id_dewey'; - protected $_loader_class = 'Class_CodifDeweyLoader'; - protected $_fixed_id = true; + return array_filter($children, + function ($child) + { + return 3 == strlen($child->getId()) || $child->hasChildren(); + }); + } -// ---------------------------------------------------------------- -// Rend une liste pour un champ suggestion -// ---------------------------------------------------------------- - public function getListeSuggestion($recherche,$mode,$limite_resultat) { - // Lancer la recherche - $new = ''; - if($mode=="1") { - for($i=0; $i < strlen($recherche); $i++) - if($recherche[$i] >="0" and $recherche[$i] <= "9") $new.=$recherche[$i]; - $req="select id_dewey,libelle from codif_dewey where id_dewey like'".$new."%' order by id_dewey limit ".$limite_resultat; - } - if($mode=="2") $req="select id_dewey,libelle from codif_dewey where libelle like'".addslashes($recherche)."%' order by id_dewey limit ".$limite_resultat; - if($mode=="3") $req="select id_dewey,libelle from codif_dewey where libelle like'%".addslashes($recherche)."%' order by id_dewey limit ".$limite_resultat; - $resultat=fetchAll($req); + public function findLeavesOfBy($instance, $params = []) { + return array_filter($instance->getChildren($params), + function ($child) + { + return 3 != strlen($child->getId()) && !$child->hasChildren(); + }); + } - // Mettre l'indice et le libelle - if(!$resultat) return false; - foreach($resultat as $enreg) - { - $liste[]=array($enreg["id_dewey"], $this->formatIndice($enreg["id_dewey"])." : ".$enreg["libelle"]); - } - return $liste; + public function findChildrenOfBy($instance, $params = []) { + $params = array_merge(['order' => 'id_dewey'], + $params, + ['where' => implode(' and ', $this->_childrenWhereClauseOf($instance))]); + + return Class_CodifDewey::findAllBy($params); + } + + + protected function _childrenWhereClauseOf($model) { + if ($model->isNew()) + return ['LENGTH(id_dewey) = 3', + 'SUBSTR(id_dewey, 2, 2) = "00"']; + + $length = strlen($model->getId()); + + if (3 > $length) + return ['1=0']; + + if (3 == $length && '00' === substr($model->getId(), 1, 2)) + return ['LENGTH(id_dewey) = 3', + 'SUBSTR(id_dewey, 3, 1) = "0"', + 'SUBSTR(id_dewey, 1, 1) = ' . substr($model->getId(), 0, 1), + 'id_dewey != ' . $model->getId()]; + + if (3 == $length && '0' === substr($model->getId(), 2, 1)) + return ['LENGTH(id_dewey) = 3', + 'SUBSTR(id_dewey, 1, 2) = ' . substr($model->getId(), 0, 2), + 'id_dewey != ' . $model->getId()]; + + return ['LENGTH(id_dewey) = ' . (strlen($model->getId()) + 1), + 'id_dewey like "' . $model->getId() . '%"']; } -// ---------------------------------------------------------------- -// Rend une liste d'indices par niveau -// ---------------------------------------------------------------- - static function getIndices($pere) - { + + public function recursiveNumberOfChildrenOf($model) { + if($model->isNew()) + return 0; + + $length = strlen($model->getId()); + + if (3 == $length && '00' === substr($model->getId(), 1, 2)) + return Class_CodifDewey::countBy(['where' => 'id_dewey like "' . substr($model->getId(), 0, 1) . '%" and id_dewey != ' . $model->getId()]); + + if (3 == $length && '0' === substr($model->getId(), 2, 1)) + return Class_CodifDewey::countBy(['where' => 'id_dewey like "' . substr($model->getId(), 0, 2) . '%" and id_dewey != ' . $model->getId()]); + + $id = $model->getId(); + $where = sprintf('id_dewey like "%s%%" and LENGTH(id_dewey) > %s', + $id, strlen($id)); + + return Class_CodifDewey::countBy(['where' => $where]); + } + + + public function findParentOf($model) { + if (!$model || $model->isNew()) + return; + + $length = strlen($model->getId()); + + $parent_id = substr($model->getId(), 0, -1); + if (3 < $length) + return Class_CodifDewey::find($parent_id); + + if (3 == $length && '00' === substr($model->getId(), 1, 2)) + return; + + if (3 == $length && '0' === substr($model->getId(), 2, 1)) + return Class_CodifDewey::find(substr($model->getId(), 1, 1) . '00'); + + if (3 == $length && '0' !== substr($model->getId(), 2, 1)) + return Class_CodifDewey::find(substr($model->getId(), 1, 2) . '0'); + } + + + public function getLibelleOf($indice) { + return ($dewey = Class_CodifDewey::find($indice)) + ? $dewey->getLibelle() + : Class_CodifDewey::formatIndice($indice); + } + + + public function getIndices($pere) { $sql = Zend_Registry::get('sql'); if($pere == "root") $liste=$sql->fetchAll("select * from codif_dewey where LENGTH(id_dewey)=1 order by id_dewey"); @@ -82,11 +144,8 @@ class Class_CodifDewey extends Storm_Model_Abstract { return $liste; } -// ---------------------------------------------------------------- -// Ponctue un indice dewey -// ---------------------------------------------------------------- - static function formatIndice($indice) - { + + public function formatIndice($indice) { if(strlen($indice)< 4) return $indice; $new=""; while(strlen($indice)>3) @@ -97,13 +156,11 @@ class Class_CodifDewey extends Storm_Model_Abstract { $new.=$indice; return $new; } -// ---------------------------------------------------------------- -// Analyse et rend l'indice s'il est valide -// ---------------------------------------------------------------- - static function filtreIndice($indice) - { + + + public function filtreIndice($indice) { $indice=trim($indice); - if(substr($indice,1,1) == ".") return ""; // si c'est de la pcdm4 on dégage + if(substr($indice,1,1) == ".") return ""; // si c'est de la dewey on dégage $new=""; for($i=0; $i<strlen($indice); $i++) { @@ -114,4 +171,47 @@ class Class_CodifDewey extends Storm_Model_Abstract { } } -?> \ No newline at end of file + + +class Class_CodifDewey extends Storm_Model_Abstract { + use Trait_TreeNode, Trait_Facetable; + + const CODE_FACETTE = 'D'; + + protected $_table_name = 'codif_dewey'; + protected $_table_primary = 'id_dewey'; + protected $_loader_class = 'Class_CodifDeweyLoader'; + protected $_fixed_id = true; + + + public function getListeSuggestion($recherche,$mode,$limite_resultat) { + // Lancer la recherche + $new = ''; + if($mode=="1") { + for($i=0; $i < strlen($recherche); $i++) + if($recherche[$i] >="0" and $recherche[$i] <= "9") $new.=$recherche[$i]; + $req="select id_dewey,libelle from codif_dewey where id_dewey like'".$new."%' order by id_dewey limit ".$limite_resultat; + } + if($mode=="2") $req="select id_dewey,libelle from codif_dewey where libelle like'".addslashes($recherche)."%' order by id_dewey limit ".$limite_resultat; + if($mode=="3") $req="select id_dewey,libelle from codif_dewey where libelle like'%".addslashes($recherche)."%' order by id_dewey limit ".$limite_resultat; + + $resultat=fetchAll($req); + + // Mettre l'indice et le libelle + if(!$resultat) return false; + foreach($resultat as $enreg) + { + $liste[]=array($enreg["id_dewey"], $this->formatIndice($enreg["id_dewey"])." : ".$enreg["libelle"]); + } + + return $liste; + } + + + public function getLibelle($id = null) { + $libelle = parent::_get('libelle'); + return $libelle + ? $libelle + : $this->getLoader()->formatIndice($this->getId()); + } +} \ No newline at end of file diff --git a/library/Class/CodifPcdm4.php b/library/Class/CodifPcdm4.php index e41d627762b2b211bad190b3ca6fddee9c437a29..62d45f4e96f3af86b4cefa3b920443ff338e6754 100644 --- a/library/Class/CodifPcdm4.php +++ b/library/Class/CodifPcdm4.php @@ -21,6 +21,11 @@ class Class_CodifPcdm4Loader extends Storm_Model_Loader { + public function root() { + return new Class_CodifPcdm4; + } + + public function findParentOf($instance) { if (!$instance || $instance->isNew()) return; @@ -84,7 +89,7 @@ class Class_CodifPcdm4Loader extends Storm_Model_Loader { } - public function getLibelle($indice) { + public function getLibelleOf($indice) { return ($pcdm4 = Class_CodifPcdm4::find($indice)) ? $pcdm4->getLibelle() : Class_CodifPcdm4::formatIndice($indice); @@ -125,11 +130,6 @@ class Class_CodifPcdm4Loader extends Storm_Model_Loader { } return $liste; } - - - public function root() { - return new Class_CodifPcdm4; - } } @@ -168,7 +168,7 @@ class Class_CodifPcdm4 extends Storm_Model_Abstract { } - public function getLibelle() { + public function getLibelle($id = null) { $libelle = parent::_get('libelle'); return $libelle ? $libelle diff --git a/library/Class/Codification.php b/library/Class/Codification.php index 40ce1e795273619b68c0cafe0ef0ff282e923ac0..9c4fa781c75fcdadf7ceea91c18d7e789bb731d1 100644 --- a/library/Class/Codification.php +++ b/library/Class/Codification.php @@ -95,8 +95,8 @@ class Class_Codification { $l=getVar("FACETTE_PCDM4_LIBELLE"); if(trim($l)) $lib[2]=$l; $l=getVar("FACETTE_DEWEY_LIBELLE"); if(trim($l)) $lib[1]=$l; return $lib[intval($id)]; - case Class_CodifDewey::CODE_FACETTE: return Class_CodifDewey::getLibelle($id); - case Class_CodifPcdm4::CODE_FACETTE: return Class_CodifPcdm4::getLibelle($id); + case Class_CodifDewey::CODE_FACETTE: return Class_CodifDewey::getLibelleOf($id); + case Class_CodifPcdm4::CODE_FACETTE: return Class_CodifPcdm4::getLibelleOf($id); case Class_Codification::CODE_AVAILABILITY: return ($bib = Class_Bib::find((int)$id)) ? $bib->getLibelle() : ''; case Class_CodifThesaurus::CODE_FACETTE: return ($thesaurus = Class_CodifThesaurus::findFirstBy(['id_thesaurus'=>$id])) ? $thesaurus->getLibelleFacette(): ''; case Class_CodifAuteur::CODE_FACETTE: return ($auteur = Class_CodifAuteur::find($id)) ? $auteur->getLibelle() : ''; diff --git a/library/Class/ListViewModeDescription/Dewey.php b/library/Class/ListViewModeDescription/Dewey.php new file mode 100644 index 0000000000000000000000000000000000000000..3d465d6af439cb56adf552e4c861e9486af74a39 --- /dev/null +++ b/library/Class/ListViewModeDescription/Dewey.php @@ -0,0 +1,73 @@ +<?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 Class_ListViewModeDescription_Dewey extends Class_ListViewModeDescription { + + public function __construct($context) { + parent::__construct($context); + if(!$this->_context->getOrder()) + $this->_context->setOrder('id_dewey'); + } + + + protected function _getLabel() { + return $this->_('Dewey'); + } + + + protected function _getController() { + return 'dewey-browser'; + } + + + protected function _getModel() { + return null === $this->_context->getParentId() + ? Class_CodifDewey::root() + : Class_CodifDewey::find($this->_context->getParentId()); + } + + + protected function _getSearchColumns() { + return ['libelle', + 'id_dewey']; + } + + + public function describeCategoriesIn($description, $list_view_mode) { + return $description + ->addColumn($this->_('Dewey'), ['attribute' => 'libelle', + 'callback' => $list_view_mode->renderCategoryClosure()]) + ->addColumn($this->_('Indice'), 'id_dewey') + ->addColumn($this->_('Code facette'), 'facet_code'); + } + + + public function describeItemsIn($description, $list_view_mode) { + return $description + ->addColumn($this->_('Dewey'), ['attribute' => 'libelle', + 'callback' => $list_view_mode->renderItemClosure()]) + ->addColumn($this->_('Indice'), 'id_dewey') + ->addColumn($this->_('Code facette'), ['attribute' => 'facet_code', + 'sort_attribute' => 'id_dewey']) + ->setSorterServer(); + } +} \ No newline at end of file diff --git a/library/ZendAfi/Controller/Action/Helper/CodificationListViewMode.php b/library/ZendAfi/Controller/Action/Helper/CodificationListViewMode.php index dad1b0188e1529b53826090396607ed0e200774b..60f7cd7fa6297ec53317ba6aabdbbc4ea8559a32 100644 --- a/library/ZendAfi/Controller/Action/Helper/CodificationListViewMode.php +++ b/library/ZendAfi/Controller/Action/Helper/CodificationListViewMode.php @@ -36,6 +36,10 @@ class ZendAfi_Controller_Action_Helper_CodificationListViewMode ->setLibelle($this->_('Auteurs')) ->setController('author-browser'), + (new Class_CodifDewey) + ->setLibelle($this->_('Dewey')) + ->setController('dewey-browser'), + (new Class_Catalogue) ->setLibelle($this->_('Domaines pour sélections')) ->setController('domain-browser'), diff --git a/library/ZendAfi/Controller/Plugin/ResourceDefinition/Abstract.php b/library/ZendAfi/Controller/Plugin/ResourceDefinition/Abstract.php index d9c1d2ae2381e98c3c29b235283f4edc636bc7d3..ebfae2e7f10607e71e2b83fd3c7d5f4b452d9d67 100644 --- a/library/ZendAfi/Controller/Plugin/ResourceDefinition/Abstract.php +++ b/library/ZendAfi/Controller/Plugin/ResourceDefinition/Abstract.php @@ -49,6 +49,7 @@ abstract class ZendAfi_Controller_Plugin_ResourceDefinition_Abstract extends Zen if($this->_request->isPost()) return $this->_redirectToRefererWithNewParams(['title_search' => $this->_getPost()['title_search'], 'page' => $this->_getParam('page', 1), + 'parent_id' => null, 'order' => $this->_getParam('order')]); $this->_view->list = $this->_listViewMode(); return $this->renderScript('admin/listViewMode.phtml'); diff --git a/library/ZendAfi/Controller/Plugin/ResourceDefinition/Codification/Dewey.php b/library/ZendAfi/Controller/Plugin/ResourceDefinition/Codification/Dewey.php new file mode 100644 index 0000000000000000000000000000000000000000..3110ebc84d4775ab4cc12694de6786db69931565 --- /dev/null +++ b/library/ZendAfi/Controller/Plugin/ResourceDefinition/Codification/Dewey.php @@ -0,0 +1,36 @@ +<?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_Controller_Plugin_ResourceDefinition_Codification_Dewey + extends ZendAfi_Controller_Plugin_ResourceDefinition_Codification_Abstract { + + public function getDefinitions() { + return + ['model' => ['class' => 'Class_CodifDewey', + 'name' => 'dewey', + 'order' => 'libelle'], + + 'listViewMode' => 'Class_ListViewModeDescription_Dewey', + + 'actions' => ['index' => ['title' => $this->_('Parcourir la codification Dewey')]]]; + } +} \ No newline at end of file diff --git a/tests/scenarios/CodificationBrowser/CodificationBrowserTest.php b/tests/scenarios/CodificationBrowser/CodificationBrowserTest.php index 1f751f9aae3b19a48e854d918c4ec267c7ab98ef..0f9478633f0943682cc26e8d0e4cc784453db887 100644 --- a/tests/scenarios/CodificationBrowser/CodificationBrowserTest.php +++ b/tests/scenarios/CodificationBrowser/CodificationBrowserTest.php @@ -43,7 +43,8 @@ class CodificationBrowserIndexDispatchTest extends Admin_AbstractControllerTestC ['subject-browser', 'Sujets et matières'], ['tag-browser', 'Tags'], ['doctype-browser', 'Types de documents'], - ['pcdm4-browser', 'PCDM4']]; + ['pcdm4-browser', 'PCDM4'], + ['dewey-browser', 'Dewey']]; } @@ -210,3 +211,12 @@ class CodificationBrowserPCDM4SimpleIndexTest extends CodificationBrowserSimpleI $_model = 'Class_CodifPcdm4', $_attribs = ['id' => '0', 'libelle' => 'Histoire']; } + + + +class CodificationBrowserDeweySimpleIndexTest extends CodificationBrowserSimpleIndexTestCase { + protected + $_controller = 'dewey-browser', + $_model = 'Class_CodifDewey', + $_attribs = ['id' => '000', 'libelle' => '']; +}