Commit d6febc20 authored by Ghislain Loas's avatar Ghislain Loas
Browse files

dev #69826 refacto to plugin manager

parent b5243f49
Pipeline #3339 failed with stage
in 25 minutes and 7 seconds
......@@ -1019,7 +1019,8 @@ class AbonneController extends ZendAfi_Controller_Action {
public function settingsAction() {
$this->view->titre = $this->view->_('Mes favoris');
$this->view->bookmarked_searches = Class_User_BookmarkedSearch::findAllBy(['id_user' => $this->_user->getId()]);
$this->view->bookmarked_searches = Class_User_BookmarkedSearch::findAllBy(['id_user' => $this->_user->getId(),
'order' => 'creation_date desc']);
$form = ZendAfi_Form_User_Settings::forUser($this->_user);
$this->view->form = $form->getElements()
? $form
......@@ -1223,96 +1224,4 @@ class AbonneController extends ZendAfi_Controller_Action {
$this->renderPopupResult($this->_('Compléter votre adresse email'),
$this->view->render('abonne/ajax-email.phtml'));
}
public function bookmarkSearchAction() {
$this->view->titre = $this->_('Enregister la recherche dans mes favoris');
if (!$this->_user)
return $this->_forward('popup-login', 'auth', 'opac', ['redirect' => $this->view->url()]);
$this->view->form = $form = new ZendAfi_Form_User_BookmarkSearch();
$form->setAction(Class_Url::absolute('/abonne/bookmark-search'));
if( !$this->_request->isPost() || !$form->isValid($this->_request->getPost()))
return;
$criterias = Class_User_BookmarkedSearch::newInstance(['id_user' => $this->_user->getId(),
'label' => $this->_getParam('label'),
'criterias' => $this->_getCriterias()]);
if(!$criterias->save()) {
$this->_helper->_notify($this->_('La recherche n\'a pas été enregistrée dans vos favoris'));
$this->_redirectToReferer();
}
$this->_helper->_notify($this->_('Cette recherche a été enregistrée dans vos favoris'));
return $this->_redirectClose($this->_getReferer());
}
protected function _getCriterias() {
if (!$namespace = new Zend_Session_Namespace('historiqueRecherche'))
return null;
if (!$searches = $namespace->criteres)
return null;
return array_pop($searches);
}
public function deleteBookmarkedSearchAction() {
$this->view->titre = $this->_('Supprimer la recherche de mes favoris');
if(!$search = $this->_checkBookmarkedSearchRequest())
return;
$search->delete();
$this->_helper->_notify($this->_('La recherche "%s" a été supprimée.', $search->getLabel()));
return $this->_redirectClose($this->_getReferer());
}
protected function _checkBookmarkedSearchRequest() {
if(!$this->_user) {
$this->_forward('popup-login', 'auth', 'opac', ['redirect' => $this->view->url()]);
return;
}
if(!$search = Class_User_BookmarkedSearch::find($this->_getParam('id', 0))) {
$this->_helper->_notify($this->_('La recherche demandée est introuvable.'));
$this->_redirectClose($this->_getReferer());
return;
}
if(!$search->belongsTo($this->_user)) {
$this->_helper->_notify($this->_('Cette recherche ne vous appartient pas.'));
$this->_redirectClose($this->_getReferer());
return;
}
return $search;
}
public function notifyBookmarkedSearchAction() {
if(!$search = $this->_checkBookmarkedSearchRequest())
return;
$search->beNotified()->save();
$this->_helper->_notify($this->_('Les notifications de nouveautés pour la recherche "%s" sont activées.', $search->getLabel()));
return $this->_redirectClose($this->_getReferer());
}
public function unnotifyBookmarkedSearchAction() {
if(!$search = $this->_checkBookmarkedSearchRequest())
return;
$search->beUnnotified()->save();
$this->_helper->_notify($this->_('Les notifications de nouveautés pour la recherche "%s" sont désactivées.', $search->getLabel()));
return $this->_redirectClose($this->_getReferer());
}
}
\ No newline at end of file
<?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 BookmarkedSearchesController extends ZendAfi_Controller_Action {
public function getPlugins() {
return ['ZendAfi_Controller_Plugin_ResourceDefinition_BookmarkedSearches',
'ZendAfi_Controller_Plugin_Manager_BookmarkedSearches'];
}
public function indexAction() {
return $this->_redirectClose($this->_getReferer());
}
}
......@@ -9,7 +9,7 @@ if($this->form) {
$description = (new Class_TableDescription('bookmarked_searches'))
->addColumn($this->_('Libellé'), function($model) { return $model->getLabel(); })
->addColumn($this->_('Critères'), function($model) { return $this->Search_Criterias($model->getUnserializedCriterias()); })
->addColumn($this->_('Date création'), function($model) { return $model->getCreationDate(); })
->addColumn($this->_('Date de création'), function($model) { return $model->getCreationDate(); })
->addRowAction(function($model) {
return $this->tagPreview(Class_Url::relative($model->getCriteriasUrl()),
$this->_('Voir le résultat de la recherche'));
......@@ -17,17 +17,17 @@ return $this->tagPreview(Class_Url::relative($model->getCriteriasUrl()),
})
->addRowAction(function($model)
{
$action = 'notify-bookmarked-search';
$action = 'notify';
$label = $this->_('Me notifier des nouveautés');
$icon = 'toggle_off';
if($model->isNotified()) {
$action = 'unnotify-bookmarked-search';
$action = 'unnotify';
$label = $this->_('Annuler la notification des nouveautés');
$icon = 'toggle_on';
}
$params = ['controller' => 'abonne',
$params = ['controller' => 'bookmarked-searches',
'action' => $action,
'id' => $model->getId()];
......@@ -40,8 +40,8 @@ return $this->tagPreview(Class_Url::relative($model->getCriteriasUrl()),
->addRowAction(function ($model) {
$label = $this->_('Supprimer la recherche "%s"', $model->getLabel());
return $this->tagAnchor($this->url(['controller' => 'abonne',
'action' => 'delete-bookmarked-search',
return $this->tagAnchor($this->url(['controller' => 'bookmarked-searches',
'action' => 'delete',
'id' => $model->getId()],null,true),
Class_Admin_Skin::current()->renderActionIconOn('delete', $this,
['title' => $label,
......
......@@ -77,4 +77,9 @@ class Class_User_BookmarkedSearch extends Storm_Model_Abstract {
public function beUnnotified() {
return $this->setNotified(0);
}
public function getLibelle() {
return $this->getLabel();
}
}
\ No newline at end of file
......@@ -52,7 +52,9 @@ class ZendAfi_Controller_Plugin_AdminAuth extends Zend_Controller_Plugin_Abstrac
$action = 'sitedown';
}
if ((!$user = Class_Users::getIdentity()) && ($controller == "abonne" && $action !== "authenticate")) {
if ((!$user = Class_Users::getIdentity())
&& $action !== "authenticate"
&& in_array($controller, ["abonne", 'bookmarked-searches'])) {
$request->setParam('redirect', Class_Url::absolute());
$controller = 'auth';
$action = ($request->getParam('render') == 'popup') ? 'popup-login' : 'login';
......
<?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_BookmarkedSearches extends ZendAfi_Controller_Plugin_Manager_Manager {
protected $_user;
public function init() {
parent::init();
$this->_user = Class_Users::getIdentity();
}
protected function _getPost() {
$post = parent::_getPost();
$post['id_user'] = $this->_user->getId();
$post['criterias'] = $this->_getCriterias();
return $post;
}
protected function _getCriterias() {
if (!$namespace = new Zend_Session_Namespace('historiqueRecherche'))
return null;
if (!$searches = $namespace->criteres)
return null;
return array_pop($searches);
}
protected function _checkBookmarkedSearchRequest() {
if(!$search = $this->_findModel()) {
$this->_helper->_notify($this->_('La recherche demandée est introuvable.'));
$this->_redirectClose($this->_getReferer());
return;
}
if(!$search->belongsTo($this->_user)) {
$this->_helper->_notify($this->_('Cette recherche ne vous appartient pas.'));
$this->_redirectClose($this->_getReferer());
return;
}
return $search;
}
protected function _redirectToIndex() {
return $this->_redirectClose($this->_getReferer());
}
protected function _canDelete($model) {
return $model->belongsTo($this->_user);
}
public function notifyAction() {
if(!$search = $this->_checkBookmarkedSearchRequest())
return;
$search->beNotified()->save();
$this->_helper->_notify($this->_('Les notifications de nouveautés pour la recherche "%s" sont activées.', $search->getLabel()));
return $this->_redirectClose($this->_getReferer());
}
public function unnotifyAction() {
if(!$search = $this->_checkBookmarkedSearchRequest())
return;
$search->beUnnotified()->save();
$this->_helper->_notify($this->_('Les notifications de nouveautés pour la recherche "%s" sont désactivées.', $search->getLabel()));
return $this->_redirectClose($this->_getReferer());
}
public function editAction() {
return $this->_redirectToIndex();
}
}
......@@ -165,18 +165,24 @@ class ZendAfi_Controller_Plugin_Manager_Manager extends ZendAfi_Controller_Plugi
}
public function deleteAction() {
if ($this->_response->isRedirect())
return;
if ($model = $this->_findModel()) {
$values = $this->_getCustomFieldModelValues($model);
$values->deleteValues();
$model->delete();
$this->_helper->notify($this->_getSuccessfulDeleteMessage($model));
if (!$model = $this->_findModel())
return $this->_redirectToIndex();
if (!$this->_canDelete($model)) {
$this->_helper->notify($this->_view->_('Vous n\'avez pas la permission "%s"',
$this->_getDeleteActionTitle($model)));
return $this->_redirectToIndex();
}
$values = $this->_getCustomFieldModelValues($model);
$values->deleteValues();
$model->delete();
$this->_helper->notify($this->_getSuccessfulDeleteMessage($model));
$this->_redirectToIndex();
$this->_getDoAfterDelete($model);
}
......@@ -190,6 +196,11 @@ class ZendAfi_Controller_Plugin_Manager_Manager extends ZendAfi_Controller_Plugi
}
protected function _canDelete($model) {
return true;
}
protected function _getEditUrl($model) {
return ['module' => 'admin',
'action' => 'edit',
......@@ -442,11 +453,22 @@ class ZendAfi_Controller_Plugin_Manager_Manager extends ZendAfi_Controller_Plugi
}
public function visitDeleteActionTitle($callback) {
$this->_delete_action_title = $callback;
return $this;
}
protected function _getEditActionTitle($model) {
return call_user_func($this->_edit_action_title, $model);
}
protected function _getDeleteActionTitle($model) {
return call_user_func($this->_delete_action_title, $model);
}
public function visitSuccessfulSaveMessage($callback) {
$this->_successful_save_message = $callback;
return $this;
......
......@@ -146,7 +146,12 @@ abstract class ZendAfi_Controller_Plugin_ResourceDefinition_Abstract extends Zen
public function editActionTitle($model) {
return sprintf($this->titleForAction('edit'),$model->getLibelle());
return sprintf($this->titleForAction('edit'), $model->getLibelle());
}
public function deleteActionTitle($model) {
return sprintf($this->titleForAction('delete'), $model->getLibelle());
}
......@@ -307,6 +312,10 @@ abstract class ZendAfi_Controller_Plugin_ResourceDefinition_Abstract extends Zen
{
return $this->editActionTitle($model);
})
->visitDeleteActionTitle(function($model)
{
return $this->deleteActionTitle($model);
})
->visitSuccessfulSaveMessage(function($model)
{
return $this->successfulSaveMessage($model);
......
<?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_BookmarkedSearches
extends ZendAfi_Controller_Plugin_ResourceDefinition_Abstract {
public function getDefinitions() {
return [
'model' => ['class' => 'Class_User_BookmarkedSearch',
'name' => 'search',
'order' => 'label'],
'messages' => ['successful_save' => $this->_('La recherche "%s" a été enregistrée dans mes favoris'),
'successful_add' => $this->_('La recherche "%s" a été enregistrée dans mes favoris'),
'successful_delete' => $this->_('La recherche "%s" a été supprimée de mes favoris')],
'actions' => ['add' => ['title' => $this->_('Enregister la recherche dans mes favoris')],
'edit' => ['title' => $this->_('Enregister la recherche dans mes favoris')],
'delete' => ['title' => $this->_('Supprimer la recherche de mes favoris')]],
'form_class_name' => 'ZendAfi_Form_User_BookmarkedSearch',
];
}
}
......@@ -20,7 +20,7 @@
*/
class ZendAfi_Form_User_BookmarkSearch extends ZendAfi_Form {
class ZendAfi_Form_User_BookmarkedSearch extends ZendAfi_Form {
public function init() {
parent::init();
$this
......@@ -31,7 +31,14 @@ class ZendAfi_Form_User_BookmarkSearch extends ZendAfi_Form {
'allowEmpty' => false,
'placeholder' => $this->_('Titre de votre recherche')])
->addDisplayGroup(['label'],
->addElement('checkbox',
'notified',
['label' => $this->_('Me notifier des nouveautés par email'),
'required' => true,
'allowEmpty' => false])
->addDisplayGroup(['label',
'notified'],
'group_bookmark_search');
}
}
\ No newline at end of file
......@@ -144,8 +144,8 @@ class ZendAfi_View_Helper_Search_Header extends ZendAfi_View_Helper_BaseHelper {
return '';
return $this->_tag('span',
$this->view->tagAnchor($this->view->url(['controller' => 'abonne',
'action' => 'bookmark-search'], null, true),
$this->view->tagAnchor($this->view->url(['controller' => 'bookmarked-searches',
'action' => 'add'], null, true),
$this->_('Enregistrer ma recherche'),
['title' => $this->_('Enregistrer ma recherche dans mes favoris'),
'data-popup' => true]),
......
......@@ -36,7 +36,7 @@ class Bookmarks_SearchLinkTest extends AbstractControllerTestCase {
/** @test */
public function linkToBookmarkSearchShouldBePresent() {
$this->assertXPathContentContains('//a[contains(@href, "/abonne/bookmark-search")]', 'Enregistrer ma recherche');
$this->assertXPathContentContains('//a[contains(@href, "/bookmarked-searches/add")]', 'Enregistrer ma recherche');
}
}
......@@ -77,7 +77,7 @@ class Bookmarks_SearchAbonneBookmarkSearchDispatchTest extends Bookmarks_Search
'criterias' => '',
'creation_date' => '2018-01-17 15:05:57']);
$this->dispatch('/abonne/bookmark-search', true);
$this->dispatch('/bookmarked-searches/add', true);
}
......@@ -85,6 +85,12 @@ class Bookmarks_SearchAbonneBookmarkSearchDispatchTest extends Bookmarks_Search
public function formShouldContainsLabel() {
$this->assertXPath('//form//input[@type="text"][@name="label"]');
}
/** @test */
public function formShouldContainsNotified() {
$this->assertXPath('//form//input[@type="checkbox"][@name="notified"]');
}
}
......@@ -93,7 +99,8 @@ class Bookmarks_SearchAbonneBookmarkSearchPostTest extends Bookmarks_SearchWith
public function setUp() {
parent::setUp();
Class_User_BookmarkedSearch::setTimeSource(new TimeSourceForTest('2018-01-17 01:01:01'));
$this->postDispatch('/abonne/bookmark-search', ['label' => 'search']);
$this->postDispatch('/bookmarked-searches/add', ['label' => 'search',
'notified' => '1']);
}
......@@ -119,6 +126,12 @@ class Bookmarks_SearchAbonneBookmarkSearchPostTest extends Bookmarks_SearchWith
public function davisSearchShouldBeBookmarkedInUserSettings() {
$this->assertEquals('Miles Davis', Class_User_BookmarkedSearch::find(1)->getUnserializedCriterias()->getParam('rech_auteurs'));
}
/** @test */
public function bookmarkedSearchShouldBeNotified() {
$this->assertTrue(Class_User_BookmarkedSearch::find(1)->isNotified());
}
}
......@@ -132,7 +145,7 @@ class Bookmarks_SearchAbonneNotLoggedTest extends AbstractControllerTestCase {
public function setUp() {
parent::setUp();
ZendAfi_Auth::getInstance()->clearIdentity();
$this->dispatch('/abonne/bookmark-search', true);
$this->dispatch('/bookmarked-searches/add', true);
}
......@@ -207,7 +220,7 @@ class Bookmarks_SearchAbonneSettingsTest extends Bookmarks_SearchWithSessionAbst
/** @test */
public function tableShouldContainsDeleteButton() {
$this->assertXPath('//td//a[contains(@onclick, "return confirm")][contains(@href,"/abonne/delete-bookmarked-search/id/5")]');
$this->assertXPath('//td//a[contains(@onclick, "return confirm")][contains(@href,"/bookmarked-searches/delete/id/5")]');
}
......@@ -225,13 +238,13 @@ class Bookmarks_SearchAbonneSettingsTest extends Bookmarks_SearchWithSessionAbst
/** @test */
public function tableShoulContainsLinkEnableMailNotification() {
$this->assertXPath('//td//a[contains(@href, "/abonne/notify-bookmarked-search/id/5")]');
$this->assertXPath('//td//a[contains(@href, "/bookmarked-searches/notify/id/5")]');
}
/** @test */
public function tableShoulContainsLinkUnableMailNotification() {
$this->assertXPath('//td//a[contains(@href, "/abonne/unnotify-bookmarked-search/id/8")]');
$this->assertXPath('//td//a[contains(@href, "/bookmarked-searches/unnotify/id/8")]');
}
}
......@@ -251,7 +264,7 @@ class Bookmarks_SearchDeleteBookmarkedSearchTest extends Bookmarks_SearchWithSes
'criterias' => '',
'creation_date' => '2018-01-17 15:05:57']);
$this->dispatch('/abonne/delete-bookmarked-search/id/5', true);
$this->dispatch('/bookmarked-searches/delete/id/5', true);
}
......@@ -263,7 +276,13 @@ class Bookmarks_SearchDeleteBookmarkedSearchTest extends Bookmarks_SearchWithSes
/** @test */
public function notifyMessageShouldBeRechercheSupprimee() {
$this->assertFlashMessengerContentContains('La recherche "Miles Davis" a été supprimée.');
$this->assertFlashMessengerContentContains('La recherche "Miles Davis" a été supprimée de mes favoris');
}
/** @test */
public function shouldRedirectToReferer() {
$this->assertRedirectTo('/');
}