aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/ContentGroupMenu.php
blob: 7f3ba5c9a2f0fbdf76bf6d06242b603d424b7aa9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
/**
 * This class represents the a more flexible menu used to group actions.
*
* @author  Timo Hartge <hartge@data-quest.de>
* @license GPL2 or any later version
* @since   Stud.IP 4.0
*/
class ContentGroupMenu
{
    const THRESHOLD = 1;

    const TEMPLATE_FILE_SINGLE   = 'shared/action-menu-single.php';
    const TEMPLATE_FILE_MULTIPLE = 'shared/action-menu.php';
    const TEMPLATE_FILE_ROWS     = 'shared/contentgroup-row.php';
    const TEMPLATE_FILE_TABLE    = 'shared/contentgroup-table.php';

    /**
     * Returns an instance.
     *
     * @return ContentGroupMenu
     */
    public static function get()
    {
        return new self();
    }

    private $actions       = [];
    private $condition_all = null;
    private $condition     = true;

    private $rows    = 1;
    private $columns = 1;
    private $image   = null;
    private $image_link_attributes = [];
    private $label;
    private $aria_label;

    private $attributes = [];


    /**
     * Private constructur.
     *
     * @see ContentGroupMenu::get()
     */
    private function __construct()
    {
        $this->label = _('Aktionen');
        $this->aria_label = _('Aktionsmenü');

        $this->addCSSClass('action-menu');
    }

    /**
     * Set condition for the next added item. If condition is false,
     * the item will not be added.
     *
     * @param bool $state State of the condition
     * @return ContentGroupMenu instance to allow chaining
     */
    public function condition($state)
    {
        $this->condition = (bool)$state;

        return $this;
    }

    /**
     * Set condition for all the next added items. If condition is false,
     * no items will be added.
     *
     * @param bool $state State of the condition
     * @return ContentGroupMenu instance to allow chaining
     */
    public function conditionAll($state)
    {
        $this->condition_all = $state;

        return $this;
    }

    /**
     * Checks the condition. Takes global and local (conditionAll() &
     * condition()) conditions into account.
     *
     * @return bool indicating whether the condition is met or not
     */
    protected function checkCondition()
    {
        $result = $this->condition;
        if ($this->condition_all !== null) {
            $result = $result && $this->condition_all;
        }

        $this->condition = true;

        return $result;
    }

    /**
     * Adds a link to the list of actions.
     *
     * @param String $link       Link target
     * @param String $label      Textual representation of the link
     * @param mixed  $icon       Optional icon (as Icon object)
     * @param array  $attributes Optional attributes to add to the <a> tag
     * @return ContentGroupMenu instance to allow chaining
     */
    public function addLink($link, $label, Icon $icon = null, array $attributes = [])
    {
        if ($this->checkCondition()) {
            $this->actions[] = [
                'type'       => 'link',
                'link'       => $link,
                'icon'       => $icon,
                'label'      => $label,
                'attributes' => $attributes,
            ];
        }

        return $this;
    }

    /**
     * Adds a button to the list of actions.
     *
     * @param String $name       Button name
     * @param String $label      Textual representation of the name
     * @param mixed  $icon       Optional icon (as Icon object)
     * @param array  $attributes Optional attributes to add to the <a> tag
     * @return ContentGroupMenu instance to allow chaining
     */
    public function addButton($name, $label, Icon $icon = null, array $attributes = [])
    {
        if ($this->checkCondition()) {
            $this->actions[] = [
                'type'       => 'button',
                'name'       => $name,
                'icon'       => $icon,
                'label'      => $label,
                'attributes' => $attributes,
            ];
        }

        return $this;
    }

    /**
     * Adds a MultiPersonSearch object to the list of actions.
     *
     * @param MultiPersonSearch $mp MultiPersonSearch object
     * @return ContentGroupMenu instance to allow chaining
     */
    public function addMultiPersonSearch(MultiPersonSearch $mp)
    {
        if ($this->checkCondition()) {
            $this->actions[] = [
                'type'   => 'multi-person-search',
                'object' => $mp,
            ];
        }

        return $this;
    }

    /**
     * Adds a css classs to the root element in html.
     *
     * @param string $class Name of the css class
     */
    public function addCSSClass($class)
    {
        $this->addAttribute('class', $class, true);
    }

    /**
     * Adds an attribute to the root element in html.
     *
     * @param string  $key    Name of the attribute
     * @param string  $value  Value of the attribute
     * @param boolean $append Whether a current value should be append or not.
     */
    public function addAttribute($key, $value, $append = false)
    {
        if (isset($this->attributes[$key]) && $append) {
            $this->attributes[$key] .= " {$value}";
        } else {
            $this->attributes[$key] = $value;
        }
    }

    /**
     * Renders the action menu. If no item was added, an empty string will
     * be returned. If a single item was added, the item itself will be
     * displayed. Otherwise the whole menu will be rendered.
     *
     * @return String containing the html representation of the action menu
     */
    public function render()
    {
        if (count($this->actions) === 0) {
            return '';
        }

        //if ($this->rows > 1 || $this->columns > 1 || $this->image) {
        if ($this->rows > 1 || $this->columns > 1) {
            $template_file = self::TEMPLATE_FILE_TABLE;
        } else {
            $template_file = self::TEMPLATE_FILE_ROWS;
        }
        $template = $GLOBALS['template_factory']->open($template_file);
        $template->actions = $this->actions;

        $has_link_icons = false;
        foreach ($this->actions as $action) {
            if (!empty($action['icon'])) {
                $has_link_icons = true;
                break;
            }
        }
        $template->has_link_icons = $has_link_icons;

        $template->rows    = $this->rows;
        $template->columns = $this->columns;

        if ($this->image) {
            $template->image = $this->image;
            $template->image_link_attributes = $this->image_link_attributes;
        } else {
            $template->image = "<div></div><div></div><div></div>";
        }

        $template->label = $this->label;
        $template->aria_label = $this->aria_label;

        $template->attributes = $this->attributes;

        /*} else {
            $template_file = count($this->actions) <= self::THRESHOLD
            ? self::TEMPLATE_FILE_SINGLE
            : self::TEMPLATE_FILE_MULTIPLE;
            $template = $GLOBALS['template_factory']->open($template_file);
            $template->actions = $this->actions;
        }*/

        return $template->render();
    }

    /**
     * Sets the number of rows to layout the elements.
     *
     * @param integer $rows number of rows
     */
    public function setRows($rows)
    {
        $this->rows = $rows;
    }

    /**
     * Sets the number of columns to layout the elements.
     *
     * @param integer $columns number of columns
     */
    public function setcolumns($columns)
    {
        $this->columns = $columns;
    }

    /**
     * Sets the icon of the menu.
     *
     * @param String $menu_image image html for the menu
     *
     * @param array $image_link_attributes Additional HTML attributes for the link that surrounds the image.
     */
    public function setIcon($menu_image, array $image_link_attributes = [])
    {
        $this->image = $menu_image;
        $this->image_link_attributes = $image_link_attributes;
    }

    /**
     * Sets the label of the menu.
     *
     * @param String $label label for the menu
     */
    public function setLabel($label)
    {
        $this->label = $label;
    }

    /**
     * Sets the label of the menu.
     *
     * @param String $label label for the menu
     */
    public function setAriaLabel($label)
    {
        $this->aria_label = $label;
    }

    /**
     * Return the number of menu actions.
     *
     * @return integer count actions
     */
    public function countLinks()
    {
        return count($this->actions);
    }
}