xref: /plugin/struct/action/bureaucracy.php (revision cfe2b908e94f36e919e231bdfc92bce35779838e)
13ad9c1eaSAndreas Gohr<?php
23ad9c1eaSAndreas Gohr/**
33ad9c1eaSAndreas Gohr * DokuWiki Plugin struct (Action Component)
43ad9c1eaSAndreas Gohr *
53ad9c1eaSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
63ad9c1eaSAndreas Gohr * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
73ad9c1eaSAndreas Gohr */
83ad9c1eaSAndreas Gohr
93ad9c1eaSAndreas Gohr// must be run within Dokuwiki
103ad9c1eaSAndreas Gohrif(!defined('DOKU_INC')) die();
113ad9c1eaSAndreas Gohr
123ad9c1eaSAndreas Gohruse plugin\struct\meta\Assignments;
13*cfe2b908SAndreas Gohruse plugin\struct\meta\Schema;
143ad9c1eaSAndreas Gohruse plugin\struct\meta\SchemaData;
153ad9c1eaSAndreas Gohruse plugin\struct\meta\ValidationException;
163ad9c1eaSAndreas Gohruse plugin\struct\meta\Validator;
173ad9c1eaSAndreas Gohruse plugin\struct\meta\Value;
183ad9c1eaSAndreas Gohruse plugin\struct\types\AbstractBaseType;
193ad9c1eaSAndreas Gohr
203ad9c1eaSAndreas Gohr/**
21*cfe2b908SAndreas Gohr * Handles bureaucracy additions
223ad9c1eaSAndreas Gohr *
233ad9c1eaSAndreas Gohr * This registers to the template action of the bureaucracy plugin and saves all struct data
24*cfe2b908SAndreas Gohr * submitted through the bureaucracy form to all newly created pages (if the schema applies).
25*cfe2b908SAndreas Gohr *
26*cfe2b908SAndreas Gohr * It also registers the struct_schema type for bureaucracy which will add all fields of the
27*cfe2b908SAndreas Gohr * schema to the form. The struct_field type is added through standard naming convention - see
28*cfe2b908SAndreas Gohr * helper/fiels.php for that.
293ad9c1eaSAndreas Gohr */
303ad9c1eaSAndreas Gohrclass action_plugin_struct_bureaucracy extends DokuWiki_Action_Plugin {
313ad9c1eaSAndreas Gohr
323ad9c1eaSAndreas Gohr    /**
333ad9c1eaSAndreas Gohr     * Registers a callback function for a given event
343ad9c1eaSAndreas Gohr     *
353ad9c1eaSAndreas Gohr     * @param Doku_Event_Handler $controller DokuWiki's event controller object
363ad9c1eaSAndreas Gohr     * @return void
373ad9c1eaSAndreas Gohr     */
383ad9c1eaSAndreas Gohr    public function register(Doku_Event_Handler $controller) {
393ad9c1eaSAndreas Gohr        $controller->register_hook('PLUGIN_BUREAUCRACY_TEMPLATE_SAVE', 'AFTER', $this, 'handle_save');
40*cfe2b908SAndreas Gohr        $controller->register_hook('PLUGIN_BUREAUCRACY_FIELD_UNKNOWN', 'BEFORE', $this, 'handle_schema');
41*cfe2b908SAndreas Gohr    }
42*cfe2b908SAndreas Gohr
43*cfe2b908SAndreas Gohr    /**
44*cfe2b908SAndreas Gohr     * Load a whole schema as fields
45*cfe2b908SAndreas Gohr     *
46*cfe2b908SAndreas Gohr     * @param Doku_Event $event event object by reference
47*cfe2b908SAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
48*cfe2b908SAndreas Gohr     *                           handler was registered]
49*cfe2b908SAndreas Gohr     * @return bool
50*cfe2b908SAndreas Gohr     */
51*cfe2b908SAndreas Gohr    public function handle_schema(Doku_Event $event, $param) {
52*cfe2b908SAndreas Gohr        $args = $event->data['args'];
53*cfe2b908SAndreas Gohr        if($args[0] != 'struct_schema') return;
54*cfe2b908SAndreas Gohr        $event->preventDefault();
55*cfe2b908SAndreas Gohr        $event->stopPropagation();
56*cfe2b908SAndreas Gohr
57*cfe2b908SAndreas Gohr        /** @var helper_plugin_bureaucracy_field $helper */
58*cfe2b908SAndreas Gohr        $helper = plugin_load('helper', 'bureaucracy_field');
59*cfe2b908SAndreas Gohr        $helper->initialize($args);
60*cfe2b908SAndreas Gohr
61*cfe2b908SAndreas Gohr        $schema = new Schema($helper->opt['label']);
62*cfe2b908SAndreas Gohr        if(!$schema->getId()) {
63*cfe2b908SAndreas Gohr            msg('This schema does not exist', -1);
64*cfe2b908SAndreas Gohr            return;
65*cfe2b908SAndreas Gohr        }
66*cfe2b908SAndreas Gohr
67*cfe2b908SAndreas Gohr        foreach($schema->getColumns(false) as $column) {
68*cfe2b908SAndreas Gohr            /** @var helper_plugin_struct_field $field */
69*cfe2b908SAndreas Gohr            $field = plugin_load('helper', 'struct_field');
70*cfe2b908SAndreas Gohr            // we don't initialize the field but set the appropriate values
71*cfe2b908SAndreas Gohr            $field->opt = $helper->opt; // copy all the settings to each field
72*cfe2b908SAndreas Gohr            $field->opt['label'] = $column->getFullQualifiedLabel();
73*cfe2b908SAndreas Gohr            $field->column = $column;
74*cfe2b908SAndreas Gohr            $event->data['fields'][] = $field;
75*cfe2b908SAndreas Gohr        }
763ad9c1eaSAndreas Gohr    }
773ad9c1eaSAndreas Gohr
783ad9c1eaSAndreas Gohr    /**
793ad9c1eaSAndreas Gohr     * Save the struct data
803ad9c1eaSAndreas Gohr     *
813ad9c1eaSAndreas Gohr     * @param Doku_Event $event event object by reference
823ad9c1eaSAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
833ad9c1eaSAndreas Gohr     *                           handler was registered]
843ad9c1eaSAndreas Gohr     * @return bool
853ad9c1eaSAndreas Gohr     */
863ad9c1eaSAndreas Gohr    public function handle_save(Doku_Event $event, $param) {
873ad9c1eaSAndreas Gohr        // get all struct values and their associated schemas
883ad9c1eaSAndreas Gohr        $tosave = array();
893ad9c1eaSAndreas Gohr        foreach($event->data['fields'] as $field) {
903ad9c1eaSAndreas Gohr            if(!is_a($field, 'helper_plugin_struct_field')) continue;
913ad9c1eaSAndreas Gohr            /** @var helper_plugin_struct_field $field */
923ad9c1eaSAndreas Gohr            $tbl = $field->column->getTable();
933ad9c1eaSAndreas Gohr            $lbl = $field->column->getLabel();
943ad9c1eaSAndreas Gohr            if(!isset($tosave[$tbl])) $tosave[$tbl] = array();
953ad9c1eaSAndreas Gohr            $tosave[$tbl][$lbl] = $field->getParam('value');
963ad9c1eaSAndreas Gohr        }
973ad9c1eaSAndreas Gohr
983ad9c1eaSAndreas Gohr        // save all the struct data of assigned schemas
993ad9c1eaSAndreas Gohr        $id = $event->data['id'];
1003ad9c1eaSAndreas Gohr
1013ad9c1eaSAndreas Gohr        $validator = new Validator();
1023ad9c1eaSAndreas Gohr        if(!$validator->validate($tosave, $id)) return false;
1033ad9c1eaSAndreas Gohr        $tosave = $validator->getCleanedData();
1043ad9c1eaSAndreas Gohr        foreach($tosave as $table => $data) {
1053ad9c1eaSAndreas Gohr            $time = filemtime(wikiFN($id));
1063ad9c1eaSAndreas Gohr            $schemaData = new SchemaData($table, $id, $time);
1073ad9c1eaSAndreas Gohr            $schemaData->saveData($data);
1083ad9c1eaSAndreas Gohr        }
1093ad9c1eaSAndreas Gohr
1103ad9c1eaSAndreas Gohr        return true;
1113ad9c1eaSAndreas Gohr    }
1123ad9c1eaSAndreas Gohr
1133ad9c1eaSAndreas Gohr}
1143ad9c1eaSAndreas Gohr
1153ad9c1eaSAndreas Gohr// vim:ts=4:sw=4:et:
116