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\AccessTable; 13use dokuwiki\plugin\struct\meta\Assignments; 14use dokuwiki\plugin\struct\meta\Schema; 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 false; 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 false; 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 return true; 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 $time = filemtime(wikiFN($id)); 98 99 $assignments = Assignments::getInstance(); 100 $assigned = $assignments->getPageAssignments($id); 101 foreach($tosave as $table => $data) { 102 if(!in_array($table, $assigned)) continue; 103 $access = AccessTable::byTableName($table, $id, $time); 104 $validator = $access->getValidator($data); 105 if($validator->validate()) { 106 $validator->saveData($time); 107 } 108 } 109 110 return true; 111 } 112 113} 114 115// vim:ts=4:sw=4:et: 116