Skip to content

Commit

Permalink
Fix copying of a public list to favorites.
Browse files Browse the repository at this point in the history
  • Loading branch information
EreMaijala committed Sep 19, 2024
1 parent ad41f57 commit 0daca76
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 8 deletions.
2 changes: 1 addition & 1 deletion module/Finna/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@
'Finna\Service\UserPreferenceService' => 'Finna\Service\UserPreferenceServiceFactory',
'Finna\Statistics\Driver\PluginManager' => 'VuFind\ServiceManager\AbstractPluginManagerFactory',
'Finna\Statistics\EventHandler' => 'Finna\Statistics\EventHandlerFactory',
'Finna\Favorites\FavoritesService' => 'VuFind\Favorites\FavoritesServiceFactory',
'Finna\Favorites\FavoritesService' => 'Finna\Favorites\FavoritesServiceFactory',
'Finna\View\CustomElement\PluginManager' => 'VuFind\ServiceManager\AbstractPluginManagerFactory',
'Finna\Video\Handler\PluginManager' => 'VuFind\ServiceManager\AbstractPluginManagerFactory',
'Finna\Video\Video' => 'Finna\Video\VideoFactory',
Expand Down
3 changes: 3 additions & 0 deletions module/Finna/src/Finna/Controller/ListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@
namespace Finna\Controller;

use Laminas\Stdlib\Parameters;
use VuFind\Db\Entity\UserEntityInterface;
use VuFind\Db\Service\TagServiceInterface;
use VuFind\Db\Service\UserListService;
use VuFind\Db\Service\UserListServiceInterface;
use VuFind\Exception\ListPermission as ListPermissionException;
use VuFind\Exception\RecordMissing as RecordMissingException;

use function assert;
use function is_object;

/**
Expand Down Expand Up @@ -222,6 +224,7 @@ protected function processSave($user, $sourceListId, $targetListId): void

// Perform the save operation:
$favorites = $this->serviceLocator->get(\VuFind\Favorites\FavoritesService::class);
assert($favorites instanceof \Finna\Favorites\FavoritesService);
$favorites->saveMany(['list' => $targetListId], $user, $records);
}

Expand Down
15 changes: 8 additions & 7 deletions module/Finna/src/Finna/Favorites/FavoritesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

namespace Finna\Favorites;

use Finna\Db\Entity\FinnaUserResourceEntityInterface;
use Finna\Db\Service\UserListService;
use VuFind\Db\Entity\UserEntityInterface;
use VuFind\Record\Cache as RecordCache;
Expand Down Expand Up @@ -73,26 +74,26 @@ public function saveMany(array $params, UserEntityInterface $user, array $record

assert($this->userListService instanceof UserListService);

// Add custom order keys for new items if the list has custom order
// Add custom order keys for new items if the list has custom order:
$index = $this->userListService->getNextAvailableCustomOrderIndex($list->getId());

// If target list is not in custom order then reverse
// If target list is not in custom order then reverse the records to get original order:
if (!$this->userListService->isCustomOrderAvailable($list->getId())) {
$params['ids'] = array_reverse($params['ids']);
$records = array_reverse($records);
}

$tags = isset($params['mytags']) ? $this->tagsService->parse($params['mytags']) : [];

foreach ($records as $record) {
// Get or create a resource object as needed:
$resource = $this->resourcePopulator->getOrCreateResourceForDriver($record);

// Create the resource link if it doesn't exist:
$resource = $this->userResourceService->createOrUpdateLink($resource, $user, $list);
$userResource = $this->userResourceService->createOrUpdateLink($resource, $user, $list);
// Update custom order index:
if ($index) {
$resource->finna_custom_order_index = $index;
$resource->save();
assert($userResource instanceof FinnaUserResourceEntityInterface);
$userResource->setFinnaCustomOrderIndex($index);
$this->userResourceService->persistEntity($resource);
++$index;
}

Expand Down
87 changes: 87 additions & 0 deletions module/Finna/src/Finna/Favorites/FavoritesServiceFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

/**
* Favorites service factory
*
* PHP version 8
*
* 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 <[email protected]>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org Main Page
*/

namespace Finna\Favorites;

use Laminas\ServiceManager\Factory\FactoryInterface;
use Psr\Container\ContainerInterface;
use VuFind\Db\Service\ResourceServiceInterface;
use VuFind\Db\Service\ResourceTagsServiceInterface;
use VuFind\Db\Service\UserListServiceInterface;
use VuFind\Db\Service\UserResourceServiceInterface;
use VuFind\Db\Service\UserServiceInterface;
use VuFind\Record\Loader;
use VuFind\Record\ResourcePopulator;
use VuFind\Tags\TagsService;

/**
* Favorites service
*
* @category VuFind
* @package Favorites
* @author Demian Katz <[email protected]>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org Main Page
*
* @codeCoverageIgnore
*/
class FavoritesServiceFactory implements FactoryInterface
{
/**
* Create service
*
* Finna version due to the hardcoded class name
*
* @param ContainerInterface $container Service container
* @param string $name Requested service name (unused)
* @param array $options Extra options (unused)
*
* @return FavoritesService
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function __invoke(ContainerInterface $container, $name, array $options = null)
{
$serviceManager = $container->get(\VuFind\Db\Service\PluginManager::class);
$sessionManager = $container->get(\Laminas\Session\SessionManager::class);
$session = new \Laminas\Session\Container('List', $sessionManager);
return new FavoritesService(
$serviceManager->get(ResourceServiceInterface::class),
$serviceManager->get(ResourceTagsServiceInterface::class),
$serviceManager->get(UserListServiceInterface::class),
$serviceManager->get(UserResourceServiceInterface::class),
$serviceManager->get(UserServiceInterface::class),
$container->get(ResourcePopulator::class),
$container->get(TagsService::class),
$container->get(Loader::class),
$container->get(\VuFind\Record\Cache::class),
$session
);
}
}

0 comments on commit 0daca76

Please sign in to comment.