1<?php
2/**
3 * Action Component for the Button Plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Xavier Decuyper <xavier.decuyper@gmail.com>
7 *
8 *
9 * @author     ThisNameIsNotAllowed
10 * 17/11/2016 : Extended for usage with the move plugin (Added eventhandler and callback)
11 *
12 * @author 	Remi Peyronnet
13 * 19/11/2016 : rewrote move plugin handler to work with all button syntaxes
14 */
15
16/*
17// Thanks to #28 : autoloaded & now deprecated (https://www.dokuwiki.org/devel:releases:refactor2020)
18
19// must be run within Dokuwiki
20if(!defined('DOKU_INC')) die();
21
22if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
23require_once(DOKU_PLUGIN.'action.php');
24*/
25
26class action_plugin_button extends DokuWiki_Action_Plugin {
27
28    function register(Doku_Event_Handler $controller){
29        $controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'handle_toolbar', array ());
30        $controller->register_hook('PLUGIN_MOVE_HANDLERS_REGISTER', 'BEFORE', $this, 'handleBeforePageMove', array());
31    }
32
33    function handle_toolbar(&$event, $param) {
34        $syntaxDiv = $this->getConf('syntaxDiv');
35        $syntaxSpan = $this->getConf('syntaxSpan');
36
37        $event->data[] = array (
38            'type' => 'format',
39            'title' => 'Insert button',
40            'icon' => '../../plugins/button/images/add-button.png',
41            'open' => '[[{}',
42            'close' => ']]',
43            'sample' => 'Wiki link|Button title'
44        );
45    }
46
47    public function handleBeforePageMove(Doku_Event $event, $param){
48        $event->data['handlers']['button'] = array($this, 'rewrite_button');
49    }
50
51    function move_newid($handler, $page, $type)
52    {
53        if (method_exists($handler, 'adaptRelativeId')) { // move plugin before version 2015-05-16
54            $newpage = $handler->adaptRelativeId($page);
55        } else {
56            $newpage = $handler->resolveMoves($page, $type);
57            $newpage = $handler->relativeLink($page, $newpage, $type);
58        }
59        return $newpage;
60    }
61
62    public function rewrite_button($match, $state, $pos, $plugin, helper_plugin_move_handler $handler)
63    {
64        $returnValue = $match;
65
66        if($state !== DOKU_LEXER_ENTER) return $returnValue;
67
68		// If same identifier do nothing  (do not work, have not found how to detect if it is a real move or only a new display...)
69		//if (($handler->id == $handler->origID) && ($handler->ns == $handler->origNS)) return $returnValue;
70
71        if (preg_match('/\[\[{(?<image>[^}\|]*)\|?(?<css>[^}]*)}(?<link>[^\]\|]*)\|?(?<title>[^\]]*)/', $match, $data))
72        {
73            // Skip syntaxes that should not be rewritten
74            if (($data['image'] != 'conf.styles') && ($data['image'] != 'conf.target') && $data['image']) {
75                $data['image'] = $this->move_newid($handler, $data['image'], 'media');
76            }
77            if($data['link']) { // Adapt link
78				$link_items=explode("#",$data['link']);
79                $link_items[0] = $this->move_newid($handler, $link_items[0], 'page');
80				$data['link'] = implode("#",$link_items);
81            }
82                // Rebuild button syntax
83                $returnValue="[[{" . $data['image'];
84                if ($data['css'])  $returnValue .= "|" . $data['css'];
85                $returnValue.="}";
86                $returnValue.=$data['link'];
87                if (substr($match,-1) == "|")  $returnValue.="|";
88                if ($data['title'])  $returnValue .= "|" . $data['title'];
89            }
90        //dbglog("REWRITE  : " . $match . "  ---->   " . $returnValue);
91        return $returnValue;
92    }
93}
94
95