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