1<?php
2
3namespace dokuwiki\plugin\gallery;
4
5use dokuwiki\plugin\prosemirror\parser\Node;
6
7/**
8 * Gallery Node in Prosemirror editor
9 */
10class GalleryNode extends Node
11{
12    protected $parent;
13    protected $data;
14
15    /**
16     * GalleryNode constructor.
17     *
18     * Todo: This constructor will likely by abstracted away into a parent class or something at somepoint.
19     *
20     * @param $data
21     * @param {Node} $parent
22     */
23    public function __construct($data, Node $parent)
24    {
25        $this->parent = &$parent;
26        $this->data = $data;
27    }
28
29    /**
30     * Get the node's representation as DokuWiki Syntax
31     *
32     * @return string
33     */
34    public function toSyntax()
35    {
36        /** @var \action_plugin_gallery_prosemirror $action */
37        $action = plugin_load('action', 'gallery_prosemirror');
38        $defaults = $action->getDefaults();
39        $query = [];
40        $attrs = $this->data['attrs'];
41        if ($attrs['thumbnailsize'] !== $defaults['thumbnailsize']) {
42            $query[] = $attrs['thumbnailsize'];
43        }
44        if ($attrs['imagesize'] !== $defaults['imagesize']) {
45            $query[] = $attrs['imagesize'];
46        }
47
48        $query[] = $this->extractFlagParam('showname', $attrs, $defaults);
49        $query[] = $this->extractFlagParam('showtitle', $attrs, $defaults);
50        $query[] = $this->extractFlagParam('cache', $attrs, $defaults);
51        $query[] = $this->extractFlagParam('crop', $attrs, $defaults);
52        $query[] = $this->extractFlagParam('direct', $attrs, $defaults);
53        $query[] = $this->extractFlagParam('lightbox', $attrs, $defaults);
54        $query[] = $this->extractFlagParam('reverse', $attrs, $defaults);
55        $query[] = $this->extractFlagParam('recursive', $attrs, $defaults);
56        $query = array_filter($query);
57
58        if ((int)$attrs['cols'] !== (int)$defaults['cols']) {
59            $query[] = $attrs['cols'];
60        }
61        if ((int)$attrs['limit'] !== (int)$defaults['limit']) {
62            $query[] = '=' . $attrs['limit'];
63        }
64        if ((int)$attrs['offset'] !== (int)$defaults['offset']) {
65            $query[] = '+' . $attrs['offset'];
66        }
67        if ((int)$attrs['paginate'] !== (int)$defaults['paginate']) {
68            $query[] = '~' . $attrs['paginate'];
69        }
70        if ((int)$attrs['sort'] !== (int)$defaults['sort']) {
71            $query[] = $attrs['sort'];
72        }
73        if ($attrs['filter'] && strpos($attrs['filter'], '*') !== false) {
74            $query[] = $attrs['filter'];
75        }
76        $alignLeft = $attrs['align'] === 'left' ? ' ' : '';
77        $alignRight = $attrs['align'] === 'right' ? ' ' : '';
78        $result = '{{gallery>' . $alignRight . $attrs['namespace'];
79        if ($query !== []) {
80            $result .= '?' . implode('&', $query);
81        }
82        $result .= $alignLeft . '}}';
83        return $result;
84    }
85
86    /**
87     * Get syntax option-string if the option is different from the default
88     *
89     * @param string $paramName The name of the parameter
90     * @param array $data The node's attributes from the editor
91     * @param array $defaults The default options
92     *
93     * @return null|string Either the string to toggle the option away from the default or null
94     */
95    protected function extractFlagParam($paramName, $data, $defaults)
96    {
97        if ((bool)$data[$paramName] !== (bool)$defaults[$paramName]) {
98            return ($defaults[$paramName] ? 'no' : '') . $paramName;
99        }
100        return null;
101    }
102}
103