Skip to content

Commit

Permalink
Moved logic for ordering a single item to ReservationList controller,…
Browse files Browse the repository at this point in the history
… marked old form method as deprecated
  • Loading branch information
LuomaJuha committed Jan 20, 2025
1 parent 829da11 commit 619890f
Show file tree
Hide file tree
Showing 20 changed files with 388 additions and 98 deletions.
5 changes: 5 additions & 0 deletions local/languages/finna/ReservationList/fi.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
; Reservation List
Add To Reservation List = "Lisää varauslistalle"
Reserve Resource = "Varaa aineisto"
All Lists = "Kaikki varauslistat"
Choose a List = "Valitse"
Confirm = "Kyllä"
Expand All @@ -9,6 +10,7 @@ Delete Selected Resources From The List = "Poista valitut aineistot listalta"
List Deleted = "Lista poistettu"
Delivery Time Request = "Toimitusaikatoive"
Description = "Kuvaus"
Title = "Otsikko"
form_email_subject = "Varauspyyntö"
form_response = "Varaus lähetetty"
Reservation Resources = "Varauksen aineistot"
Expand All @@ -28,6 +30,9 @@ Reservation List Name = "Varauslistan nimi"
Send Reservation Request = "Lähetä varauspyyntö"
select_desired_contact_method = "Valitse haluamasi yhteydenottotapa"
Select = "Valitse"
Reserve only this item = "Varaa vain tämä aineisto"
Resource Institution = "Aineiston tarjoaja"
Resource = "Aineisto"

; List statuses:
status_unknown = ""
Expand Down
10 changes: 6 additions & 4 deletions module/Finna/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -1094,11 +1094,13 @@
],
'MyResearch' => ['sortList' => 'SortList/[:id]'],
'ReservationList' => [
'reservationlist-placeorderoptions' => 'PlaceOrderOptions/[:id]',
'reservationlist-displaylists' => 'DisplayLists',
'reservationlist-displaylist' => 'DisplayList/[:id]',
'reservationlist-placeorder' => 'PlaceOrder/[:id]',
'reservationlist-deletelist' => 'DeleteList/[:id]',
'reservationlist-deletebulk' => 'DeleteBulk/[:id]',
'reservationlist-displaylist' => 'DisplayList/[:rl_list_id]',
'reservationlist-placeorder' => 'PlaceOrder/[:rl_list_id]',
'reservationlist-placesingleorder' => 'PlaceSingleOrder/[:rl_list_id]',
'reservationlist-deletelist' => 'DeleteList/[:rl_list_id]',
'reservationlist-deletebulk' => 'DeleteBulk/[:rl_list_id]',
'reservationlist-additemtolist' => 'AddItemToList',
'reservationlist-createlist' => 'CreateList',
],
Expand Down
1 change: 1 addition & 0 deletions module/Finna/src/Finna/Controller/RecordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public function repositoryLibraryRequestAction()
*
* @return \Laminas\View\Model\ViewModel
* @throws \Exception
* @deprecated Use ReservationList::placeSingleOrderAction
*/
public function archiveRequestAction()
{
Expand Down
158 changes: 124 additions & 34 deletions module/Finna/src/Finna/Controller/ReservationListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
namespace Finna\Controller;

use Exception;
use Finna\ReservationList\Form\Form;
use Finna\ReservationList\Handler\AbstractBase as ConnectionAbstractBase;
use Finna\ReservationList\Handler\PluginManager;
use Finna\ReservationList\ReservationListService;
use Finna\View\Helper\Root\ReservationList;
use Laminas\ServiceManager\ServiceLocatorInterface;
Expand Down Expand Up @@ -71,17 +73,26 @@ class ReservationListController extends AbstractBase
*/
protected const RESERVATION_LISTS_DISABLED = 'Reservation lists disabled';

/**
* Single item order identifier
*
* @var string
*/
protected const SINGLE_ORDER_ID = 'single_item_order';

/**
* Constructor
*
* @param ServiceLocatorInterface $sm Service locator
* @param ReservationListService $reservationListService Reservation list service
* @param ReservationList $reservationListHelper Reservation list helper
* @param ServiceLocatorInterface $sm Service locator
* @param ReservationListService $reservationListService Reservation list service
* @param ReservationList $reservationListHelper Reservation list helper
* @param PluginManager $reservationListPluginManager Reservation list helper
*/
public function __construct(
ServiceLocatorInterface $sm,
protected ReservationListService $reservationListService,
protected ReservationList $reservationListHelper
protected ReservationList $reservationListHelper,
protected PluginManager $reservationListPluginManager
) {
parent::__construct($sm);
}
Expand Down Expand Up @@ -130,10 +141,10 @@ public function addItemToListAction()
}
$view = $this->createViewModel(
[
'institution' => $this->getParam('institution'),
'listIdentifier' => $this->getParam('listIdentifier'),
'recordId' => $this->getParam('recordId'),
'source' => $this->getParam('source'),
'institution' => $this->getParam(Form::INSTITUTION_KEY),
'listIdentifier' => $this->getParam(Form::LIST_IDENTIFIER_KEY),
'recordId' => $this->getParam(Form::LIST_RECORD_KEY),
'source' => $this->getParam(Form::SOURCE_KEY),
]
);
$driver = $this->getRecordLoader()->load(
Expand Down Expand Up @@ -202,10 +213,10 @@ public function createListAction(): \Laminas\View\Model\ViewModel

$view = $this->createViewModel(
[
'source' => $this->getParam('source'),
'recordId' => $this->getParam('recordId'),
'institution' => $this->getParam('institution'),
'listIdentifier' => $this->getParam('listIdentifier'),
'source' => $this->getParam(Form::SOURCE_KEY),
'recordId' => $this->getParam(Form::LIST_RECORD_KEY),
'institution' => $this->getParam(Form::INSTITUTION_KEY),
'listIdentifier' => $this->getParam(Form::LIST_IDENTIFIER_KEY),
]
);
$listProperties = $this->reservationListService->getListProperties(
Expand All @@ -225,12 +236,18 @@ public function createListAction(): \Laminas\View\Model\ViewModel
return $view;
}
$list = $this->reservationListService->createListForUser($user);
$newListValues = [
'title' => $title,
'desc' => $this->getParam('desc'),
'institution' => $this->getParam(Form::INSTITUTION_KEY),
'listIdentifier' => $this->getParam(Form::LIST_IDENTIFIER_KEY),
'connection' => ReservationListService::DEFAULT_CONNECTION_HANDLER,
];
$this->reservationListService->updateListFromRequest(
$list,
$user,
$this->getRequest()->getPost()
$newListValues
);

return $this->forwardTo(\Finna\Controller\ReservationListController::class, 'AddItemToList');
}
return $view;
Expand All @@ -252,7 +269,7 @@ public function displayListAction(): \Laminas\View\Model\ViewModel|\Laminas\Http
}
try {
$list = $this->reservationListService->getListById(
$this->getParam('id'),
$this->getParam(Form::SINGLE_LIST_ID_KEY),
$user
);
} catch (RecordMissingException $e) {
Expand All @@ -275,6 +292,17 @@ public function displayListAction(): \Laminas\View\Model\ViewModel|\Laminas\Http
}
}

/**
* Action route to select how to order the singular item currently selected.
*
* @return \Laminas\View\Model\ViewModel
*/
public function placeOrderOptionsAction()
{
$routeParams = $this->params()->fromQuery();
return $this->createViewModel(compact('routeParams'));
}

/**
* Handles ordering of reservation lists
*
Expand All @@ -290,7 +318,7 @@ public function placeOrderAction()
return $this->forceLogin();
}
$request = $this->getRequest();
$listId = $request->getPost('rl_list_id') ?? $this->getParam('id');
$listId = $this->getParam(Form::SINGLE_LIST_ID_KEY);
$list = $this->reservationListService->getListById($listId, $user);
if ($list->getOrdered()) {
throw new \VuFind\Exception\Forbidden('List already ordered');
Expand All @@ -302,10 +330,13 @@ public function placeOrderAction()
if (!($listProperties['Enabled'] ?? true)) {
throw new \VuFind\Exception\Forbidden('ReservationList: No list properties found.');
}

$handler = $this->getService(\Finna\ReservationList\Handler\PluginManager::class)
->getWithConfig($listProperties);
$orderSpecificValues = $handler->getValuesForPlaceOrderForm($list, $user, $request->getPost()->toArray());
$request = $this->getRequest();
$handler = $this->reservationListPluginManager->getWithConfig($listProperties);
$orderSpecificValues = $handler->getValuesForPlaceOrderForm(
$list,
$user,
$request->getPost()->toArray()
);
$form = $handler->getPlaceOrderForm($orderSpecificValues);

$formId = ConnectionAbstractBase::FORM_ID;
Expand All @@ -314,13 +345,12 @@ public function placeOrderAction()
$view->useCaptcha = false;

if (!$this->formWasSubmitted(useCaptcha: false)) {
$form->setData(
[
'firstName' => $user->getFirstname(),
'lastName' => $user->getLastname(),
'email' => $user->getEmail(),
]
$queryValues = $handler->getValuesForPlaceOrderForm(
$list,
$user,
$request->getQuery()->toArray()
);
$form->setData($queryValues);
return $view;
}

Expand All @@ -337,6 +367,67 @@ public function placeOrderAction()
return $view;
}

/**
* Handles ordering of a singular item
*
* @return mixed
*/
public function placeSingleOrderAction()
{
if (!$this->reservationListHelper->isFunctionalityEnabled()) {
throw new ForbiddenException(self::RESERVATION_LISTS_DISABLED);
}
$user = $this->getUser();
if (!$user) {
return $this->forceLogin();
}
$institution = $this->getParam(Form::INSTITUTION_KEY);
$listIdentifier = $this->getParam(Form::LIST_IDENTIFIER_KEY);
$listProperties = $this->reservationListService->getListProperties(
$institution,
$listIdentifier
)['properties'];
if (!($listProperties['Enabled'] ?? true)) {
throw new \VuFind\Exception\Forbidden('ReservationList: No list properties found.');
}
$singularListValues = [
'title' => 'temporary_title',
'desc' => '',
'institution' => $institution,
'listIdentifier' => $listProperties['Identifier'],
'connection' => $listProperties['Connection']['type'],
];
$request = $this->getRequest();
// Create an empty list for the user, but do not save it.
$list = $this->reservationListService->createListForUser($user, $singularListValues);
$handler = $this->reservationListPluginManager->getWithConfig($listProperties);
$formId = ConnectionAbstractBase::FORM_ID;
$queryValues = $handler->getValuesForPlaceSingleOrderForm(
$list,
$user,
$request->isGet() ? $request->getQuery()->toArray() : $request->getPost()->toArray()
);
$form = $handler->getPlaceOrderForm($queryValues);
$view = $this->createViewModel(compact('formId', 'user', 'form'));
$view->formActionRoute = 'reservationlist-placesingleorder';
$view->setTemplate('reservationlist/form');
$view->useCaptcha = false;
if (!$this->formWasSubmitted(useCaptcha: false)) {
$form->setData($queryValues);
return $view;
}
if (!$form->isValid()) {
return $view;
}
$result = $handler->placeOrder($queryValues, $user);
if ($result['success']) {
$this->flashMessenger()->addSuccessMessage($form->getSubmitResponse());
return $this->getRefreshResponse();
}
$this->flashMessenger()->addErrorMessage('could_not_process_feedback');
return $view;
}

/**
* Deletes a list.
*
Expand All @@ -351,7 +442,7 @@ public function deleteListAction()
if (!$user) {
return $this->forceLogin();
}
$listID = $this->getParam('listID');
$listID = $this->getParam(Form::SINGLE_LIST_ID_KEY);
if ($this->getParam('confirm')) {
try {
$list = $this->reservationListService->getListById((int)$listID, $user);
Expand All @@ -372,7 +463,7 @@ public function deleteListAction()
$this->url()->fromRoute('reservationlist-deletelist'),
$this->url()->fromRoute('reservationlist-displaylists'),
'confirm_delete_list_text',
['id' => $listID]
[Form::SINGLE_LIST_ID_KEY => $listID]
);
}

Expand All @@ -392,7 +483,7 @@ public function deleteBulkAction()
return $this->forceLogin();
}

$listID = $this->getParam('listID', false);
$listID = $this->getParam(Form::SINGLE_LIST_ID_KEY, false);
if (false === $listID) {
throw new \Exception('List ID not defined in deleteBulkAction');
}
Expand All @@ -416,7 +507,7 @@ public function deleteBulkAction()
// Redirect to MyResearch home
return $this->inLightbox() // different behavior for lightbox context
? $this->getRefreshResponse()
: $this->redirect()->toRoute('reservationlist-displaylist', ['id' => $listID]);
: $this->redirect()->toRoute('reservationlist-displaylist', [Form::SINGLE_LIST_ID_KEY => $listID]);
}

return $this->createViewModel($viewParams);
Expand Down Expand Up @@ -453,8 +544,8 @@ protected function getRequestAsArray(): array
$request = $this->getRequest()->getQuery()->toArray()
+ $this->getRequest()->getPost()->toArray();

if (!null !== $this->params()->fromRoute('id')) {
$request += ['id' => $this->params()->fromRoute('id')];
if (null !== $this->params()->fromRoute(Form::SINGLE_LIST_ID_KEY)) {
$request += ['id' => $this->params()->fromRoute(Form::SINGLE_LIST_ID_KEY)];
}
return $request;
}
Expand All @@ -469,8 +560,7 @@ protected function getListAsResults()
$request = $this->getRequestAsArray();
$runner = $this->serviceLocator->get(\VuFind\Search\SearchRunner::class);
// Set up listener for recommendations:
$rManager = $this->serviceLocator
->get(\VuFind\Recommend\PluginManager::class);
$rManager = $this->getService(\VuFind\Recommend\PluginManager::class);
$setupCallback = function ($runner, $params, $searchId) use ($rManager) {
$listener = new \VuFind\Search\RecommendListener($rManager, $searchId);
$listener->setConfig(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function __invoke(
[
$container->get(\Finna\ReservationList\ReservationListService::class),
$container->get('ViewHelperManager')->get('reservationList'),
$container->get(\Finna\ReservationList\Handler\PluginManager::class),
]
);
}
Expand Down
3 changes: 3 additions & 0 deletions module/Finna/src/Finna/Db/Row/FinnaResourceList.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ public function __construct($adapter, protected ?Container $session = null)
*/
public function getId(): int
{
if (!isset($this->id)) {
return -1;
}
return $this->id;
}

Expand Down
2 changes: 2 additions & 0 deletions module/Finna/src/Finna/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class Form extends \VuFind\Form\Form
* Archive request form id.
*
* @var string
* @deprecated Logic moved to ReservationList::placeSingleOrder
*/
public const ARCHIVE_MATERIAL_REQUEST = 'ArchiveRequest';

Expand Down Expand Up @@ -837,6 +838,7 @@ protected function getFormElementSettingFields()
{
$fields = parent::getFormElementSettingFields();
$fields[] = 'recipient';
$fields[] = 'rl_record_id[]';

return $fields;
}
Expand Down
Loading

0 comments on commit 619890f

Please sign in to comment.