From 9ed5b5681e259726fc5f599b15d34d29f90ddb3d Mon Sep 17 00:00:00 2001
From: Dorian Merz <merz@ub.uni-leipzig.de>
Date: Mon, 14 Jan 2019 16:14:17 +0100
Subject: [PATCH] refs #9979

* introduces paiaGetSystemMessages() reading "notifications" from PAIA service
---
 module/finc/src/finc/ILS/Driver/PAIA.php | 47 ++++++++++++++++++++++++
 1 file changed, 47 insertions(+)

diff --git a/module/finc/src/finc/ILS/Driver/PAIA.php b/module/finc/src/finc/ILS/Driver/PAIA.php
index 638dcfbda37..de96ede8da0 100644
--- a/module/finc/src/finc/ILS/Driver/PAIA.php
+++ b/module/finc/src/finc/ILS/Driver/PAIA.php
@@ -64,6 +64,7 @@ class PAIA extends \VuFind\ILS\Driver\PAIA
     const SCOPE_READ_ITEMS = 'read_items';
     const SCOPE_WRITE_ITEMS = 'write_items';
     const SCOPE_CHANGE_PASSWORD = 'change_password';
+    const SCOPE_READ_NOTIFICATIOS = 'read_notifications';
 
     protected $last_error = null;
 
@@ -721,6 +722,52 @@ class PAIA extends \VuFind\ILS\Driver\PAIA
         return [];
     }
 
+    /**
+     * PAIA support method for PAIA core method 'notifications'
+     *
+     * @param array $patron Array with patron information
+     *
+     * @return array|mixed Array of system notifications for the patron
+     * @throws \Exception
+     * @throws ILSException You are not entitled to read notifications
+     */
+    protected function paiaGetSystemMessages($patron)
+    {
+        // check if user has appropriate scope
+        if (!$this->paiaCheckScope(self::SCOPE_READ_NOTIFICATIOS)) {
+            throw new ILSException('You are not entitled to read notifications.');
+        }
+
+        try {
+            $response = $this->paiaGetAsArray(
+                'core/'.$patron['cat_username'].'/notifications'
+            );
+        } catch (\Exception $e) {
+            // all error handling is done in paiaHandleErrors so pass on the excpetion
+            throw $e;
+        }
+        foreach ($response as &$message) {
+            //Fernleihmedium erhalten.[Barcode]7016265550[Titel: *]König von Deutschland
+            if (preg_match('/\[Barcode\](\w\d*)/',$message['about'],$matches)) {
+                $message['barcode'] = $matches[1];
+            }
+            if (preg_match('/\[Titel(.*)\](.+)/',$message['about'],$matches)) {
+                $message['title'] = $matches[2];
+            }
+            $message['message'] = $message['about'];
+            //2017-08-10T16:59:00+02:00
+            $date = date_create_from_format(\DATE_RFC3339,$message['date']);
+            if ($date !== FALSE) {
+                $message['datetime'] = $date->format('d.m.Y H:i');
+            } else {
+                //$errs = date_get_last_errors();
+                $message['datetime'] = $message['date'];
+            }
+        }
+
+        return $response;
+    }
+
     /**
      * This PAIA helper function allows custom overrides for mapping of PAIA response
      * to getMyHolds data structure.
-- 
GitLab