xref: /plugin/struct/action/aggregationeditor.php (revision ed77599ce8e3ec56adc1cd498a167c9e43de2beb) !
1308cc83fSAndreas Gohr<?php
2308cc83fSAndreas Gohr
3308cc83fSAndreas Gohr/**
4308cc83fSAndreas Gohr * DokuWiki Plugin struct (Action Component)
5308cc83fSAndreas Gohr *
6308cc83fSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7308cc83fSAndreas Gohr * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
8308cc83fSAndreas Gohr */
9308cc83fSAndreas Gohr
10308cc83fSAndreas Gohruse dokuwiki\plugin\struct\meta\AccessTable;
11308cc83fSAndreas Gohruse dokuwiki\plugin\struct\meta\AccessTableGlobal;
12308cc83fSAndreas Gohruse dokuwiki\plugin\struct\meta\Column;
13308cc83fSAndreas Gohruse dokuwiki\plugin\struct\meta\AggregationEditorTable;
14308cc83fSAndreas Gohruse dokuwiki\plugin\struct\meta\Schema;
15308cc83fSAndreas Gohruse dokuwiki\plugin\struct\meta\SearchConfig;
16308cc83fSAndreas Gohruse dokuwiki\plugin\struct\meta\StructException;
17308cc83fSAndreas Gohruse dokuwiki\plugin\struct\meta\Value;
18308cc83fSAndreas Gohr
19308cc83fSAndreas Gohr/**
20308cc83fSAndreas Gohr * Class action_plugin_struct_lookup
21308cc83fSAndreas Gohr *
22308cc83fSAndreas Gohr * Handle global and serial data table editing
23308cc83fSAndreas Gohr */
24308cc83fSAndreas Gohrclass action_plugin_struct_aggregationeditor extends DokuWiki_Action_Plugin
25308cc83fSAndreas Gohr{
26308cc83fSAndreas Gohr
27308cc83fSAndreas Gohr    /** @var  Column */
28308cc83fSAndreas Gohr    protected $column = null;
29308cc83fSAndreas Gohr
30308cc83fSAndreas Gohr    /** @var string */
31308cc83fSAndreas Gohr    protected $pid = '';
32308cc83fSAndreas Gohr
33308cc83fSAndreas Gohr    /** @var int */
34308cc83fSAndreas Gohr    protected $rid = 0;
35308cc83fSAndreas Gohr
36308cc83fSAndreas Gohr    /**
37308cc83fSAndreas Gohr     * Registers a callback function for a given event
38308cc83fSAndreas Gohr     *
39308cc83fSAndreas Gohr     * @param Doku_Event_Handler $controller DokuWiki's event controller object
40308cc83fSAndreas Gohr     * @return void
41308cc83fSAndreas Gohr     */
42308cc83fSAndreas Gohr    public function register(Doku_Event_Handler $controller)
43308cc83fSAndreas Gohr    {
44308cc83fSAndreas Gohr        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleAjax');
45308cc83fSAndreas Gohr    }
46308cc83fSAndreas Gohr
47308cc83fSAndreas Gohr    /**
48308cc83fSAndreas Gohr     * @param Doku_Event $event
49308cc83fSAndreas Gohr     * @param $param
50308cc83fSAndreas Gohr     */
51308cc83fSAndreas Gohr    public function handleAjax(Doku_Event $event, $param)
52308cc83fSAndreas Gohr    {
53308cc83fSAndreas Gohr        $len = strlen('plugin_struct_aggregationeditor_');
54308cc83fSAndreas Gohr        if (substr($event->data, 0, $len) != 'plugin_struct_aggregationeditor_') {
55308cc83fSAndreas Gohr            return;
56308cc83fSAndreas Gohr        }
57308cc83fSAndreas Gohr        $event->preventDefault();
58308cc83fSAndreas Gohr        $event->stopPropagation();
59308cc83fSAndreas Gohr
60308cc83fSAndreas Gohr        try {
61308cc83fSAndreas Gohr            if (substr($event->data, $len) == 'new') {
62308cc83fSAndreas Gohr                $this->newRowEditor();
63308cc83fSAndreas Gohr            }
64308cc83fSAndreas Gohr
65308cc83fSAndreas Gohr            if (substr($event->data, $len) == 'save') {
66308cc83fSAndreas Gohr                $this->saveRow();
67308cc83fSAndreas Gohr            }
68308cc83fSAndreas Gohr
69308cc83fSAndreas Gohr            if (substr($event->data, $len) == 'delete') {
70308cc83fSAndreas Gohr                $this->deleteRow();
71308cc83fSAndreas Gohr            }
72308cc83fSAndreas Gohr        } catch (StructException $e) {
73308cc83fSAndreas Gohr            http_status(500);
74308cc83fSAndreas Gohr            header('Content-Type: text/plain');
75308cc83fSAndreas Gohr            echo $e->getMessage();
76308cc83fSAndreas Gohr        }
77308cc83fSAndreas Gohr    }
78308cc83fSAndreas Gohr
79308cc83fSAndreas Gohr    /**
80308cc83fSAndreas Gohr     * Deletes a row
81308cc83fSAndreas Gohr     */
82308cc83fSAndreas Gohr    protected function deleteRow()
83308cc83fSAndreas Gohr    {
84308cc83fSAndreas Gohr        global $INPUT;
85308cc83fSAndreas Gohr        $tablename = $INPUT->str('schema');
86308cc83fSAndreas Gohr        if (!$tablename) {
87308cc83fSAndreas Gohr            throw new StructException('No schema given');
88308cc83fSAndreas Gohr        }
89308cc83fSAndreas Gohr
90308cc83fSAndreas Gohr        $this->rid = $INPUT->int('rid');
91308cc83fSAndreas Gohr        $this->validate();
92308cc83fSAndreas Gohr
93308cc83fSAndreas Gohr        action_plugin_struct_inline::checkCSRF();
94308cc83fSAndreas Gohr
95308cc83fSAndreas Gohr        $access = $this->getAccess($tablename);
96308cc83fSAndreas Gohr        if (!$access->getSchema()->isEditable()) {
97308cc83fSAndreas Gohr            throw new StructException('lookup delete error: no permission for schema');
98308cc83fSAndreas Gohr        }
99308cc83fSAndreas Gohr        $access->clearData();
100308cc83fSAndreas Gohr    }
101308cc83fSAndreas Gohr
102308cc83fSAndreas Gohr    /**
103308cc83fSAndreas Gohr     * Save one new row
104308cc83fSAndreas Gohr     */
105308cc83fSAndreas Gohr    protected function saveRow()
106308cc83fSAndreas Gohr    {
107308cc83fSAndreas Gohr        global $INPUT;
108308cc83fSAndreas Gohr        $tablename = $INPUT->str('schema');
109308cc83fSAndreas Gohr        $data = $INPUT->arr('entry');
110308cc83fSAndreas Gohr        $this->pid = $INPUT->str('pid');
111308cc83fSAndreas Gohr        action_plugin_struct_inline::checkCSRF();
112308cc83fSAndreas Gohr
113308cc83fSAndreas Gohr        // create a new row based on the original aggregation config
114308cc83fSAndreas Gohr        $access = $this->getAccess($tablename);
115308cc83fSAndreas Gohr
116308cc83fSAndreas Gohr        /** @var helper_plugin_struct $helper */
117308cc83fSAndreas Gohr        $helper = plugin_load('helper', 'struct');
118308cc83fSAndreas Gohr        $helper->saveLookupData($access, $data);
119308cc83fSAndreas Gohr
120308cc83fSAndreas Gohr        $config = json_decode($INPUT->str('searchconf'), true);
121308cc83fSAndreas Gohr        // update row id
122308cc83fSAndreas Gohr        $this->rid = $access->getRid();
123308cc83fSAndreas Gohr        $config = $this->addTypeFilter($config);
124308cc83fSAndreas Gohr
125308cc83fSAndreas Gohr        $editorTable = new AggregationEditorTable(
126308cc83fSAndreas Gohr            $this->pid,
127308cc83fSAndreas Gohr            'xhtml',
128308cc83fSAndreas Gohr            new Doku_Renderer_xhtml(),
129308cc83fSAndreas Gohr            new SearchConfig($config)
130308cc83fSAndreas Gohr        );
131308cc83fSAndreas Gohr
132308cc83fSAndreas Gohr        echo $editorTable->getFirstRow();
133308cc83fSAndreas Gohr    }
134308cc83fSAndreas Gohr
135308cc83fSAndreas Gohr    /**
136308cc83fSAndreas Gohr     * Create the Editor for a new row
137308cc83fSAndreas Gohr     */
138308cc83fSAndreas Gohr    protected function newRowEditor()
139308cc83fSAndreas Gohr    {
140308cc83fSAndreas Gohr        global $INPUT;
141308cc83fSAndreas Gohr        global $lang;
142308cc83fSAndreas Gohr        $tablename = $INPUT->str('schema');
143308cc83fSAndreas Gohr
144308cc83fSAndreas Gohr        $schema = new Schema($tablename);
145308cc83fSAndreas Gohr        if (!$schema->isEditable()) {
146308cc83fSAndreas Gohr            return;
147308cc83fSAndreas Gohr        } // no permissions, no editor
148308cc83fSAndreas Gohr
149308cc83fSAndreas Gohr        echo '<div class="struct_entry_form">';
150308cc83fSAndreas Gohr        echo '<fieldset>';
151308cc83fSAndreas Gohr        echo '<legend>' . $this->getLang('lookup new entry') . '</legend>';
152308cc83fSAndreas Gohr        /** @var action_plugin_struct_edit $edit */
153308cc83fSAndreas Gohr        $edit = plugin_load('action', 'struct_edit');
154308cc83fSAndreas Gohr        foreach ($schema->getColumns(false) as $column) {
155308cc83fSAndreas Gohr            $label = $column->getLabel();
156308cc83fSAndreas Gohr            $field = new Value($column, '');
157308cc83fSAndreas Gohr            echo $edit->makeField($field, "entry[$label]");
158308cc83fSAndreas Gohr        }
159308cc83fSAndreas Gohr        formSecurityToken(); // csrf protection
160308cc83fSAndreas Gohr        echo '<input type="hidden" name="call" value="plugin_struct_aggregationeditor_save" />';
161308cc83fSAndreas Gohr        echo '<input type="hidden" name="schema" value="' . hsc($tablename) . '" />';
162308cc83fSAndreas Gohr
163308cc83fSAndreas Gohr        echo '<button type="submit">' . $lang['btn_save'] . '</button>';
164308cc83fSAndreas Gohr
165308cc83fSAndreas Gohr        echo '<div class="err"></div>';
166308cc83fSAndreas Gohr        echo '</fieldset>';
167308cc83fSAndreas Gohr        echo '</div>';
168308cc83fSAndreas Gohr    }
169308cc83fSAndreas Gohr
170308cc83fSAndreas Gohr    /**
171308cc83fSAndreas Gohr     * Returns data accessor
172308cc83fSAndreas Gohr     *
173308cc83fSAndreas Gohr     * @param string $tablename
174308cc83fSAndreas Gohr     * @return AccessTableGlobal
175308cc83fSAndreas Gohr     */
176308cc83fSAndreas Gohr    protected function getAccess($tablename)
177308cc83fSAndreas Gohr    {
178308cc83fSAndreas Gohr        if ($this->pid) {
179308cc83fSAndreas Gohr            return AccessTable::getSerialAccess($tablename, $this->pid, $this->rid);
180308cc83fSAndreas Gohr        }
181*ed77599cSAnna Dabrowska        return AccessTable::getGlobalAccess($tablename, $this->rid);
182308cc83fSAndreas Gohr    }
183308cc83fSAndreas Gohr
184308cc83fSAndreas Gohr    /**
185308cc83fSAndreas Gohr     * Adds filter to search config to differentiate data types
186308cc83fSAndreas Gohr     *
187308cc83fSAndreas Gohr     * @param array $config
188308cc83fSAndreas Gohr     * @return array
189308cc83fSAndreas Gohr     */
190308cc83fSAndreas Gohr    protected function addTypeFilter($config)
191308cc83fSAndreas Gohr    {
192308cc83fSAndreas Gohr        $config['filter'][] = ['%rowid%', '=', $this->rid, 'AND'];
193308cc83fSAndreas Gohr        if ($this->pid) {
194308cc83fSAndreas Gohr            $config['filter'][] = ['%pageid%', '=', $this->pid, 'AND'];
195308cc83fSAndreas Gohr        }
196308cc83fSAndreas Gohr        return $config;
197308cc83fSAndreas Gohr    }
198308cc83fSAndreas Gohr
199308cc83fSAndreas Gohr    /**
200308cc83fSAndreas Gohr     * Throws an exception if data is invalid
201308cc83fSAndreas Gohr     */
202308cc83fSAndreas Gohr    protected function validate()
203308cc83fSAndreas Gohr    {
204308cc83fSAndreas Gohr        if (!$this->rid) {
205308cc83fSAndreas Gohr            throw new StructException('No row id given');
206308cc83fSAndreas Gohr        }
207308cc83fSAndreas Gohr    }
208308cc83fSAndreas Gohr}
209