The Gitlab instance will be restarted on Monday April 28th at 2AM. There will be a short interruption of service.

Skip to content
Snippets Groups Projects
Commit 530ee397 authored by Demian Katz's avatar Demian Katz Committed by GitHub
Browse files

Refactor "save to favorites" as a service. (#872)

parent 7c93ef36
No related merge requests found
......@@ -192,6 +192,7 @@ $config = [
'VuFind\DbAdapterFactory' => 'VuFind\Service\Factory::getDbAdapterFactory',
'VuFind\DbTablePluginManager' => 'VuFind\Service\Factory::getDbTablePluginManager',
'VuFind\Export' => 'VuFind\Service\Factory::getExport',
'VuFind\Favorites\FavoritesService' => 'VuFind\Favorites\FavoritesServiceFactory',
'VuFind\HierarchyDriverPluginManager' => 'VuFind\Service\Factory::getHierarchyDriverPluginManager',
'VuFind\HierarchyTreeDataFormatterPluginManager' => 'VuFind\Service\Factory::getHierarchyTreeDataFormatterPluginManager',
'VuFind\HierarchyTreeDataSourcePluginManager' => 'VuFind\Service\Factory::getHierarchyTreeDataSourcePluginManager',
......
......@@ -289,7 +289,9 @@ class AbstractRecord extends AbstractBase
$tagParser = $this->getServiceLocator()->get('VuFind\Tags');
$post['mytags']
= $tagParser->parse(isset($post['mytags']) ? $post['mytags'] : '');
$results = $driver->saveToFavorites($post, $user);
$favorites = $this->getServiceLocator()
->get('VuFind\Favorites\FavoritesService');
$results = $favorites->save($post, $user, $driver);
// Display a success status message:
$listUrl = $this->url()->fromRoute('userList', ['id' => $results['listId']]);
......
......@@ -545,21 +545,23 @@ class MyResearchController extends AbstractBase
{
$lists = $this->params()->fromPost('lists');
$tagParser = $this->getServiceLocator()->get('VuFind\Tags');
$favorites = $this->getServiceLocator()
->get('VuFind\Favorites\FavoritesService');
foreach ($lists as $list) {
$tags = $this->params()->fromPost('tags' . $list);
$driver->saveToFavorites(
$favorites->save(
[
'list' => $list,
'mytags' => $tagParser->parse($tags),
'notes' => $this->params()->fromPost('notes' . $list)
],
$user
$user, $driver
);
}
// add to a new list?
$addToList = $this->params()->fromPost('addToList');
if ($addToList > -1) {
$driver->saveToFavorites(['list' => $addToList], $user);
$favorites->save(['list' => $addToList], $user, $driver);
}
$this->flashMessenger()->addMessage('edit_list_success', 'success');
......
<?php
/**
* Favorites service
*
* PHP version 5
*
* Copyright (C) Villanova University 2016.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category VuFind
* @package Favorites
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org Main Page
*/
namespace VuFind\Favorites;
use VuFind\Record\Cache as RecordCache;
use VuFind\RecordDriver\AbstractBase as RecordDriver;
use VuFind\Db\Table\Resource as ResourceTable;
use VuFind\Db\Table\UserList as UserListTable;
/**
* Favorites service
*
* @category VuFind
* @package Favorites
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org Main Page
*/
class FavoritesService implements \VuFind\I18n\Translator\TranslatorAwareInterface
{
use \VuFind\I18n\Translator\TranslatorAwareTrait;
/**
* Record cache
*
* @var RecordCache
*/
protected $recordCache = null;
/**
* Resource database table
*
* @var ResourceTable
*/
protected $resourceTable;
/**
* UserList database table
*
* @var UserListTable
*/
protected $userListTable;
/**
* Constructor
*
* @param UserListTable $userList UserList table object
* @param ResourceTable $resource Resource table object
* @param RecordCache $cache Record cache
*/
public function __construct(UserListTable $userList, ResourceTable $resource,
RecordCache $cache = null
) {
$this->recordCache = $cache;
$this->userListTable = $userList;
$this->resourceTable = $resource;
}
/**
* Get a list object for the specified ID (or 'NEW' to create a new list).
*
* @param string $listId List ID (or 'NEW')
* @param \VuFind\Db\Row\User $user The user saving the record
*
* @return \VuFind\Db\Row\UserList
*
* @throws \VuFind\Exception\ListPermission
*/
protected function getListObject($listId, \VuFind\Db\Row\User $user)
{
if (empty($listId) || $listId == 'NEW') {
$list = $this->userListTable->getNew($user);
$list->title = $this->translate('My Favorites');
$list->save($user);
} else {
$list = $this->userListTable->getExisting($listId);
// Validate incoming list ID:
if (!$list->editAllowed($user)) {
throw new \VuFind\Exception\ListPermission('Access denied.');
}
$list->rememberLastUsed(); // handled by save() in other case
}
return $list;
}
/**
* Persist a resource to the record cache (if applicable).
*
* @param RecordDriver $driver Record driver to persist
* @param \VuFind\Db\Row\Resource $resource Resource row
*
* @return void
*/
protected function persistToCache(RecordDriver $driver,
\VuFind\Db\Row\Resource $resource
) {
if ($this->recordCache) {
$this->recordCache->setContext(RecordCache::CONTEXT_FAVORITE);
$this->recordCache->createOrUpdate(
$resource->record_id, $resource->source,
$driver->getRawData()
);
}
}
/**
* Save this record to the user's favorites.
*
* @param array $params Array with some or all of these keys:
* <ul>
* <li>mytags - Tag array to associate with record (optional)</li>
* <li>notes - Notes to associate with record (optional)</li>
* <li>list - ID of list to save record into (omit to create new list)</li>
* </ul>
* @param \VuFind\Db\Row\User $user The user saving the record
* @param RecordDriver $driver Record driver for record being saved
*
* @return array list information
*/
public function save(array $params, \VuFind\Db\Row\User $user,
RecordDriver $driver
) {
// Validate incoming parameters:
if (!$user) {
throw new LoginRequiredException('You must be logged in first');
}
// Get or create a list object as needed:
$list = $this->getListObject(
isset($params['list']) ? $params['list'] : '',
$user
);
// Get or create a resource object as needed:
$resource = $this->resourceTable->findResource(
$driver->getUniqueId(), $driver->getSourceIdentifier(), true, $driver
);
// Persist record in the database for "offline" use
$this->persistToCache($driver, $resource);
// Add the information to the user's account:
$user->saveResource(
$resource, $list,
isset($params['mytags']) ? $params['mytags'] : [],
isset($params['notes']) ? $params['notes'] : ''
);
return ['listId' => $list->id];
}
}
<?php
/**
* Favorites service factory
*
* PHP version 5
*
* Copyright (C) Villanova University 2016.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category VuFind
* @package Favorites
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org Main Page
*/
namespace VuFind\Favorites;
use Zend\ServiceManager\ServiceLocatorInterface;
/**
* Favorites service
*
* @category VuFind
* @package Favorites
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org Main Page
*
* @codeCoverageIgnore
*/
class FavoritesServiceFactory implements \Zend\ServiceManager\FactoryInterface
{
/**
* Create service
*
* @param ServiceLocatorInterface $sm Service manager
*
* @return mixed
*/
public function createService(ServiceLocatorInterface $sm)
{
$tableManager = $sm->get('VuFind\DbTablePluginManager');
return new FavoritesService(
$tableManager->get('userlist'),
$tableManager->get('resource'),
$sm->get('VuFind\RecordCache')
);
}
}
......@@ -28,7 +28,6 @@
namespace VuFind\RecordDriver;
use VuFind\Exception\LoginRequired as LoginRequiredException,
VuFind\XSLT\Import\VuFind as ArticleStripper;
use VuFind\Record\Cache;
/**
* Abstract base record model.
......@@ -43,12 +42,10 @@ use VuFind\Record\Cache;
*/
abstract class AbstractBase implements \VuFind\Db\Table\DbTableAwareInterface,
\VuFind\I18n\Translator\TranslatorAwareInterface,
\VuFindSearch\Response\RecordInterface,
\VuFind\Record\Cache\RecordCacheAwareInterface
\VuFindSearch\Response\RecordInterface
{
use \VuFind\Db\Table\DbTableAwareTrait;
use \VuFind\I18n\Translator\TranslatorAwareTrait;
use \VuFind\Record\Cache\RecordCacheAwareTrait;
/**
* Used for identifying search backends
......@@ -227,66 +224,6 @@ abstract class AbstractBase implements \VuFind\Db\Table\DbTableAwareInterface,
}
}
/**
* Save this record to the user's favorites.
*
* @param array $params Array with some or all of these keys:
* <ul>
* <li>mytags - Tag array to associate with record (optional)</li>
* <li>notes - Notes to associate with record (optional)</li>
* <li>list - ID of list to save record into (omit to create new list)</li>
* </ul>
* @param \VuFind\Db\Row\User $user The user saving the record
*
* @return array list information
*/
public function saveToFavorites($params, $user)
{
// Validate incoming parameters:
if (!$user) {
throw new LoginRequiredException('You must be logged in first');
}
// Get or create a list object as needed:
$listId = isset($params['list']) ? $params['list'] : '';
$table = $this->getDbTable('UserList');
if (empty($listId) || $listId == 'NEW') {
$list = $table->getNew($user);
$list->title = $this->translate('My Favorites');
$list->save($user);
} else {
$list = $table->getExisting($listId);
// Validate incoming list ID:
if (!$list->editAllowed($user)) {
throw new \VuFind\Exception\ListPermission('Access denied.');
}
$list->rememberLastUsed(); // handled by save() in other case
}
// Get or create a resource object as needed:
$resourceTable = $this->getDbTable('Resource');
$resource = $resourceTable->findResource(
$this->getUniqueId(), $this->getSourceIdentifier(), true, $this
);
// Persist record in the database for "offline" use
if ($recordCache = $this->getRecordCache()) {
$recordCache->setContext(Cache::CONTEXT_FAVORITE);
$recordCache->createOrUpdate(
$resource->record_id, $resource->source,
$this->getRawData()
);
}
// Add the information to the user's account:
$user->saveResource(
$resource, $list,
isset($params['mytags']) ? $params['mytags'] : [],
isset($params['notes']) ? $params['notes'] : ''
);
return ['listId' => $list->id];
}
/**
* Get notes associated with this record in user lists.
*
......
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