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