1<?php
2/**
3 * ImageBox's action component for supporting Move plugin.
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     FFTiger <fftiger@wikisquare.com>, myst6re <myst6re@wikisquare.com>
7 */
8
9class action_plugin_imagebox extends DokuWiki_Action_Plugin {
10
11    function register(Doku_Event_handler $controller) {
12        $controller->register_hook('PLUGIN_MOVE_HANDLERS_REGISTER', 'BEFORE', $this, 'handle_move_register');
13    }
14
15    public function handle_move_register(Doku_Event $event, $params) {
16        $event->data['handlers']['imagebox'] = array($this, 'rewrite_imagebox');
17    }
18
19    public function rewrite_imagebox($match, $state, $pos, $plugin, helper_plugin_move_handler $handler) {
20
21        // Only work on enter pattern. (Do not change description and exit pattern.)
22        if (substr($match, 0, 3) != '[{{') return $match;
23
24        // Get pure syntax without markup.
25        if (substr($match, -1) == '|') {
26            $syntax = substr($match, 3, -1);
27        } else {
28            $syntax = substr($match, 3);
29        }
30
31        $left_blank = false;
32        $right_blank = false;
33        if (substr($syntax, 0, 1) == ' ') {
34            $left_blank = true;
35            $syntax = substr($syntax, 1);
36        }
37        if (substr($syntax, -1) == ' ') {
38            $right_blank = true;
39            $syntax = substr($syntax, 0, -1);
40        }
41
42        list($src, $option) = array_pad(explode('?', $syntax, 2), 2, '');
43
44        // Resolve new source.
45        if (method_exists($handler, 'adaptRelativeId')) {
46            $new_src = $handler->adaptRelativeId($src);
47        } else {
48            $new_src = $handler->resolveMoves($src, 'media');
49            $new_src = $handler->relativeLink($src, $new_src, 'media');
50        }
51
52        if ($src == $new_src) {
53            return $match;
54        } else {
55            // Construct result.
56            $result = '[{{';
57            if ($left_blank) $result .= ' ';
58            $result .= $new_src;
59            if ($option) $result .= "?".$option;
60            if ($right_blank) $result .= ' ';
61            $result .= "|";
62
63            return $result;
64        }
65
66    }
67
68}
69?>