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; 15345c3838SSzymon Olewniczakuse dokuwiki\plugin\struct\meta\Search; 16e46eaffdSSzymon Olewniczakuse dokuwiki\plugin\struct\types\Lookup; 173ad9c1eaSAndreas Gohr 183ad9c1eaSAndreas Gohr/** 19cfe2b908SAndreas Gohr * Handles bureaucracy additions 203ad9c1eaSAndreas Gohr * 213ad9c1eaSAndreas Gohr * This registers to the template action of the bureaucracy plugin and saves all struct data 22cfe2b908SAndreas Gohr * submitted through the bureaucracy form to all newly created pages (if the schema applies). 23cfe2b908SAndreas Gohr * 24cfe2b908SAndreas Gohr * It also registers the struct_schema type for bureaucracy which will add all fields of the 25cfe2b908SAndreas Gohr * schema to the form. The struct_field type is added through standard naming convention - see 26cfe2b908SAndreas Gohr * helper/fiels.php for that. 273ad9c1eaSAndreas Gohr */ 283ad9c1eaSAndreas Gohrclass action_plugin_struct_bureaucracy extends DokuWiki_Action_Plugin { 293ad9c1eaSAndreas Gohr 303ad9c1eaSAndreas Gohr /** 313ad9c1eaSAndreas Gohr * Registers a callback function for a given event 323ad9c1eaSAndreas Gohr * 333ad9c1eaSAndreas Gohr * @param Doku_Event_Handler $controller DokuWiki's event controller object 343ad9c1eaSAndreas Gohr * @return void 353ad9c1eaSAndreas Gohr */ 363ad9c1eaSAndreas Gohr public function register(Doku_Event_Handler $controller) { 37345c3838SSzymon Olewniczak $controller->register_hook('PLUGIN_BUREAUCRACY_TEMPLATE_SAVE', 'BEFORE', $this, 'handle_lookup_fields'); 383ad9c1eaSAndreas Gohr $controller->register_hook('PLUGIN_BUREAUCRACY_TEMPLATE_SAVE', 'AFTER', $this, 'handle_save'); 39cfe2b908SAndreas Gohr $controller->register_hook('PLUGIN_BUREAUCRACY_FIELD_UNKNOWN', 'BEFORE', $this, 'handle_schema'); 40cfe2b908SAndreas Gohr } 41cfe2b908SAndreas Gohr 42cfe2b908SAndreas Gohr /** 43cfe2b908SAndreas Gohr * Load a whole schema as fields 44cfe2b908SAndreas Gohr * 45cfe2b908SAndreas Gohr * @param Doku_Event $event event object by reference 46cfe2b908SAndreas Gohr * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 47cfe2b908SAndreas Gohr * handler was registered] 48cfe2b908SAndreas Gohr * @return bool 49cfe2b908SAndreas Gohr */ 50cfe2b908SAndreas Gohr public function handle_schema(Doku_Event $event, $param) { 51cfe2b908SAndreas Gohr $args = $event->data['args']; 5293ca6f4fSAndreas Gohr if($args[0] != 'struct_schema') return false; 53cfe2b908SAndreas Gohr $event->preventDefault(); 54cfe2b908SAndreas Gohr $event->stopPropagation(); 55cfe2b908SAndreas Gohr 56cfe2b908SAndreas Gohr /** @var helper_plugin_bureaucracy_field $helper */ 57cfe2b908SAndreas Gohr $helper = plugin_load('helper', 'bureaucracy_field'); 58cfe2b908SAndreas Gohr $helper->initialize($args); 59cfe2b908SAndreas Gohr 60cfe2b908SAndreas Gohr $schema = new Schema($helper->opt['label']); 61cfe2b908SAndreas Gohr if(!$schema->getId()) { 62cfe2b908SAndreas Gohr msg('This schema does not exist', -1); 6393ca6f4fSAndreas Gohr return false; 64cfe2b908SAndreas Gohr } 65cfe2b908SAndreas Gohr 66cfe2b908SAndreas Gohr foreach($schema->getColumns(false) as $column) { 67cfe2b908SAndreas Gohr /** @var helper_plugin_struct_field $field */ 68cfe2b908SAndreas Gohr $field = plugin_load('helper', 'struct_field'); 69cfe2b908SAndreas Gohr // we don't initialize the field but set the appropriate values 70cfe2b908SAndreas Gohr $field->opt = $helper->opt; // copy all the settings to each field 71cfe2b908SAndreas Gohr $field->opt['label'] = $column->getFullQualifiedLabel(); 72cfe2b908SAndreas Gohr $field->column = $column; 73cfe2b908SAndreas Gohr $event->data['fields'][] = $field; 74cfe2b908SAndreas Gohr } 7593ca6f4fSAndreas Gohr return true; 763ad9c1eaSAndreas Gohr } 773ad9c1eaSAndreas Gohr 783ad9c1eaSAndreas Gohr /** 79345c3838SSzymon Olewniczak * Replace lookup fields placeholder's values 80345c3838SSzymon Olewniczak * 81345c3838SSzymon Olewniczak * @param Doku_Event $event event object by reference 82345c3838SSzymon Olewniczak * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 83345c3838SSzymon Olewniczak * handler was registered] 84345c3838SSzymon Olewniczak * @return bool 85345c3838SSzymon Olewniczak */ 86345c3838SSzymon Olewniczak public function handle_lookup_fields(Doku_Event $event, $param) { 87345c3838SSzymon Olewniczak foreach($event->data['fields'] as $field) { 88345c3838SSzymon Olewniczak if(!is_a($field, 'helper_plugin_struct_field')) continue; 89e46eaffdSSzymon Olewniczak if(!$field->column->getType() instanceof Lookup) continue; 90345c3838SSzymon Olewniczak 91987ccc7fSSzymon Olewniczak $value = $field->getParam('value'); 92987ccc7fSSzymon Olewniczak if (!is_array($value)) $value = array($value); 93987ccc7fSSzymon Olewniczak 94345c3838SSzymon Olewniczak $config = $field->column->getType()->getConfig(); 95345c3838SSzymon Olewniczak 96345c3838SSzymon Olewniczak // find proper value 97345c3838SSzymon Olewniczak // current Search implementation doesn't allow doing it using SQL 98345c3838SSzymon Olewniczak $search = new Search(); 99345c3838SSzymon Olewniczak $search->addSchema($config['schema']); 100345c3838SSzymon Olewniczak $search->addColumn($config['field']); 101345c3838SSzymon Olewniczak $result = $search->execute(); 102345c3838SSzymon Olewniczak $pids = $search->getPids(); 103345c3838SSzymon Olewniczak 104987ccc7fSSzymon Olewniczak $field->opt['struct_pids'] = array(); 105987ccc7fSSzymon Olewniczak $new_value = array(); 106987ccc7fSSzymon Olewniczak foreach ($value as $pid) { 107987ccc7fSSzymon Olewniczak for($i = 0; $i < count($result); $i++) { 108345c3838SSzymon Olewniczak if ($pids[$i] == $pid) { 109987ccc7fSSzymon Olewniczak $field->opt['struct_pids'][] = $pid; 110987ccc7fSSzymon Olewniczak $new_value[] = $result[$i][0]->getDisplayValue(); 111987ccc7fSSzymon Olewniczak } 112345c3838SSzymon Olewniczak } 113345c3838SSzymon Olewniczak } 114345c3838SSzymon Olewniczak 115345c3838SSzymon Olewniczak //replace previous value 116987ccc7fSSzymon Olewniczak if ($field->column->isMulti()) { 117987ccc7fSSzymon Olewniczak $field->opt['value'] = $new_value; 118987ccc7fSSzymon Olewniczak } else { 119987ccc7fSSzymon Olewniczak $event->data['values'][$field->column->getFullQualifiedLabel()] = $new_value[0]; 120345c3838SSzymon Olewniczak } 121987ccc7fSSzymon Olewniczak 122345c3838SSzymon Olewniczak } 123345c3838SSzymon Olewniczak return true; 124345c3838SSzymon Olewniczak } 125345c3838SSzymon Olewniczak 126345c3838SSzymon Olewniczak /** 1273ad9c1eaSAndreas Gohr * Save the struct data 1283ad9c1eaSAndreas Gohr * 1293ad9c1eaSAndreas Gohr * @param Doku_Event $event event object by reference 1303ad9c1eaSAndreas Gohr * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 1313ad9c1eaSAndreas Gohr * handler was registered] 1323ad9c1eaSAndreas Gohr * @return bool 1333ad9c1eaSAndreas Gohr */ 1343ad9c1eaSAndreas Gohr public function handle_save(Doku_Event $event, $param) { 1353ad9c1eaSAndreas Gohr // get all struct values and their associated schemas 1363ad9c1eaSAndreas Gohr $tosave = array(); 1373ad9c1eaSAndreas Gohr foreach($event->data['fields'] as $field) { 1383ad9c1eaSAndreas Gohr if(!is_a($field, 'helper_plugin_struct_field')) continue; 1393ad9c1eaSAndreas Gohr /** @var helper_plugin_struct_field $field */ 1403ad9c1eaSAndreas Gohr $tbl = $field->column->getTable(); 1413ad9c1eaSAndreas Gohr $lbl = $field->column->getLabel(); 1423ad9c1eaSAndreas Gohr if(!isset($tosave[$tbl])) $tosave[$tbl] = array(); 143987ccc7fSSzymon Olewniczak 144e46eaffdSSzymon Olewniczak if ($field->column->isMulti() && $field->column->getType() instanceof Lookup) { 145987ccc7fSSzymon Olewniczak $tosave[$tbl][$lbl] = $field->opt['struct_pids']; 146987ccc7fSSzymon Olewniczak } else { 1473ad9c1eaSAndreas Gohr $tosave[$tbl][$lbl] = $field->getParam('value'); 1483ad9c1eaSAndreas Gohr } 149987ccc7fSSzymon Olewniczak } 1503ad9c1eaSAndreas Gohr 1513ad9c1eaSAndreas Gohr // save all the struct data of assigned schemas 1523ad9c1eaSAndreas Gohr $id = $event->data['id']; 1533ad9c1eaSAndreas Gohr $time = filemtime(wikiFN($id)); 1549fc5ecc2SMichael Grosse 155025cb9daSAndreas Gohr $assignments = Assignments::getInstance(); 15693ca6f4fSAndreas Gohr $assigned = $assignments->getPageAssignments($id); 15793ca6f4fSAndreas Gohr foreach($tosave as $table => $data) { 15893ca6f4fSAndreas Gohr if(!in_array($table, $assigned)) continue; 15993ca6f4fSAndreas Gohr $access = AccessTable::byTableName($table, $id, $time); 16093ca6f4fSAndreas Gohr $validator = $access->getValidator($data); 16193ca6f4fSAndreas Gohr if($validator->validate()) { 16293ca6f4fSAndreas Gohr $validator->saveData($time); 1635c5d9fe5SMichael Grosse 1645c5d9fe5SMichael Grosse // make sure this schema is assigned 1655c5d9fe5SMichael Grosse $assignments->assignPageSchema( 1665c5d9fe5SMichael Grosse $id, 1675c5d9fe5SMichael Grosse $validator->getAccessTable()->getSchema()->getTable() 1685c5d9fe5SMichael Grosse ); 1690e120badSMichael Grosse 1700e120badSMichael Grosse // trigger meta data rendering to set page title 171*2a09b9b1SAnna Dabrowska p_get_metadata($id); 17293ca6f4fSAndreas Gohr } 1733ad9c1eaSAndreas Gohr } 1743ad9c1eaSAndreas Gohr 1753ad9c1eaSAndreas Gohr return true; 1763ad9c1eaSAndreas Gohr } 1773ad9c1eaSAndreas Gohr 1783ad9c1eaSAndreas Gohr} 1793ad9c1eaSAndreas Gohr 1803ad9c1eaSAndreas Gohr// vim:ts=4:sw=4:et: 181