Skip to content

Commit

Permalink
Separated add list and add item to list functions to their own functions
Browse files Browse the repository at this point in the history
  • Loading branch information
LuomaJuha committed Oct 8, 2024
1 parent 8159701 commit cb12488
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 61 deletions.
3 changes: 2 additions & 1 deletion module/Finna/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,7 @@
'reservationlist-deletelist' => 'DeleteList/[:id]',
'reservationlist-deletebulk' => 'DeleteBulk/[:id]',
'reservationlist-additemtolist' => 'AddItemToList',
'reservationlist-createlist' => 'CreateList',
],
];

Expand All @@ -1108,7 +1109,7 @@
'Record/DownloadFile',
'Bazaar/Home',
'Bazaar/Cancel',
'ReservationList/AddList',
'ReservationList/CreateList',
];

$routeGenerator = new \VuFind\Route\RouteGenerator();
Expand Down
113 changes: 65 additions & 48 deletions module/Finna/src/Finna/Controller/ReservationListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

namespace Finna\Controller;

use Exception;
use Finna\Form\Form;
use Finna\ReservationList\ReservationListService;
use Finna\View\Helper\Root\ReservationList;
Expand Down Expand Up @@ -116,79 +117,95 @@ public function addItemToListAction()
if (!$user) {
return $this->forceLogin();
}
$view = $this->createViewModel();
$view->institution = $institution = $this->getParam('institution');
$view->listIdentifier = $listIdentifier = $this->getParam('listIdentifier');

$recordId = $this->getParam('recordId');
$source = $this->getParam('source');
$newList = $this->getParam('newList');
if ($newList && !$this->formWasSubmitted('submit')) {
$view->setTemplate('reservationlist/add-list');
$view->source = $source;
$view->recordId = $recordId;
return $view;
}
$view = $this->createViewModel(
[
'institution' => $this->getParam('institution'),
'listIdentifier' => $this->getParam('listIdentifier'),
'recordId' => $this->getParam('recordId'),
'source' => $this->getParam('source'),
]
);
$driver = $this->getRecordLoader()->load(
$recordId,
$source ?: DEFAULT_SEARCH_BACKEND,
$view->recordId,
$view->source ?: DEFAULT_SEARCH_BACKEND,
false
);
[
'properties' => $listProperties,
'institution_information' => $institutionInformation,
'translation_keys' => $listTranslations,
] = ($this->reservationListHelper)($user)->getListProperties($institution, $listIdentifier);
] = ($this->reservationListHelper)($user)->getListProperties($view->institution, $view->listIdentifier);
if (!$listProperties) {
throw new \VuFind\Exception\Forbidden('Record is not allowed in the list');
}
$view->driver = $driver;
$lists = $this->reservationListService->getListsNotContainingRecord(
$user,
$driver,
$listIdentifier,
$institution
$view->listIdentifier,
$view->institution
);

// Filter out already ordered lists
$view->lists = array_filter(
$lists,
fn ($list) => !$list->getOrdered()
);
$view->setTemplate('reservationlist/select-list');

if ($this->formWasSubmitted('submit')) {
if ($this->formWasSubmitted('list_selected')) {
if (!$this->validateCsrf()) {
$this->flashMessenger()->addErrorMessage('csrf_validation_failed');
return $view;
}
$this->reservationListService->saveRecordToReservationList(
$this->getRequest()
->getPost()
->set('institution', $view->institution),
$user,
$driver,
);
return $this->inLightbox() // different behavior for lightbox context
? $this->getRefreshResponse()
: $this->redirect()->toRoute('reservationlist-displaylists');
}
return $view;
}

/**
* Add a new list action
*
* @return \Laminas\View\Model\ViewModel
*/
public function createListAction(): \Laminas\View\Model\ViewModel
{
$user = $this->getUser();
if (!$user) {
return $this->forceLogin();
}

$view = $this->createViewModel(
[
'source' => $this->getParam('source'),
'recordId' => $this->getParam('recordId'),
'institution' => $this->getParam('institution'),
'listIdentifier' => $this->getParam('listIdentifier'),
]
);
if ($this->formWasSubmitted('list_created')) {
if (!$this->validateCsrf()) {
$this->flashMessenger()->addErrorMessage('csrf_validation_failed');
return $view;
}
$state = $this->getParam('state');
$title = $this->getParam('title');
$request = $this->getRequest();
$request->getPost()->set('datasource', $driver->getDatasource())->set('institution', $institution);
if ('saveList' === $state) {
if (!$title) {
$view->setTemplate('reservationlist/add-list');
$view->source = $source;
$view->recordId = $recordId;
return $view;
}
$list = $this->reservationListService->createListForUser($user);
$this->reservationListService->updateListFromRequest(
$list,
$user,
$request->getPost()
);
} elseif ('saveItem' === $state) {
$this->reservationListService->saveRecordToReservationList(
$request->getPost(),
$user,
$driver,
);
return $this->inLightbox() // different behavior for lightbox context
? $this->getRefreshResponse()
: $this->redirect()->toRoute('reservationlist-displaylists');
if (!$title) {
return $view;
}
$list = $this->reservationListService->createListForUser($user);
$this->reservationListService->updateListFromRequest(
$list,
$user,
$this->getRequest()->getPost()
);

return $this->forwardTo(\Finna\Controller\ReservationListController::class, 'AddItemToList');
}
return $view;
}
Expand Down Expand Up @@ -328,7 +345,7 @@ public function deleteListAction()
if ($this->getParam('confirm')) {
try {
$user = $this->getUser();
$list = $this->reservationListService->getAndRememberListObject($listID, $user);
$list = $this->reservationListService->getListById((int)$listID, $user);
$this->reservationListService->destroyList($list, $user);
$this->flashMessenger()->addSuccessMessage('ReservationList::List Deleted');
} catch (LoginRequiredException | ListPermissionException $e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,9 @@ public function saveRecordToReservationList(
if (!$user) {
throw new LoginRequiredException('You must be logged in first');
}

// Get or create a list object as needed:
$list = $this->getAndRememberListObject($params->get('list', 'NEW'), $user);
$listId = (int)$params->get('list');
$list = $this->getListById($listId, $user);

// Get or create a resource object as needed:
$resource = $this->resourcePopulator->getOrCreateResourceForDriver($driver);
Expand Down Expand Up @@ -322,7 +322,7 @@ public function saveListForUser(
* sharing form processing between multiple actions.
*
* @param FinnaResourceListEntityInterface $list List to update
* @param ?UserEntityInterface $user Logged-in user (false if none)
* @param UserEntityInterface $user Logged-in user
* @param Parameters $request Request to process
*
* @return int ID of newly created row
Expand All @@ -331,7 +331,7 @@ public function saveListForUser(
*/
public function updateListFromRequest(
FinnaResourceListEntityInterface $list,
?UserEntityInterface $user,
UserEntityInterface $user,
Parameters $request
): int {
$list->setTitle($request->get('title'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
<p><?= $this->transEsc(['ReservationList', $listTranslations['description']]) ?> </p>
<div>
<?php if (!empty($this->lists)): ?>
<form method="post" name="FinnaResourceListAdd" data-lightbox-onclose="VuFind.refreshPage">
<input type="hidden" name="state" value="saveItem">
<form method="post" name="AddItemToReservationList" data-lightbox-onclose="VuFind.refreshPage" action="<?=$this->url('reservationlist-additemtolist'); ?>">
<input type="hidden" name="csrf" value="<?=$this->escapeHtmlAttr($this->auth()->getManager()->getCsrfHash())?>">
<input type="hidden" name="recordId" value="<?=$this->escapeHtmlAttr($this->recordId)?>">
<input type="hidden" name="source" value="<?=$this->escapeHtmlAttr($this->source)?>">
<input type="hidden" name="institution" value="<?=$this->escapeHtmlAttr($this->institution)?>">
<input type="hidden" name="listIdentifier" value="<?=$this->escapeHtmlAttr($this->listIdentifier)?>">
<label class="control-label" for="save_list"><?=$this->transEsc('ReservationList::Choose a List') ?></label>
<div class="form-group">
<select class="form-control" id="save_list" name="list">
Expand All @@ -34,11 +37,11 @@
<textarea class="form-control" id="add_notes" name="desc" rows="3"></textarea>
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" name="submit" value="<?=$this->transEscAttr('ReservationList::Add To Reservation List') ?>">
<input class="btn btn-primary" type="submit" name="list_selected" value="<?=$this->transEscAttr('ReservationList::Add To Reservation List') ?>">
</div>
</form>
<?php else: ?>
<?= $this->transEsc('ReservationList::No Lists Found') ?>
<?php endif; ?>
<a class="btn btn-link" id="make-list" href="<?=$this->url('reservationlist-additemtolist', [], ['query' => ['institution' => $this->institution, 'listIdentifier' => $this->listIdentifier, 'newList' => '1', 'recordId' => urlencode($this->driver->getUniqueId()), 'source' => urlencode($this->driver->getSourceIdentifier())]])?>"><?=$this->transEsc('ReservationList::Create New Reservation List')?></a>
<a class="btn btn-link" id="make-list" href="<?=$this->url('reservationlist-createlist', [], ['query' => ['institution' => $this->institution, 'listIdentifier' => $this->listIdentifier, 'newList' => '1', 'recordId' => urlencode($this->recordId), 'source' => urlencode($this->source)]])?>"><?=$this->transEsc('ReservationList::Create New Reservation List')?></a>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@
$this->headTitle($this->translate($pageTitle));

// Set up breadcrumbs:
$this->layout()->breadcrumbs = '<li><a href="' . $this->url('reservationlist-addlist') . '">' . $this->transEsc('ReservationList::Add List') . '</a></li>'
$this->layout()->breadcrumbs = '<li><a href="' . $this->url('reservationlist-createlist') . '">' . $this->transEsc('ReservationList::Add List') . '</a></li>'
. '<li>' . $this->transEsc($pageTitle) . '</li>';
?>

<?=$this->flashmessages()?>

<h2><?=$this->transEsc($pageTitle); ?></h2>

<form class="form-edit-list" method="post" name="finnareservationlist">
<form class="form-edit-list" method="post" name="CreateReservationList" action="<?=$this->url('reservationlist-createlist'); ?>">
<input type="hidden" name="recordId" value="<?=$this->escapeHtmlAttr($this->recordId)?>">
<input type="hidden" name="source" value="<?=$this->escapeHtmlAttr($this->source)?>">
<input type="hidden" name="institution" value="<?=$this->escapeHtmlAttr($this->institution)?>">
<input type="hidden" name="listIdentifier" value="<?=$this->escapeHtmlAttr($this->listIdentifier)?>">
<input type="hidden" name="state" value="saveList">
<input type="hidden" name="csrf" value="<?=$this->escapeHtmlAttr($this->auth()->getManager()->getCsrfHash())?>">
<div class="form-group">
<label class="control-label" for="list_title"><?=$this->transEsc('ReservationList::Reservation List Name'); ?>*:</label>
Expand All @@ -28,6 +27,6 @@
<textarea id="list_desc" class="form-control" name="desc" rows="3"></textarea>
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" name="submit" value="<?=$this->transEscAttr('ReservationList::Create New Reservation List') ?>">
<input class="btn btn-primary" type="submit" name="list_created" value="<?=$this->transEscAttr('ReservationList::Create New Reservation List') ?>">
</div>
</form>

0 comments on commit cb12488

Please sign in to comment.