1<?php
2
3/**
4 * DokuWiki Plugin struct (Action Component)
5 *
6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7 * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
8 */
9
10use dokuwiki\plugin\struct\meta\AccessTable;
11use dokuwiki\plugin\struct\meta\Assignments;
12use dokuwiki\plugin\struct\meta\Schema;
13use dokuwiki\plugin\struct\meta\Search;
14use dokuwiki\plugin\struct\types\Lookup;
15
16/**
17 * Handles bureaucracy additions
18 *
19 * This registers to the template action of the bureaucracy plugin and saves all struct data
20 * submitted through the bureaucracy form to all newly created pages (if the schema applies).
21 *
22 * It also registers the struct_schema type for bureaucracy which will add all fields of the
23 * schema to the form. The struct_field type is added through standard naming convention - see
24 * helper/fiels.php for that.
25 */
26class action_plugin_struct_bureaucracy extends DokuWiki_Action_Plugin
27{
28
29    /**
30     * Registers a callback function for a given event
31     *
32     * @param Doku_Event_Handler $controller DokuWiki's event controller object
33     * @return void
34     */
35    public function register(Doku_Event_Handler $controller)
36    {
37        $controller->register_hook('PLUGIN_BUREAUCRACY_PAGENAME', 'BEFORE', $this, 'handleLookupFields');
38        $controller->register_hook('PLUGIN_BUREAUCRACY_EMAIL_SEND', 'BEFORE', $this, 'handleLookupFields');
39        $controller->register_hook('PLUGIN_BUREAUCRACY_TEMPLATE_SAVE', 'AFTER', $this, 'handleSave');
40        $controller->register_hook('PLUGIN_BUREAUCRACY_FIELD_UNKNOWN', 'BEFORE', $this, 'handleSchema');
41    }
42
43    /**
44     * Load a whole schema as fields
45     *
46     * @param Doku_Event $event event object by reference
47     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
48     *                           handler was registered]
49     * @return bool
50     */
51    public function handleSchema(Doku_Event $event, $param)
52    {
53        $args = $event->data['args'];
54        if ($args[0] != 'struct_schema') return false;
55        $event->preventDefault();
56        $event->stopPropagation();
57
58        /** @var helper_plugin_bureaucracy_field $helper */
59        $helper = plugin_load('helper', 'bureaucracy_field');
60        $helper->initialize($args);
61
62        $schema = new Schema($helper->opt['label']);
63        if (!$schema->getId()) {
64            msg('This schema does not exist', -1);
65            return false;
66        }
67
68        foreach ($schema->getColumns(false) as $column) {
69            /** @var helper_plugin_struct_field $field */
70            $field = plugin_load('helper', 'struct_field');
71            // we don't initialize the field but set the appropriate values
72            $field->opt = $helper->opt; // copy all the settings to each field
73            $field->opt['label'] = $column->getFullQualifiedLabel();
74            $field->column = $column;
75            $event->data['fields'][] = $field;
76        }
77        return true;
78    }
79
80    /**
81     * Replace lookup fields placeholder's values
82     *
83     * @param Doku_Event $event event object by reference
84     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
85     *                           handler was registered]
86     * @return bool
87     */
88    public function handleLookupFields(Doku_Event $event, $param)
89    {
90        foreach ($event->data['fields'] as $field) {
91            if (!is_a($field, 'helper_plugin_struct_field')) continue;
92            if (!$field->column->getType() instanceof Lookup) continue;
93
94            $value = $field->getParam('value');
95            if (!is_array($value)) $value = array($value);
96
97            $config = $field->column->getType()->getConfig();
98
99            // find proper value
100            // current Search implementation doesn't allow doing it using SQL
101            $search = new Search();
102            $search->addSchema($config['schema']);
103            $search->addColumn($config['field']);
104            $result = $search->execute();
105            $pids = $search->getPids();
106            $rids = $search->getRids();
107
108            $field->opt['struct_pids'] = array();
109            $new_value = array();
110            foreach ($value as $pid) {
111                for ($i = 0; $i < count($result); $i++) {
112                    // lookups can reference pages or global data, so check both pid and rid
113                    $pid = json_decode($pid)[0] ?: $pid;
114                    $rid = json_decode($pid)[1];
115                    if (($pid && $pids[$i] === $pid) || ($rid && $rids[$i] === (string)$rid)) {
116                        $field->opt['struct_pids'][] = $pid;
117                        $new_value[] = $result[$i][0]->getDisplayValue();
118                    }
119                }
120            }
121
122            //replace previous value
123            if ($field->column->isMulti()) {
124                $field->opt['value'] = $new_value;
125            } else {
126                $event->data['values'][$field->column->getFullQualifiedLabel()] = $new_value[0];
127            }
128        }
129        return true;
130    }
131
132    /**
133     * Save the struct data
134     *
135     * @param Doku_Event $event event object by reference
136     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
137     *                           handler was registered]
138     * @return bool
139     */
140    public function handleSave(Doku_Event $event, $param)
141    {
142        // get all struct values and their associated schemas
143        $tosave = array();
144        foreach ($event->data['fields'] as $field) {
145            if (!is_a($field, 'helper_plugin_struct_field')) continue;
146            /** @var helper_plugin_struct_field $field */
147            $tbl = $field->column->getTable();
148            $lbl = $field->column->getLabel();
149            if (!isset($tosave[$tbl])) $tosave[$tbl] = array();
150
151            if ($field->column->isMulti() && $field->column->getType() instanceof Lookup) {
152                $tosave[$tbl][$lbl] = $field->opt['struct_pids'];
153            } else {
154                $tosave[$tbl][$lbl] = $field->getParam('value');
155            }
156        }
157
158        // save all the struct data of assigned schemas
159        $id = $event->data['id'];
160        $time = filemtime(wikiFN($id));
161
162        $assignments = Assignments::getInstance();
163        $assigned = $assignments->getPageAssignments($id);
164        foreach ($tosave as $table => $data) {
165            if (!in_array($table, $assigned)) continue;
166            $access = AccessTable::getPageAccess($table, $id, $time);
167            $validator = $access->getValidator($data);
168            if ($validator->validate()) {
169                $validator->saveData($time);
170
171                // make sure this schema is assigned
172                $assignments->assignPageSchema(
173                    $id,
174                    $validator->getAccessTable()->getSchema()->getTable()
175                );
176
177                // trigger meta data rendering to set page title
178                p_get_metadata($id);
179            }
180        }
181
182        return true;
183    }
184}
185
186// vim:ts=4:sw=4:et:
187