From f3141033465235c8db5bc231df2fe2807dd4090f Mon Sep 17 00:00:00 2001 From: gloas <gloas@afi-sa.fr> Date: Thu, 5 Jan 2017 11:17:04 +0100 Subject: [PATCH] migrate bib-controller to full plugin manager --- .../admin/controllers/BibController.php | 89 +++++++------------ .../Controller/Plugin/Manager/Library.php | 57 ++++++++++++ 2 files changed, 89 insertions(+), 57 deletions(-) create mode 100644 library/ZendAfi/Controller/Plugin/Manager/Library.php diff --git a/application/modules/admin/controllers/BibController.php b/application/modules/admin/controllers/BibController.php index 583a51acef7..a622037b176 100644 --- a/application/modules/admin/controllers/BibController.php +++ b/application/modules/admin/controllers/BibController.php @@ -22,6 +22,11 @@ class Admin_BibController extends ZendAfi_Controller_Action { private $id_zone; + public function getPlugins() { + return ['ZendAfi_Controller_Plugin_Manager_Library']; + } + + public function getRessourceDefinitions() { return ['model' => ['class' => 'Class_Bib', 'name' => 'bib', @@ -51,7 +56,7 @@ class Admin_BibController extends ZendAfi_Controller_Action { public function indexAction() { $this->view->titre = $this->view->_('Gestion des bibliothèques'); - if (!$this->_canAccessToLibrary()) + if (!$this->canAccessToLibrary()) return $this->_redirect('admin/index'); $user = Class_Users::getIdentity(); @@ -61,45 +66,6 @@ class Admin_BibController extends ZendAfi_Controller_Action { } - protected function _canEdit($model) { - return $this->_canAccessToLibrary(); - } - - - protected function _canAdd() { - return $this->_canAccessToLibrary(); - } - - - protected function _canAccessToLibrary() { - $user = Class_Users::getIdentity(); - if (!$user->hasRightsOnLibraries()) - return false; - - $id = (int)($this->_request->isPost()) ? - $this->_request->getPost('id_bib') : - $this->_request->getParam('id'); - - if(!$id) - return true; - - return $user->hasRightsForLibrary($id); - } - - - public function deleteAction() { - $bib = Class_Bib::find((int)$this->_request->getParam('id')); - $this->view->titre = $this->view->_('Supprimer la bibliothèque: %s', $bib->getLibelle()); - $this->view->bib = $bib; - } - - - public function forceDeleteAction() { - $bib = Class_Bib::find((int)$this->_request->getParam('id')); - $bib->delete(); - $this->_helper->notify('La bibliothèque "'.$bib->getLibelle().'" a été supprimée'); - $this->_redirect('admin/bib/index'); - } public function localisationsAction() { @@ -286,16 +252,6 @@ class Admin_BibController extends ZendAfi_Controller_Action { return Class_Localisation::find($id_location); } - protected function checkForm() { - $this->view->erreurs=[]; - if(!$this->_getParam("libelle")) - $this->view->erreurs[] = $this->view->_("le libellé est obligatoire."); - if(!$this->_getParam("image")) - $this->view->erreurs[]=$this->view->_("L'image du plan est obligatoire."); - - return (!$this->view->erreurs); - - } public function plansmajAction() { $cls_loc = new Class_Localisation(); @@ -411,15 +367,19 @@ class Admin_BibController extends ZendAfi_Controller_Action { null, true)); } + protected function checkForm() { + $this->view->erreurs=[]; + if(!$this->_getParam("libelle")) + $this->view->erreurs[] = $this->view->_("le libellé est obligatoire."); + if(!$this->_getParam("image")) + $this->view->erreurs[]=$this->view->_("L'image du plan est obligatoire."); + + return (!$this->view->erreurs); - protected function _doBeforeSave($model) { - if ($location = $this->_getOrCreateLocation($this->_getParam('id_lieu'), $model)) - $model->setLieu($location); - return $this; } - protected function _getOrCreateLocation($location_id, $model) { + public function getOrCreateLocation($location_id, $model) { if (Class_Lieu::GENERATE != $location_id) return Class_Lieu::find($location_id); @@ -432,7 +392,6 @@ class Admin_BibController extends ZendAfi_Controller_Action { } - public function updateLieuElementAction() { if (!$library = Class_Bib::find($this->_getParam('id'))) return $this->_renderUpdateLieuElement(function() @@ -443,7 +402,7 @@ class Admin_BibController extends ZendAfi_Controller_Action { $library->updateAttributes($this->getRequest()->getParams()); - if (!$location = $this->_getOrCreateLocation($this->_getParam('id_lieu'), $library)) + if (!$location = $this->getOrCreateLocation($this->_getParam('id_lieu'), $library)) return $this->_renderUpdateLieuElement(function() { return $this->view->renderError('Vous devez d\'abord définir le nom de la bibliothèque'); @@ -471,4 +430,20 @@ class Admin_BibController extends ZendAfi_Controller_Action { $html = $callback($location_element); $this->getResponse()->setBody($location_element->render($this->view) . $html); } + + + public function canAccessToLibrary() { + $user = Class_Users::getIdentity(); + if (!$user->hasRightsOnLibraries()) + return false; + + $id = (int)($this->_request->isPost()) ? + $this->_request->getPost('id_bib') : + $this->_request->getParam('id'); + + if(!$id) + return true; + + return $user->hasRightsForLibrary($id); + } } diff --git a/library/ZendAfi/Controller/Plugin/Manager/Library.php b/library/ZendAfi/Controller/Plugin/Manager/Library.php new file mode 100644 index 00000000000..43a1e462ff8 --- /dev/null +++ b/library/ZendAfi/Controller/Plugin/Manager/Library.php @@ -0,0 +1,57 @@ +<?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_Manager_Library extends ZendAfi_Controller_Plugin_Manager_Manager { + protected function _canEdit($model) { + return $this->_controller->canAccessToLibrary(); + } + + + protected function _canAdd() { + return $this->_controller->canAccessToLibrary(); + } + + + + protected function _doBeforeSave($model) { + if ($location = $this->_controller->getOrCreateLocation($this->_getParam('id_lieu'), $model)) + $model->setLieu($location); + return $this; + } + + + public function deleteAction() { + $bib = Class_Bib::find((int)$this->_request->getParam('id')); + $this->_view->titre = $this->_view->_('Supprimer la bibliothèque: %s', $bib->getLibelle()); + $this->_view->bib = $bib; + } + + + public function forceDeleteAction() { + $bib = Class_Bib::find((int)$this->_request->getParam('id')); + $bib->delete(); + $this->_helper->notify('La bibliothèque "'.$bib->getLibelle().'" a été supprimée'); + $this->_redirect('admin/bib/index'); + } + +} +?> \ No newline at end of file -- GitLab