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