Skip to content

Commit

Permalink
Paging for exporting transaction history until work processor is impl…
Browse files Browse the repository at this point in the history
…emented
  • Loading branch information
LuomaJuha committed Nov 4, 2024
1 parent a4a66c3 commit 9c2bf86
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 57 deletions.
2 changes: 1 addition & 1 deletion local/languages/finna/en-gb.ini
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ list_order_saved = "Sort order saved"
list-tags-info = "Add a new keyword"
Loading = "Loading"
Loan Details = "Loan Details"
loan_history_download = "Export all"
loan_history_download = "Export all in page"
loan_history_download_csv = "Export CSV"
loan_history_download_ods = "Export OpenOffice (ods)"
loan_history_download_xlsx = "Export Excel (xlsx)"
Expand Down
2 changes: 1 addition & 1 deletion local/languages/finna/fi.ini
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ list_order_saved = "Järjestys tallennettu"
list-tags-info = "Lisää uusi avainsana"
Loading = "Ladataan"
Loan Details = "Lainan tiedot"
loan_history_download = "Vie kaikki"
loan_history_download = "Vie kaikki sivulta"
loan_history_download_csv = "Vie CSV"
loan_history_download_ods = "Vie OpenOffice (ods)"
loan_history_download_xlsx = "Vie Excel (xlsx)"
Expand Down
2 changes: 1 addition & 1 deletion local/languages/finna/sv.ini
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ list_order_saved = "Sortering sparad"
list-tags-info = "Lägg till nytt nyckelord"
Loading = "Laddar"
Loan Details = "Information om lånet"
loan_history_download = "Exportera alla"
loan_history_download = "Exportera alla på sidan"
loan_history_download_csv = "Exportera CSV"
loan_history_download_ods = "Exportera OpenOffice (ods)"
loan_history_download_xlsx = "Exportera Excel (xlsx)"
Expand Down
98 changes: 48 additions & 50 deletions module/Finna/src/Finna/Controller/MyResearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1356,8 +1356,10 @@ public function downloadLoanHistoryAction()
throw new \Exception('Invalid parameters.');
}

$page = (int)$this->params()->fromQuery('page', 1);
$sort = $this->params()->fromQuery('sort', '');
$recordLoader = $this->serviceLocator->get(\VuFind\Record\Loader::class);
$page = 1;
$config = $this->getConfig();
try {
$tmp = fopen('php://temp/maxmemory:' . (5 * 1024 * 1024), 'r+');
$header = [
Expand All @@ -1377,55 +1379,50 @@ public function downloadLoanHistoryAction()
if ('xlsx' === $fileFormat) {
Cell::setValueBinder(new AdvancedValueBinder());
}
do {
// Try to use large page size, but take ILS limits into account
$pageOptions = $this->getPaginationHelper()
->getOptions($page, null, 1000, $functionConfig);
$result = $catalog
->getMyTransactionHistory($patron, $pageOptions['ilsParams']);

if (isset($result['success']) && !$result['success']) {
$this->flashMessenger()->addErrorMessage($result['status']);
return $this->redirect()->toRoute('checkouts-history');
}
$pageOptions = $this->getPaginationHelper()->getOptions(
$page,
$sort,
$config->Catalog->historic_loan_page_size ?? 50,
$functionConfig
);
$result = $catalog->getMyTransactionHistory($patron, $pageOptions['ilsParams']);

$ids = [];
foreach ($result['transactions'] as $current) {
$id = $current['id'] ?? '';
$source = $current['source'] ?? DEFAULT_SEARCH_BACKEND;
$ids[] = compact('id', 'source');
}
$records = $recordLoader->loadBatch($ids, true);
foreach ($result['transactions'] as $i => $current) {
$driver = $records[$i];
$format = $driver->getFormats();
$format = end($format);
$author = $driver->tryMethod('getNonPresenterAuthors');

$loan = [];
$loan[] = $current['title'] ?? $driver->getTitle() ?? '';
$loan[] = $this->translate($format);
$loan[] = $author[0]['name'] ?? '';
$loan[] = $current['publication_year'] ?? '';
$loan[] = empty($current['institution_name'])
? ''
: $this->translateWithPrefix('location_', $current['institution_name']);
$loan[] = empty($current['borrowingLocation'])
? ''
: $this->translateWithPrefix('location_', $current['borrowingLocation']);
$loan[] = $current['checkoutDate'] ?? '';
$loan[] = $current['returnDate'] ?? '';
$loan[] = $current['dueDate'] ?? '';

$nextRow = $worksheet->getHighestRow() + 1;
$worksheet->fromArray($loan, null, 'A' . (string)$nextRow);
}
if (isset($result['success']) && !$result['success']) {
$this->flashMessenger()->addErrorMessage($result['status']);
return $this->redirect()->toRoute('checkouts-history');
}

$pageEnd = $pageOptions['ilsPaging']
? ceil($result['count'] / $pageOptions['limit'])
: 1;
$page++;
} while ($page <= $pageEnd);
$ids = [];
foreach ($result['transactions'] as $current) {
$id = $current['id'] ?? '';
$source = $current['source'] ?? DEFAULT_SEARCH_BACKEND;
$ids[] = compact('id', 'source');
}
$records = $recordLoader->loadBatch($ids, true);
foreach ($result['transactions'] as $i => $current) {
$driver = $records[$i];
$format = $driver->getFormats();
$format = end($format);
$author = $driver->tryMethod('getNonPresenterAuthors');

$loan = [];
$loan[] = $current['title'] ?? $driver->getTitle() ?? '';
$loan[] = $this->translate($format);
$loan[] = $author[0]['name'] ?? '';
$loan[] = $current['publication_year'] ?? '';
$loan[] = empty($current['institution_name'])
? ''
: $this->translateWithPrefix('location_', $current['institution_name']);
$loan[] = empty($current['borrowingLocation'])
? ''
: $this->translateWithPrefix('location_', $current['borrowingLocation']);
$loan[] = $current['checkoutDate'] ?? '';
$loan[] = $current['returnDate'] ?? '';
$loan[] = $current['dueDate'] ?? '';

$nextRow = $worksheet->getHighestRow() + 1;
$worksheet->fromArray($loan, null, 'A' . (string)$nextRow);
}
if ('xlsx' === $fileFormat) {
$worksheet->getStyle('G2:I' . $worksheet->getHighestRow())
->getNumberFormat()
Expand All @@ -1442,11 +1439,12 @@ public function downloadLoanHistoryAction()
);
$writer = new $this->exportFormats[$fileFormat]['writer']($spreadsheet);
$writer->save($tmp);

$fileName = implode('-', ['finna-loan-history-page', $page, $sort,]);
$fileName = str_replace(' ', '-', $fileName) . '.' . $fileFormat;
$response->getHeaders()
->addHeaderLine(
'Content-Disposition',
'attachment; filename="finna-loan-history.' . $fileFormat . '"'
'attachment; filename="' . $fileName . '"'
);

rewind($tmp);
Expand Down
21 changes: 17 additions & 4 deletions themes/finna2/templates/checkouts/history.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,31 @@
<?php endif; ?>
</div>
<?php if (!empty($this->transactions)): ?>
<div class="btn-group">
<a data-lightbox class="btn btn-primary btn-finna-toolbar" role="button" href="<?=$this->url('myresearch-savehistoricloans') ?>"><?=$this->transEsc('loan_history_save')?></a>
</div>
<div class="btn-group">
<div class="dropdown loan-history-download">
<button class="btn btn-primary btn-finna-toolbar dropdown-toggle download" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<?=$this->transEsc('loan_history_download')?> <span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-menu-<?php if ($purgeSelectedAllowed): ?>right<?php else: ?>left<?php endif;?> download">
<li>
<a data-lightbox href="<?=$this->url('myresearch-savehistoricloans') ?>"><?=$this->transEsc('loan_history_save')?></a>
</li>
<?php
$queryParams = [];
foreach ($this->sortList as $sortType => $sortData) {
if ($sortData['selected']) {
$queryParams['sort'] = $sortType;
break;
}
}
if ($this->paginator) {
$queryParams['page'] = $this->paginator->getCurrentPageNumber();
}
?>
<?php foreach (['csv', 'ods', 'xlsx'] as $format): ?>
<?php $queryParams['format'] = $format; ?>
<li>
<a href="<?=$this->url('myresearch-downloadloanhistory', [], ['query' => ['format' => $format]])?>"><?=$this->transEsc('loan_history_download_' . $format)?></a>
<a href="<?=$this->url('myresearch-downloadloanhistory', [], ['query' => $queryParams])?>"><?=$this->transEsc('loan_history_download_' . $format)?></a>
</li>
<?php endforeach; ?>
</ul>
Expand Down

0 comments on commit 9c2bf86

Please sign in to comment.