Skip to content

Commit

Permalink
Merge branch 'dev' into doctrine
Browse files Browse the repository at this point in the history
  • Loading branch information
demiankatz committed Jun 6, 2024
2 parents 31db1e1 + 235cc5e commit ac1d867
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 30 deletions.
36 changes: 21 additions & 15 deletions module/VuFind/src/VuFind/AjaxHandler/GetSaveStatuses.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@

use Laminas\Mvc\Controller\Plugin\Params;
use Laminas\Mvc\Controller\Plugin\Url;
use VuFind\Db\Entity\UserResourceEntityInterface;
use VuFind\Db\Row\User;
use VuFind\Db\Service\UserResourceServiceInterface;
use VuFind\I18n\Translator\TranslatorAwareInterface;
use VuFind\Session\Settings as SessionSettings;

use function count;
use function is_array;

/**
Expand All @@ -56,28 +57,34 @@ class GetSaveStatuses extends AbstractBase implements TranslatorAwareInterface
/**
* Constructor
*
* @param SessionSettings $ss Session settings
* @param ?User $user Logged in user (or null)
* @param Url $urlHelper URL helper
* @param SessionSettings $ss Session settings
* @param ?User $user Logged in user (or null)
* @param Url $urlHelper URL helper
* @param UserResourceServiceInterface $userResourceService User resource database service
*/
public function __construct(SessionSettings $ss, protected ?User $user, protected Url $urlHelper)
{
public function __construct(
SessionSettings $ss,
protected ?User $user,
protected Url $urlHelper,
protected UserResourceServiceInterface $userResourceService
) {
$this->sessionSettings = $ss;
}

/**
* Format list object into array.
* Format UserResourceEntityInterface object into array.
*
* @param array $list List data
* @param UserResourceEntityInterface $data UserResourceEntityInterface object
*
* @return array
*/
protected function formatListData($list)
protected function formatListData(UserResourceEntityInterface $data): array
{
return [
$list = $data->getUserList();
return !$list ? [] : [
'list_url' =>
$this->urlHelper->fromRoute('userList', ['id' => $list['list_id']]),
'list_title' => $list['list_title'],
$this->urlHelper->fromRoute('userList', ['id' => $list->getId()]),
'list_title' => $list->getTitle(),
];
}

Expand All @@ -101,9 +108,8 @@ protected function getDataFromUser($ids, $sources)
if (!isset($checked[$selector])) {
$checked[$selector] = true;

$data = $this->user->getSavedData($id, null, $source);
$result[$selector] = ($data && count($data) > 0)
? array_map([$this, 'formatListData'], $data->toArray()) : [];
$data = $this->userResourceService->getFavoritesForRecord($id, $source, null, $this->user);
$result[$selector] = array_filter(array_map([$this, 'formatListData'], $data));
}
}
return $result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
use Psr\Container\ContainerExceptionInterface as ContainerException;
use Psr\Container\ContainerInterface;
use VuFind\Db\Service\PluginManager as DbServiceManager;
use VuFind\Db\Service\UserResourceServiceInterface;

/**
* Factory for GetSaveStatuses AJAX handler.
Expand Down Expand Up @@ -72,7 +74,8 @@ public function __invoke(
return new $requestedName(
$container->get(\VuFind\Session\Settings::class),
$container->get(\VuFind\Auth\Manager::class)->getUserObject(),
$container->get('ControllerPluginManager')->get('url')
$container->get('ControllerPluginManager')->get('url'),
$container->get(DbServiceManager::class)->get(UserResourceServiceInterface::class)
);
}
}
10 changes: 7 additions & 3 deletions module/VuFind/src/VuFind/Controller/AbstractRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

namespace VuFind\Controller;

use VuFind\Db\Service\UserResourceServiceInterface;
use VuFind\Exception\BadRequest as BadRequestException;
use VuFind\Exception\Forbidden as ForbiddenException;
use VuFind\Exception\Mail as MailException;
Expand Down Expand Up @@ -492,13 +493,16 @@ public function saveAction()

// Find out if the item is already part of any lists; save list info/IDs
$listIds = [];
$resources = $user->getSavedData(
$resources = $this->getDbService(UserResourceServiceInterface::class)->getFavoritesForRecord(
$driver->getUniqueId(),
$driver->getSourceIdentifier(),
null,
$driver->getSourceIdentifier()
$user
);
foreach ($resources as $userResource) {
$listIds[] = $userResource->list_id;
if ($currentList = $userResource->getUserList()) {
$listIds[] = $currentList->getId();
}
}

// Loop through all user lists and sort out containing/non-containing lists
Expand Down
26 changes: 17 additions & 9 deletions module/VuFind/src/VuFind/Controller/MyResearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use VuFind\Controller\Feature\ListItemSelectionTrait;
use VuFind\Db\Entity\UserEntityInterface;
use VuFind\Db\Service\UserListServiceInterface;
use VuFind\Db\Service\UserResourceServiceInterface;
use VuFind\Db\Service\UserServiceInterface;
use VuFind\Exception\Auth as AuthException;
use VuFind\Exception\AuthEmailNotVerified as AuthEmailNotVerifiedException;
Expand Down Expand Up @@ -1011,26 +1012,33 @@ public function editAction()
}

// Get saved favorites for selected list (or all lists if $listID is null)
$userResources = $user->getSavedData($id, $listID, $source);
$userResourceService = $this->getDbService(UserResourceServiceInterface::class);
$userResources = $userResourceService->getFavoritesForRecord($id, $source, $listID, $user);
$savedData = [];
foreach ($userResources as $current) {
$savedData[] = [
'listId' => $current->list_id,
'listTitle' => $current->list_title,
'notes' => $current->notes,
'tags' => $user->getTagString($id, $current->list_id, $source),
];
// There should always be list data based on the way we retrieve this result, but
// check just to be on the safe side.
if ($currentList = $current->getUserList()) {
$savedData[] = [
'listId' => $currentList->getId(),
'listTitle' => $currentList->getTitle(),
'notes' => $current->getNotes(),
'tags' => $user->getTagString($id, $currentList->getId(), $source),
];
}
}

// In order to determine which lists contain the requested item, we may
// need to do an extra database lookup if the previous lookup was limited
// to a particular list ID:
$containingLists = [];
if (!empty($listID)) {
$userResources = $user->getSavedData($id, null, $source);
$userResources = $userResourceService->getFavoritesForRecord($id, $source, null, $user);
}
foreach ($userResources as $current) {
$containingLists[] = $current->list_id;
if ($currentList = $current->getUserList()) {
$containingLists[] = $currentList->getId();
}
}
// Send non-containing lists to the view for user selection:
$userLists = $this->getDbService(UserListServiceInterface::class)->getListsForUser($user->id);
Expand Down
2 changes: 2 additions & 0 deletions module/VuFind/src/VuFind/Db/Row/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ public function formatTagString($tags)
* @param string $source Source of record to look up
*
* @return array
*
* @deprecated Use UserResourceServiceInterface::getFavoritesForRecord()
*/
public function getSavedData(
$resourceId,
Expand Down
42 changes: 42 additions & 0 deletions module/VuFind/src/VuFind/Db/Service/UserResourceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@
use Laminas\Log\LoggerAwareInterface;
use VuFind\Db\Entity\Resource;
use VuFind\Db\Entity\User;
use VuFind\Db\Entity\UserEntityInterface;
use VuFind\Db\Entity\UserList;
use VuFind\Db\Entity\UserListEntityInterface;
use VuFind\Db\Entity\UserResource;
use VuFind\Db\Entity\UserResourceEntityInterface;
use VuFind\Log\LoggerAwareTrait;

use function is_int;

/**
* Database service for UserResource.
*
Expand Down Expand Up @@ -122,6 +127,43 @@ public function deduplicate()
}
}

/**
* Get information saved in a user's favorites for a particular record.
*
* @param string $recordId ID of record being checked.
* @param string $source Source of record to look up
* @param int|UserListEntityInterface|null $list Optional list ID or entity
* (to limit results to a particular list).
* @param int|UserEntityInterface|null $user Optional user ID or entity
* (to limit results to a particular user).
*
* @return UserResourceEntityInterface[]
*/
public function getFavoritesForRecord(
string $recordId,
string $source = DEFAULT_SEARCH_BACKEND,
int|UserListEntityInterface|null $list = null,
int|UserEntityInterface|null $user = null
): array {
$dql = 'SELECT DISTINCT ur FROM ' . $this->getEntityClass(UserResource::class) . ' ur '
. 'JOIN ' . $this->getEntityClass(Resource::class) . ' r WITH r.id = ur.resource '
. 'WHERE r.source = :source AND r.recordId = :recordId ';

$parameters = compact('source', 'recordId');
if (null !== $user) {
$dql .= 'AND ur.user = :user ';
$parameters['user'] = $this->getDoctrineReference(User::class, $user);
}
if (null !== $list) {
$dql .= 'AND ur.list = :list';
$parameters['list'] = $this->getDoctrineReference(UserList::class, $list);
}

$query = $this->entityManager->createQuery($dql);
$query->setParameters($parameters);
return $query->getResult();
}

/**
* Get statistics on use of UserResource.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@

namespace VuFind\Db\Service;

use VuFind\Db\Entity\UserEntityInterface;
use VuFind\Db\Entity\UserListEntityInterface;
use VuFind\Db\Entity\UserResourceEntityInterface;

/**
* Database service interface for UserResource.
*
Expand All @@ -40,6 +44,25 @@
*/
interface UserResourceServiceInterface extends DbServiceInterface
{
/**
* Get information saved in a user's favorites for a particular record.
*
* @param string $recordId ID of record being checked.
* @param string $source Source of record to look up
* @param int|UserListEntityInterface|null $list Optional list ID or entity
* (to limit results to a particular list).
* @param int|UserEntityInterface|null $user Optional user ID or entity
* (to limit results to a particular user).
*
* @return UserResourceEntityInterface[]
*/
public function getFavoritesForRecord(
string $recordId,
string $source = DEFAULT_SEARCH_BACKEND,
int|UserListEntityInterface|null $list = null,
int|UserEntityInterface|null $user = null
): array;

/**
* Get statistics on use of UserResource.
*
Expand Down
2 changes: 2 additions & 0 deletions module/VuFind/src/VuFind/RecordDriver/AbstractBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ public function getRatingBreakdown(array $groups)
* @param int $user_id ID of user to load tags from (null for all users)
*
* @return array
*
* @deprecated Use \VuFind\View\Helper\Root\Record::getListNotes()
*/
public function getListNotes($list_id = null, $user_id = null)
{
Expand Down
26 changes: 26 additions & 0 deletions module/VuFind/src/VuFind/View/Helper/Root/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use VuFind\Db\Service\DbServiceAwareInterface;
use VuFind\Db\Service\DbServiceAwareTrait;
use VuFind\Db\Service\UserListServiceInterface;
use VuFind\Db\Service\UserResourceServiceInterface;

use function get_class;
use function in_array;
Expand Down Expand Up @@ -258,6 +259,31 @@ public function getListEntry($list = null, $user = null)
);
}

/**
* Get notes associated with this record in user lists.
*
* @param int $list_id ID of list to load tags from (null for all lists)
* @param int $user_id ID of user to load tags from (null for all users)
*
* @return string[]
*/
public function getListNotes($list_id = null, $user_id = null)
{
$data = $this->getDbService(UserResourceServiceInterface::class)->getFavoritesForRecord(
$this->driver->getUniqueId(),
$this->driver->getSourceIdentifier(),
$list_id,
$user_id
);
$notes = [];
foreach ($data as $current) {
if (!empty($note = $current->getNotes())) {
$notes[] = $note;
}
}
return $notes;
}

/**
* Render previews (data and link) of the item if configured.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
<?php endforeach; ?>
<br>
<?php endif; ?>
<?php $listNotes = $this->driver->getListNotes($list_id, $user_id); ?>
<?php $listNotes = $this->record($this->driver)->getListNotes($list_id, $user_id); ?>
<?php if (count($listNotes) > 0): ?>
<strong><?=$this->transEsc('Notes')?>:</strong>
<?php if (count($listNotes) > 1): ?><br><?php endif; ?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
<?php endforeach; ?>
<br>
<?php endif; ?>
<?php $listNotes = $this->driver->getListNotes($list_id, $user_id); ?>
<?php $listNotes = $this->record($this->driver)->getListNotes($list_id, $user_id); ?>
<?php if (count($listNotes) > 0): ?>
<strong><?=$this->transEsc('Notes')?>:</strong>
<?php if (count($listNotes) > 1): ?><br><?php endif; ?>
Expand Down

0 comments on commit ac1d867

Please sign in to comment.