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 plugin\struct\meta\Assignments; 13use plugin\struct\meta\SchemaData; 14 15class action_plugin_struct_entry extends DokuWiki_Action_Plugin { 16 17 18 /** @var helper_plugin_sqlite */ 19 protected $sqlite; 20 21 /** 22 * Registers a callback function for a given event 23 * 24 * @param Doku_Event_Handler $controller DokuWiki's event controller object 25 * @return void 26 */ 27 public function register(Doku_Event_Handler $controller) { 28 29 $controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'handle_editform'); 30 $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_pagesave'); 31 32 } 33 34 /** 35 * Save values of Schemas but do not interfere with saving the page. 36 * 37 * @param Doku_Event $event event object by reference 38 * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 39 * handler was registered] 40 * @return bool 41 */ 42 public function handle_pagesave(Doku_Event &$event, $param) { 43 global $ID, $INPUT; 44 if (act_clean($event->data) !== "save") return false; 45 46 $assignments = new Assignments(); 47 $tables = $assignments->getPageAssignments($ID); 48 $structData = $INPUT->arr('Schema'); 49 $timestamp = time(); //FIXME we should use the time stamp used to save the page data 50 51 foreach ($tables as $table) { 52 $schema = new SchemaData($table, $ID, $timestamp); 53 if(!$schema->getId()) { 54 // this schema is not available for some reason. skip it 55 continue; 56 } 57 58 $schemaData = $structData[$table]; 59 foreach ($schema->getColumns() as $col) { 60 $type = $col->getType(); 61 $label = $type->getLabel(); 62 if ($type->isMulti() && !is_array($schemaData[$label])) { 63 $schemaData[$label] = $type->splitValues($schemaData[$label]); 64 } 65 } 66 $schema->saveData($schemaData); 67 } 68 return false; 69 } 70 71 /* 72 * Enhance the editing form with structural data editing 73 * 74 * @param Doku_Event $event event object by reference 75 * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 76 * handler was registered] 77 * @return bool 78 */ 79 public function handle_editform(Doku_Event $event, $param) { 80 global $ID; 81 82 $assignments = new Assignments(); 83 $tables = $assignments->getPageAssignments($ID); 84 85 $html = ''; 86 foreach($tables as $table) { 87 $html .= $this->createForm($table); 88 } 89 90 /** @var Doku_Form $form */ 91 $form = $event->data; 92 $html = "<div class=\"struct\">$html</div>"; 93 $pos = $form->findElementById('wiki__editbar'); // insert the form before the main buttons 94 $form->insertElement($pos, $html); 95 96 return true; 97 } 98 99 /** 100 * Create the form to edit schemadata 101 * 102 * @param string $tablename 103 * @return string The HTML for this schema's form 104 */ 105 protected function createForm($tablename) { 106 global $ID; 107 global $REV; 108 $schema = new SchemaData($tablename, $ID, $REV); 109 $schemadata = $schema->getData(); 110 111 $html = "<h3>$tablename</h3>"; 112 foreach($schemadata as $field) { 113 $label = $field->getColumn()->getLabel(); 114 $trans = hsc($field->getColumn()->getTranslatedLabel()); 115 $name = "Schema[$tablename][$label]"; 116 $input = $field->getValueEditor($name); 117 $element = "<label>$trans $input</label><br />"; 118 $html .= $element; 119 } 120 121 return $html; 122 } 123 124} 125 126// vim:ts=4:sw=4:et: 127