From c4476fd2a4c00fb4e7449454a1b47e592fa9d2c4 Mon Sep 17 00:00:00 2001
From: Alexander Purr <purr@ub.uni-leipzig.de>
Date: Mon, 19 Apr 2021 12:10:36 +0200
Subject: [PATCH] refs #19646 [fid] copy mylistaction from
 finc\MyResearchController from finc * set additional view context parameter

---
 .../src/Controller/MyResearchController.php   | 78 +++++++++++++++++++
 1 file changed, 78 insertions(+)

diff --git a/module/fid/src/Controller/MyResearchController.php b/module/fid/src/Controller/MyResearchController.php
index e8999fd059b..e0e032ed4b2 100644
--- a/module/fid/src/Controller/MyResearchController.php
+++ b/module/fid/src/Controller/MyResearchController.php
@@ -31,6 +31,8 @@ use fid\Service\Client;
 use fid\Service\ClientException;
 use fid\Service\DataTransferObject\Library;
 use VuFind\Exception\Forbidden;
+use VuFind\Exception\ListPermission as ListPermissionException;
+use VuFind\Search\RecommendListener;
 use Zend\ServiceManager\ServiceLocatorInterface;
 
 /**
@@ -215,4 +217,80 @@ class MyResearchController extends \VuFind\Controller\MyResearchController
 
         return $licenses;
     }
+
+    /**
+     * Send user's saved favorites from a particular list to the view
+     *
+     * @return mixed
+     */
+    public function mylistAction()
+    {
+        // Fail if lists are disabled:
+        if (!$this->listsEnabled()) {
+            throw new ForbiddenException('Lists disabled');
+        }
+
+        // Check for "delete item" request; parameter may be in GET or POST depending
+        // on calling context.
+        $deleteId = $this->params()->fromPost(
+            'delete', $this->params()->fromQuery('delete')
+        );
+        if ($deleteId) {
+            $deleteSource = $this->params()->fromPost(
+                'source',
+                $this->params()->fromQuery('source', DEFAULT_SEARCH_BACKEND)
+            );
+            // If the user already confirmed the operation, perform the delete now;
+            // otherwise prompt for confirmation:
+            $confirm = $this->params()->fromPost(
+                'confirm', $this->params()->fromQuery('confirm')
+            );
+            if ($confirm) {
+                $layout = $this->params()->fromPost(
+                    'layout', $this->params()->fromQuery('layout')
+                );
+
+                /* #17712 not necessary to fetch items after deleting by ajax */
+                $success = $this->performDeleteFavorite($deleteId, $deleteSource);
+                if ($layout === 'lightbox' || $success !== true) {
+                    return $success;
+                }
+
+            } else {
+                return $this->confirmDeleteFavorite($deleteId, $deleteSource);
+            }
+        }
+
+        // If we got this far, we just need to display the favorites:
+        try {
+            $runner = $this->serviceLocator->get('VuFind\Search\SearchRunner');
+
+            // We want to merge together GET, POST and route parameters to
+            // initialize our search object:
+            $request = $this->getRequest()->getQuery()->toArray()
+                + $this->getRequest()->getPost()->toArray()
+                + ['id' => $this->params()->fromRoute('id')];
+
+            // Set up listener for recommendations:
+            $rManager = $this->serviceLocator
+                ->get('VuFind\Recommend\PluginManager');
+            $setupCallback = function ($runner, $params, $searchId) use ($rManager) {
+                $listener = new RecommendListener($rManager, $searchId);
+                $listener->setConfig(
+                    $params->getOptions()->getRecommendationSettings()
+                );
+                $listener->attach($runner->getEventManager()->getSharedManager());
+            };
+
+            $results = $runner->run($request, 'Favorites', $setupCallback);
+            return $this->createViewModel(
+                ['params' => $results->getParams(), 'results' => $results, 'context' => "favorite"]
+            );
+        } catch (ListPermissionException $e) {
+            if (!$this->getUser()) {
+                return $this->forceLogin();
+            }
+            throw $e;
+        }
+    }
 }
-- 
GitLab