13ad9c1eaSAndreas Gohr<?php 23ad9c1eaSAndreas Gohr/** 33ad9c1eaSAndreas Gohr * DokuWiki Plugin struct (Action Component) 43ad9c1eaSAndreas Gohr * 53ad9c1eaSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 63ad9c1eaSAndreas Gohr * @author Andreas Gohr, Michael Große <dokuwiki@cosmocode.de> 73ad9c1eaSAndreas Gohr */ 83ad9c1eaSAndreas Gohr 93ad9c1eaSAndreas Gohr// must be run within Dokuwiki 103ad9c1eaSAndreas Gohrif(!defined('DOKU_INC')) die(); 113ad9c1eaSAndreas Gohr 12f411d872SAndreas Gohruse dokuwiki\plugin\struct\meta\AccessTable; 139fc5ecc2SMichael Grosseuse dokuwiki\plugin\struct\meta\Assignments; 14ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Schema; 153ad9c1eaSAndreas Gohr 163ad9c1eaSAndreas Gohr/** 17cfe2b908SAndreas Gohr * Handles bureaucracy additions 183ad9c1eaSAndreas Gohr * 193ad9c1eaSAndreas Gohr * This registers to the template action of the bureaucracy plugin and saves all struct data 20cfe2b908SAndreas Gohr * submitted through the bureaucracy form to all newly created pages (if the schema applies). 21cfe2b908SAndreas Gohr * 22cfe2b908SAndreas Gohr * It also registers the struct_schema type for bureaucracy which will add all fields of the 23cfe2b908SAndreas Gohr * schema to the form. The struct_field type is added through standard naming convention - see 24cfe2b908SAndreas Gohr * helper/fiels.php for that. 253ad9c1eaSAndreas Gohr */ 263ad9c1eaSAndreas Gohrclass action_plugin_struct_bureaucracy extends DokuWiki_Action_Plugin { 273ad9c1eaSAndreas Gohr 283ad9c1eaSAndreas Gohr /** 293ad9c1eaSAndreas Gohr * Registers a callback function for a given event 303ad9c1eaSAndreas Gohr * 313ad9c1eaSAndreas Gohr * @param Doku_Event_Handler $controller DokuWiki's event controller object 323ad9c1eaSAndreas Gohr * @return void 333ad9c1eaSAndreas Gohr */ 343ad9c1eaSAndreas Gohr public function register(Doku_Event_Handler $controller) { 353ad9c1eaSAndreas Gohr $controller->register_hook('PLUGIN_BUREAUCRACY_TEMPLATE_SAVE', 'AFTER', $this, 'handle_save'); 36cfe2b908SAndreas Gohr $controller->register_hook('PLUGIN_BUREAUCRACY_FIELD_UNKNOWN', 'BEFORE', $this, 'handle_schema'); 37cfe2b908SAndreas Gohr } 38cfe2b908SAndreas Gohr 39cfe2b908SAndreas Gohr /** 40cfe2b908SAndreas Gohr * Load a whole schema as fields 41cfe2b908SAndreas Gohr * 42cfe2b908SAndreas Gohr * @param Doku_Event $event event object by reference 43cfe2b908SAndreas Gohr * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 44cfe2b908SAndreas Gohr * handler was registered] 45cfe2b908SAndreas Gohr * @return bool 46cfe2b908SAndreas Gohr */ 47cfe2b908SAndreas Gohr public function handle_schema(Doku_Event $event, $param) { 48cfe2b908SAndreas Gohr $args = $event->data['args']; 49*93ca6f4fSAndreas Gohr if($args[0] != 'struct_schema') return false; 50cfe2b908SAndreas Gohr $event->preventDefault(); 51cfe2b908SAndreas Gohr $event->stopPropagation(); 52cfe2b908SAndreas Gohr 53cfe2b908SAndreas Gohr /** @var helper_plugin_bureaucracy_field $helper */ 54cfe2b908SAndreas Gohr $helper = plugin_load('helper', 'bureaucracy_field'); 55cfe2b908SAndreas Gohr $helper->initialize($args); 56cfe2b908SAndreas Gohr 57cfe2b908SAndreas Gohr $schema = new Schema($helper->opt['label']); 58cfe2b908SAndreas Gohr if(!$schema->getId()) { 59cfe2b908SAndreas Gohr msg('This schema does not exist', -1); 60*93ca6f4fSAndreas Gohr return false; 61cfe2b908SAndreas Gohr } 62cfe2b908SAndreas Gohr 63cfe2b908SAndreas Gohr foreach($schema->getColumns(false) as $column) { 64cfe2b908SAndreas Gohr /** @var helper_plugin_struct_field $field */ 65cfe2b908SAndreas Gohr $field = plugin_load('helper', 'struct_field'); 66cfe2b908SAndreas Gohr // we don't initialize the field but set the appropriate values 67cfe2b908SAndreas Gohr $field->opt = $helper->opt; // copy all the settings to each field 68cfe2b908SAndreas Gohr $field->opt['label'] = $column->getFullQualifiedLabel(); 69cfe2b908SAndreas Gohr $field->column = $column; 70cfe2b908SAndreas Gohr $event->data['fields'][] = $field; 71cfe2b908SAndreas Gohr } 72*93ca6f4fSAndreas Gohr return true; 733ad9c1eaSAndreas Gohr } 743ad9c1eaSAndreas Gohr 753ad9c1eaSAndreas Gohr /** 763ad9c1eaSAndreas Gohr * Save the struct data 773ad9c1eaSAndreas Gohr * 783ad9c1eaSAndreas Gohr * @param Doku_Event $event event object by reference 793ad9c1eaSAndreas Gohr * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 803ad9c1eaSAndreas Gohr * handler was registered] 813ad9c1eaSAndreas Gohr * @return bool 823ad9c1eaSAndreas Gohr */ 833ad9c1eaSAndreas Gohr public function handle_save(Doku_Event $event, $param) { 843ad9c1eaSAndreas Gohr // get all struct values and their associated schemas 853ad9c1eaSAndreas Gohr $tosave = array(); 863ad9c1eaSAndreas Gohr foreach($event->data['fields'] as $field) { 873ad9c1eaSAndreas Gohr if(!is_a($field, 'helper_plugin_struct_field')) continue; 883ad9c1eaSAndreas Gohr /** @var helper_plugin_struct_field $field */ 893ad9c1eaSAndreas Gohr $tbl = $field->column->getTable(); 903ad9c1eaSAndreas Gohr $lbl = $field->column->getLabel(); 913ad9c1eaSAndreas Gohr if(!isset($tosave[$tbl])) $tosave[$tbl] = array(); 923ad9c1eaSAndreas Gohr $tosave[$tbl][$lbl] = $field->getParam('value'); 933ad9c1eaSAndreas Gohr } 943ad9c1eaSAndreas Gohr 953ad9c1eaSAndreas Gohr // save all the struct data of assigned schemas 963ad9c1eaSAndreas Gohr $id = $event->data['id']; 973ad9c1eaSAndreas Gohr $time = filemtime(wikiFN($id)); 989fc5ecc2SMichael Grosse 999fc5ecc2SMichael Grosse $assignments = new Assignments(); 100*93ca6f4fSAndreas Gohr $assigned = $assignments->getPageAssignments($id); 101*93ca6f4fSAndreas Gohr foreach($tosave as $table => $data) { 102*93ca6f4fSAndreas Gohr if(!in_array($table, $assigned)) continue; 103*93ca6f4fSAndreas Gohr $access = AccessTable::byTableName($table, $id, $time); 104*93ca6f4fSAndreas Gohr $validator = $access->getValidator($data); 105*93ca6f4fSAndreas Gohr if($validator->validate()) { 106*93ca6f4fSAndreas Gohr $validator->saveData($time); 107*93ca6f4fSAndreas Gohr } 1083ad9c1eaSAndreas Gohr } 1093ad9c1eaSAndreas Gohr 1103ad9c1eaSAndreas Gohr return true; 1113ad9c1eaSAndreas Gohr } 1123ad9c1eaSAndreas Gohr 1133ad9c1eaSAndreas Gohr} 1143ad9c1eaSAndreas Gohr 1153ad9c1eaSAndreas Gohr// vim:ts=4:sw=4:et: 116