From becfa414b5b024ded4e094b1c113a72f39d8b763 Mon Sep 17 00:00:00 2001
From: Gerrit Uitslag <klapinklapin@gmail.com>
Date: Tue, 25 Feb 2014 15:45:00 +0100
Subject: [PATCH] refactor detail.php to template.php

- refactor detail en mediamanager link creation
- refactor metadata listing
---
 conf/mediameta.php                  |  2 +-
 inc/lang/en/lang.php                |  4 +-
 inc/template.php                    | 98 +++++++++++++++++++++++++++--
 lib/tpl/dokuwiki/css/pagetools.less |  1 +
 lib/tpl/dokuwiki/detail.php         | 54 +++-------------
 5 files changed, 105 insertions(+), 54 deletions(-)

diff --git a/conf/mediameta.php b/conf/mediameta.php
index 0428a4b88..f75fa0821 100644
--- a/conf/mediameta.php
+++ b/conf/mediameta.php
@@ -5,7 +5,7 @@
  * following contents:
  *   fieldname - Where data will be saved (EXIF or IPTC field)
  *   label     - key to lookup in the $lang var, if not found printed as is
- *   htmltype  - 'text' or 'textarea'
+ *   htmltype  - 'text', 'textarea' or 'date'
  *   lookups   - array additional fields to lookup the data (EXIF or IPTC fields)
  *
  * The fields are not ordered continously to make inserting additional items
diff --git a/inc/lang/en/lang.php b/inc/lang/en/lang.php
index cbdef8661..e945341d7 100644
--- a/inc/lang/en/lang.php
+++ b/inc/lang/en/lang.php
@@ -52,6 +52,8 @@ $lang['btn_register']          = 'Register';
 $lang['btn_apply']             = 'Apply';
 $lang['btn_media']             = 'Media Manager';
 $lang['btn_deleteuser']        = 'Remove My Account';
+$lang['btn_img_backto']        = 'Back to %s';
+$lang['btn_mediaManager']      = 'View in media manager';
 
 $lang['loggedinas']            = 'Logged in as';
 $lang['user']                  = 'Username';
@@ -253,7 +255,6 @@ $lang['admin_register']        = 'Add new user';
 $lang['metaedit']              = 'Edit Metadata';
 $lang['metasaveerr']           = 'Writing metadata failed';
 $lang['metasaveok']            = 'Metadata saved';
-$lang['img_backto']            = 'Back to';
 $lang['img_title']             = 'Title';
 $lang['img_caption']           = 'Caption';
 $lang['img_date']              = 'Date';
@@ -266,7 +267,6 @@ $lang['img_camera']            = 'Camera';
 $lang['img_keywords']          = 'Keywords';
 $lang['img_width']             = 'Width';
 $lang['img_height']            = 'Height';
-$lang['img_manager']           = 'View in media manager';
 
 $lang['subscr_subscribe_success']   = 'Added %s to subscription list for %s';
 $lang['subscr_subscribe_error']     = 'Error adding %s to subscription list for %s';
diff --git a/inc/template.php b/inc/template.php
index 0a6a9e4aa..aa3c658a8 100644
--- a/inc/template.php
+++ b/inc/template.php
@@ -548,6 +548,7 @@ function tpl_actionlink($type, $pre = '', $suf = '', $inner = '', $return = fals
          * @var string $method
          * @var bool   $nofollow
          * @var array  $params
+         * @var string $replacement
          */
         extract($data);
         if(strpos($id, '#') === 0) {
@@ -556,6 +557,9 @@ function tpl_actionlink($type, $pre = '', $suf = '', $inner = '', $return = fals
             $linktarget = wl($id, $params);
         }
         $caption = $lang['btn_'.$type];
+        if(strpos($caption, '%s')){
+            $caption = sprintf($caption, $replacement);
+        }
         $akey    = $addTitle = '';
         if($accesskey) {
             $akey     = 'accesskey="'.$accesskey.'" ';
@@ -609,11 +613,12 @@ function tpl_get_action($type) {
     if ($type == 'subscription') $type = 'subscribe';
     if(!actionOK($type)) return false;
 
-    $accesskey = null;
-    $id        = $ID;
-    $method    = 'get';
-    $params    = array('do' => $type);
-    $nofollow  = true;
+    $accesskey   = null;
+    $id          = $ID;
+    $method      = 'get';
+    $params      = array('do' => $type);
+    $nofollow    = true;
+    $replacement = '';
     switch($type) {
         case 'edit':
             // most complicated type - we need to decide on current action
@@ -670,6 +675,11 @@ function tpl_get_action($type) {
             $params    = array();
             $accesskey = 'b';
             break;
+        case 'img_backto':
+            $params = array();
+            $accesskey = 'b';
+            $replacement = $ID;
+            break;
         case 'login':
             $params['sectok'] = getSecurityToken();
             if(isset($_SERVER['REMOTE_USER'])) {
@@ -717,11 +727,26 @@ function tpl_get_action($type) {
         case 'media':
             $params['ns'] = getNS($ID);
             break;
+        case 'mediaManager':
+            // View image in media manager
+            global $IMG;
+            $imgNS = getNS($IMG);
+            $authNS = auth_quickaclcheck("$imgNS:*");
+            if ($authNS < AUTH_UPLOAD) {
+                return false;
+            }
+            $params = array(
+                'ns' => $imgNS,
+                'image' => $IMG,
+                'do' => 'media'
+            );
+            //$type = 'media';
+            break;
         default:
             return '[unknown %s type]';
             break;
     }
-    return compact('accesskey', 'type', 'id', 'method', 'params', 'nofollow');
+    return compact('accesskey', 'type', 'id', 'method', 'params', 'nofollow', 'replacement');
 }
 
 /**
@@ -1016,6 +1041,67 @@ function tpl_img_getTag($tags, $alt = '', $src = null) {
     return $info;
 }
 
+/**
+ * Returns a description list of the metatags of the current image
+ *
+ * @return string html of description list
+ */
+function tpl_img_meta() {
+    global $lang;
+
+    $tags = tpl_get_img_meta();
+
+    echo '<dl>';
+    foreach($tags as $tag) {
+        $label = $lang[$tag['langkey']];
+        if(!$label) $label = $tag['langkey'];
+
+        echo '<dt>'.$label.':</dt><dd>';
+        if ($tag['type'] == 'date') {
+            echo dformat($tag['value']);
+        } else {
+            echo hsc($tag['value']);
+        }
+        echo '</dd>';
+    }
+    echo '</dl>';
+}
+
+/**
+ * Returns metadata as configured in mediameta config file, ready for creating html
+ *
+ * @return array with arrays containing the entries:
+ *   - string langkey  key to lookup in the $lang var, if not found printed as is
+ *   - string type     type of value
+ *   - string value    tag value (unescaped)
+ */
+function tpl_get_img_meta() {
+
+    $config_files = getConfigFiles('mediameta');
+    foreach ($config_files as $config_file) {
+        if(@file_exists($config_file)) {
+            include($config_file);
+        }
+    }
+    /** @var array $fields the included array with metadata */
+
+    $tags = array();
+    foreach($fields as $tag){
+        $t = array();
+        if (!empty($tag[0])) {
+            $t = array($tag[0]);
+        }
+        if(is_array($tag[3])) {
+            $t = array_merge($t,$tag[3]);
+        }
+        $value = tpl_img_getTag($t);
+        if ($value) {
+            $tags[] = array('langkey' => $tag[1], 'type' => $tag[2], 'value' => $value);
+        }
+    }
+    return $tags;
+}
+
 /**
  * Prints the image with a link to the full sized version
  *
diff --git a/lib/tpl/dokuwiki/css/pagetools.less b/lib/tpl/dokuwiki/css/pagetools.less
index ecb3038c3..77d2670a6 100644
--- a/lib/tpl/dokuwiki/css/pagetools.less
+++ b/lib/tpl/dokuwiki/css/pagetools.less
@@ -231,3 +231,4 @@
 .pagetools-item(subscribe, 9, subscribe);
 .pagetools-item(mediaManager, 11);
 .pagetools-item(back, 12);
+.pagetools-item(img_backto, 12);
diff --git a/lib/tpl/dokuwiki/detail.php b/lib/tpl/dokuwiki/detail.php
index ec846f6fd..8fe2c88a2 100644
--- a/lib/tpl/dokuwiki/detail.php
+++ b/lib/tpl/dokuwiki/detail.php
@@ -55,37 +55,7 @@ header('X-UA-Compatible: IE=edge,chrome=1');
                         <?php tpl_img(900,700); /* parameters: maximum width, maximum height (and more) */ ?>
 
                         <div class="img_detail">
-                            <dl>
-                                <?php
-                                    // @todo: logic should be transferred to backend
-                                    $config_files = getConfigFiles('mediameta');
-                                    foreach ($config_files as $config_file) {
-                                        if(@file_exists($config_file)) {
-                                            include($config_file);
-                                        }
-                                    }
-
-                                    foreach($fields as $key => $tag){
-                                        $t = array();
-                                        if (!empty($tag[0])) {
-                                            $t = array($tag[0]);
-                                        }
-                                        if(is_array($tag[3])) {
-                                            $t = array_merge($t,$tag[3]);
-                                        }
-                                        $value = tpl_img_getTag($t);
-                                        if ($value) {
-                                            echo '<dt>'.$lang[$tag[1]].':</dt><dd>';
-                                            if ($tag[2] == 'date') {
-                                                echo dformat($value);
-                                            } else {
-                                                echo hsc($value);
-                                            }
-                                            echo '</dd>';
-                                        }
-                                    }
-                                ?>
-                            </dl>
+                            <?php tpl_img_meta(); ?>
                         </div>
                         <?php //Comment in for Debug// dbg(tpl_img_getTag('Simple.Raw'));?>
                     <?php endif; ?>
@@ -109,23 +79,17 @@ header('X-UA-Compatible: IE=edge,chrome=1');
                     <div class="tools">
                         <ul>
                             <?php
-                                $data = array();
-                                $data['view'] = 'detail';
-
-                                // View in media manager; @todo: transfer logic to backend
-                                $imgNS = getNS($IMG);
-                                $authNS = auth_quickaclcheck("$imgNS:*");
-                                if (($authNS >= AUTH_UPLOAD) && function_exists('media_managerURL')) {
-                                    $mmURL = media_managerURL(array('ns' => $imgNS, 'image' => $IMG));
-                                    $data['items']['mediaManager'] = '<li><a href="'.$mmURL.'" class="mediaManager"><span>'.$lang['img_manager'].'</span></a></li>';
-                                }
-
-                                // Back to [ID]; @todo: transfer logic to backend
-                                $data['items']['img_backto'] = '<li><a href="'.wl($ID).'" class="back"><span>'.$lang['img_backto'].' '.$ID.'</span></a></li>';
+                                $data = array(
+                                    'view' => 'detail',
+                                    'items' => array(
+                                        'mediaManager' => tpl_action('mediaManager', 1, 'li', 1, '<span>', '</span>'),
+                                        'img_backto' =>   tpl_action('img_backto',   1, 'li', 1, '<span>', '</span>'),
+                                    )
+                                );
 
                                 // the page tools can be amended through a custom plugin hook
                                 $evt = new Doku_Event('TEMPLATE_PAGETOOLS_DISPLAY', $data);
-                                if($evt->advise_before()){
+                                if($evt->advise_before()) {
                                     foreach($evt->data['items'] as $k => $html) echo $html;
                                 }
                                 $evt->advise_after();
-- 
GitLab