Commit 08fd3693 authored by Ghislain Loas's avatar Ghislain Loas
Browse files

dev #69826 : save bookmarked searches in new table.

parent 8fc7f2df
......@@ -1003,7 +1003,7 @@ class AbonneController extends ZendAfi_Controller_Action {
return $this->_forward('popup-login', 'auth', 'opac', ['redirect' => $this->view->url()]);
$this->view->form = $form = ZendAfi_Form_User_Settings::forUser($this->_user);
$this->view->bookmarked_searches = Class_User_BookmarkedSearch::findAllBy(['id_user' => $this->_user->getId()]);
if ($this->_request->isPost()) {
$data = $this->_request->getPost();
unset($data['id_items']);
......@@ -1212,11 +1212,37 @@ class AbonneController extends ZendAfi_Controller_Action {
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->_user->bookmarkSearch()->save();
$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(),
'email' => $this->_user->getMail(),
'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'));
$this->_redirectClose($this->_getReferer());
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);
}
}
\ No newline at end of file
<?php
echo $this->renderForm($this->form);
?>
$description = (new Class_TableDescription('bookmarked_searches'))
->addColumn($this->_('Libellé'), function($model) { return $model->getLabel(); })
->addColumn($this->_('Date création'), function($model) { return $model->getCreationDate(); });
echo $this->renderTable($description, $this->bookmarked_searches, ['sorter' => true]);
<?php
Zend_Db_Table_Abstract::getDefaultAdapter()
->query('CREATE TABLE if not exists `bookmarked_search` ( '
. 'id int(11) unsigned not null auto_increment,'
. 'label TEXT not null,'
. 'criterias text not null,'
. 'email text not null,'
. 'creation_date timestamp not null,'
. 'modification_date timestamp not null,'
. 'id_user int(11) unsigned not null default 0,'
. 'primary key (id),'
. 'key (`id_user`)'
. ') engine=MyISAM default charset=utf8');
\ No newline at end of file
......@@ -221,7 +221,9 @@ class Class_AdminVarLoader extends Storm_Model_Loader {
protected function _getUsersVars() {
return ['NDAYS_EXPIRY_NOTICE' => Class_AdminVar_Meta::newDefault($this->_('Prévenir l\'utilisateur xx jour(s) avant l\'expiration de son abonnement (par défaut 30 jours).'), ['value' => 30]),
'DISABLE_SUGGESTIONS' => Class_AdminVar_Meta::newOnOff($this->_('Désactivation des suggestions d\'achats'))->bePrivate(),
'ENABLE_BOOKMARKABLE_LIBRARIES' => Class_AdminVar_Meta::newOnOff($this->_('Les utilisateurs peuvent sélectionner des bibliothèques favorites pour leurs recherche'), ['value' => 1])];
'ENABLE_BOOKMARKABLE_LIBRARIES' => Class_AdminVar_Meta::newOnOff($this->_('Les utilisateurs peuvent sélectionner des bibliothèques favorites pour leurs recherche'), ['value' => 1]),
'ENABLE_BOOKMARKABLE_SEARCHES' => Class_AdminVar_Meta::newOnOff($this->_('Les utilisateurs peuvent enregister des recherches dans leurs favoris'),
['value' => 0])];
}
......
<?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 UserBookmarkedSearch extends Storm_Model_Loader {
public function newInstance($attributes = null) {
$instance = parent::newInstance($attributes);
$instance->setCreationDate($instance->getCurrentDateTime());
return $instance;
}
}
class Class_User_BookmarkedSearch extends Storm_Model_Abstract {
use Trait_TimeSource;
protected
$_loader_class = 'UserBookmarkedSearch',
$_table_name = 'bookmarked_search',
$_belongs_to = ['user' => ['model' => 'Class_Users',
'referenced_in' => 'id_user']],
$_default_attribute = ['label' => '',
'creation_date' => '',
'modification_date' => '',
'criterias' => '',
'email' => '',
'id_user' => ''];
public function getUnserialzedCriterias() {
return unserialize($this->getCriterias());
}
}
\ No newline at end of file
......@@ -47,6 +47,11 @@ class Class_User_Settings {
}
public static function isBookmarkSearchesReady() {
return Class_AdminVar::isModuleEnabled('ENABLE_BOOKMARKABLE_SEARCHES');
}
public static function isBookmarkDomainsReady() {
return Class_Catalogue::hasViewableDomain();
}
......@@ -54,7 +59,8 @@ class Class_User_Settings {
public static function isBookmarkReady() {
return static::isBookmarkDomainsReady()
|| static::isBookmarkLibraryReady();
|| static::isBookmarkLibraryReady()
|| static::isBookmarkSearchesReady();
}
......@@ -261,20 +267,4 @@ class Class_User_Settings {
$this->set(self::BOOKMARKED_LIBRARIES, $id);
$this->_user->save();
}
public function bookmarkSearch() {
if (!$namespace = new Zend_Session_Namespace('historiqueRecherche'))
return $this->_user;
if (!$searches = $namespace->criteres)
return $this->_user;
if ( !$bookmarks = $this->get(self::BOOKMARKED_SEARCHES))
$bookmarks = [];
$this->set(self::BOOKMARKED_SEARCHES, array_merge($bookmarks,[array_pop($searches)]));
return $this->_user;
}
}
?>
\ No newline at end of file
}
\ 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 ZendAfi_Form_User_BookmarkSearch extends ZendAfi_Form {
public function init() {
parent::init();
$this
->addElement('text',
'label',
['label' => $this->_('Libellé'),
'required' => true,
'allowEmpty' => false,
'placeholder' => $this->_('Titre de votre recherche')])
->addDisplayGroup(['label'],
'group_bookmark_search');
}
}
\ No newline at end of file
......@@ -114,8 +114,8 @@ class ZendAfi_View_Helper_Search_Header extends ZendAfi_View_Helper_BaseHelper {
$this->_tag('span',
$this->view->tagAnchor($this->view->url(['controller' => 'abonne',
'action' => 'bookmark-search'], null, true),
$this->_('Enregistrer cette recherche'),
['title' => $this->_('Enregistrer cette recherche dans mes favoris'),
$this->_('Enregistrer ma recherche'),
['title' => $this->_('Enregistrer ma recherche dans mes favoris'),
'data-popup' => true]),
['class' => 'save']),
......
......@@ -1995,3 +1995,36 @@ class UpgradeDB_341_Test extends UpgradeDBTestCase {
$this->assertEquals(0, $datas['numberOf']);
}
}
class UpgradeDB_342_Test extends UpgradeDBTestCase {
public function prepare() {
try {
$this->query('drop table bookmarked_search');
} catch (Exception $e) {}
}
public function datas() {
return
[
['label', 'text'],
['id_user', 'int(11) unsigned'],
['email', 'text'],
['creation_date', 'timestamp'],
['modification_date', 'timestamp'],
['criterias', 'text'],
];
}
/**
* @test
* @dataProvider datas
*/
public function fieldsShouldExists($field, $type) {
$this->assertFieldType('bookmarked_search', $field, $type);
}
}
......@@ -20,9 +20,31 @@
*/
class Bookmarks_SearchAbonneTest extends Admin_AbstractControllerTestCase {
protected $_storm_default_to_volatile = true,
class Bookmarks_SearchLinkTest extends AbstractControllerTestCase {
protected $_storm_default_to_volatile = true;
public function setUp() {
parent::setUp();
$this->dispatch('/recherche/pomme', true);
}
/** @test */
public function linkToBookmarkSearchShouldBePresent() {
$this->assertXPathContentContains('//a[contains(@href, "/abonne/bookmark-search")]', 'Enregistrer ma recherche');
}
}
abstract class Bookmarks_SearchWithSessionAbstract extends Admin_AbstractControllerTestCase {
protected
$_storm_default_to_volatile = true,
$_user;
public function setUp() {
parent::setUp();
$this->_user = Class_Users::getIdentity();
......@@ -36,31 +58,139 @@ class Bookmarks_SearchAbonneTest extends Admin_AbstractControllerTestCase {
$session = new Zend_Session_Namespace('historiqueRecherche');
$session->criteres = [serialize($criteres_potter), serialize($criteres_davis)];
}
}
class Bookmarks_SearchAbonneBookmarkSearchDispatchTest extends Bookmarks_SearchWithSessionAbstract {
public function setUp() {
parent::setUp();
$this->fixture('Class_User_BookmarkedSearch',
['id' => 5,
'label' => 'Ma première recherche sauvegardée',
'criterias' => '',
'creation_date' => '2018-01-17 15:05:57']);
$this->dispatch('/abonne/bookmark-search', true);
}
/** @test */
public function formShouldContainsLabel() {
$this->assertXPath('//form//input[@type="text"][@name="label"]');
}
}
class Bookmarks_SearchAbonneBookmarkSearchPostTest extends Bookmarks_SearchWithSessionAbstract {
public function setUp() {
parent::setUp();
Class_User_BookmarkedSearch::setTimeSource(new TimeSourceForTest('2018-01-17 01:01:01'));
$this->postDispatch('/abonne/bookmark-search', ['label' => 'search']);
}
/** @test */
public function bookmarkedSearchLabelShouldBeSearch() {
$this->assertEquals('search', Class_User_BookmarkedSearch::find(1)->getLabel());
}
$this->dispatch('/abonne/bookmark-search',true);
/** @test */
public function dateShouldBe20180117() {
$this->assertEquals('2018-01-17 01:01:01', Class_User_BookmarkedSearch::find(1)->getCreationDate());
}
/** @test */
public function idUserShouldBe666() {
$this->assertEquals(666, Class_User_BookmarkedSearch::find(1)->getIdUser());
}
/** @test */
public function lastSearchDavisShouldBeBookmarkedInUserSettings() {
$this->assertEquals('Miles Davis',unserialize(Class_User_Settings::newWith($this->_user)->get('bookmarked_searches')[0])->getParam('rech_auteurs'));
public function emailShouldBeSet() {
$this->assertEquals('', Class_User_BookmarkedSearch::find(1)->getEmail());
}
/** @test */
public function davisSearchShouldBeBookmarkedInUserSettings() {
$this->assertEquals('Miles Davis', Class_User_BookmarkedSearch::find(1)->getUnserialzedCriterias()->getParam('rech_auteurs'));
}
}
class Bookmarks_SearchLinkTest extends AbstractControllerTestCase {
protected $_storm_default_to_volatile = true;
class Bookmarks_SearchAbonneNotLoggedTest extends AbstractControllerTestCase {
protected
$_storm_default_to_volatile = true;
public function setUp() {
parent::setUp();
$this->dispatch('/recherche/pomme', true);
ZendAfi_Auth::getInstance()->clearIdentity();
$this->dispatch('/abonne/bookmark-search', true);
}
/** @test */
public function shouldContainsLoginForm() {
$this->assertXPath('//div[@class="contenu"]//form[@action="/auth/login"]');
}
}
/** @test */
public function linkToBookmarkSearchShouldBePresent() {
$this->assertXPathContentContains('//a[contains(@href, "/abonne/bookmark-search")]', 'Enregistrer cette recherche');
class Bookmarks_SearchAbonneFicheTest extends Bookmarks_SearchWithSessionAbstract {
protected $_storm_default_to_volatile = true;
public function setUp() {
parent::setUp();
$this->fixture('Class_AdminVar',
['id' => 'ENABLE_BOOKMARKABLE_SEARCHES',
'valeur' => 1]);
$this->_user->bookmarkSearch()->save();
$this->dispatch('/abonne/fiche', true);
}
/** @test */
public function manageBookmarksLinkShouldBePresent() {
$this->assertXPath('//a[contains(@href, "abonne/settings")]');
}
}
class Bookmarks_SearchAbonneSettingsTest extends Bookmarks_SearchWithSessionAbstract {
protected $_storm_default_to_volatile = true;
public function setUp() {
parent::setUp();
$this->fixture('Class_AdminVar',
['id' => 'ENABLE_BOOKMARKABLE_SEARCHES',
'valeur' => 1]);
$this->fixture('Class_User_BookmarkedSearch',
['id' => 5,
'id_user' => $this->_user->getId(),
'label' => 'Miles Davis',
'criterias' => '',
'creation_date' => '2018-01-17 15:05:57']);
$this->dispatch('/abonne/settings', true);
}
/** @test */
public function tableShouldContainsMilesDavis() {
$this->assertXPathContentContains('//td', 'Miles Davis');
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment