From 14825f5c31d305e67125528f1c13360e347cc961 Mon Sep 17 00:00:00 2001 From: Patrick Barroca <pbarroca@sandbox.pergame.net> Date: Thu, 17 Apr 2014 18:39:46 +0200 Subject: [PATCH] rel #13105 : added record comment modification in record view --- .../opac/controllers/AbonneController.php | 32 ++++++- .../views/scripts/abonne/editavisnotice.phtml | 15 +++ library/ZendAfi/Form/Admin/EditAvis.php | 14 ++- library/ZendAfi/View/Helper/Avis.php | 28 +++++- library/ZendAfi/View/Helper/Notice/Avis.php | 3 + .../controllers/AbonneControllerAvisTest.php | 96 +++++++++++++++++++ .../controllers/NoticeAjaxControllerTest.php | 57 +++++++++++ 7 files changed, 233 insertions(+), 12 deletions(-) create mode 100644 application/modules/opac/views/scripts/abonne/editavisnotice.phtml diff --git a/application/modules/opac/controllers/AbonneController.php b/application/modules/opac/controllers/AbonneController.php index 75c3cc4391a..56b82d66589 100644 --- a/application/modules/opac/controllers/AbonneController.php +++ b/application/modules/opac/controllers/AbonneController.php @@ -233,7 +233,7 @@ class AbonneController extends ZendAfi_Controller_Action { if ($this->_request->isPost()) { if ($avis == null) $avis = new Class_AvisNotice(); - + $avis ->setEntete($this->_request->getParam('avisEntete')) ->setAvis($this->_request->getParam('avisTexte')) @@ -269,6 +269,36 @@ class AbonneController extends ZendAfi_Controller_Action { } + public function editavisnoticeAction() { + $user = Class_Users::getIdentity(); + if (!$user->isBibliothecaire() + || (!$avis = Class_AvisNotice::find((int)$this->_getParam('id')))) { + $this->_javascriptRedirectToReferrer(); + return; + } + + $form = ZendAfi_Form_Admin_EditAvis::newWith($avis, ''); + $form->setAttrib('action', $this->view->url()); + $form->addElement('submit', 'edit-avis-submit', ['label' => 'Valider']); + $this->view->form = $form; + + if ($this->_request->isPost() + && $form->isValid($this->_request->getPost())) { + $avis + ->setAvis($form->avis->getValue()) + ->setEntete($form->entete->getValue()) + ->save(); + + $this->_javascriptRedirectToReferrer(); + return; + } + + + $this->renderPopupResult($this->view->_('Modifier l\'avis "%s"', $avis->getEntete()), + $this->view->render('abonne/editavisnotice.phtml')); + } + + public function cmsavisAction() { $this->handleAvis('getCmsAvisById', 'ecrireCmsAvis'); } diff --git a/application/modules/opac/views/scripts/abonne/editavisnotice.phtml b/application/modules/opac/views/scripts/abonne/editavisnotice.phtml new file mode 100644 index 00000000000..74e73f91a30 --- /dev/null +++ b/application/modules/opac/views/scripts/abonne/editavisnotice.phtml @@ -0,0 +1,15 @@ +<?php echo $this->form;?> + +<script type="text/javascript"> +$('#opac-dialog form').submit(function(event) { + event.preventDefault(); + var form = $(this); + $.post(form.attr('action'), + form.serialize(), + function(data) { opacDialogClose(); + opacDialogFromData(data); + } + ); +} +); +</script> diff --git a/library/ZendAfi/Form/Admin/EditAvis.php b/library/ZendAfi/Form/Admin/EditAvis.php index 2f9dd357f9c..80dfbb91e4d 100644 --- a/library/ZendAfi/Form/Admin/EditAvis.php +++ b/library/ZendAfi/Form/Admin/EditAvis.php @@ -21,14 +21,11 @@ class ZendAfi_Form_Admin_EditAvis extends ZendAfi_Form { - public static function newWith($avis, $back_url) { - - + public static function newWith($avis, $back_action) { $form = new self(); - $form ->populate($avis->toArray()) - ->addBackUrl($back_url) + ->addBackUrl($back_action) ->addPermalinkFor($avis); return $form; @@ -47,15 +44,16 @@ class ZendAfi_Form_Admin_EditAvis extends ZendAfi_Form { 'value' => '']); } - public function AddBackUrl($back_url) { + public function addBackUrl($action) { $this->setAttrib('data-backurl', $this->getView()->url(['module' => 'admin', - 'controller' => 'modo', - 'action' => $back_url], null, true)); + 'controller' => 'modo', + 'action' => $action], null, true)); return $this; } + public function addPermalinkFor($avis) { if($avis->isAvisNotice()) { $this->addElement('text', 'url', ['label' => 'Permalien de la notice concernée *', diff --git a/library/ZendAfi/View/Helper/Avis.php b/library/ZendAfi/View/Helper/Avis.php index 11ec16475f2..325f486b3e5 100644 --- a/library/ZendAfi/View/Helper/Avis.php +++ b/library/ZendAfi/View/Helper/Avis.php @@ -23,6 +23,7 @@ class ZendAfi_View_Helper_Avis extends ZendAfi_View_Helper_BaseHelper { protected $_vignette_link_to_avis = false; protected $_limit_nb_word = 0; protected $_actions = []; + protected $_admin_actions = []; protected $_url_context = []; function setVignetteLinkToAvis() { @@ -40,6 +41,12 @@ class ZendAfi_View_Helper_Avis extends ZendAfi_View_Helper_BaseHelper { $this->_actions = $actions; } + + function setAdminActions($actions) { + $this->_admin_actions = $actions; + return $this; + } + /** @param $context array for url helper */ function addUrlContext($context) { @@ -110,11 +117,11 @@ class ZendAfi_View_Helper_Avis extends ZendAfi_View_Helper_BaseHelper { $html = "<div class='contenu_critique'>". - $this->view->noteImg($avis->getNote()). + $this->view->noteImg($avis->getNote()). "<a class='entete_critique' href='$url_avis'>$entete</a>". "<span class='auteur_critique'>". - ('' != $auteur ? " <a href='$url_auteur'>$auteur</a>" : ''). - " <span>- $date_avis</span>".$actions_tag. + ('' != $auteur ? " <a href='$url_auteur'>$auteur</a>" : ''). + " <span>- $date_avis</span>" . $actions_tag . $this->_getAdminActionsTag($avis). "</span>". "$read_speaker_tag". "<p>$text_avis</p>". @@ -168,6 +175,21 @@ class ZendAfi_View_Helper_Avis extends ZendAfi_View_Helper_BaseHelper { } + protected function _getAdminActionsTag($avis) { + $html_actions = ''; + + foreach($this->_admin_actions as $action) { + $link = $this->view->tagAnchor($this->_url(['controller' => 'abonne', + 'action' => $action.'avisnotice', + 'id' => $avis->getId()]), + $this->view->boutonIco("type=$action"), ['data-popup' => 'true']); + $html_actions .= "<span rel='$action'>$link</span>"; + } + + return ($html_actions ? "<span class='actions'>$html_actions</span>" : ''); + } + + protected function _getUrlAvis($avis) { if (null == $avis->getId()) return '#'; diff --git a/library/ZendAfi/View/Helper/Notice/Avis.php b/library/ZendAfi/View/Helper/Notice/Avis.php index d2112cc3eb6..8b40fb35ebf 100644 --- a/library/ZendAfi/View/Helper/Notice/Avis.php +++ b/library/ZendAfi/View/Helper/Notice/Avis.php @@ -69,6 +69,9 @@ class ZendAfi_View_Helper_Notice_Avis extends Zend_View_Helper_HtmlElement { if($source) { $avis_helper = $this->view->getHelper('Avis'); + if (($user = Class_Users::getIdentity()) + && $user->isBibliothecaire()) + $avis_helper->setAdminActions(['edit']); $html.='<tr><td colspan="3"> </td></tr>'; $html.='<tr><td class="notice_info_ligne_titre" align="left" colspan="3">' . $avis[$source]["titre"] . '</td></tr>'; diff --git a/tests/application/modules/opac/controllers/AbonneControllerAvisTest.php b/tests/application/modules/opac/controllers/AbonneControllerAvisTest.php index bd030be681f..97aa64ce883 100644 --- a/tests/application/modules/opac/controllers/AbonneControllerAvisTest.php +++ b/tests/application/modules/opac/controllers/AbonneControllerAvisTest.php @@ -728,4 +728,100 @@ class AbonneControllerAvisBlogControllerViewReadAvisTest extends AbonneFlorence } } + + +class AbonneControllerEditAvisNoticeNotFoundActionTest extends AbstractControllerTestCase { + public function setUp() { + parent::setUp(); + Class_AvisNotice::beVolatile(); + $this->dispatch('/opac/abonne/editavisnotice/id/54', true); + } + + + /** @test */ + public function responseShouldReloadPage() { + $this->assertTrue(false !== strpos(json_decode($this->_response->getBody())->title, 'Sauvegarde en cours')); + } +} + + +class AbonneControllerEditAvisNoticeNotAdminLoggedActionTest extends AbstractControllerTestCase { + protected function _loginHook($account) { + $account->ROLE_LEVEL = ZendAfi_Acl_AdminControllerRoles::INVITE; + $account->ROLE = 'invite'; + } + + public function setUp() { + parent::setUp(); + $avis = $this->fixture('Class_AvisNotice', ['id' => 54, + 'entete' => 'Bonjour !', + 'avis' => 'Ceci est le contenu de l\'avis']); + $this->dispatch('/opac/abonne/editavisnotice/id/54', true); + } + + + /** @test */ + public function responseShouldReloadPage() { + $this->assertTrue(false !== strpos(json_decode($this->_response->getBody())->title, 'Sauvegarde en cours'), + $this->_response->getBody()); + } +} + + + + +class AbonneControllerEditAvisNoticeAdminLoggedActionTest extends AbstractControllerTestCase { + public function setUp() { + parent::setUp(); + $avis = $this->fixture('Class_AvisNotice', ['id' => 54, + 'entete' => 'Bonjour !', + 'avis' => 'Ceci est le contenu de l\'avis']); + + $this->dispatch('/opac/abonne/editavisnotice/id/54', true); + $this->json = json_decode($this->_response->getBody()); + } + + + /** @test */ + public function titleShouldBeBonjour() { + $this->assertEquals('Modifier l\'avis "Bonjour !"', $this->json->title); + } + + + /** @test */ + public function formShouldBePresent() { + $this->assertTrue(false !== strpos($this->json->content, '<form ')); + } +} + + + +class AbonneControllerEditAvisNoticeAdminLoggedPostActionTest extends AbstractControllerTestCase { + public function setUp() { + parent::setUp(); + $this->fixture('Class_AvisNotice', ['id' => 54, + 'entete' => 'Bonjour !', + 'avis' => 'Ceci est le contenu de l\'avis']); + + $this->postDispatch('/opac/abonne/editavisnotice/id/54', + ['entete' => 'bye', + 'avis' => 'ceci n\'est pas le contenu'], + true); + + $this->avis = Class_AvisNotice::find(54); + } + + + /** @test */ + public function enteteShouldBeBye() { + $this->assertEquals('bye', $this->avis->getEntete()); + } + + + /** @test */ + public function avisShouldBeCeciNEstPasLeContenu() { + $this->assertEquals('ceci n\'est pas le contenu', $this->avis->getAvis()); + } +} + ?> \ No newline at end of file diff --git a/tests/application/modules/opac/controllers/NoticeAjaxControllerTest.php b/tests/application/modules/opac/controllers/NoticeAjaxControllerTest.php index 189df3da57a..b627ffff1a6 100644 --- a/tests/application/modules/opac/controllers/NoticeAjaxControllerTest.php +++ b/tests/application/modules/opac/controllers/NoticeAjaxControllerTest.php @@ -908,6 +908,63 @@ abstract class NoticeAjaxControllerNoticeWithAvisTestCase extends AbstractContro +class NoticeAjaxControllerNoticeWithAvisEditLinkNotLoggedTest extends NoticeAjaxControllerNoticeWithAvisTestCase { + /** + * parent::_login setup an admin account by default -> do not log at all in this test + */ + protected function _login() {} + + + /** @test */ + public function editLinkShouldNotBePresent() { + $this->dispatch('/opac/noticeajax/avis/id_notice/34/page/0/onglet/bloc', true); + $this->assertNotXPath('//a[contains(@href, "abonne/editavisnotice")]'); + } +} + + + +class NoticeAjaxControllerNoticeWithAvisEditLinkGuestLoggedTest extends NoticeAjaxControllerNoticeWithAvisTestCase { + protected function _loginHook($account) { + $account->ROLE_LEVEL = ZendAfi_Acl_AdminControllerRoles::INVITE; + $account->ROLE = 'invite'; + + } + + /** @test */ + public function editLinkShouldNotBePresent() { + $this->dispatch('/opac/noticeajax/avis/id_notice/34/page/0/onglet/bloc', true); + $this->assertNotXPath('//a[contains(@href, "abonne/editavisnotice")]'); + } +} + + + +class NoticeAjaxControllerNoticeWithAvisEditLinkModoLoggedTest extends NoticeAjaxControllerNoticeWithAvisTestCase { + protected function _loginHook($account) { + $account->ROLE_LEVEL = ZendAfi_Acl_AdminControllerRoles::MODO_BIB; + $account->ROLE = 'modo_bib'; + } + + public function setUp() { + parent::setUp(); + $this->dispatch('/opac/noticeajax/avis/id_notice/34/page/0/onglet/bloc', true); + } + + + /** @test */ + public function editLinkShouldBePresent() { + $this->assertXPath('//a[contains(@href, "abonne/editavisnotice")]'); + } + + + /** @test */ + public function editLinkShouldBePopup() { + $this->assertXPath('//a[contains(@href, "abonne/editavisnotice")][@data-popup="true"]'); + } +} + + class NoticeAjaxControllerAvisNoticeNotFoundTest extends AbstractControllerTestCase { public function setUp() { -- GitLab