xref: /plugin/struct/action/bureaucracy.php (revision fc26989e4442674ded604c12f445cb8ca3510cf9)
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\AccessTableData;
16use dokuwiki\plugin\struct\meta\Validator;
17
18/**
19 * Handles bureaucracy additions
20 *
21 * This registers to the template action of the bureaucracy plugin and saves all struct data
22 * submitted through the bureaucracy form to all newly created pages (if the schema applies).
23 *
24 * It also registers the struct_schema type for bureaucracy which will add all fields of the
25 * schema to the form. The struct_field type is added through standard naming convention - see
26 * helper/fiels.php for that.
27 */
28class action_plugin_struct_bureaucracy extends DokuWiki_Action_Plugin {
29
30    /**
31     * Registers a callback function for a given event
32     *
33     * @param Doku_Event_Handler $controller DokuWiki's event controller object
34     * @return void
35     */
36    public function register(Doku_Event_Handler $controller) {
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;
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;
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    }
75
76    /**
77     * Save the struct data
78     *
79     * @param Doku_Event $event event object by reference
80     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
81     *                           handler was registered]
82     * @return bool
83     */
84    public function handle_save(Doku_Event $event, $param) {
85        // get all struct values and their associated schemas
86        $tosave = array();
87        foreach($event->data['fields'] as $field) {
88            if(!is_a($field, 'helper_plugin_struct_field')) continue;
89            /** @var helper_plugin_struct_field $field */
90            $tbl = $field->column->getTable();
91            $lbl = $field->column->getLabel();
92            if(!isset($tosave[$tbl])) $tosave[$tbl] = array();
93            $tosave[$tbl][$lbl] = $field->getParam('value');
94        }
95
96        // save all the struct data of assigned schemas
97        $id = $event->data['id'];
98
99        $validator = new Validator();
100        if(!$validator->validate($tosave, $id)) return false;
101        $tosave = $validator->getCleanedData();
102        foreach($tosave as $table => $data) {
103            $time = filemtime(wikiFN($id));
104            $schemaData = AccessTable::byTableName($table, $id, $time);
105            $schemaData->saveData($data);
106
107            $assignments = new Assignments();
108            $assignments->assignPageSchema($id, $table);
109        }
110
111        return true;
112    }
113
114}
115
116// vim:ts=4:sw=4:et:
117