Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrated UserList Service #26

Merged
merged 56 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
11161e6
Partial migration
skellamp Oct 22, 2023
3b3c648
Code migrated, yet to test
skellamp Nov 1, 2023
8828cb0
moved editAllowed method to entity class
skellamp Nov 3, 2023
77ae9b1
bug fixes
skellamp Nov 4, 2023
c22c4db
tested adding records to lists
skellamp Nov 5, 2023
1a8805c
delete query fix
skellamp Nov 6, 2023
b9ad162
fixed deletion of lists
skellamp Nov 6, 2023
b138ee6
Merge branch 'doctrine' into userlist
demiankatz Nov 6, 2023
acfaadb
constructor parameter fix
skellamp Nov 7, 2023
1f477d7
LIstItems fix
skellamp Nov 7, 2023
c0dada7
LIstItems fix
skellamp Nov 7, 2023
192d00f
added view helper
skellamp Nov 8, 2023
149fb35
Merge branch 'userlist' of https://github.com/skellamp/vufind into us…
skellamp Nov 8, 2023
f34962e
Merge branch 'doctrine' into userlist
demiankatz Nov 8, 2023
3fe9739
fixed bulk delete
skellamp Nov 8, 2023
34ff2af
non empty variable fix
skellamp Nov 8, 2023
bde5603
Merge branch 'doctrine' into userlist
demiankatz Nov 9, 2023
8140d77
Remove unnecessary change.
demiankatz Nov 9, 2023
f44c4a3
Clean up getListsContainingResource.
demiankatz Nov 9, 2023
73df02f
Adjust indentation.
demiankatz Nov 9, 2023
ed40529
Fix typo.
demiankatz Nov 9, 2023
bf8f3d5
Reduce calls to getDescription.
demiankatz Nov 9, 2023
152b030
More code simplification.
demiankatz Nov 9, 2023
0356a02
Merge branch 'doctrine' into userlist
demiankatz Nov 10, 2023
2a88829
Remove refactored method; add types.
demiankatz Nov 10, 2023
6e6acb1
changes based on review
skellamp Nov 12, 2023
40e5b9d
Merge branch 'doctrine' into userlist
demiankatz Nov 13, 2023
22ae229
Remove migrated code.
demiankatz Nov 13, 2023
23d212f
php-cs-fixer.
demiankatz Nov 13, 2023
bf8acd2
Unwrap comment.
demiankatz Nov 13, 2023
1d1558a
Remove migrated method.
demiankatz Nov 13, 2023
b56a2b7
Merge branch 'doctrine' into userlist
demiankatz Nov 13, 2023
c3617f1
Remove more migrated code.
demiankatz Nov 13, 2023
c2bc3d7
Remove more migrated code.
demiankatz Nov 13, 2023
22e48f3
Add type.
demiankatz Nov 13, 2023
f67a1e3
Remove migrated code.
demiankatz Nov 13, 2023
251e455
refactored getPublicLists
skellamp Nov 14, 2023
60b9df9
cosmetic fix
skellamp Nov 14, 2023
6a5a7f8
Merge branch 'doctrine' into userlist
demiankatz Nov 14, 2023
1180e61
renamed getLists to getListsForUser
skellamp Nov 14, 2023
bd5b9f0
Merge branch 'userlist' of https://github.com/skellamp/vufind into us…
skellamp Nov 14, 2023
87f233b
Remove migrated code.
demiankatz Nov 14, 2023
c28e8bf
php-cs-fixer
demiankatz Nov 14, 2023
869de57
fix in the getPublicLists
skellamp Nov 14, 2023
889e0e3
Merge branch 'userlist' of https://github.com/skellamp/vufind into us…
skellamp Nov 14, 2023
37ec6b6
changes based on review 13 Nov 23
skellamp Nov 14, 2023
0c9f52a
shifted the call to getController
skellamp Nov 14, 2023
52ebd85
Merge branch 'doctrine' into userlist
demiankatz Nov 15, 2023
1896c61
Modified getPublicLists
skellamp Nov 16, 2023
2b16bb9
added userlistService test
skellamp Nov 17, 2023
d49fdbc
Fix test.
demiankatz Nov 17, 2023
48295cb
Simplify test.
demiankatz Nov 17, 2023
22fdf24
Remove migrated test.
demiankatz Nov 17, 2023
bf9803a
Delete old UserList classes.
demiankatz Nov 17, 2023
ea1317c
Merge branch 'doctrine' into userlist
demiankatz Nov 17, 2023
cdad5b4
Code simplification.
demiankatz Nov 17, 2023
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
75 changes: 24 additions & 51 deletions module/VuFind/src/VuFind/ChannelProvider/ListItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

use Laminas\Mvc\Controller\Plugin\Url;
use Laminas\Stdlib\Parameters;
use VuFind\Db\Entity\UserList;
use VuFind\RecordDriver\AbstractBase as RecordDriver;
use VuFind\Search\Base\Results;

Expand Down Expand Up @@ -86,11 +87,11 @@ class ListItems extends AbstractChannelProvider
protected $initialListsToDisplay;

/**
* UserList table
* UserList database service
*
* @var \VuFind\Db\Table\UserList
* @var \VuFind\Db\Service\UserListService
*/
protected $userList;
protected $listService;

/**
* Tag database service
Expand All @@ -116,21 +117,21 @@ class ListItems extends AbstractChannelProvider
/**
* Constructor
*
* @param \VuFind\Db\Table\UserList $userList UserList table
* @param \VuFind\Db\Service\UserListService $listService UserList table
* @param \VuFind\Db\Service\TagService $tagService Tag database service
* @param Url $url URL helper
* @param \VuFind\Search\Results\PluginManager $resultsManager Results manager
* @param array $options Settings
* (optional)
*/
public function __construct(
\VuFind\Db\Table\UserList $userList,
\VuFind\Db\Service\UserListService $listService,
\VuFind\Db\Service\TagService $tagService,
Url $url,
\VuFind\Search\Results\PluginManager $resultsManager,
array $options = []
) {
$this->userList = $userList;
$this->listService = $listService;
$this->tagService = $tagService;
$this->url = $url;
$this->resultsManager = $resultsManager;
Expand Down Expand Up @@ -222,35 +223,8 @@ protected function getListsById($ids)
{
$lists = [];
foreach ($ids as $id) {
$list = $this->userList->getExisting($id);
if ($list->public) {
$lists[] = $list;
}
}
return $lists;
}

/**
* Given an array of lists, add public lists if configured to do so.
*
* @param array $lists List to expand.
*
* @return array
*/
protected function addPublicLists($lists)
{
if ($this->displayPublicLists) {
$resultIds = [];
foreach ($lists as $list) {
$resultIds[] = $list->id;
}
$callback = function ($select) use ($resultIds) {
$select->where->equalTo('public', 1);
if (!empty($resultIds)) {
$select->where->notIn('id', $resultIds);
}
};
foreach ($this->userList->select($callback) as $list) {
$list = $this->listService->getExisting($id);
if ($list->isPublic()) {
$lists[] = $list;
}
}
Expand All @@ -271,7 +245,11 @@ protected function getLists()
: $this->getListsById($this->ids);

// Next, we add other public lists if necessary:
return $this->addPublicLists($baseLists);
$publicLists = [];
if ($this->displayPublicLists) {
$publicLists = $this->listService->getPublicLists($baseLists);
}
return array_merge($baseLists, $publicLists);
}

/**
Expand All @@ -282,22 +260,17 @@ protected function getLists()
protected function getListsByTagAndId()
{
// Get public lists by search criteria
$lists = $this->tagService->getListsForTag(
$listIds = $this->tagService->getListsForTag(
$this->tags,
$this->ids,
true,
$this->andTags
);

// Format result set into an array:
$result = [];
if (count($lists)) {
$callback = function ($select) use ($lists) {
$select->where->in('id', $lists);
};

foreach ($this->userList->select($callback) as $list) {
$result[] = $list;
if (count($listIds)) {
foreach ($listIds as $id) {
$result[] = $this->listService->getEntityById(\VuFind\Db\Entity\UserList::class, $id);
skellamp marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -306,8 +279,8 @@ protected function getListsByTagAndId()
$orderIds = (array)$this->ids;
$sortFn = function ($left, $right) use ($orderIds) {
return
array_search($left->id, $orderIds)
<=> array_search($right->id, $orderIds);
array_search($left->getId(), $orderIds)
<=> array_search($right->getId(), $orderIds);
};
usort($result, $sortFn);
}
Expand All @@ -326,21 +299,21 @@ protected function getListsByTagAndId()
protected function getChannelFromList($list, $tokenOnly)
{
$retVal = [
'title' => $list->title,
'title' => $list->getTitle(),
'providerId' => $this->providerId,
'token' => $list->id,
'token' => $list->getId(),
'links' => [],
];
if ($tokenOnly) {
return $retVal;
}
$results = $this->resultsManager->get('Favorites');
$results->getParams()->initFromRequest(new Parameters(['id' => $list->id]));
$results->getParams()->initFromRequest(new Parameters(['id' => $list->getId()]));
$retVal['contents'] = $this->summarizeRecordDrivers($results->getResults());
$retVal['links'][] = [
'label' => 'channel_search',
'icon' => 'fa-list',
'url' => $this->url->fromRoute('userList', ['id' => $list->id]),
'url' => $this->url->fromRoute('userList', ['id' => $list->getId()]),
];
return $retVal;
}
Expand Down
7 changes: 3 additions & 4 deletions module/VuFind/src/VuFind/ChannelProvider/ListItemsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,10 @@ public function __invoke(
if ($options !== null) {
throw new \Exception('Unexpected options sent to factory!');
}
$sm = $container->get(\VuFind\Db\Service\PluginManager::class);
return new $requestedName(
$container->get(\VuFind\Db\Table\PluginManager::class)->get('UserList'),
$container
->get(\VuFind\Db\Service\PluginManager::class)
->get(\VuFind\Db\Service\TagService::class),
$sm->get(\VuFind\Db\Service\UserListService::class),
$sm->get(\VuFind\Db\Service\TagService::class),
$container->get('ControllerPluginManager')->get('url'),
$container->get(\VuFind\Search\Results\PluginManager::class)
);
Expand Down
10 changes: 6 additions & 4 deletions module/VuFind/src/VuFind/Controller/AbstractRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,13 +496,15 @@ public function saveAction()

// Loop through all user lists and sort out containing/non-containing lists
$containingLists = $nonContainingLists = [];
foreach ($user->getLists() as $list) {
$lists = $this->getDbService(\VuFind\Db\Service\UserListService::class)->getLists($user->id);
foreach ($lists as $current) {
// Assign list to appropriate array based on whether or not we found
// it earlier in the list of lists containing the selected record.
if (in_array($list->id, $listIds)) {
$containingLists[] = $list->toArray();
$list = $current[0];
if (in_array($list->getId(), $listIds)) {
$containingLists[] = $list;
} else {
$nonContainingLists[] = $list->toArray();
$nonContainingLists[] = $list;
}
}

Expand Down
5 changes: 3 additions & 2 deletions module/VuFind/src/VuFind/Controller/CartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,12 +485,13 @@ public function saveAction()
$this->flashMessenger()->addMessage($message, 'success');
return $this->redirect()->toUrl($listUrl);
}

$listService = $this->serviceLocator->get(\VuFind\Db\Service\PluginManager::class)
->get(\VuFind\Db\Service\UserListService::class);
// Pass record and list information to view:
return $this->createViewModel(
[
'records' => $this->getRecordLoader()->loadBatch($ids),
'lists' => $user->getLists(),
'lists' => $listService->getLists($user->id),
]
);
}
Expand Down
52 changes: 31 additions & 21 deletions module/VuFind/src/VuFind/Controller/MyResearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -807,8 +807,7 @@ public function deleteAction()
if (empty($listID)) {
$list = false;
} else {
$table = $this->getTable('UserList');
$list = $table->getExisting($listID);
$list = $this->getListService()->getExisting($listID);
}
return $this->createViewModel(
[
Expand Down Expand Up @@ -845,9 +844,9 @@ public function performDeleteFavorite($id, $source)
// Perform delete and send appropriate flash message:
if (null !== $listID) {
// ...Specific List
$table = $this->getTable('UserList');
$list = $table->getExisting($listID);
$list->removeResourcesById($user, [$id], $source);
$listService = $this->getListService();
$list = $listService->getExisting($listID);
$listService->removeResourcesById($user->id, $list, [$id], $source);
demiankatz marked this conversation as resolved.
Show resolved Hide resolved
$this->flashMessenger()->addMessage('Item removed from list', 'success');
} else {
// ...My Favorites
Expand Down Expand Up @@ -958,13 +957,13 @@ public function editAction()
foreach ($userResources as $current) {
$containingLists[] = $current->list_id;
}

// Send non-containing lists to the view for user selection:
$userLists = $user->getLists();
$userLists = $this->getListService()->getLists($user->id);
$lists = [];
foreach ($userLists as $userList) {
if (!in_array($userList->id, $containingLists)) {
$lists[$userList->id] = $userList->title;
$list = $userList[0];
if (!in_array($list->getId(), $containingLists)) {
$lists[$list->getId()] = $list->getTitle();
}
}

Expand Down Expand Up @@ -1065,7 +1064,8 @@ public function mylistAction()

if ($this->listTagsEnabled()) {
if ($list = $results->getListObject()) {
foreach ($list->getListTags() as $tag) {
$tagService = $this->getDbService(\VuFind\Db\Service\TagService::class);
foreach ($tagService->getForList($list, $list->getUser()) as $tag) {
$listTags[$tag['id']] = $tag['tag'];
}
}
Expand All @@ -1087,8 +1087,8 @@ public function mylistAction()
/**
* Process the "edit list" submission.
*
* @param \VuFind\Db\Row\User $user Logged in user
* @param \VuFind\Db\Row\UserList $list List being created/edited
* @param \VuFind\Db\Row\User $user Logged in user
* @param \VuFind\Db\Entity\UserList $list List being created/edited
*
* @return object|bool Response object if redirect is
* needed, false if form needs to be redisplayed.
Expand All @@ -1097,8 +1097,7 @@ protected function processEditList($user, $list)
{
// Process form within a try..catch so we can handle errors appropriately:
try {
$finalId
= $list->updateFromRequest($user, $this->getRequest()->getPost());
$finalId = $this->getListService()->updateFromRequest($user->id, $list, $this->getRequest()->getPost());

// If the user is in the process of saving a record, send them back
// to the save screen; otherwise, send them back to the list they
Expand Down Expand Up @@ -1164,12 +1163,12 @@ public function editlistAction()
// Is this a new list or an existing list? Handle the special 'NEW' value
// of the ID parameter:
$id = $this->params()->fromRoute('id', $this->params()->fromQuery('id'));
$table = $this->getTable('UserList');
$listService = $this->getListService();
$newList = ($id == 'NEW');
$list = $newList ? $table->getNew($user) : $table->getExisting($id);
$list = $newList ? $listService->getNew($user->id) : $listService->getExisting($id);

// Make sure the user isn't fishing for other people's lists:
if (!$newList && !$list->editAllowed($user)) {
if (!$newList && !$list->editAllowed($user->id)) {
throw new ListPermissionException('Access denied.');
}

Expand All @@ -1182,7 +1181,8 @@ public function editlistAction()

$listTags = null;
if ($this->listTagsEnabled() && !$newList) {
$listTags = $user->formatTagString($list->getListTags());
$tags = $this->getDbService(\VuFind\Db\Service\TagService::class)->getForList($list, $user->id);
$listTags = $user->formatTagString($tags);
}
// Send the list to the view:
return $this->createViewModel(
Expand Down Expand Up @@ -1245,9 +1245,9 @@ public function deletelistAction()
);
if ($confirm) {
try {
$table = $this->getTable('UserList');
$list = $table->getExisting($listID);
$list->delete($this->getUser());
$listService = $this->getListService();
$list = $listService->getExisting($listID);
$listService->delete($list, $this->getUser()->id);

// Success Message
$this->flashMessenger()->addMessage('fav_list_delete', 'success');
Expand Down Expand Up @@ -2267,4 +2267,14 @@ protected function addPendingEmailChangeMessage($user)
);
}
}

/**
* Get userlist database service
*
* @return \VuFind\Db\Service\AbstractService
*/
protected function getListService()
{
return $this->getDbService(\VuFind\Db\Service\UserListService::class);
}
}
20 changes: 12 additions & 8 deletions module/VuFind/src/VuFind/Controller/Plugin/Favorites.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* PHP version 8
*
* Copyright (C) Villanova University 2010.
* Copyright (C) Villanova University 2010-2023.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
Expand Down Expand Up @@ -83,8 +83,12 @@ class Favorites extends \Laminas\Mvc\Controller\Plugin\AbstractPlugin
* @param Tags $tags Tag parser
* @param FavoritesService $favorites Favorites service
*/
public function __construct(Loader $loader, Cache $cache, Tags $tags, FavoritesService $favorites)
{
public function __construct(
Loader $loader,
Cache $cache,
Tags $tags,
FavoritesService $favorites,
) {
$this->loader = $loader;
$this->cache = $cache;
$this->tags = $tags;
Expand Down Expand Up @@ -152,7 +156,7 @@ public function saveBulk($params, $user)
// Add the information to the user's account:
$tags = isset($params['mytags'])
? $this->tags->parse($params['mytags']) : [];
$user->saveResource($resource, $list, $tags, '', false);
$user->saveResource($resource, $list->getId(), $tags, '', false);

// Collect record IDs for caching
if ($this->cache->isCachable($resource->source)) {
Expand All @@ -161,7 +165,7 @@ public function saveBulk($params, $user)
}

$this->cacheBatch($cacheRecordIds);
return ['listId' => $list->id];
return ['listId' => $list->getId()];
}

/**
Expand Down Expand Up @@ -193,10 +197,10 @@ public function delete($ids, $listID, $user)
$user->removeResourcesById($ids, $source);
}
} else {
$table = $this->getController()->getTable('UserList');
$list = $table->getExisting($listID);
$listService = $this->favoritesService->getDbService(\VuFind\Db\Service\UserListService::class);
demiankatz marked this conversation as resolved.
Show resolved Hide resolved
$list = $listService->getExisting($listID);
foreach ($sorted as $source => $ids) {
$list->removeResourcesById($user, $ids, $source);
$listService->removeResourcesById($user->id, $list, $ids, $source);
}
}
}
Expand Down
Loading