From ec2a6a37062ca203ec73a6c2275a909875af658c Mon Sep 17 00:00:00 2001
From: Robert Lange <robert.lange@uni-leipzig.de>
Date: Mon, 12 Jun 2023 15:21:52 +0200
Subject: [PATCH] refs #23952 [finc] add Traits for historicloansAction,
 mediaReadyToPickupAction and lockAccountAction

* add historicloansAction and getDriverForILSRecordWithBarcode from de_zwi2 to finc HistoricLoansTrait.php
* move mediaReadyToPickupAction from zwi2 to finc MediaReadyToPickupTrait
* move lockAccountAction from zwi2 to finc LockAccountTrait
---
 .../CustomTraits/HistoricLoansTrait.php       | 185 ++++++++++++++++++
 .../CustomTraits/LockAccountTrait.php         |  78 ++++++++
 .../CustomTraits/MediaReadyToPickupTrait.php  |  97 +++++++++
 3 files changed, 360 insertions(+)
 create mode 100644 module/finc/src/finc/Controller/CustomTraits/HistoricLoansTrait.php
 create mode 100644 module/finc/src/finc/Controller/CustomTraits/LockAccountTrait.php
 create mode 100644 module/finc/src/finc/Controller/CustomTraits/MediaReadyToPickupTrait.php

diff --git a/module/finc/src/finc/Controller/CustomTraits/HistoricLoansTrait.php b/module/finc/src/finc/Controller/CustomTraits/HistoricLoansTrait.php
new file mode 100644
index 00000000000..36b5d90f838
--- /dev/null
+++ b/module/finc/src/finc/Controller/CustomTraits/HistoricLoansTrait.php
@@ -0,0 +1,185 @@
+<?php
+/**
+ * Search Trait
+ *
+ * PHP version 7
+ *
+ * Copyright (C) Villanova University 2023.
+ * Copyright (C) Leipzig University Library 2023.
+ *
+ * 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  Controller
+ * @author   Frank Morgner   <morgner.f@ub.uni-leipzig.de>
+ * @author   Robert Lange <lange@ub.uni-leipzig.de>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     http://vufind.org   Main Site
+ */
+namespace finc\Controller\CustomTraits;
+
+/**
+ * Search Trait
+ *
+ * @category VuFind
+ * @package  Controller
+ * @author   Robert Lange <lange@ub.uni-leipzig.de>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     http://vufind.org   Main Site
+ */
+trait HistoricLoansTrait
+{
+    /**
+     * Send list of current & past loans to view
+     *
+     * @return mixed
+     * @throws Exception
+     *
+     * @todo Enrich metadata of in results of view.
+     */
+    public function historicloansAction()
+    {
+        // Stop now if the user does not have valid catalog credentials available:
+        if (!is_array($patron = $this->catalogLogin())) {
+            return $patron;
+        }
+        $currentPage = ($this->params()->fromQuery('page'))
+            ? $this->params()->fromQuery('page') : 1;
+
+        // Connect to the ILS:
+        $catalog = $this->getILS();
+
+        // Check function config
+        $functionConfig = $catalog->checkFunction(
+            'getMyTransactionHistory', compact('patron')
+        );
+        if (false === $functionConfig) {
+            $this->flashMessenger()->addErrorMessage('ils_action_unavailable');
+            return $this->createViewModel();
+        }
+
+        // Get paging setup:
+        $config = $this->getConfig();
+        $pageOptions = $this->getPaginationHelper()->getOptions(
+            (int)$currentPage,
+            $this->params()->fromQuery('sort'),
+            $config->Catalog->historic_loan_page_size ?? 30,
+            $functionConfig
+        );
+
+        $result = $catalog->getMyTransactionHistory(
+            $patron,
+            $pageOptions['ilsParams']['page'] ?? $currentPage
+        );
+
+        if (isset($result['success']) && !$result['success']) {
+            $this->flashMessenger()->addErrorMessage($result['status']);
+            return $this->createViewModel();
+        }
+
+        $paginator = $this->getPaginationHelper()->getPaginator(
+            $pageOptions, $result['total'], $result['items']
+        );
+        if ($paginator) {
+            $pageStart = $paginator->getAbsoluteItemNumber(1) - 1;
+            $pageEnd = $paginator->getAbsoluteItemNumber($pageOptions['limit']) - 1;
+        } else {
+            $paginator = false;
+            $pageStart = 0;
+            $pageEnd = $result['total'];
+        }
+
+        $sortList = $pageOptions['sortList'];
+        $params = $pageOptions['ilsParams'];
+
+        $recordList = $this->getDriverForILSRecordWithBarcode($result['items']);
+
+        return $this->createViewModel(
+            [
+                'recordList' => $recordList,
+                'items' => $result['items'],
+                'currentPage' => $currentPage,
+                'paginator' => $paginator,
+                'params' => $params
+            ]
+        );
+    }
+
+    /**
+     * Redirect to new MyResearch/Home
+     *
+     * @return void
+     */
+    public function loanhistoryAction()
+    {
+        $this->redirect()->toRoute('myresearch-historicloans');
+    }
+
+    /**
+     * Libero specific override to inject barcode and callnumber data from Solr
+     * record to ils_details.
+     *
+     * @param array $ilsResults Return of Libero request.
+     *
+     * @return array<\VuFind\RecordDriver\AbstractBase> records.
+     */
+    protected function getDriverForILSRecordWithBarcode(&$ilsResults)
+    {
+        // collect barcodes
+        foreach ($ilsResults as $result) {
+            if (isset($result['barcode'])) {
+                $barcodeIds[] = $result['barcode'];
+            }
+        }
+        // form query
+        if (!isset($barcodeIds)) {
+            return $ilsResults;
+        }
+        $config = $this->getConfig();
+        if (!isset($config->CustomIndex->indexExtension)) {
+            return $ilsResults;
+        }
+        $barcodeSolrField = 'barcode_' . $config->CustomIndex->indexExtension;
+        $query_barcode_part = implode(' OR ', $barcodeIds);
+        $query = new \VuFindSearch\Query\Query(
+            $barcodeSolrField . ':(' . $query_barcode_part . ')'
+        );
+        $source = $current['source'] ?? DEFAULT_SEARCH_BACKEND;
+        // search records
+        $result = $this->serviceLocator->get('VuFind\Search')
+            ->search($source, $query, 0, count($barcodeIds) + 1, null);
+        $records = $result->getRecords();
+        // match and merge data
+        if (count($records) == 0) {
+            return $ilsResults;
+        }
+        // $barcodeMap = array_column($ilsResults, 'barcode'); PHP < 5.5
+        $barcodeMap = array_map(
+            function ($element) {
+                return $element['barcode'];
+            }, $ilsResults
+        );
+        foreach ($records as $record) {
+            $bcFields = $record->getField($barcodeSolrField);
+            $match = array_flip(array_intersect($barcodeMap, $bcFields));
+            if (count($match) == 1
+            ) {
+                $keyOfIlsRecord = reset($match);
+                $ilsResults[$keyOfIlsRecord]['id'] = $record->getUniqueID();
+                $ilsResults[$keyOfIlsRecord]['record'] = $record;
+            }
+        }
+        return $records;
+    }
+}
diff --git a/module/finc/src/finc/Controller/CustomTraits/LockAccountTrait.php b/module/finc/src/finc/Controller/CustomTraits/LockAccountTrait.php
new file mode 100644
index 00000000000..76f584e6d1e
--- /dev/null
+++ b/module/finc/src/finc/Controller/CustomTraits/LockAccountTrait.php
@@ -0,0 +1,78 @@
+<?php
+/**
+ * Search Trait
+ *
+ * PHP version 7
+ *
+ * Copyright (C) Villanova University 2023.
+ * Copyright (C) Leipzig University Library 2023.
+ *
+ * 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  Controller
+ * @author   Frank Morgner   <morgner.f@ub.uni-leipzig.de>
+ * @author   Robert Lange <lange@ub.uni-leipzig.de>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     http://vufind.org   Main Site
+ */
+namespace finc\Controller\CustomTraits;
+
+/**
+ * Search Trait
+ *
+ * @category VuFind
+ * @package  Controller
+ * @author   Robert Lange <lange@ub.uni-leipzig.de>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     http://vufind.org   Main Site
+ */
+trait LockAccountTrait
+{
+    /**
+     * Enable view of locking account by user
+     *
+     * @return mixed
+     */
+    public function lockAccountAction()
+    {
+        // Stop now if the user does not have valid catalog credentials available:
+        if (!is_array($patron = $this->catalogLogin())) {
+            return $patron;
+        }
+
+        if ($this->formWasSubmitted('submitLockAccount')
+            && $this->params()->fromPost('lock_account') == "1"
+        ) {
+            // Connect to the ILS:
+            $catalog = $this->getILS();
+            $isSuccess = $catalog->setLockMyAccount($patron);
+            if ($isSuccess === false) {
+                $this->flashMessenger()->addMessage(
+                    'finclibero_lock_account_error', 'error'
+                );
+            } else {
+                $this->getAuthManager()->logout('/', false);
+                $this->flashMessenger()->addMessage(
+                    'finclibero_lock_account_locked', 'success'
+                );
+                return $this->redirect()->toUrl(
+                    $this->getServerUrl('home')
+                );
+
+            }
+        }
+        return $this->createViewModel();
+    }
+}
diff --git a/module/finc/src/finc/Controller/CustomTraits/MediaReadyToPickupTrait.php b/module/finc/src/finc/Controller/CustomTraits/MediaReadyToPickupTrait.php
new file mode 100644
index 00000000000..d4f4cf3bbbe
--- /dev/null
+++ b/module/finc/src/finc/Controller/CustomTraits/MediaReadyToPickupTrait.php
@@ -0,0 +1,97 @@
+<?php
+/**
+ * Search Trait
+ *
+ * PHP version 7
+ *
+ * Copyright (C) Villanova University 2023.
+ * Copyright (C) Leipzig University Library 2023.
+ *
+ * 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  Controller
+ * @author   Frank Morgner   <morgner.f@ub.uni-leipzig.de>
+ * @author   Robert Lange <lange@ub.uni-leipzig.de>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     http://vufind.org   Main Site
+ */
+namespace finc\Controller\CustomTraits;
+
+/**
+ * Search Trait
+ *
+ * @category VuFind
+ * @package  Controller
+ * @author   Robert Lange <lange@ub.uni-leipzig.de>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     http://vufind.org   Main Site
+ */
+trait MediaReadyToPickupTrait
+{
+    
+    /**
+     * Send list of ready to pickup media to view
+     *
+     * @return mixed
+     */
+    public function mediaReadyToPickupAction()
+    {
+        // Stop now if the user does not have valid catalog credentials available:
+        if (!is_array($patron = $this->catalogLogin())) {
+            return $patron;
+        }
+        // Connect to the ILS:
+        $catalog = $this->getILS();
+        // Process cancel requests if necessary:
+        $cancelStatus = $catalog->checkFunction('cancelHolds', compact('patron'));
+        $view = $this->createViewModel();
+        $view->cancelResults = $cancelStatus
+            ? $this->holds()->cancelHolds($catalog, $patron) : [];
+        // If we need to confirm
+        if (!is_array($view->cancelResults)) {
+            return $view->cancelResults;
+        }
+        // By default, assume we will not need to display a cancel form:
+        $view->cancelForm = false;
+        // Get held item details:
+        $result = $catalog->getMyMediaReadyToPickup($patron);
+        $recordList = [];
+        $this->holds()->resetValidation();
+        foreach ($result as $current) {
+            // Add cancel details if appropriate:
+            $current = $this->holds()->addCancelDetails(
+                $catalog, $current, $cancelStatus
+            );
+            if ($cancelStatus && $cancelStatus['function'] != "getCancelHoldLink"
+                && isset($current['cancel_details'])
+            ) {
+                // Enable cancel form if necessary:
+                $view->cancelForm = true;
+            }
+            // Build record driver:
+            $recordList[] = $this->getDriverForILSRecord($current);
+        }
+        // Get List of PickUp Libraries based on patron's home library
+        try {
+            $view->pickup = $catalog->getPickUpLocations($patron);
+        } catch (\Exception $e) {
+            // Do nothing; if we're unable to load information about pickup
+            // locations, they are not supported and we should ignore them.
+        }
+        $view->recordList = $recordList;
+        $view->accountStatus = $this->collectRequestAccountStats($recordList);
+        return $view;
+    }
+}
-- 
GitLab