From 8bfdf0aadfc5a11659d04f390e1fb618bcbb8fb0 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Thu, 2 May 2013 11:57:03 -0400
Subject: [PATCH] Moved tag parsing out of record drivers; centralized
 trimming.

---
 .../VuFind/src/VuFind/Controller/AbstractRecord.php   |  3 ++-
 .../VuFind/src/VuFind/Controller/AjaxController.php   |  2 +-
 .../src/VuFind/Controller/MyResearchController.php    |  3 ++-
 .../VuFind/src/VuFind/Controller/Plugin/Favorites.php |  5 ++---
 .../VuFind/src/VuFind/RecordDriver/AbstractBase.php   | 11 +++++------
 module/VuFind/src/VuFind/Tags.php                     |  2 +-
 6 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/module/VuFind/src/VuFind/Controller/AbstractRecord.php b/module/VuFind/src/VuFind/Controller/AbstractRecord.php
index efa8be37af4..f190a7c99a6 100644
--- a/module/VuFind/src/VuFind/Controller/AbstractRecord.php
+++ b/module/VuFind/src/VuFind/Controller/AbstractRecord.php
@@ -187,7 +187,8 @@ class AbstractRecord extends AbstractBase
 
         // Save tags, if any:
         if ($this->params()->fromPost('submit')) {
-            $driver->addTags($user, $this->params()->fromPost('tag'));
+            $tags = $this->params()->fromPost('tag');
+            $driver->addTags($user, \VuFind\Tags::parse($tags));
             return $this->redirectToRecord();
         }
 
diff --git a/module/VuFind/src/VuFind/Controller/AjaxController.php b/module/VuFind/src/VuFind/Controller/AjaxController.php
index 4a86be6eb5c..ff96d9e8c1c 100644
--- a/module/VuFind/src/VuFind/Controller/AjaxController.php
+++ b/module/VuFind/src/VuFind/Controller/AjaxController.php
@@ -618,7 +618,7 @@ class AjaxController extends AbstractBase
             );
             $tag = $this->params()->fromPost('tag', '');
             if (strlen($tag) > 0) { // don't add empty tags
-                $driver->addTags($user, $tag);
+                $driver->addTags($user, \VuFind\Tags::parse($tag));
             }
         } catch (\Exception $e) {
             return $this->output(
diff --git a/module/VuFind/src/VuFind/Controller/MyResearchController.php b/module/VuFind/src/VuFind/Controller/MyResearchController.php
index fd9d38a54df..992ce214948 100644
--- a/module/VuFind/src/VuFind/Controller/MyResearchController.php
+++ b/module/VuFind/src/VuFind/Controller/MyResearchController.php
@@ -387,10 +387,11 @@ class MyResearchController extends AbstractBase
     {
         $lists = $this->params()->fromPost('lists');
         foreach ($lists as $list) {
+            $tags = $this->params()->fromPost('tags'.$list);
             $driver->saveToFavorites(
                 array(
                     'list'  => $list,
-                    'mytags'  => $this->params()->fromPost('tags'.$list),
+                    'mytags'  => \VuFind\Tags::parse($tags),
                     'notes' => $this->params()->fromPost('notes'.$list)
                 ),
                 $user
diff --git a/module/VuFind/src/VuFind/Controller/Plugin/Favorites.php b/module/VuFind/src/VuFind/Controller/Plugin/Favorites.php
index 485eb7a04d0..58f47ed533f 100644
--- a/module/VuFind/src/VuFind/Controller/Plugin/Favorites.php
+++ b/module/VuFind/src/VuFind/Controller/Plugin/Favorites.php
@@ -26,7 +26,7 @@
  * @link     http://www.vufind.org  Main Page
  */
 namespace VuFind\Controller\Plugin;
-use VuFind\Exception\LoginRequired as LoginRequiredException, VuFind\Tags,
+use VuFind\Exception\LoginRequired as LoginRequiredException,
     Zend\Mvc\Controller\Plugin\AbstractPlugin;
 
 /**
@@ -83,8 +83,7 @@ class Favorites extends AbstractPlugin
 
             // Add the information to the user's account:
             $tags = isset($params['mytags'])
-                ? Tags::parse(trim($params['mytags']))
-                : array();
+                ? \VuFind\Tags::parse($params['mytags']) : array();
             $user->saveResource($resource, $list, $tags, '', false);
         }
     }
diff --git a/module/VuFind/src/VuFind/RecordDriver/AbstractBase.php b/module/VuFind/src/VuFind/RecordDriver/AbstractBase.php
index 76994bf420e..ea358d1bcbf 100644
--- a/module/VuFind/src/VuFind/RecordDriver/AbstractBase.php
+++ b/module/VuFind/src/VuFind/RecordDriver/AbstractBase.php
@@ -27,7 +27,7 @@
  */
 namespace VuFind\RecordDriver;
 use VuFind\Exception\LoginRequired as LoginRequiredException,
-    VuFind\Tags, VuFind\XSLT\Import\VuFind as ArticleStripper;
+    VuFind\XSLT\Import\VuFind as ArticleStripper;
 
 /**
  * Abstract base record model.
@@ -198,7 +198,7 @@ abstract class AbstractBase implements \VuFind\Db\Table\DbTableAwareInterface,
      * Add tags to the record.
      *
      * @param \VuFind\Db\Row\User $user The user posting the tag
-     * @param string              $tags The user-provided tag string
+     * @param array               $tags The user-provided tags
      *
      * @return void
      */
@@ -208,7 +208,7 @@ abstract class AbstractBase implements \VuFind\Db\Table\DbTableAwareInterface,
         $resource = $resources->findResource(
             $this->getUniqueId(), $this->getResourceSource()
         );
-        foreach (Tags::parse($tags) as $tag) {
+        foreach ($tags as $tag) {
             $resource->addTag($tag, $user);
         }
     }
@@ -218,7 +218,7 @@ abstract class AbstractBase implements \VuFind\Db\Table\DbTableAwareInterface,
      *
      * @param array               $params Array with some or all of these keys:
      *  <ul>
-     *    <li>mytags - Unparsed tag string to associate with record (optional)</li>
+     *    <li>mytags - Tag array to associate with record (optional)</li>
      *    <li>notes - Notes to associate with record (optional)</li>
      *    <li>list - ID of list to save record into (omit to create new list)</li>
      *  </ul>
@@ -254,8 +254,7 @@ abstract class AbstractBase implements \VuFind\Db\Table\DbTableAwareInterface,
         // Add the information to the user's account:
         $user->saveResource(
             $resource, $list,
-            isset($params['mytags'])
-            ? Tags::parse(trim($params['mytags'])) : array(),
+            isset($params['mytags']) ? $params['mytags'] : array(),
             isset($params['notes']) ? $params['notes'] : ''
         );
     }
diff --git a/module/VuFind/src/VuFind/Tags.php b/module/VuFind/src/VuFind/Tags.php
index 89f0fafc162..f7c376cb695 100644
--- a/module/VuFind/src/VuFind/Tags.php
+++ b/module/VuFind/src/VuFind/Tags.php
@@ -47,7 +47,7 @@ class Tags
      */
     public static function parse($tags)
     {
-        preg_match_all('/"[^"]*"|[^ ]+/', $tags, $words);
+        preg_match_all('/"[^"]*"|[^ ]+/', trim($tags), $words);
         $result = array();
         foreach ($words[0] as $tag) {
             $result[] = str_replace('"', '', $tag);
-- 
GitLab