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; 15use dokuwiki\plugin\struct\meta\Search; 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', 'BEFORE', $this, 'handle_lookup_fields'); 37 $controller->register_hook('PLUGIN_BUREAUCRACY_TEMPLATE_SAVE', 'AFTER', $this, 'handle_save'); 38 $controller->register_hook('PLUGIN_BUREAUCRACY_FIELD_UNKNOWN', 'BEFORE', $this, 'handle_schema'); 39 } 40 41 /** 42 * Load a whole schema as fields 43 * 44 * @param Doku_Event $event event object by reference 45 * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 46 * handler was registered] 47 * @return bool 48 */ 49 public function handle_schema(Doku_Event $event, $param) { 50 $args = $event->data['args']; 51 if($args[0] != 'struct_schema') return false; 52 $event->preventDefault(); 53 $event->stopPropagation(); 54 55 /** @var helper_plugin_bureaucracy_field $helper */ 56 $helper = plugin_load('helper', 'bureaucracy_field'); 57 $helper->initialize($args); 58 59 $schema = new Schema($helper->opt['label']); 60 if(!$schema->getId()) { 61 msg('This schema does not exist', -1); 62 return false; 63 } 64 65 foreach($schema->getColumns(false) as $column) { 66 /** @var helper_plugin_struct_field $field */ 67 $field = plugin_load('helper', 'struct_field'); 68 // we don't initialize the field but set the appropriate values 69 $field->opt = $helper->opt; // copy all the settings to each field 70 $field->opt['label'] = $column->getFullQualifiedLabel(); 71 $field->column = $column; 72 $event->data['fields'][] = $field; 73 } 74 return true; 75 } 76 77 /** 78 * Replace lookup fields placeholder's values 79 * 80 * @param Doku_Event $event event object by reference 81 * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 82 * handler was registered] 83 * @return bool 84 */ 85 public function handle_lookup_fields(Doku_Event $event, $param) { 86 foreach($event->data['fields'] as $field) { 87 if(!is_a($field, 'helper_plugin_struct_field')) continue; 88 if($field->column->getType()->getClass() != 'Lookup') continue; 89 90 $pid = $field->getParam('value'); 91 $config = $field->column->getType()->getConfig(); 92 93 // find proper value 94 // current Search implementation doesn't allow doing it using SQL 95 $search = new Search(); 96 $search->addSchema($config['schema']); 97 $search->addColumn($config['field']); 98 $result = $search->execute(); 99 $pids = $search->getPids(); 100 $len = count($result); 101 102 $value = ''; 103 for($i = 0; $i < $len; $i++) { 104 if ($pids[$i] == $pid) { 105 $value = $result[$i][0]->getDisplayValue(); 106 break; 107 } 108 } 109 110 //replace previous value 111 if ($value) { 112 $event->data['values'][$field->column->getFullQualifiedLabel()] = $value; 113 } 114 } 115 return true; 116 } 117 118 /** 119 * Save the struct data 120 * 121 * @param Doku_Event $event event object by reference 122 * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 123 * handler was registered] 124 * @return bool 125 */ 126 public function handle_save(Doku_Event $event, $param) { 127 // get all struct values and their associated schemas 128 $tosave = array(); 129 foreach($event->data['fields'] as $field) { 130 if(!is_a($field, 'helper_plugin_struct_field')) continue; 131 /** @var helper_plugin_struct_field $field */ 132 $tbl = $field->column->getTable(); 133 $lbl = $field->column->getLabel(); 134 if(!isset($tosave[$tbl])) $tosave[$tbl] = array(); 135 $tosave[$tbl][$lbl] = $field->getParam('value'); 136 } 137 138 // save all the struct data of assigned schemas 139 $id = $event->data['id']; 140 $time = filemtime(wikiFN($id)); 141 142 $assignments = Assignments::getInstance(); 143 $assigned = $assignments->getPageAssignments($id); 144 foreach($tosave as $table => $data) { 145 if(!in_array($table, $assigned)) continue; 146 $access = AccessTable::byTableName($table, $id, $time); 147 $validator = $access->getValidator($data); 148 if($validator->validate()) { 149 $validator->saveData($time); 150 151 // make sure this schema is assigned 152 $assignments->assignPageSchema( 153 $id, 154 $validator->getAccessTable()->getSchema()->getTable() 155 ); 156 157 // trigger meta data rendering to set page title 158 // expire the cache in order to correctly render the struct header on the first page visit 159 p_get_metadata($id, array('cache' => 'expire')); 160 } 161 } 162 163 return true; 164 } 165 166} 167 168// vim:ts=4:sw=4:et: 169