From b2bc67c695cd6f4e6990d428723050366267dd8d Mon Sep 17 00:00:00 2001
From: Dorian Merz <merz@ub.uni-leipzig.de>
Date: Mon, 10 May 2021 14:44:08 +0200
Subject: [PATCH] refs #19815 [finc] move DE-15 HoldsTrait to finc

* adds sysMessage to success message when placing a hold
* cf. #7730
---
 .../finc/src/finc/Controller/HoldsTrait.php   | 204 ++++++++++++++++++
 .../src/finc/Controller/RecordController.php  |   1 +
 2 files changed, 205 insertions(+)
 create mode 100644 module/finc/src/finc/Controller/HoldsTrait.php

diff --git a/module/finc/src/finc/Controller/HoldsTrait.php b/module/finc/src/finc/Controller/HoldsTrait.php
new file mode 100644
index 00000000000..6190199a96e
--- /dev/null
+++ b/module/finc/src/finc/Controller/HoldsTrait.php
@@ -0,0 +1,204 @@
+<?php
+/**
+ * Holds trait (for subclasses of AbstractRecord)
+ *
+ * PHP version 5
+ *
+ * Copyright (C) Villanova University 2010.
+ *
+ * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * @category VuFind
+ * @package  Controller
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org Main Site
+ */
+namespace finc\Controller;
+
+/**
+ * Holds trait (for subclasses of AbstractRecord)
+ *
+ * @category VuFind
+ * @package  Controller
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org Main Site
+ */
+trait HoldsTrait
+{
+    /**
+     * Action for dealing with blocked holds.
+     *
+     * @return mixed
+     */
+    public function blockedholdAction()
+    {
+        $this->flashMessenger()->addMessage('hold_error_blocked', 'error');
+        return $this->redirectToRecord('#top');
+    }
+
+    /**
+     * Action for dealing with holds.
+     *
+     * @return mixed
+     */
+    public function holdAction()
+    {
+        $driver = $this->loadRecord();
+
+        // Stop now if the user does not have valid catalog credentials available:
+        if (!is_array($patron = $this->catalogLogin())) {
+            return $patron;
+        }
+
+        // If we're not supposed to be here, give up now!
+        $catalog = $this->getILS();
+        $checkHolds = $catalog->checkFunction(
+            'Holds',
+            [
+                'id' => $driver->getUniqueID(),
+                'patron' => $patron
+            ]
+        );
+        if (!$checkHolds) {
+            return $this->redirectToRecord();
+        }
+
+        // Do we have valid information?
+        // Sets $this->logonURL and $this->gatheredDetails
+        $gatheredDetails = $this->holds()->validateRequest($checkHolds['HMACKeys']);
+        if (!$gatheredDetails) {
+            return $this->redirectToRecord();
+        }
+
+        // Block invalid requests:
+        if (!$catalog->checkRequestIsValid(
+            $driver->getUniqueID(), $gatheredDetails, $patron
+        )) {
+            return $this->blockedholdAction();
+        }
+
+        // Send various values to the view so we can build the form:
+        $requestGroups = $catalog->checkCapability(
+            'getRequestGroups', [$driver->getUniqueID(), $patron]
+        ) ? $catalog->getRequestGroups($driver->getUniqueID(), $patron) : [];
+        $extraHoldFields = isset($checkHolds['extraHoldFields'])
+            ? explode(":", $checkHolds['extraHoldFields']) : [];
+
+        $requestGroupNeeded = in_array('requestGroup', $extraHoldFields)
+            && !empty($requestGroups)
+            && (empty($gatheredDetails['level'])
+                || $gatheredDetails['level'] != 'copy');
+
+        $pickupDetails = $gatheredDetails;
+        if (!$requestGroupNeeded && !empty($requestGroups)
+            && count($requestGroups) == 1
+        ) {
+            // Request group selection is not required, but we have a single request
+            // group, so make sure pickup locations match with the group
+            $pickupDetails['requestGroupId'] = $requestGroups[0]['id'];
+        }
+        $pickup = $catalog->getPickUpLocations($patron, $pickupDetails);
+
+        // Process form submissions if necessary:
+        if (!is_null($this->params()->fromPost('placeHold'))) {
+            // If the form contained a pickup location or request group, make sure
+            // they are valid:
+            $valid = $this->holds()->validateRequestGroupInput(
+                $gatheredDetails, $extraHoldFields, $requestGroups
+            );
+            if (!$valid) {
+                $this->flashMessenger()
+                    ->addMessage('hold_invalid_request_group', 'error');
+            } elseif (!$this->holds()->validatePickUpInput(
+                $gatheredDetails['pickUpLocation'], $extraHoldFields, $pickup
+            )) {
+                $this->flashMessenger()->addMessage('hold_invalid_pickup', 'error');
+            } else {
+                // If we made it this far, we're ready to place the hold;
+                // if successful, we will redirect and can stop here.
+
+                // Add Patron Data to Submitted Data
+                $holdDetails = $gatheredDetails + ['patron' => $patron];
+
+                // Attempt to place the hold:
+                $function = (string)$checkHolds['function'];
+                $results = $catalog->$function($holdDetails);
+
+                // Success: Go to Display Holds
+                if (isset($results['success']) && $results['success'] == true) {
+                    $msg = [
+                        'html' => true,
+                        'msg' => 'hold_place_success_html',
+                        'tokens' => [
+                            '%%url%%' => $this->url()->fromRoute('myresearch-holds')
+                        ],
+                    ];
+                    $this->flashMessenger()->addMessage(isset($results['sysMessage']) ? $results['sysMessage'] : $msg, 'success');
+                    return $this->redirectToRecord('#top');
+                } else {
+                    // Failure: use flash messenger to display messages, stay on
+                    // the current form.
+                    if (isset($results['status'])) {
+                        $this->flashMessenger()
+                            ->addMessage($results['status'], 'error');
+                    }
+                    if (isset($results['sysMessage'])) {
+                        $this->flashMessenger()
+                            ->addMessage($results['sysMessage'], 'error');
+                    }
+                }
+            }
+        }
+
+        // Find and format the default required date:
+        $defaultRequired = $this->holds()->getDefaultRequiredDate(
+            $checkHolds, $catalog, $patron, $gatheredDetails
+        );
+        $defaultRequired = $this->serviceLocator->get('VuFind\DateConverter')
+            ->convertToDisplayDate("U", $defaultRequired);
+        try {
+            $defaultPickup
+                = $catalog->getDefaultPickUpLocation($patron, $gatheredDetails);
+        } catch (\Exception $e) {
+            $defaultPickup = false;
+        }
+        try {
+            $defaultRequestGroup = empty($requestGroups)
+                ? false
+                : $catalog->getDefaultRequestGroup($patron, $gatheredDetails);
+        } catch (\Exception $e) {
+            $defaultRequestGroup = false;
+        }
+
+        $view = $this->createViewModel(
+            [
+                'gatheredDetails' => $gatheredDetails,
+                'pickup' => $pickup,
+                'defaultPickup' => $defaultPickup,
+                'homeLibrary' => $this->getUser()->home_library,
+                'extraHoldFields' => $extraHoldFields,
+                'defaultRequiredDate' => $defaultRequired,
+                'requestGroups' => $requestGroups,
+                'defaultRequestGroup' => $defaultRequestGroup,
+                'requestGroupNeeded' => $requestGroupNeeded,
+                'helpText' => isset($checkHolds['helpText'])
+                    ? $checkHolds['helpText'] : null
+            ]
+        );
+        $view->setTemplate('record/hold');
+        return $view;
+    }
+}
diff --git a/module/finc/src/finc/Controller/RecordController.php b/module/finc/src/finc/Controller/RecordController.php
index acb8975cea6..7e3702e24fc 100644
--- a/module/finc/src/finc/Controller/RecordController.php
+++ b/module/finc/src/finc/Controller/RecordController.php
@@ -52,6 +52,7 @@ class RecordController extends \VuFind\Controller\RecordController implements
     use CustomTraits\EmailProfileTrait;
     use CustomTraits\PdaTrait;
     use CustomTraits\ReportErrorsTrait;
+    use HoldsTrait;
 
     /**
      * Returns rewrite object
-- 
GitLab