1<?php 2/** 3 * DokuWiki Plugin struct (Action Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Andreas Gohr, Michael Große <dokuwiki@cosmocode.de> 7 */ 8 9// must be run within Dokuwiki 10if(!defined('DOKU_INC')) die(); 11 12use dokuwiki\plugin\struct\meta\Schema; 13use dokuwiki\plugin\struct\meta\SchemaData; 14use dokuwiki\plugin\struct\meta\Validator; 15 16/** 17 * Handles bureaucracy additions 18 * 19 * This registers to the template action of the bureaucracy plugin and saves all struct data 20 * submitted through the bureaucracy form to all newly created pages (if the schema applies). 21 * 22 * It also registers the struct_schema type for bureaucracy which will add all fields of the 23 * schema to the form. The struct_field type is added through standard naming convention - see 24 * helper/fiels.php for that. 25 */ 26class action_plugin_struct_bureaucracy extends DokuWiki_Action_Plugin { 27 28 /** 29 * Registers a callback function for a given event 30 * 31 * @param Doku_Event_Handler $controller DokuWiki's event controller object 32 * @return void 33 */ 34 public function register(Doku_Event_Handler $controller) { 35 $controller->register_hook('PLUGIN_BUREAUCRACY_TEMPLATE_SAVE', 'AFTER', $this, 'handle_save'); 36 $controller->register_hook('PLUGIN_BUREAUCRACY_FIELD_UNKNOWN', 'BEFORE', $this, 'handle_schema'); 37 } 38 39 /** 40 * Load a whole schema as fields 41 * 42 * @param Doku_Event $event event object by reference 43 * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 44 * handler was registered] 45 * @return bool 46 */ 47 public function handle_schema(Doku_Event $event, $param) { 48 $args = $event->data['args']; 49 if($args[0] != 'struct_schema') return; 50 $event->preventDefault(); 51 $event->stopPropagation(); 52 53 /** @var helper_plugin_bureaucracy_field $helper */ 54 $helper = plugin_load('helper', 'bureaucracy_field'); 55 $helper->initialize($args); 56 57 $schema = new Schema($helper->opt['label']); 58 if(!$schema->getId()) { 59 msg('This schema does not exist', -1); 60 return; 61 } 62 63 foreach($schema->getColumns(false) as $column) { 64 /** @var helper_plugin_struct_field $field */ 65 $field = plugin_load('helper', 'struct_field'); 66 // we don't initialize the field but set the appropriate values 67 $field->opt = $helper->opt; // copy all the settings to each field 68 $field->opt['label'] = $column->getFullQualifiedLabel(); 69 $field->column = $column; 70 $event->data['fields'][] = $field; 71 } 72 } 73 74 /** 75 * Save the struct data 76 * 77 * @param Doku_Event $event event object by reference 78 * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 79 * handler was registered] 80 * @return bool 81 */ 82 public function handle_save(Doku_Event $event, $param) { 83 // get all struct values and their associated schemas 84 $tosave = array(); 85 foreach($event->data['fields'] as $field) { 86 if(!is_a($field, 'helper_plugin_struct_field')) continue; 87 /** @var helper_plugin_struct_field $field */ 88 $tbl = $field->column->getTable(); 89 $lbl = $field->column->getLabel(); 90 if(!isset($tosave[$tbl])) $tosave[$tbl] = array(); 91 $tosave[$tbl][$lbl] = $field->getParam('value'); 92 } 93 94 // save all the struct data of assigned schemas 95 $id = $event->data['id']; 96 97 $validator = new Validator(); 98 if(!$validator->validate($tosave, $id)) return false; 99 $tosave = $validator->getCleanedData(); 100 foreach($tosave as $table => $data) { 101 $time = filemtime(wikiFN($id)); 102 $schemaData = new SchemaData($table, $id, $time); 103 $schemaData->saveData($data); 104 } 105 106 return true; 107 } 108 109} 110 111// vim:ts=4:sw=4:et: 112