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