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