]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - plugins/demo_plugin/demo_plugin.php
Shaarli's translation
[github/shaarli/Shaarli.git] / plugins / demo_plugin / demo_plugin.php
1 <?php
2 /**
3 * Demo Plugin.
4 *
5 * This plugin try to cover Shaarli's plugin API entirely.
6 * Can be used by plugin developper to make their own.
7 */
8
9 /*
10 * RENDER HEADER, INCLUDES, FOOTER
11 *
12 * Those hooks are called at every page rendering.
13 * You can filter its execution by checking _PAGE_ value
14 * and check user status with _LOGGEDIN_.
15 */
16
17 /**
18 * Initialization function.
19 * It will be called when the plugin is loaded.
20 * This function can be used to return a list of initialization errors.
21 *
22 * @param $conf ConfigManager instance.
23 *
24 * @return array List of errors (optional).
25 */
26 function demo_plugin_init($conf)
27 {
28 $conf->get('toto', 'nope');
29
30 $errors[] = 'This a demo init error.';
31 return $errors;
32 }
33
34 /**
35 * Hook render_header.
36 * Executed on every page redering.
37 *
38 * Template placeholders:
39 * - buttons_toolbar
40 * - fields_toolbar
41 *
42 * @param array $data data passed to plugin
43 *
44 * @return array altered $data.
45 */
46 function hook_demo_plugin_render_header($data)
47 {
48 // Only execute when linklist is rendered.
49 if ($data['_PAGE_'] == Router::$PAGE_LINKLIST) {
50
51 // If loggedin
52 if ($data['_LOGGEDIN_'] === true) {
53 /*
54 * Links in toolbar:
55 * A link is an array of its attributes (key="value"),
56 * and a mandatory `html` key, which contains its value.
57 */
58 $button = array(
59 'attr' => array (
60 'href' => '#',
61 'class' => 'mybutton',
62 'title' => 'hover me',
63 ),
64 'html' => 'DEMO buttons toolbar',
65 );
66 $data['buttons_toolbar'][] = $button;
67 }
68
69 /*
70 * Add additional input fields in the tools.
71 * A field is an array containing:
72 * [
73 * 'form-attribute-1' => 'form attribute 1 value',
74 * 'form-attribute-2' => 'form attribute 2 value',
75 * 'inputs' => [
76 * [
77 * 'input-1-attribute-1 => 'input 1 attribute 1 value',
78 * 'input-1-attribute-2 => 'input 1 attribute 2 value',
79 * ],
80 * [
81 * 'input-2-attribute-1 => 'input 2 attribute 1 value',
82 * ],
83 * ],
84 * ]
85 * This example renders as:
86 * <form form-attribute-1="form attribute 1 value" form-attribute-2="form attribute 2 value">
87 * <input input-1-attribute-1="input 1 attribute 1 value" input-1-attribute-2="input 1 attribute 2 value">
88 * <input input-2-attribute-1="input 2 attribute 1 value">
89 * </form>
90 */
91 $form = array(
92 'attr' => array(
93 'method' => 'GET',
94 'action' => '?',
95 'class' => 'addform',
96 ),
97 'inputs' => array(
98 array(
99 'type' => 'text',
100 'name' => 'demo',
101 'placeholder' => 'demo',
102 )
103 )
104 );
105 $data['fields_toolbar'][] = $form;
106 }
107 // Another button always displayed
108 $button = array(
109 'attr' => array(
110 'href' => '#',
111 ),
112 'html' => 'Demo',
113 );
114 $data['buttons_toolbar'][] = $button;
115
116 return $data;
117 }
118
119 /**
120 * Hook render_includes.
121 * Executed on every page redering.
122 *
123 * Template placeholders:
124 * - css_files
125 *
126 * Data:
127 * - _PAGE_: current page
128 * - _LOGGEDIN_: true/false
129 *
130 * @param array $data data passed to plugin
131 *
132 * @return array altered $data.
133 */
134 function hook_demo_plugin_render_includes($data)
135 {
136 // List of plugin's CSS files.
137 // Note that you just need to specify CSS path.
138 $data['css_files'][] = PluginManager::$PLUGINS_PATH . '/demo_plugin/custom_demo.css';
139
140 return $data;
141 }
142
143 /**
144 * Hook render_footer.
145 * Executed on every page redering.
146 *
147 * Template placeholders:
148 * - text
149 * - endofpage
150 * - js_files
151 *
152 * Data:
153 * - _PAGE_: current page
154 * - _LOGGEDIN_: true/false
155 *
156 * @param array $data data passed to plugin
157 *
158 * @return array altered $data.
159 */
160 function hook_demo_plugin_render_footer($data)
161 {
162 // footer text
163 $data['text'][] = 'Shaarli is now enhanced by the awesome demo_plugin.';
164
165 // Free elements at the end of the page.
166 $data['endofpage'][] = '<marquee id="demo_marquee">' .
167 'DEMO: it\'s 1999 all over again!' .
168 '</marquee>';
169
170 // List of plugin's JS files.
171 // Note that you just need to specify CSS path.
172 $data['js_files'][] = PluginManager::$PLUGINS_PATH . '/demo_plugin/demo_plugin.js';
173
174 return $data;
175 }
176
177 /*
178 * SPECIFIC PAGES
179 */
180
181 /**
182 * Hook render_linklist.
183 *
184 * Template placeholders:
185 * - action_plugin: next to 'private only' button.
186 * - plugin_start_zone: page start
187 * - plugin_end_zone: page end
188 * - link_plugin: icons below each links.
189 *
190 * Data:
191 * - _LOGGEDIN_: true/false
192 *
193 * @param array $data data passed to plugin
194 *
195 * @return array altered $data.
196 */
197 function hook_demo_plugin_render_linklist($data)
198 {
199 /*
200 * Action links (action_plugin):
201 * A link is an array of its attributes (key="value"),
202 * and a mandatory `html` key, which contains its value.
203 * It's also recommended to add key 'on' or 'off' for theme rendering.
204 */
205 $action = array(
206 'attr' => array(
207 'href' => '?up',
208 'title' => 'Uppercase!',
209 ),
210 'html' => '←',
211 );
212
213 if (isset($_GET['up'])) {
214 // Manipulate link data
215 foreach ($data['links'] as &$value) {
216 $value['description'] = strtoupper($value['description']);
217 $value['title'] = strtoupper($value['title']);
218 }
219 $action['on'] = true;
220 } else {
221 $action['off'] = true;
222 }
223 $data['action_plugin'][] = $action;
224
225 // link_plugin (for each link)
226 foreach ($data['links'] as &$value) {
227 $value['link_plugin'][] = ' DEMO \o/';
228 }
229
230 // plugin_start_zone
231 $data['plugin_start_zone'][] = '<center>BEFORE</center>';
232 // plugin_start_zone
233 $data['plugin_end_zone'][] = '<center>AFTER</center>';
234
235 return $data;
236 }
237
238 /**
239 * Hook render_editlink.
240 *
241 * Template placeholders:
242 * - field_plugin: add link fields after tags.
243 *
244 * @param array $data data passed to plugin
245 *
246 * @return array altered $data.
247 */
248 function hook_demo_plugin_render_editlink($data)
249 {
250 // Load HTML into a string
251 $html = file_get_contents(PluginManager::$PLUGINS_PATH .'/demo_plugin/field.html');
252
253 // replace value in HTML if it exists in $data
254 if (!empty($data['link']['stuff'])) {
255 $html = sprintf($html, $data['link']['stuff']);
256 } else {
257 $html = sprintf($html, '');
258 }
259
260 // field_plugin
261 $data['edit_link_plugin'][] = $html;
262
263 return $data;
264 }
265
266 /**
267 * Hook render_tools.
268 *
269 * Template placeholders:
270 * - tools_plugin: after other tools.
271 *
272 * @param array $data data passed to plugin
273 *
274 * @return array altered $data.
275 */
276 function hook_demo_plugin_render_tools($data)
277 {
278 // field_plugin
279 $data['tools_plugin'][] = 'tools_plugin';
280
281 return $data;
282 }
283
284 /**
285 * Hook render_picwall.
286 *
287 * Template placeholders:
288 * - plugin_start_zone: page start.
289 * - plugin_end_zone: page end.
290 *
291 * Data:
292 * - _LOGGEDIN_: true/false
293 *
294 * @param array $data data passed to plugin
295 *
296 * @return array altered $data.
297 */
298 function hook_demo_plugin_render_picwall($data)
299 {
300 // plugin_start_zone
301 $data['plugin_start_zone'][] = '<center>BEFORE</center>';
302 // plugin_end_zone
303 $data['plugin_end_zone'][] = '<center>AFTER</center>';
304
305 return $data;
306 }
307
308 /**
309 * Hook render_tagcloud.
310 *
311 * Template placeholders:
312 * - plugin_start_zone: page start.
313 * - plugin_end_zone: page end.
314 *
315 * Data:
316 * - _LOGGEDIN_: true/false
317 *
318 * @param array $data data passed to plugin
319 *
320 * @return array altered $data.
321 */
322 function hook_demo_plugin_render_tagcloud($data)
323 {
324 // plugin_start_zone
325 $data['plugin_start_zone'][] = '<center>BEFORE</center>';
326 // plugin_end_zone
327 $data['plugin_end_zone'][] = '<center>AFTER</center>';
328
329 return $data;
330 }
331
332 /**
333 * Hook render_daily.
334 *
335 * Template placeholders:
336 * - plugin_start_zone: page start.
337 * - plugin_end_zone: page end.
338 *
339 * Data:
340 * - _LOGGEDIN_: true/false
341 *
342 * @param array $data data passed to plugin
343 *
344 * @return array altered $data.
345 */
346 function hook_demo_plugin_render_daily($data)
347 {
348 // plugin_start_zone
349 $data['plugin_start_zone'][] = '<center>BEFORE</center>';
350 // plugin_end_zone
351 $data['plugin_end_zone'][] = '<center>AFTER</center>';
352
353
354 // Manipulate columns data
355 foreach ($data['cols'] as &$value) {
356 foreach ($value as &$value2) {
357 $value2['formatedDescription'] .= ' ಠ_ಠ';
358 }
359 }
360
361 // Add plugin content at the end of each link
362 foreach ($data['cols'] as &$value) {
363 foreach ($value as &$value2) {
364 $value2['link_plugin'][] = 'DEMO';
365 }
366 }
367
368 return $data;
369 }
370
371 /*
372 * DATA SAVING HOOK.
373 */
374
375 /**
376 * Hook savelink.
377 *
378 * Triggered when a link is save (new or edit).
379 * All new links now contain a 'stuff' value.
380 *
381 * @param array $data contains the new link data.
382 *
383 * @return array altered $data.
384 */
385 function hook_demo_plugin_save_link($data)
386 {
387
388 // Save stuff added in editlink field
389 if (!empty($_POST['lf_stuff'])) {
390 $data['stuff'] = escape($_POST['lf_stuff']);
391 }
392
393 return $data;
394 }
395
396 /**
397 * Hook delete_link.
398 *
399 * Triggered when a link is deleted.
400 *
401 * @param array $data contains the link to be deleted.
402 *
403 * @return array altered data.
404 */
405 function hook_demo_plugin_delete_link($data)
406 {
407 if (strpos($data['url'], 'youtube.com') !== false) {
408 exit('You can not delete a YouTube link. Don\'t ask.');
409 }
410 }
411
412 /**
413 * Execute render_feed hook.
414 * Called with ATOM and RSS feed.
415 *
416 * Special data keys:
417 * - _PAGE_: current page
418 * - _LOGGEDIN_: true/false
419 *
420 * @param array $data data passed to plugin
421 *
422 * @return array altered $data.
423 */
424 function hook_demo_plugin_render_feed($data)
425 {
426 foreach ($data['links'] as &$link) {
427 if ($data['_PAGE_'] == Router::$PAGE_FEED_ATOM) {
428 $link['description'] .= ' - ATOM Feed' ;
429 }
430 elseif ($data['_PAGE_'] == Router::$PAGE_FEED_RSS) {
431 $link['description'] .= ' - RSS Feed';
432 }
433 }
434 return $data;
435 }
436
437 /**
438 * This function is never called, but contains translation calls for GNU gettext extraction.
439 */
440 function demo_dummy_translation()
441 {
442 // meta
443 t('A demo plugin covering all use cases for template designers and plugin developers.');
444 }