xref: /plugin/struct/action/bureaucracy.php (revision 3ad9c1eaefd634b03b03b85327536e38c6df025a)
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 plugin\struct\meta\Assignments;
13use plugin\struct\meta\SchemaData;
14use plugin\struct\meta\ValidationException;
15use plugin\struct\meta\Validator;
16use plugin\struct\meta\Value;
17use plugin\struct\types\AbstractBaseType;
18
19/**
20 * Handles saving from bureaucracy forms
21 *
22 * This registers to the template action of the bureaucracy plugin and saves all struct data
23 * submitted through the bureaucracy form to all newly created pages (if the schema applies)
24 */
25class action_plugin_struct_bureaucracy extends DokuWiki_Action_Plugin {
26
27    /**
28     * Registers a callback function for a given event
29     *
30     * @param Doku_Event_Handler $controller DokuWiki's event controller object
31     * @return void
32     */
33    public function register(Doku_Event_Handler $controller) {
34        $controller->register_hook('PLUGIN_BUREAUCRACY_TEMPLATE_SAVE', 'AFTER', $this, 'handle_save');
35    }
36
37    /**
38     * Save the struct data
39     *
40     * @param Doku_Event $event event object by reference
41     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
42     *                           handler was registered]
43     * @return bool
44     */
45    public function handle_save(Doku_Event $event, $param) {
46        // get all struct values and their associated schemas
47        $tosave = array();
48        foreach($event->data['fields'] as $field) {
49            if(!is_a($field, 'helper_plugin_struct_field')) continue;
50            /** @var helper_plugin_struct_field $field */
51            $tbl = $field->column->getTable();
52            $lbl = $field->column->getLabel();
53            if(!isset($tosave[$tbl])) $tosave[$tbl] = array();
54            $tosave[$tbl][$lbl] = $field->getParam('value');
55        }
56
57        // save all the struct data of assigned schemas
58        $id = $event->data['id'];
59
60        $validator = new Validator();
61        if(!$validator->validate($tosave, $id)) return false;
62        $tosave = $validator->getCleanedData();
63        foreach($tosave as $table => $data) {
64            $time = filemtime(wikiFN($id));
65            $schemaData = new SchemaData($table, $id, $time);
66            $schemaData->saveData($data);
67        }
68
69        return true;
70    }
71
72}
73
74// vim:ts=4:sw=4:et:
75