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