Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions module/VuFind/src/VuFind/Db/Row/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use VuFind\Db\Service\TagServiceInterface;
use VuFind\Db\Service\UserCardServiceInterface;
use VuFind\Db\Service\UserListServiceInterface;
use VuFind\Db\Service\UserResourceServiceInterface;
use VuFind\Db\Service\UserServiceInterface;
use VuFind\Favorites\FavoritesService;

Expand Down Expand Up @@ -440,10 +441,8 @@ public function saveResource(
$notes,
$replaceExisting = true
) {
// Create the resource link if it doesn't exist and update the notes in any
// case:
$linkTable = $this->getDbTable('UserResource');
$linkTable->createOrUpdateLink($resource->id, $this->id, $list->id, $notes);
// Create the resource link if it doesn't exist and update the notes in any case:
$this->getDbService(UserResourceServiceInterface::class)->createOrUpdateLink($resource, $this, $list, $notes);

// If we're replacing existing tags, delete the old ones before adding the
// new ones:
Expand Down
62 changes: 62 additions & 0 deletions module/VuFind/src/VuFind/Db/Service/UserResourceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

namespace VuFind\Db\Service;

use Exception;
use VuFind\Db\Entity\ResourceEntityInterface;
use VuFind\Db\Entity\UserEntityInterface;
use VuFind\Db\Entity\UserListEntityInterface;
use VuFind\Db\Entity\UserResourceEntityInterface;
Expand All @@ -48,8 +50,10 @@
*/
class UserResourceService extends AbstractDbService implements
DbTableAwareInterface,
DbServiceAwareInterface,
UserResourceServiceInterface
{
use DbServiceAwareTrait;
use DbTableAwareTrait;

/**
Expand Down Expand Up @@ -86,4 +90,62 @@ public function getStatistics(): array
{
return $this->getDbTable('UserResource')->getStatistics();
}

/**
* Create user/resource/list link if one does not exist; update notes if one does.
*
* @param ResourceEntityInterface|int $resourceOrId Entity or ID of resource to link up
* @param UserEntityInterface|int $userOrId Entity or ID of user creating link
* @param UserListEntityInterface|int $listOrId Entity or ID of list to link up
* @param string $notes Notes to associate with link
*
* @return UserResource|false
*/
public function createOrUpdateLink(
ResourceEntityInterface|int $resourceOrId,
UserEntityInterface|int $userOrId,
UserListEntityInterface|int $listOrId,
string $notes = ''
): UserResourceEntityInterface {
$resource = $resourceOrId instanceof ResourceEntityInterface
? $resourceOrId : $this->getDbService(ResourceServiceInterface::class)->getResourceById($resourceOrId);
if (!$resource) {
throw new Exception("Cannot retrieve resource $resourceOrId");
}
$list = $listOrId instanceof UserListEntityInterface
? $listOrId : $this->getDbService(UserListServiceInterface::class)->getUserListById($listOrId);
if (!$list) {
throw new Exception("Cannot retrieve list $listOrId");
}
$user = $userOrId instanceof UserEntityInterface
? $userOrId : $this->getDbService(UserServiceInterface::class)->getUserById($userOrId);
if (!$user) {
throw new Exception("Cannot retrieve user $userOrId");
}
$params = [
'resource_id' => $resource->getId(),
'list_id' => $list->getId(),
'user_id' => $user->getId(),
];
if (!($result = $this->getDbTable('UserResource')->select($params)->current())) {
$result = $this->createEntity()
->setResource($resource)
->setUser($user)
->setUserList($list);
}
// Update the notes:
$result->setNotes($notes);
$this->persistEntity($result);
return $result;
}

/**
* Create a UserResource entity object.
*
* @return UserResourceEntityInterface
*/
public function createEntity(): UserResourceEntityInterface
{
return $this->getDbTable('UserResource')->createRow();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

namespace VuFind\Db\Service;

use VuFind\Db\Entity\ResourceEntityInterface;
use VuFind\Db\Entity\UserEntityInterface;
use VuFind\Db\Entity\UserListEntityInterface;
use VuFind\Db\Entity\UserResourceEntityInterface;
Expand Down Expand Up @@ -69,4 +70,28 @@ public function getFavoritesForRecord(
* @return array
*/
public function getStatistics(): array;

/**
* Create user/resource/list link if one does not exist; update notes if one does.
*
* @param ResourceEntityInterface|int $resourceOrId Entity or ID of resource to link up
* @param UserEntityInterface|int $userOrId Entity or ID of user creating link
* @param UserListEntityInterface|int $listOrId Entity or ID of list to link up
* @param string $notes Notes to associate with link
*
* @return UserResource|false
*/
public function createOrUpdateLink(
ResourceEntityInterface|int $resourceOrId,
UserEntityInterface|int $userOrId,
UserListEntityInterface|int $listOrId,
string $notes = ''
): UserResourceEntityInterface;

/**
* Create a UserResource entity object.
*
* @return UserResourceEntityInterface
*/
public function createEntity(): UserResourceEntityInterface;
}
29 changes: 10 additions & 19 deletions module/VuFind/src/VuFind/Db/Table/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
use Laminas\Db\Sql\Expression;
use Laminas\Db\Sql\Select;
use VuFind\Db\Row\RowGateway;
use VuFind\Db\Service\DbServiceAwareInterface;
use VuFind\Db\Service\DbServiceAwareTrait;
use VuFind\Db\Service\UserResourceServiceInterface;

use function is_array;

Expand All @@ -45,8 +48,10 @@
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org Main Page
*/
class UserResource extends Gateway
class UserResource extends Gateway implements DbServiceAwareInterface
{
use DbServiceAwareTrait;

/**
* Constructor
*
Expand Down Expand Up @@ -126,31 +131,17 @@ public function getSavedData(
* @param string $notes Notes to associate with link
*
* @return \VuFind\Db\Row\UserResource
*
* @deprecated Use UserResourceServiceInterface::createOrUpdateLink()
*/
public function createOrUpdateLink(
$resource_id,
$user_id,
$list_id,
$notes = ''
) {
$params = [
'resource_id' => $resource_id, 'list_id' => $list_id,
'user_id' => $user_id,
];
$result = $this->select($params)->current();

// Only create row if it does not already exist:
if (empty($result)) {
$result = $this->createRow();
$result->resource_id = $resource_id;
$result->list_id = $list_id;
$result->user_id = $user_id;
}

// Update the notes:
$result->notes = $notes;
$result->save();
return $result;
return $this->getDbService(UserResourceServiceInterface::class)
->createOrUpdateLink($resource_id, $user_id, $list_id, $notes);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions module/VuFind/src/VuFind/Favorites/FavoritesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use VuFind\Db\Service\ResourceTagsServiceInterface;
use VuFind\Db\Service\TagServiceInterface;
use VuFind\Db\Service\UserListServiceInterface;
use VuFind\Db\Service\UserResourceServiceInterface;
use VuFind\Db\Service\UserServiceInterface;
use VuFind\Db\Table\DbTableAwareInterface;
use VuFind\Db\Table\DbTableAwareTrait;
Expand Down Expand Up @@ -76,6 +77,7 @@ class FavoritesService implements \VuFind\I18n\Translator\TranslatorAwareInterfa
* @param ResourceTagsServiceInterface $resourceTagsService Resource tags database service
* @param TagServiceInterface $tagService Tag database service
* @param UserListServiceInterface $userListService UserList database service
* @param UserResourceServiceInterface $userResourceService UserResource database service
* @param UserServiceInterface $userService User database service
* @param ResourcePopulator $resourcePopulator Resource populator service
* @param Tags $tagHelper Tag helper service
Expand All @@ -88,6 +90,7 @@ public function __construct(
protected ResourceTagsServiceInterface $resourceTagsService,
protected TagServiceInterface $tagService,
protected UserListServiceInterface $userListService,
protected UserResourceServiceInterface $userResourceService,
protected UserServiceInterface $userService,
protected ResourcePopulator $resourcePopulator,
protected Tags $tagHelper,
Expand Down Expand Up @@ -345,8 +348,7 @@ public function saveResourceToFavorites(

// Create the resource link if it doesn't exist and update the notes in any
// case:
$linkTable = $this->getDbTable('UserResource');
$linkTable->createOrUpdateLink($resource->getId(), $user->getId(), $list->getId(), $notes);
$this->userResourceService->createOrUpdateLink($resource, $user, $list, $notes);

// If we're replacing existing tags, delete the old ones before adding the
// new ones:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use VuFind\Db\Service\ResourceTagsServiceInterface;
use VuFind\Db\Service\TagServiceInterface;
use VuFind\Db\Service\UserListServiceInterface;
use VuFind\Db\Service\UserResourceServiceInterface;
use VuFind\Db\Service\UserServiceInterface;
use VuFind\Record\Loader;
use VuFind\Record\ResourcePopulator;
Expand Down Expand Up @@ -74,6 +75,7 @@ public function __invoke(ContainerInterface $container, $name, array $options =
$serviceManager->get(ResourceTagsServiceInterface::class),
$serviceManager->get(TagServiceInterface::class),
$serviceManager->get(UserListServiceInterface::class),
$serviceManager->get(UserResourceServiceInterface::class),
$serviceManager->get(UserServiceInterface::class),
$container->get(ResourcePopulator::class),
$container->get(Tags::class),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use VuFind\Db\Service\ResourceTagsServiceInterface;
use VuFind\Db\Service\TagServiceInterface;
use VuFind\Db\Service\UserListServiceInterface;
use VuFind\Db\Service\UserResourceServiceInterface;
use VuFind\Db\Service\UserServiceInterface;
use VuFind\Favorites\FavoritesService;
use VuFind\Record\Loader;
Expand Down Expand Up @@ -67,6 +68,7 @@ protected function getFavoritesService(?UserListServiceInterface $listService =
$this->createMock(ResourceTagsServiceInterface::class),
$this->createMock(TagServiceInterface::class),
$listService ?? $this->createMock(UserListServiceInterface::class),
$this->createMock(UserResourceServiceInterface::class),
$this->createMock(UserServiceInterface::class),
$this->createMock(ResourcePopulator::class),
$this->createMock(Tags::class),
Expand Down