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