1<?php
2/**
3 * Table editor
4 *
5 * @author     Adrian Lang <lang@cosmocode.de>
6 */
7
8/**
9 * Handles the inserting of a new table in a running edit session
10 */
11class action_plugin_edittable_newtable extends DokuWiki_Action_Plugin
12{
13    /**
14     * Register its handlers with the DokuWiki's event controller
15     */
16    function register(Doku_Event_Handler $controller)
17    {
18        $controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'toolbar');
19
20        //$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_newtable');
21        $controller->register_hook('PLUGIN_EDITTABLE_PREPROCESS_NEWTABLE', 'BEFORE', $this, 'handle_newtable');
22    }
23
24    /**
25     * Add a button for inserting tables to the toolbar array
26     *
27     * @param Doku_Event $event
28     */
29    public function toolbar(Doku_Event $event)
30    {
31        $event->data[] = array(
32            'title' => $this->getLang('add_table'),
33            'type'  => 'NewTable',
34            'icon'  => '../../plugins/edittable/images/add_table.png',
35            'block' => true
36        );
37    }
38
39    /**
40     * Handle the click on the new table button in the toolbar
41     *
42     * @param Doku_Event $event
43     */
44    public function handle_newtable(Doku_Event $event)
45    {
46        global $INPUT;
47        global $TEXT;
48
49        if (!$INPUT->post->has('edittable__new')) return;
50
51        /*
52         * $fields['pre']  has all data before the selection when the "Insert table" button was clicked
53         * $fields['text'] has all data inside the selection when the "Insert table" button was clicked
54         * $fields['suf']  has all data after the selection when the "Insert table" button was clicked
55         * $TEXT has the table created by the editor (from action_plugin_edittable_editor::handle_table_post())
56         */
57        $fields = $INPUT->post->arr('edittable__new');
58
59        // clean the fields (undos formText()) and update the post and request arrays
60        $fields['pre'] = cleanText($fields['pre']);
61        $fields['text'] = cleanText($fields['text']);
62        $fields['suf'] = cleanText($fields['suf']);
63        $INPUT->post->set('edittable__new', $fields);
64
65
66        $event->data = act_clean($event->data);
67        switch ($event->data) {
68            case 'preview':
69                // preview view of a table edit
70                $INPUT->post->set('target', 'table');
71                break;
72            case 'edit':
73                // edit view of a table (first edit)
74                $INPUT->post->set('target', 'table');
75                $TEXT = "^  ^  ^\n";
76                foreach (explode("\n", $fields['text']) as $line) {
77                    $TEXT .= "| $line |  |\n";
78                }
79                break;
80            case 'draftdel':
81                // not sure if/how this would happen, we restore all data and hand over to section edit
82                $INPUT->post->set('target', 'section');
83                $TEXT = $fields['pre'].$fields['text'].$fields['suf'];
84                $event->data = 'edit';
85                break;
86            case 'save':
87                // return to edit page
88                $INPUT->post->set('target', 'section');
89                $TEXT = $fields['pre'].$TEXT.$fields['suf'];
90                $event->data = 'edit';
91                break;
92        }
93    }
94}
95