xref: /plugin/struct/action/bureaucracy.php (revision 9fc5ecc2263354abd0d12ae477d68c624fdf63ee)
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
12*9fc5ecc2SMichael Grosseuse dokuwiki\plugin\struct\meta\Assignments;
13ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Schema;
14ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaData;
15ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Validator;
163ad9c1eaSAndreas Gohr
173ad9c1eaSAndreas Gohr/**
18cfe2b908SAndreas Gohr * Handles bureaucracy additions
193ad9c1eaSAndreas Gohr *
203ad9c1eaSAndreas Gohr * This registers to the template action of the bureaucracy plugin and saves all struct data
21cfe2b908SAndreas Gohr * submitted through the bureaucracy form to all newly created pages (if the schema applies).
22cfe2b908SAndreas Gohr *
23cfe2b908SAndreas Gohr * It also registers the struct_schema type for bureaucracy which will add all fields of the
24cfe2b908SAndreas Gohr * schema to the form. The struct_field type is added through standard naming convention - see
25cfe2b908SAndreas Gohr * helper/fiels.php for that.
263ad9c1eaSAndreas Gohr */
273ad9c1eaSAndreas Gohrclass action_plugin_struct_bureaucracy extends DokuWiki_Action_Plugin {
283ad9c1eaSAndreas Gohr
293ad9c1eaSAndreas Gohr    /**
303ad9c1eaSAndreas Gohr     * Registers a callback function for a given event
313ad9c1eaSAndreas Gohr     *
323ad9c1eaSAndreas Gohr     * @param Doku_Event_Handler $controller DokuWiki's event controller object
333ad9c1eaSAndreas Gohr     * @return void
343ad9c1eaSAndreas Gohr     */
353ad9c1eaSAndreas Gohr    public function register(Doku_Event_Handler $controller) {
363ad9c1eaSAndreas Gohr        $controller->register_hook('PLUGIN_BUREAUCRACY_TEMPLATE_SAVE', 'AFTER', $this, 'handle_save');
37cfe2b908SAndreas Gohr        $controller->register_hook('PLUGIN_BUREAUCRACY_FIELD_UNKNOWN', 'BEFORE', $this, 'handle_schema');
38cfe2b908SAndreas Gohr    }
39cfe2b908SAndreas Gohr
40cfe2b908SAndreas Gohr    /**
41cfe2b908SAndreas Gohr     * Load a whole schema as fields
42cfe2b908SAndreas Gohr     *
43cfe2b908SAndreas Gohr     * @param Doku_Event $event event object by reference
44cfe2b908SAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
45cfe2b908SAndreas Gohr     *                           handler was registered]
46cfe2b908SAndreas Gohr     * @return bool
47cfe2b908SAndreas Gohr     */
48cfe2b908SAndreas Gohr    public function handle_schema(Doku_Event $event, $param) {
49cfe2b908SAndreas Gohr        $args = $event->data['args'];
50cfe2b908SAndreas Gohr        if($args[0] != 'struct_schema') return;
51cfe2b908SAndreas Gohr        $event->preventDefault();
52cfe2b908SAndreas Gohr        $event->stopPropagation();
53cfe2b908SAndreas Gohr
54cfe2b908SAndreas Gohr        /** @var helper_plugin_bureaucracy_field $helper */
55cfe2b908SAndreas Gohr        $helper = plugin_load('helper', 'bureaucracy_field');
56cfe2b908SAndreas Gohr        $helper->initialize($args);
57cfe2b908SAndreas Gohr
58cfe2b908SAndreas Gohr        $schema = new Schema($helper->opt['label']);
59cfe2b908SAndreas Gohr        if(!$schema->getId()) {
60cfe2b908SAndreas Gohr            msg('This schema does not exist', -1);
61cfe2b908SAndreas Gohr            return;
62cfe2b908SAndreas Gohr        }
63cfe2b908SAndreas Gohr
64cfe2b908SAndreas Gohr        foreach($schema->getColumns(false) as $column) {
65cfe2b908SAndreas Gohr            /** @var helper_plugin_struct_field $field */
66cfe2b908SAndreas Gohr            $field = plugin_load('helper', 'struct_field');
67cfe2b908SAndreas Gohr            // we don't initialize the field but set the appropriate values
68cfe2b908SAndreas Gohr            $field->opt = $helper->opt; // copy all the settings to each field
69cfe2b908SAndreas Gohr            $field->opt['label'] = $column->getFullQualifiedLabel();
70cfe2b908SAndreas Gohr            $field->column = $column;
71cfe2b908SAndreas Gohr            $event->data['fields'][] = $field;
72cfe2b908SAndreas Gohr        }
733ad9c1eaSAndreas Gohr    }
743ad9c1eaSAndreas Gohr
753ad9c1eaSAndreas Gohr    /**
763ad9c1eaSAndreas Gohr     * Save the struct data
773ad9c1eaSAndreas Gohr     *
783ad9c1eaSAndreas Gohr     * @param Doku_Event $event event object by reference
793ad9c1eaSAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
803ad9c1eaSAndreas Gohr     *                           handler was registered]
813ad9c1eaSAndreas Gohr     * @return bool
823ad9c1eaSAndreas Gohr     */
833ad9c1eaSAndreas Gohr    public function handle_save(Doku_Event $event, $param) {
843ad9c1eaSAndreas Gohr        // get all struct values and their associated schemas
853ad9c1eaSAndreas Gohr        $tosave = array();
863ad9c1eaSAndreas Gohr        foreach($event->data['fields'] as $field) {
873ad9c1eaSAndreas Gohr            if(!is_a($field, 'helper_plugin_struct_field')) continue;
883ad9c1eaSAndreas Gohr            /** @var helper_plugin_struct_field $field */
893ad9c1eaSAndreas Gohr            $tbl = $field->column->getTable();
903ad9c1eaSAndreas Gohr            $lbl = $field->column->getLabel();
913ad9c1eaSAndreas Gohr            if(!isset($tosave[$tbl])) $tosave[$tbl] = array();
923ad9c1eaSAndreas Gohr            $tosave[$tbl][$lbl] = $field->getParam('value');
933ad9c1eaSAndreas Gohr        }
943ad9c1eaSAndreas Gohr
953ad9c1eaSAndreas Gohr        // save all the struct data of assigned schemas
963ad9c1eaSAndreas Gohr        $id = $event->data['id'];
973ad9c1eaSAndreas Gohr
983ad9c1eaSAndreas Gohr        $validator = new Validator();
993ad9c1eaSAndreas Gohr        if(!$validator->validate($tosave, $id)) return false;
1003ad9c1eaSAndreas Gohr        $tosave = $validator->getCleanedData();
1013ad9c1eaSAndreas Gohr        foreach($tosave as $table => $data) {
1023ad9c1eaSAndreas Gohr            $time = filemtime(wikiFN($id));
1033ad9c1eaSAndreas Gohr            $schemaData = new SchemaData($table, $id, $time);
1043ad9c1eaSAndreas Gohr            $schemaData->saveData($data);
105*9fc5ecc2SMichael Grosse
106*9fc5ecc2SMichael Grosse            $assignments = new Assignments();
107*9fc5ecc2SMichael Grosse            $assignments->assignPageSchema($id, $table);
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