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// must be run within Dokuwiki
17if(!defined('DOKU_INC')) die();
18
19if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
20require_once(DOKU_PLUGIN.'action.php');
21
22class action_plugin_button extends DokuWiki_Action_Plugin {
23
24    function register(Doku_Event_Handler $controller){
25        $controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'handle_toolbar', array ());
26        $controller->register_hook('PLUGIN_MOVE_HANDLERS_REGISTER', 'BEFORE', $this, 'handleBeforePageMove', array());
27    }
28
29    function handle_toolbar(&$event, $param) {
30        $syntaxDiv = $this->getConf('syntaxDiv');
31        $syntaxSpan = $this->getConf('syntaxSpan');
32
33        $event->data[] = array (
34            'type' => 'format',
35            'title' => 'Insert button',
36            'icon' => '../../plugins/button/images/add-button.png',
37            'open' => '[[{}',
38            'close' => ']]',
39            'sample' => 'Wiki link|Button title'
40        );
41    }
42
43    public function handleBeforePageMove(Doku_Event $event, $param){
44        $event->data['handlers']['button'] = array($this, 'rewrite_button');
45    }
46
47    function move_newid($handler, $page, $type)
48    {
49        if (method_exists($handler, 'adaptRelativeId')) { // move plugin before version 2015-05-16
50            $newpage = $handler->adaptRelativeId($page);
51        } else {
52            $newpage = $handler->resolveMoves($page, $type);
53            $newpage = $handler->relativeLink($page, $newpage, $type);
54        }
55        return $newpage;
56    }
57
58    public function rewrite_button($match, $state, $pos, $plugin, helper_plugin_move_handler $handler)
59    {
60        $returnValue = $match;
61
62        if($state !== DOKU_LEXER_ENTER) return $returnValue;
63
64		// 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...)
65		//if (($handler->id == $handler->origID) && ($handler->ns == $handler->origNS)) return $returnValue;
66
67        if (preg_match('/\[\[{(?<image>[^}\|]*)\|?(?<css>[^}]*)}(?<link>[^\]\|]*)\|?(?<title>[^\]]*)/', $match, $data))
68        {
69            // Skip syntaxes that should not be rewritten
70            if (($data['image'] != 'conf.styles') && ($data['image'] != 'conf.target') && $data['image']) {
71                $data['image'] = $this->move_newid($handler, $data['image'], 'media');
72            }
73            if($data['link']) { // Adapt link
74				$link_items=explode("#",$data['link']);
75                $link_items[0] = $this->move_newid($handler, $link_items[0], 'page');
76				$data['link'] = implode("#",$link_items);
77            }
78                // Rebuild button syntax
79                $returnValue="[[{" . $data['image'];
80                if ($data['css'])  $returnValue .= "|" . $data['css'];
81                $returnValue.="}";
82                $returnValue.=$data['link'];
83                if (substr($match,-1) == "|")  $returnValue.="|";
84                if ($data['title'])  $returnValue .= "|" . $data['title'];
85            }
86        //dbglog("REWRITE  : " . $match . "  ---->   " . $returnValue);
87        return $returnValue;
88    }
89}
90
91