1<?php
2/**
3 * DokuWiki Plugin timestamp (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Trenton Ivey <Trenton.Ivey@gmail.com>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class action_plugin_timestamp_Timestamp extends DokuWiki_Action_Plugin {
13
14    /**
15     * Registers a callback function for a given event
16     *
17     * @param Doku_Event_Handler $controller DokuWiki's event controller object
18     * @return void
19     */
20    public function register(Doku_Event_Handler $controller) {
21
22       $controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'handle_toolbar_define');
23       $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call_unknown');
24
25    }
26
27    /**
28     * Adds a toolbar button to insert timestamps
29     *
30     * @param Doku_Event $event  event object by reference
31     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
32     *                           handler was registered]
33     * @return void
34     */
35    public function handle_toolbar_define(Doku_Event &$event, $param) {
36        $event->data[] = array(
37            'type'   => 'timestamp',
38            'title'  => $this->getLang('tb_timestamp_button'),
39            'icon'   => '../../plugins/timestamp/timestamp.png',
40            'key'    => ':',
41            'block' => false
42        );
43    }
44
45    /**
46     * Handles AJAX 'timestamp' request and returns the strftime formatted time
47     *
48     * @param Doku_Event $event  event object by reference
49     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
50     *                           handler was registered]
51     * @return void
52     */
53    public function handle_ajax_call_unknown(Doku_Event &$event, $param) {
54        // Make sure we are handleing the correct AJAX
55        if ($event->data !== 'timestamp') {
56            return;
57        }
58        // No other ajax call handlers are needed
59        $event->stopPropagation();
60        $event->preventDefault();
61        // Set the content type
62        header('Content-Type: text/plain');
63        // 'Return' the strftime formated time
64        echo strftime($this->getConf('timestamp_format'));
65    }
66}
67
68// vim:ts=4:sw=4:et:
69