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

[VUFIND-1674] Show count of saved items in account menu #3836

Draft
wants to merge 6 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions config/vufind/AccountMenu.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ MenuItems:
label: saved_items
route: myresearch-favorites
icon: user-favorites
status: true
checkMethod: checkFavorites

- name: checkedout
Expand Down
78 changes: 78 additions & 0 deletions module/VuFind/src/VuFind/AjaxHandler/GetUserFavoritesStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/**
* "Get User Favorites Status" AJAX handler
*
* PHP version 8
*
* Copyright (C) Villanova University 2024.
*
* 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 AJAX
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development Wiki
*/

namespace VuFind\AjaxHandler;

use Laminas\Mvc\Controller\Plugin\Params;
use VuFind\Db\Entity\UserEntityInterface;
use VuFind\Db\Service\ResourceServiceInterface;
use VuFind\Session\Settings as SessionSettings;

use function count;

/**
* "Get User Favorites Status" AJAX handler
*
* @category VuFind
* @package AJAX
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development Wiki
*/
class GetUserFavoritesStatus extends AbstractBase
{
/**
* Constructor
*
* @param SessionSettings $ss Session settings
* @param ?UserEntityInterface $user Logged in user (or null)
* @param ResourceServiceInterface $resourceService Resoruce database service
*/
public function __construct(
SessionSettings $ss,
protected ?UserEntityInterface $user,
protected ResourceServiceInterface $resourceService
) {
$this->sessionSettings = $ss;
}

/**
* Handle a request.
*
* @param Params $params Parameter helper from controller
*
* @return array [response data, internal status code, HTTP status code]
*/
public function handleRequest(Params $params)
{
$this->disableSessionWrites(); // avoid session write timing bug
$count = $this->user ? count($this->resourceService->getFavorites($this->user)) : 0;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth creating a new resourceService method that just returns a count to reduce the amount of data pushed around, or is it better to keep this simple and trust the caching?

return $this->formatResponse(compact('count'));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/**
* Factory for GetUserFavoritesStatus AJAX handler.
*
* PHP version 8
*
* Copyright (C) Villanova University 2024.
*
* 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 AJAX
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development Wiki
*/

namespace VuFind\AjaxHandler;

use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
use Psr\Container\ContainerExceptionInterface as ContainerException;
use Psr\Container\ContainerInterface;
use VuFind\Db\Service\ResourceServiceInterface;

/**
* Factory for GetUserFavoritesStatus AJAX handler.
*
* @category VuFind
* @package AJAX
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development Wiki
*/
class GetUserFavoritesStatusFactory implements \Laminas\ServiceManager\Factory\FactoryInterface
{
/**
* Create an object
*
* @param ContainerInterface $container Service manager
* @param string $requestedName Service being created
* @param null|array $options Extra options (optional)
*
* @return object
*
* @throws ServiceNotFoundException if unable to resolve the service.
* @throws ServiceNotCreatedException if an exception is raised when
* creating a service.
* @throws ContainerException&\Throwable if any other error occurs
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function __invoke(
ContainerInterface $container,
$requestedName,
array $options = null
) {
return new $requestedName(
$container->get(\VuFind\Session\Settings::class),
$container->get(\VuFind\Auth\Manager::class)->getUserObject(),
$container->get(\VuFind\Db\Service\PluginManager::class)->get(ResourceServiceInterface::class),
...($options ?: [])
);
}
}
2 changes: 2 additions & 0 deletions module/VuFind/src/VuFind/AjaxHandler/PluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager
'getSaveStatuses' => GetSaveStatuses::class,
'getSearchResults' => GetSearchResults::class,
'getSideFacets' => GetSideFacets::class,
'getUserFavoritesStatus' => GetUserFavoritesStatus::class,
'getUserFines' => GetUserFines::class,
'getUserHolds' => GetUserHolds::class,
'getUserILLRequests' => GetUserILLRequests::class,
Expand Down Expand Up @@ -108,6 +109,7 @@ class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager
GetSaveStatuses::class => GetSaveStatusesFactory::class,
GetSearchResults::class => GetSearchResultsFactory::class,
GetSideFacets::class => GetSideFacetsFactory::class,
GetUserFavoritesStatus::class => GetUserFavoritesStatusFactory::class,
GetUserFines::class => GetUserFinesFactory::class,
GetUserHolds::class => AbstractIlsAndUserActionFactory::class,
GetUserILLRequests::class => AbstractIlsAndUserActionFactory::class,
Expand Down
14 changes: 14 additions & 0 deletions themes/bootstrap3/js/account_ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,20 @@ $(function registerAccountAjax() {
}
});

VuFind.account.register("favorites", {
selector: ".favorites-status",
ajaxMethod: "getUserFavoritesStatus",
render: function render($element, status, ICON_LEVELS) {
var html = '<span class="badge">' + parseInt(status.count || 0) + '</span>';
var level = ICON_LEVELS.NONE;
$element.html(html);
return level;
},
updateNeeded: function updateNeeded(currentStatus, status) {
return currentStatus.count !== status.count;
}
});

VuFind.account.register("holds", {
selector: ".holds-status",
ajaxMethod: "getUserHolds",
Expand Down
4 changes: 4 additions & 0 deletions themes/bootstrap3/templates/myresearch/mylist.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,7 @@
<?=$this->recommend($current)?>
<?php endforeach; ?>
</div>

<?php if (!isset($list)): ?>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, the cache only gets invalidated when you go to the favorites page. Maybe it would be better to always invalidate the cache on all list pages, so that list actions cause the favorites total to get recalculated. However, this would make a lot of unnecessary requests or would require the addition of some additional logic to figure out when invalidation is really needed.

<?=$this->render('myresearch/notify-account-status.phtml', ['method' => 'favorites', 'accountStatus' => ['count' => $recordTotal]]); ?>
<?php endif; ?>
Loading