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