From b4b0a66654b44904bb4758c6dc4291b9d492412f Mon Sep 17 00:00:00 2001
From: Andreas Gohr <andi@splitbrain.org>
Date: Sun, 5 Feb 2017 07:41:35 +0100
Subject: [PATCH] proper action handling for pagetools

The whole pagetool items are now build in an extra helper class that can
be reused by plugins.
---
 lib/tpl/dokuwiki/detail.php                   |  4 +-
 .../tools/11-mediamanager_folder-image.svg    |  1 +
 .../images/tools/12-back_arrow-left.svg       |  1 +
 lib/tpl/dokuwiki/main.php                     | 12 +--
 lib/tpl/dokuwiki/tpl.php                      | 91 +++++++++++++++++++
 5 files changed, 101 insertions(+), 8 deletions(-)
 create mode 100644 lib/tpl/dokuwiki/images/tools/11-mediamanager_folder-image.svg
 create mode 100644 lib/tpl/dokuwiki/images/tools/12-back_arrow-left.svg
 create mode 100644 lib/tpl/dokuwiki/tpl.php

diff --git a/lib/tpl/dokuwiki/detail.php b/lib/tpl/dokuwiki/detail.php
index c26d81cb7..ad957d3b8 100644
--- a/lib/tpl/dokuwiki/detail.php
+++ b/lib/tpl/dokuwiki/detail.php
@@ -96,8 +96,8 @@ header('X-UA-Compatible: IE=edge,chrome=1');
                                 $data = array(
                                     'view' => 'detail',
                                     'items' => array(
-                                        'mediaManager' => tpl_action('mediaManager', true, 'li', true, '<span>', '</span>'),
-                                        'img_backto' =>   tpl_action('img_backto',   true, 'li', true, '<span>', '</span>'),
+                                        'mediaManager' => dokuwiki\template\dokuwiki\tpl::pageToolAction('mediaManager'),
+                                        'img_backto' =>   dokuwiki\template\dokuwiki\tpl::pageToolAction('img_backto'),
                                     )
                                 );
 
diff --git a/lib/tpl/dokuwiki/images/tools/11-mediamanager_folder-image.svg b/lib/tpl/dokuwiki/images/tools/11-mediamanager_folder-image.svg
new file mode 100644
index 000000000..51c5380fd
--- /dev/null
+++ b/lib/tpl/dokuwiki/images/tools/11-mediamanager_folder-image.svg
@@ -0,0 +1 @@
+<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="24" height="24" viewBox="0 0 24 24"><path d="M5,17L9.5,11L13,15.5L15.5,12.5L19,17M20,6H12L10,4H4A2,2 0 0,0 2,6V18A2,2 0 0,0 4,20H20A2,2 0 0,0 22,18V8A2,2 0 0,0 20,6Z" /></svg>
\ No newline at end of file
diff --git a/lib/tpl/dokuwiki/images/tools/12-back_arrow-left.svg b/lib/tpl/dokuwiki/images/tools/12-back_arrow-left.svg
new file mode 100644
index 000000000..72f5e6ddb
--- /dev/null
+++ b/lib/tpl/dokuwiki/images/tools/12-back_arrow-left.svg
@@ -0,0 +1 @@
+<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="24" height="24" viewBox="0 0 24 24"><path d="M20,11V13H8L13.5,18.5L12.08,19.92L4.16,12L12.08,4.08L13.5,5.5L8,11H20Z" /></svg>
\ No newline at end of file
diff --git a/lib/tpl/dokuwiki/main.php b/lib/tpl/dokuwiki/main.php
index 006468e38..01f77be25 100644
--- a/lib/tpl/dokuwiki/main.php
+++ b/lib/tpl/dokuwiki/main.php
@@ -77,12 +77,12 @@ $showSidebar = $hasSidebar && ($ACT=='show');
                             $data = array(
                                 'view'  => 'main',
                                 'items' => array(
-                                    'edit'      => tpl_action('edit',      true, 'li', true, '<span>', '</span>'.inlinSVG(__DIR__ . '/images/tools/01-edit_pencil.svg')),
-                                    'revert'    => tpl_action('revert',    true, 'li', true, '<span>', '</span>'.inlinSVG(__DIR__ . '/images/tools/06-revert_replay.svg')),
-                                    'revisions' => tpl_action('revisions', true, 'li', true, '<span>', '</span>'.inlinSVG(__DIR__ . '/images/tools/07-revisions_history.svg')),
-                                    'backlink'  => tpl_action('backlink',  true, 'li', true, '<span>', '</span>'.inlinSVG(__DIR__ . '/images/tools/08-backlink_link-variant.svg')),
-                                    'subscribe' => tpl_action('subscribe', true, 'li', true, '<span>', '</span>'.inlinSVG(__DIR__ . '/images/tools/09-subscribe_email-outline.svg')),
-                                    'top'       => tpl_action('top',       true, 'li', true, '<span>', '</span>'.inlinSVG(__DIR__ . '/images/tools/10-top_arrow-up.svg'))
+                                    'edit'      => dokuwiki\template\dokuwiki\tpl::pageToolAction('edit'),
+                                    'revert'    => dokuwiki\template\dokuwiki\tpl::pageToolAction('revert'),
+                                    'revisions' => dokuwiki\template\dokuwiki\tpl::pageToolAction('revisions'),
+                                    'backlink'  => dokuwiki\template\dokuwiki\tpl::pageToolAction('backlink'),
+                                    'subscribe' => dokuwiki\template\dokuwiki\tpl::pageToolAction('subscribe'),
+                                    'top'       => dokuwiki\template\dokuwiki\tpl::pageToolAction('top'),
                                 )
                             );
 
diff --git a/lib/tpl/dokuwiki/tpl.php b/lib/tpl/dokuwiki/tpl.php
new file mode 100644
index 000000000..e7c0b7fd9
--- /dev/null
+++ b/lib/tpl/dokuwiki/tpl.php
@@ -0,0 +1,91 @@
+<?php
+
+namespace dokuwiki\template\dokuwiki;
+
+/**
+ * Class tpl
+ *
+ * Provides additional template functions for the dokuwiki template
+ * @package dokuwiki\tpl\dokuwiki
+ */
+class tpl {
+
+    static $icons = array(
+        'default' => '00-default_checkbox-blank-circle-outline.svg',
+        'edit' => '01-edit_pencil.svg',
+        'create' => '02-create_pencil.svg',
+        'draft' => '03-draft_android-studio.svg',
+        'show' => '04-show_file-document.svg',
+        'source' => '05-source_file-xml.svg',
+        'revert' => '06-revert_replay.svg',
+        'revs' => '07-revisions_history.svg',
+        'backlink' => '08-backlink_link-variant.svg',
+        'subscribe' => '09-subscribe_email-outline.svg',
+        'top' => '10-top_arrow-up.svg',
+        'mediaManager' => '11-mediamanager_folder-image.svg',
+        'img_backto' => '12-back_arrow-left.svg',
+
+    );
+
+    /**
+     * Return the HTML for one of the default actions
+     *
+     * Reimplements parts of tpl_actionlink
+     *
+     * @param string $action
+     * @return string
+     */
+    static public function pageToolAction($action) {
+        $data = tpl_get_action($action);
+        if(!is_array($data)) return '';
+        global $lang;
+
+        if($data['id'][0] == '#') {
+            $linktarget = $data['id'];
+        } else {
+            $linktarget = wl($data['id'], $data['params']);
+        }
+        $caption = $lang['btn_' . $data['type']];
+        if(strpos($caption, '%s')) {
+            $caption = sprintf($caption, $data['replacement']);
+        }
+
+        $svg = __DIR__ . '/images/tools/' . self::$icons[$data['type']];
+
+        return self::pageToolItem(
+            $linktarget,
+            $caption,
+            $svg,
+            $data['accesskey']
+        );
+    }
+
+    /**
+     * Return the HTML for a page action
+     *
+     * Plugins could use this
+     *
+     * @param string $link The link
+     * @param string $caption The label of the action
+     * @param string $svg The icon to show
+     * @param string $key Accesskey
+     * @return string
+     */
+    static public function pageToolItem($link, $caption, $svg, $key = '') {
+        $title = $caption;
+        if($key) {
+            $title .= ' [' . strtoupper($key) . ']';
+            $key = 'accesskey="' . $key . '"';
+        }
+
+        $svg = inlinSVG($svg);
+        if(!$svg) $svg = inlinSVG(__DIR__ . '/images/tools/' . self::$icons['default']);
+
+        $out = '<li>';
+        $out .= '<a href="' . $link . '" title="' . hsc($title) . '" rel="nofollow" ' . $key . '>';
+        $out .= '<span>' . hsc($caption) . '</span>';
+        $out .= $svg;
+        $out .= '</li>';
+        return $out;
+    }
+}
-- 
GitLab