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

Render account AJAX information with server-side templates #4175

Merged
merged 10 commits into from
Jan 9, 2025
10 changes: 10 additions & 0 deletions config/vufind/Demo.ini
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ services[] = 'custom'
; driver.
;historicTransactions = '[{"id":"1234", ... "dueDate": "01/01/2017"}]';

; This setting can be used to create fake fines for specific records.
; The value is a JSON document representing the fine information returned by the
; driver.
;fines = '[{"amount": 123, "checkout": "2024-12-01", "createdate": "2024-12-19", "description": "Overdue fee", "id":"1234", "title": "Record"}]';

; This setting can be used to create fake holds for specific records.
; The value is a JSON document representing the hold information returned by the
; driver.
;holds = '[{"reqnum": 1, "location": "Main Library", "create": "2024-12-01", "expire": "2025-12-01", "available": false, "id":"1234", "title": "Record"}]';

; This setting can be used to flag specific records as recently returned; if
; commented out, a random set of IDs will be selected.
;recently_returned[] = myBibId001
Expand Down
47 changes: 47 additions & 0 deletions module/VuFind/src/VuFind/Account/AccountStatusLevelType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/**
* Account status level enum
*
* PHP version 8
*
* Copyright (C) The National Library of Finland 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 Account
* @author Ere Maijala <[email protected]>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development Wiki
*/

namespace VuFind\Account;

/**
* Account status level enum
*
* @category VuFind
* @package Account
* @author Ere Maijala <[email protected]>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development Wiki
*/
enum AccountStatusLevelType: int
{
case Normal = 0;
case Good = 1;
case Attention = 2;
case ActionRequired = 3;
}
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\Account\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,6 +30,8 @@
namespace VuFind\AjaxHandler;

use Laminas\Mvc\Controller\Plugin\Params;
use Laminas\View\Renderer\PhpRenderer;
use VuFind\Account\AccountStatusLevelType;
use VuFind\Auth\ILSAuthenticator;
use VuFind\Db\Entity\UserEntityInterface;
use VuFind\ILS\Connection;
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
Loading
Loading