Skip to content

Commit

Permalink
Use templates to render account status notifications server-side.
Browse files Browse the repository at this point in the history
Requires less JS code and makes it easier to customize the notification badges.
  • Loading branch information
EreMaijala committed Dec 19, 2024
1 parent 97657b1 commit aa71bd5
Show file tree
Hide file tree
Showing 17 changed files with 406 additions and 129 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/**
* Abstract base class for handlers depending on the ILS, a logged-in user and view renderer.
*
* PHP version 8
*
* Copyright (C) Villanova University 2018.
*
* 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 <[email protected]>
* @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\View\Renderer\PhpRenderer;
use VuFind\Auth\ILSAuthenticator;
use VuFind\Db\Entity\UserEntityInterface;
use VuFind\I18n\Translator\TranslatorAwareInterface;
use VuFind\ILS\Connection;
use VuFind\Session\Settings as SessionSettings;

/**
* Abstract base class for handlers depending on the ILS and a logged-in user.
*
* @category VuFind
* @package AJAX
* @author Demian Katz <[email protected]>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development Wiki
*/
abstract class AbstractIlsUserAndRendererAction extends AbstractBase implements TranslatorAwareInterface
{
use \VuFind\I18n\Translator\TranslatorAwareTrait;

/**
* Constructor
*
* @param SessionSettings $ss Session settings
* @param Connection $ils ILS connection
* @param ILSAuthenticator $ilsAuthenticator ILS authenticator
* @param ?UserEntityInterface $user Logged in user (or null)
* @param PhpRenderer $renderer View renderer
*/
public function __construct(
SessionSettings $ss,
protected Connection $ils,
protected ILSAuthenticator $ilsAuthenticator,
protected ?UserEntityInterface $user,
protected PhpRenderer $renderer
) {
$this->sessionSettings = $ss;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/**
* Factory for AbstractIlsUserAndRendererAction AJAX handlers.
*
* PHP version 8
*
* Copyright (C) Villanova University 2018.
*
* 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 <[email protected]>
* @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;

/**
* Factory for AbstractIlsAndUserAction AJAX handlers.
*
* @category VuFind
* @package AJAX
* @author Demian Katz <[email protected]>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development Wiki
*/
class AbstractIlsUserAndRendererActionFactory 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\ILS\Connection::class),
$container->get(\VuFind\Auth\ILSAuthenticator::class),
$container->get(\VuFind\Auth\Manager::class)->getUserObject(),
$container->get('ViewRenderer'),
...($options ?: [])
);
}
}
24 changes: 22 additions & 2 deletions module/VuFind/src/VuFind/AjaxHandler/AbstractUserRequestAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
namespace VuFind\AjaxHandler;

use Laminas\Mvc\Controller\Plugin\Params;
use VuFind\ILS\Logic\AccountStatusLevelType;

/**
* Abstract base class for fetching information about user requests.
Expand All @@ -40,7 +41,7 @@
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development Wiki
*/
abstract class AbstractUserRequestAction extends AbstractIlsAndUserAction
abstract class AbstractUserRequestAction extends AbstractIlsUserAndRendererAction
{
use \VuFind\ILS\Logic\SummaryTrait;

Expand Down Expand Up @@ -69,6 +70,25 @@ public function handleRequest(Params $params)
return $this->formatResponse('', self::STATUS_HTTP_ERROR);
}
$requests = $this->ils->{$this->lookupMethod}($patron);
return $this->formatResponse($this->getRequestSummary($requests));
$result = $this->getRequestSummary($requests);
$result['level'] = $this->getAccountStatusLevel($result);
$result['html'] = $this->renderer->render('ajax/account/requests.phtml', $result);
return $this->formatResponse($result);
}

/**
* Get account status level for notification icon
*
* @param array $status Status information
*
* @return AccountStatusLevelType
*/
protected function getAccountStatusLevel(array $status): AccountStatusLevelType
{
if ($status['available']) {
// This is equivalent to the GOOD level in account_ajax.js, though e.g. ActionRequired could also make sense
return AccountStatusLevelType::Good;
}
return AccountStatusLevelType::Normal;
}
}
37 changes: 23 additions & 14 deletions module/VuFind/src/VuFind/AjaxHandler/GetUserFines.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
namespace VuFind\AjaxHandler;

use Laminas\Mvc\Controller\Plugin\Params;
use Laminas\View\Renderer\PhpRenderer;
use VuFind\Auth\ILSAuthenticator;
use VuFind\Db\Entity\UserEntityInterface;
use VuFind\ILS\Connection;
use VuFind\ILS\Logic\AccountStatusLevelType;
use VuFind\Service\CurrencyFormatter;
use VuFind\Session\Settings as SessionSettings;

Expand All @@ -45,35 +47,29 @@
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development Wiki
*/
class GetUserFines extends AbstractIlsAndUserAction
class GetUserFines extends AbstractIlsUserAndRendererAction
{
use \VuFind\ILS\Logic\SummaryTrait;

/**
* Currency formatter
*
* @var CurrencyFormatter
*/
protected $currencyFormatter;

/**
* Constructor
*
* @param SessionSettings $ss Session settings
* @param Connection $ils ILS connection
* @param ILSAuthenticator $ilsAuthenticator ILS authenticator
* @param ?UserEntityInterface $user Logged in user (or false)
* @param PhpRenderer $renderer Renderer
* @param CurrencyFormatter $currencyFormatter Currency formatter
*/
public function __construct(
SessionSettings $ss,
Connection $ils,
ILSAuthenticator $ilsAuthenticator,
?UserEntityInterface $user,
CurrencyFormatter $currencyFormatter
PhpRenderer $renderer,
protected CurrencyFormatter $currencyFormatter,
) {
parent::__construct($ss, $ils, $ilsAuthenticator, $user);
$this->currencyFormatter = $currencyFormatter;
parent::__construct($ss, $ils, $ilsAuthenticator, $user, $renderer);
}

/**
Expand All @@ -94,8 +90,21 @@ public function handleRequest(Params $params)
return $this->formatResponse('', self::STATUS_HTTP_ERROR);
}
$fines = $this->ils->getMyFines($patron);
return $this->formatResponse(
$this->getFineSummary($fines, $this->currencyFormatter)
);
$result = $this->getFineSummary($fines, $this->currencyFormatter);
$result['level'] = $this->getAccountStatusLevel($result);
$result['html'] = $this->renderer->render('ajax/account/fines.phtml', $result);
return $this->formatResponse($result);
}

/**
* Get account status level for notification icon
*
* @param array $status Status information
*
* @return AccountStatusLevelType
*/
protected function getAccountStatusLevel(array $status): AccountStatusLevelType
{
return $status['total'] ? AccountStatusLevelType::ActionRequired : AccountStatusLevelType::Normal;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development Wiki
*/
class GetUserFinesFactory extends AbstractIlsAndUserActionFactory implements FactoryInterface
class GetUserFinesFactory extends AbstractIlsUserAndRendererActionFactory implements FactoryInterface
{
/**
* Create an object
Expand Down
36 changes: 28 additions & 8 deletions module/VuFind/src/VuFind/AjaxHandler/GetUserTransactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
namespace VuFind\AjaxHandler;

use Laminas\Mvc\Controller\Plugin\Params;
use VuFind\ILS\Logic\AccountStatusLevelType;

/**
* "Get User Transactions" AJAX handler
Expand All @@ -40,7 +41,7 @@
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development Wiki
*/
class GetUserTransactions extends AbstractIlsAndUserAction
class GetUserTransactions extends AbstractIlsUserAndRendererAction
{
use \VuFind\ILS\Logic\SummaryTrait;

Expand Down Expand Up @@ -69,27 +70,46 @@ public function handleRequest(Params $params)
return $this->formatResponse('', self::STATUS_HTTP_ERROR);
}

$counts = [];
$result = [];
$functionConfig = $this->ils->checkFunction('getMyTransactions', $patron);
$page = 1;
do {
// Try to use large page size, but take ILS limits into account
$pageOptions = $this->getPaginationHelper()
->getOptions($page, null, 1000, $functionConfig);
$result = $this->ils
->getMyTransactions($patron, $pageOptions['ilsParams']);
$transactions = $this->ils->getMyTransactions($patron, $pageOptions['ilsParams']);

$summary = $this->getTransactionSummary($result['records']);
$summary = $this->getTransactionSummary($transactions['records']);
foreach ($summary as $key => $value) {
$counts[$key] = ($counts[$key] ?? 0) + $value;
$result[$key] = ($result[$key] ?? 0) + $value;
}
$pageEnd = $pageOptions['ilsPaging']
? ceil($result['count'] / $pageOptions['limit'])
? ceil($transactions['count'] / $pageOptions['limit'])
: 1;
$page++;
} while ($page <= $pageEnd);

return $this->formatResponse($counts);
$result['level'] = $this->getAccountStatusLevel($result);
$result['html'] = $this->renderer->render('ajax/account/checkouts.phtml', $result);
return $this->formatResponse($result);
}

/**
* Get account status level for notification icon
*
* @param array $status Status information
*
* @return AccountStatusLevelType
*/
protected function getAccountStatusLevel(array $status): AccountStatusLevelType
{
if ($status['overdue']) {
return AccountStatusLevelType::ActionRequired;
}
if ($status['warn']) {
return AccountStatusLevelType::Attention;
}
return AccountStatusLevelType::Normal;
}

/**
Expand Down
9 changes: 4 additions & 5 deletions module/VuFind/src/VuFind/AjaxHandler/PluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,10 @@ class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager
GetSearchResults::class => GetSearchResultsFactory::class,
GetSideFacets::class => GetSideFacetsFactory::class,
GetUserFines::class => GetUserFinesFactory::class,
GetUserHolds::class => AbstractIlsAndUserActionFactory::class,
GetUserILLRequests::class => AbstractIlsAndUserActionFactory::class,
GetUserStorageRetrievalRequests::class =>
AbstractIlsAndUserActionFactory::class,
GetUserTransactions::class => AbstractIlsAndUserActionFactory::class,
GetUserHolds::class => AbstractIlsUserAndRendererActionFactory::class,
GetUserILLRequests::class => AbstractIlsUserAndRendererActionFactory::class,
GetUserStorageRetrievalRequests::class => AbstractIlsUserAndRendererActionFactory::class,
GetUserTransactions::class => AbstractIlsUserAndRendererActionFactory::class,
GetVisData::class => GetVisDataFactory::class,
KeepAlive::class => KeepAliveFactory::class,
Recommend::class => RecommendFactory::class,
Expand Down
Loading

0 comments on commit aa71bd5

Please sign in to comment.