1<?php 2/** 3 * DokuWiki Plugin structcombolookup (Action Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Szymon Olewniczak <it@rid.pl> 7 */ 8 9// must be run within Dokuwiki 10use dokuwiki\plugin\struct\meta\Search; 11use dokuwiki\plugin\structcombolookup\types\NarrowingLookup; 12 13if (!defined('DOKU_INC')) { 14 die(); 15} 16 17class action_plugin_structcombolookup extends DokuWiki_Action_Plugin 18{ 19 20 /** 21 * Registers a callback function for a given event 22 * 23 * @param Doku_Event_Handler $controller DokuWiki's event controller object 24 * 25 * @return void 26 */ 27 public function register(Doku_Event_Handler $controller) 28 { 29 $controller->register_hook('PLUGIN_STRUCT_TYPECLASS_INIT', 'BEFORE', $this, 'handle_plugin_struct_typeclass_init'); 30 $controller->register_hook('PLUGIN_BUREAUCRACY_TEMPLATE_SAVE', 'BEFORE', $this, 'handle_lookup_fields'); 31 32 } 33 34 /** 35 * [Custom event handler which performs action] 36 * 37 * Called for event: 38 * 39 * @param Doku_Event $event event object by reference 40 * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 41 * handler was registered] 42 * 43 * @return void 44 */ 45 public function handle_plugin_struct_typeclass_init(Doku_Event $event, $param) 46 { 47 $event->data['ComboLookup'] = 'dokuwiki\\plugin\\structcombolookup\\types\\ComboLookup'; 48 $event->data['NarrowingLookup'] = 'dokuwiki\\plugin\\structcombolookup\\types\\NarrowingLookup'; 49 50 } 51 52 public function handle_lookup_fields(Doku_Event $event, $param) { 53 /** @var helper_plugin_struct_field $field */ 54 foreach($event->data['fields'] as $field) { 55 if(!is_a($field, 'helper_plugin_struct_field')) continue; 56 if(!$field->column->getType() instanceof NarrowingLookup) continue; 57 58 $rawvalue = $field->getParam('value'); 59 60 $config = $field->column->getType()->getConfig(); 61 $search = new Search(); 62 $search->addSchema($config['schema']); 63 64 $schema = $search->getSchemas()[0]; 65 if ($schema->isLookup()) { 66 $id = '%rowid%'; 67 } else { 68 $id = '%pageid%'; 69 } 70 71 $search->addColumn($config['narrow by']); 72 $search->addFilter($id, $rawvalue, '='); 73 $result = $search->execute(); 74 //cannot determine parent 75 if (!isset($result[0][0])) continue; 76 $parentValue = $result[0][0]->getDisplayValue(); 77 78 $schemaName = $field->column->getTable(); 79 $colLabel = $field->column->getLabel(); 80 $key = "$schemaName.$colLabel.narrowBy"; 81 $event->data['patterns'][$key] = "/(@@|##)$schemaName\\.$colLabel\\.narrowBy\\1/"; 82 $event->data['values'][$key] = $parentValue; 83 } 84 return true; 85 } 86 87} 88 89