xref: /plugin/combo/action/imgmove.php (revision 37748cd8654635afbeca80942126742f0f4cc346)
1<?php
2
3use ComboStrap\DokuPath;
4use ComboStrap\LinkUtility;
5use ComboStrap\LogUtility;
6use ComboStrap\Page;
7use ComboStrap\PluginUtility;
8
9if (!defined('DOKU_INC')) die();
10require_once(__DIR__ . '/../ComboStrap/PluginUtility.php');
11require_once(__DIR__ . '/../ComboStrap/LinkUtility.php');
12
13/**
14 * Handle the move of a image
15 */
16class action_plugin_combo_imgmove extends DokuWiki_Action_Plugin
17{
18
19    /**
20     * As explained https://www.dokuwiki.org/plugin:move
21     * @param Doku_Event_Handler $controller
22     */
23    function register(Doku_Event_Handler $controller)
24    {
25        $controller->register_hook('PLUGIN_MOVE_HANDLERS_REGISTER', 'BEFORE', $this, 'handle_move', array());
26    }
27
28    /**
29     * Handle the move of a image
30     * @param Doku_Event $event
31     * @param $params
32     */
33    function handle_move(Doku_Event $event, $params)
34    {
35        /**
36         * The handlers is the name of the component (ie refers to the {@link syntax_plugin_combo_media} handler)
37         * and 'move_combo_img' to the below method
38         */
39        $event->data['handlers'][syntax_plugin_combo_media::COMPONENT] = array($this, 'move_combo_img');
40        $event->data['handlers'][syntax_plugin_combo_frontmatter::COMPONENT] = array($this, 'move_combo_frontmatter_img');
41    }
42
43    /**
44     *
45     * @param $match
46     * @param $state
47     * @param $pos
48     * @param $plugin
49     * @param helper_plugin_move_handler $handler
50     */
51    public function move_combo_img($match, $state, $pos, $plugin, helper_plugin_move_handler $handler)
52    {
53        /**
54         * The original move method
55         * is {@link helper_plugin_move_handler::media()}
56         *
57         */
58        $handler->media($match, $state, $pos);
59
60    }
61
62    /**
63     *
64     * @param $match
65     * @param $state
66     * @param $pos
67     * @param $plugin
68     * @param helper_plugin_move_handler $handler
69     * @return string
70     */
71    public function move_combo_frontmatter_img($match, $state, $pos, $plugin, helper_plugin_move_handler $handler)
72    {
73        /**
74         * The original move method
75         * is {@link helper_plugin_move_handler::media()}
76         *
77         */
78        $jsonArray = syntax_plugin_combo_frontmatter::FrontMatterMatchToAssociativeArray($match);
79        if ($jsonArray === null) {
80            return $match;
81        } else {
82
83            if (!isset($jsonArray[Page::IMAGE_META_PROPERTY])) {
84                return $match;
85            }
86
87            try {
88                $images = &$jsonArray[Page::IMAGE_META_PROPERTY];
89                if (is_array($images)) {
90                    foreach ($images as &$subImage) {
91                        if (is_array($subImage)) {
92                            foreach($subImage as &$subSubImage){
93                                if(is_string($subSubImage)) {
94                                    $this->moveImage($subSubImage, $handler);
95                                } else {
96                                    LogUtility::msg("The image frontmatter value (".hsc(var_export($subSubImage))." is not a string and cannot be therefore moved", LogUtility::LVL_MSG_ERROR,syntax_plugin_combo_frontmatter::METADATA_IMAGE_CANONICAL);
97                                    return $match;
98                                }
99                            }
100                        } else {
101                            $this->moveImage( $subImage, $handler);
102                        }
103                    }
104                } else {
105                    $this->moveImage($images, $handler);
106                }
107            } catch(Exception $e){
108                // Could not resolve the image, return the data without modification
109                return $match;
110            }
111
112            $jsonEncode = json_encode($jsonArray, JSON_PRETTY_PRINT);
113            if ($jsonEncode === false) {
114                LogUtility::msg("A move error has occurred while trying to store the modified metadata as json (" . hsc(var_export($images, true)) . ")", LogUtility::LVL_MSG_ERROR);
115                return $match;
116            }
117            $frontmatterStartTag = syntax_plugin_combo_frontmatter::START_TAG;
118            $frontmatterEndTag = syntax_plugin_combo_frontmatter::END_TAG;
119
120            /**
121             * All good,
122             * We don't modify the metadata for the page
123             * because the handler does not give it unfortunately
124             */
125
126            /**
127             * Return the match modified
128             */
129            return <<<EOF
130$frontmatterStartTag
131$jsonEncode
132$frontmatterEndTag
133EOF;
134
135        }
136
137    }
138
139    /**
140     * Move a single image and update the JSon
141     * @param $value
142     * @param helper_plugin_move_handler $handler
143     * @throws Exception on bad argument
144     */
145    private function moveImage(&$value, $handler)
146    {
147        try {
148            $newId = $handler->resolveMoves($value, "media");
149            $value = DokuPath::IdToAbsolutePath($newId);
150        } catch (Exception $e) {
151            LogUtility::msg("A move error has occurred while trying to move the image ($value). The target resolution function send the following error message: " . $e->getMessage(), LogUtility::LVL_MSG_ERROR);
152            throw new RuntimeException();
153        }
154    }
155
156
157}
158