From 11261bfde314e40e289d518a6f72a94422936516 Mon Sep 17 00:00:00 2001
From: Ere Maijala <ere.maijala@helsinki.fi>
Date: Tue, 5 Feb 2019 21:06:10 +0200
Subject: [PATCH] Linkify URLs in holdings data (#1313)

---
 composer.json                                 |  1 +
 module/VuFind/src/VuFind/ILS/Driver/Demo.php  |  3 +-
 .../src/VuFind/View/Helper/Root/Linkify.php   | 64 +++++++++++++++++++
 .../templates/RecordTab/holdingsils.phtml     |  2 +-
 themes/root/theme.config.php                  |  2 +
 5 files changed, 70 insertions(+), 2 deletions(-)
 create mode 100644 module/VuFind/src/VuFind/View/Helper/Root/Linkify.php

diff --git a/composer.json b/composer.json
index 7a7d7fdde17..0f8f1c4ca17 100644
--- a/composer.json
+++ b/composer.json
@@ -16,6 +16,7 @@
         "ghislainf/zf2-whoops": "dev-master#2649cf7caf400409942ddc3f8fe15b89381fc74e",
         "jasig/phpcas": "1.3.6",
         "matthiasmullie/minify": "1.3.61",
+        "misd/linkify": "1.1.4",
         "ocramius/proxy-manager": "2.0.4",
         "oyejorge/less.php": "1.7.0.14",
         "pear/archive_tar": "^1.4",
diff --git a/module/VuFind/src/VuFind/ILS/Driver/Demo.php b/module/VuFind/src/VuFind/ILS/Driver/Demo.php
index 003e2757bb1..c9ee6510859 100644
--- a/module/VuFind/src/VuFind/ILS/Driver/Demo.php
+++ b/module/VuFind/src/VuFind/ILS/Driver/Demo.php
@@ -651,7 +651,8 @@ class Demo extends AbstractBase
             $status[$i]['holdings_notes'] = [];
             $status[$i]['item_notes'] = [];
             for ($j = 1; $j <= $noteCount; $j++) {
-                $status[$i]['holdings_notes'][] = "Item $itemNum holdings note $j";
+                $status[$i]['holdings_notes'][] = "Item $itemNum holdings note $j"
+                    . ($j === 1 ? ' https://vufind.org#sample_link' : '');
                 $status[$i]['item_notes'][] = "Item $itemNum note $j";
             }
             $summCount = rand(1, 3);
diff --git a/module/VuFind/src/VuFind/View/Helper/Root/Linkify.php b/module/VuFind/src/VuFind/View/Helper/Root/Linkify.php
new file mode 100644
index 00000000000..c5518e7ec60
--- /dev/null
+++ b/module/VuFind/src/VuFind/View/Helper/Root/Linkify.php
@@ -0,0 +1,64 @@
+<?php
+/**
+ * Linkify a string so that the links become clickable HTML
+ *
+ * PHP version 7
+ *
+ * Copyright (C) The National Library of Finland 2019.
+ *
+ * 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  View_Helpers
+ * @author   Ere Maijala <ere.maijala@helsinki.fi>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org/wiki/development Wiki
+ */
+namespace VuFind\View\Helper\Root;
+
+use Zend\View\Helper\AbstractHelper;
+
+/**
+ * Linkify a string so that the links become clickable HTML
+ *
+ * @category VuFind
+ * @package  View_Helpers
+ * @author   Ere Maijala <ere.maijala@helsinki.fi>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org/wiki/development Wiki
+ */
+class Linkify extends AbstractHelper
+{
+    /**
+     * Linkify a string
+     *
+     * @param string $str String to linkify
+     *
+     * @return string
+     */
+    public function __invoke($str)
+    {
+        $linkify = new \Misd\Linkify\Linkify();
+        $proxyUrl = $this->getView()->plugin('proxyUrl');
+        $escapeHtml = $this->getView()->plugin('escapeHtml');
+        $escapeHtmlAttr = $this->getView()->plugin('escapeHtmlAttr');
+        $callback = function ($url, $caption, $isEmail) use ($proxyUrl, $escapeHtml,
+            $escapeHtmlAttr
+        ) {
+            return '<a href="' . $escapeHtmlAttr($proxyUrl($url)) . '">'
+                . $escapeHtml($caption) . '</a>';
+        };
+        return $linkify->process($str, ['callback' => $callback]);
+    }
+}
diff --git a/themes/bootstrap3/templates/RecordTab/holdingsils.phtml b/themes/bootstrap3/templates/RecordTab/holdingsils.phtml
index fefcb15662d..8c4f33b0b27 100644
--- a/themes/bootstrap3/templates/RecordTab/holdingsils.phtml
+++ b/themes/bootstrap3/templates/RecordTab/holdingsils.phtml
@@ -86,7 +86,7 @@
       <th><?=$textFieldName == 'summary' ? $this->transEsc("Volume Holdings") : $this->transEsc(ucfirst($textFieldName))?>:</th>
       <td>
         <?php foreach ($textFields as $current): ?>
-          <?=$this->escapeHtml($current)?><br/>
+          <?=$this->linkify($this->escapeHtml($current))?><br/>
         <?php endforeach; ?>
       </td>
     </tr>
diff --git a/themes/root/theme.config.php b/themes/root/theme.config.php
index 0f2215e799b..f8d4915c908 100644
--- a/themes/root/theme.config.php
+++ b/themes/root/theme.config.php
@@ -30,6 +30,7 @@ return [
             'VuFind\View\Helper\Root\Ils' => 'VuFind\View\Helper\Root\IlsFactory',
             'VuFind\View\Helper\Root\JsTranslations' => 'VuFind\View\Helper\Root\JsTranslationsFactory',
             'VuFind\View\Helper\Root\KeepAlive' => 'VuFind\View\Helper\Root\KeepAliveFactory',
+            'VuFind\View\Helper\Root\Linkify' => 'Zend\ServiceManager\Factory\InvokableFactory',
             'VuFind\View\Helper\Root\LocalizedNumber' => 'Zend\ServiceManager\Factory\InvokableFactory',
             'VuFind\View\Helper\Root\OpenUrl' => 'VuFind\View\Helper\Root\OpenUrlFactory',
             'VuFind\View\Helper\Root\Permission' => 'VuFind\View\Helper\Root\PermissionFactory',
@@ -90,6 +91,7 @@ return [
             'ils' => 'VuFind\View\Helper\Root\Ils',
             'jsTranslations' => 'VuFind\View\Helper\Root\JsTranslations',
             'keepAlive' => 'VuFind\View\Helper\Root\KeepAlive',
+            'linkify' => 'VuFind\View\Helper\Root\Linkify',
             'localizedNumber' => 'VuFind\View\Helper\Root\LocalizedNumber',
             'openUrl' => 'VuFind\View\Helper\Root\OpenUrl',
             'permission' => 'VuFind\View\Helper\Root\Permission',
-- 
GitLab