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