aboutsummaryrefslogtreecommitdiffhomepage
path: root/plugins/demo_plugin/demo_plugin.php
blob: 15482fe0fb867c8bfe05674de05333041f024deb (plain) (blame)
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<?php
/**
 * Demo Plugin.
 *
 * This plugin try to cover Shaarli's plugin API entirely.
 * Can be used by plugin developper to make their own.
 */

/*
 * RENDER HEADER, INCLUDES, FOOTER
 *
 * Those hooks are called at every page rendering.
 * You can filter its execution by checking _PAGE_ value
 * and check user status with _LOGGEDIN_.
 */

/**
 * Initialization function.
 * It will be called when the plugin is loaded.
 * This function can be used to return a list of initialization errors.
 *
 * @param $conf ConfigManager instance.
 *
 * @return array List of errors (optional).
 */
function demo_plugin_init($conf)
{
    $conf->get('toto', 'nope');

    $errors[] = 'This a demo init error.';
    return $errors;
}

/**
 * Hook render_header.
 * Executed on every page redering.
 *
 * Template placeholders:
 *   - buttons_toolbar
 *   - fields_toolbar
 *
 * @param array $data data passed to plugin
 *
 * @return array altered $data.
 */
function hook_demo_plugin_render_header($data)
{
    // Only execute when linklist is rendered.
    if ($data['_PAGE_'] == Router::$PAGE_LINKLIST) {

        // If loggedin
        if ($data['_LOGGEDIN_'] === true) {
            /*
             * Links in toolbar:
             * A link is an array of its attributes (key="value"),
             * and a mandatory `html` key, which contains its value.
             */
            $button = array(
                'href' => '#',
                'class' => 'mybutton',
                'title' => 'hover me',
                'html' => 'DEMO buttons toolbar',
            );
            $data['buttons_toolbar'][] = $button;
        }

        /*
         * Add additional input fields in the tools.
         * A field is an array containing:
         *  [
         *      'form-attribute-1' => 'form attribute 1 value',
         *      'form-attribute-2' => 'form attribute 2 value',
         *      'inputs' => [
         *          [
         *              'input-1-attribute-1 => 'input 1 attribute 1 value',
         *              'input-1-attribute-2 => 'input 1 attribute 2 value',
         *          ],
         *          [
         *              'input-2-attribute-1 => 'input 2 attribute 1 value',
         *          ],
         *      ],
         *  ]
         * This example renders as:
         *      <form form-attribute-1="form attribute 1 value" form-attribute-2="form attribute 2 value">
         *          <input input-1-attribute-1="input 1 attribute 1 value" input-1-attribute-2="input 1 attribute 2 value">
         *          <input input-2-attribute-1="input 2 attribute 1 value">
         *      </form>
         */
        $form = array(
            'method' => 'GET',
            'action' => '?',
            'class' => 'addform',
            'inputs' => array(
                array(
                    'type' => 'text',
                    'name' => 'demo',
                    'placeholder' => 'demo',
                )
            )
        );
        $data['fields_toolbar'][] = $form;
    }
    // Another button always displayed
    $button = array(
        'href' => '#',
        'html' => 'Demo',
    );
    $data['buttons_toolbar'][] = $button;

    return $data;
}

/**
 * Hook render_includes.
 * Executed on every page redering.
 *
 * Template placeholders:
 *   - css_files
 *
 * Data:
 *   - _PAGE_: current page
 *   - _LOGGEDIN_: true/false
 *
 * @param array $data data passed to plugin
 *
 * @return array altered $data.
 */
function hook_demo_plugin_render_includes($data)
{
    // List of plugin's CSS files.
    // Note that you just need to specify CSS path.
    $data['css_files'][] = PluginManager::$PLUGINS_PATH . '/demo_plugin/custom_demo.css';

    return $data;
}

/**
 * Hook render_footer.
 * Executed on every page redering.
 *
 * Template placeholders:
 *   - text
 *   - endofpage
 *   - js_files
 *
 * Data:
 *   - _PAGE_: current page
 *   - _LOGGEDIN_: true/false
 *
 * @param array $data data passed to plugin
 *
 * @return array altered $data.
 */
function hook_demo_plugin_render_footer($data)
{
    // footer text
    $data['text'][] = 'Shaarli is now enhanced by the awesome demo_plugin.';

    // Free elements at the end of the page.
    $data['endofpage'][] = '<marquee id="demo_marquee">' .
            'DEMO: it\'s 1999 all over again!' .
        '</marquee>';

    // List of plugin's JS files.
    // Note that you just need to specify CSS path.
    $data['js_files'][] = PluginManager::$PLUGINS_PATH . '/demo_plugin/demo_plugin.js';

    return $data;
}

/*
 * SPECIFIC PAGES
 */

/**
 * Hook render_linklist.
 *
 * Template placeholders:
 *   - action_plugin: next to 'private only' button.
 *   - plugin_start_zone: page start
 *   - plugin_end_zone: page end
 *   - link_plugin: icons below each links.
 *
 * Data:
 *   - _LOGGEDIN_: true/false
 *
 * @param array $data data passed to plugin
 *
 * @return array altered $data.
 */
function hook_demo_plugin_render_linklist($data)
{
    /*
     * Action links (action_plugin):
     * A link is an array of its attributes (key="value"),
     * and a mandatory `html` key, which contains its value.
     * It's also recommended to add key 'on' or 'off' for theme rendering.
     */
    $action = array(
        'href' => '?up',
        'title' => 'Uppercase!',
        'html' => '←',
    );

    if (isset($_GET['up'])) {
        // Manipulate link data
        foreach ($data['links'] as &$value) {
            $value['description'] = strtoupper($value['description']);
            $value['title'] = strtoupper($value['title']);
        }
        $action['on'] = true;
    } else {
        $action['off'] = true;
    }
    $data['action_plugin'][] = $action;

    // link_plugin (for each link)
    foreach ($data['links'] as &$value) {
        $value['link_plugin'][] = ' DEMO \o/';
    }

    // plugin_start_zone
    $data['plugin_start_zone'][] = '<center>BEFORE</center>';
    // plugin_start_zone
    $data['plugin_end_zone'][] = '<center>AFTER</center>';

    return $data;
}

/**
 * Hook render_editlink.
 *
 * Template placeholders:
 *   - field_plugin: add link fields after tags.
 *
 * @param array $data data passed to plugin
 *
 * @return array altered $data.
 */
function hook_demo_plugin_render_editlink($data)
{
    // Load HTML into a string
    $html = file_get_contents(PluginManager::$PLUGINS_PATH .'/demo_plugin/field.html');

    // replace value in HTML if it exists in $data
    if (!empty($data['link']['stuff'])) {
        $html = sprintf($html, $data['link']['stuff']);
    } else {
        $html = sprintf($html, '');
    }

    // field_plugin
    $data['edit_link_plugin'][] = $html;

    return $data;
}

/**
 * Hook render_tools.
 *
 * Template placeholders:
 *   - tools_plugin: after other tools.
 *
 * @param array $data data passed to plugin
 *
 * @return array altered $data.
 */
function hook_demo_plugin_render_tools($data)
{
    // field_plugin
    $data['tools_plugin'][] = 'tools_plugin';

    return $data;
}

/**
 * Hook render_picwall.
 *
 * Template placeholders:
 *   - plugin_start_zone: page start.
 *   - plugin_end_zone: page end.
 *
 * Data:
 *   - _LOGGEDIN_: true/false
 *
 * @param array $data data passed to plugin
 *
 * @return array altered $data.
 */
function hook_demo_plugin_render_picwall($data)
{
    // plugin_start_zone
    $data['plugin_start_zone'][] = '<center>BEFORE</center>';
    // plugin_end_zone
    $data['plugin_end_zone'][] = '<center>AFTER</center>';

    return $data;
}

/**
 * Hook render_tagcloud.
 *
 * Template placeholders:
 *   - plugin_start_zone: page start.
 *   - plugin_end_zone: page end.
 *
 * Data:
 *   - _LOGGEDIN_: true/false
 *
 * @param array $data data passed to plugin
 *
 * @return array altered $data.
 */
function hook_demo_plugin_render_tagcloud($data)
{
    // plugin_start_zone
    $data['plugin_start_zone'][] = '<center>BEFORE</center>';
    // plugin_end_zone
    $data['plugin_end_zone'][] = '<center>AFTER</center>';

    return $data;
}

/**
 * Hook render_daily.
 *
 * Template placeholders:
 *   - plugin_start_zone: page start.
 *   - plugin_end_zone: page end.
 *
 * Data:
 *   - _LOGGEDIN_: true/false
 *
 * @param array $data data passed to plugin
 *
 * @return array altered $data.
 */
function hook_demo_plugin_render_daily($data)
{
    // plugin_start_zone
    $data['plugin_start_zone'][] = '<center>BEFORE</center>';
    // plugin_end_zone
    $data['plugin_end_zone'][] = '<center>AFTER</center>';


    // Manipulate columns data
    foreach ($data['cols'] as &$value) {
        foreach ($value as &$value2) {
            $value2['formatedDescription'] .= ' ಠ_ಠ';
        }
    }

    // Add plugin content at the end of each link
    foreach ($data['cols'] as &$value) {
        foreach ($value as &$value2) {
            $value2['link_plugin'][] = 'DEMO';
        }
    }

    return $data;
}

/*
 * DATA SAVING HOOK.
 */

/**
 * Hook savelink.
 *
 * Triggered when a link is save (new or edit).
 * All new links now contain a 'stuff' value.
 *
 * @param array $data contains the new link data.
 *
 * @return array altered $data.
 */
function hook_demo_plugin_save_link($data)
{

    // Save stuff added in editlink field
    if (!empty($_POST['lf_stuff'])) {
        $data['stuff'] = escape($_POST['lf_stuff']);
    }

    return $data;
}

/**
 * Hook delete_link.
 *
 * Triggered when a link is deleted.
 *
 * @param array $data contains the link to be deleted.
 *
 * @return array altered data.
 */
function hook_demo_plugin_delete_link($data)
{
    if (strpos($data['url'], 'youtube.com') !== false) {
        exit('You can not delete a YouTube link. Don\'t ask.');
    }
}

/**
 * Execute render_feed hook.
 * Called with ATOM and RSS feed.
 *
 * Special data keys:
 *   - _PAGE_: current page
 *   - _LOGGEDIN_: true/false
 *
 * @param array $data data passed to plugin
 *
 * @return array altered $data.
 */
function hook_demo_plugin_render_feed($data)
{
    foreach ($data['links'] as &$link) {
        if ($data['_PAGE_'] == Router::$PAGE_FEED_ATOM) {
            $link['description'] .= ' - ATOM Feed' ;
        }
        elseif ($data['_PAGE_'] == Router::$PAGE_FEED_RSS) {
            $link['description'] .= ' - RSS Feed';
        }
    }
    return $data;
}