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 /** @var \helper_plugin_struct_db $helper */ 47 $helper = plugin_load('helper', 'struct_db'); 48 $this->sqlite = $helper->getDB(); 49 50 $res = $this->sqlite->query("SELECT tbl FROM schema_assignments WHERE assign = ?",array($ID,)); 51 if (!$this->sqlite->res2count($res)) return false; 52 53 $tables = array_map( 54 function ($value) { 55 return $value['tbl']; 56 }, 57 $this->sqlite->res2arr($res) 58 ); 59 $this->sqlite->res_close($res); 60 61 $structData = $INPUT->arr('Schema'); 62 $timestamp = time(); //FIXME we should use the time stamp used to save the page data 63 64 foreach ($tables as $table) { 65 $schema = new SchemaData($table, $ID, $timestamp); 66 if(!$schema->getId()) { 67 // this schema is not available for some reason. skip it 68 continue; 69 } 70 71 $schemaData = $structData[$table]; 72 foreach ($schema->getColumns() as $col) { 73 $type = $col->getType(); 74 $label = $type->getLabel(); 75 if ($type->isMulti() && !is_array($schemaData[$label])) { 76 $schemaData[$label] = $type->splitValues($schemaData[$label]); 77 } 78 } 79 $schema->saveData($schemaData); 80 } 81 return false; 82 } 83 84 /* 85 * Enhance the editing form with structural data editing 86 * 87 * @param Doku_Event $event event object by reference 88 * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 89 * handler was registered] 90 * @return bool 91 */ 92 public function handle_editform(Doku_Event $event, $param) { 93 global $ID; 94 95 $assignments = new Assignments(); 96 $tables = $assignments->getPageAssignments($ID); 97 98 $html = ''; 99 foreach($tables as $table) { 100 $html .= $this->createForm($table); 101 } 102 103 /** @var Doku_Form $form */ 104 $form = $event->data; 105 $html = "<div class=\"struct\">$html</div>"; 106 $pos = $form->findElementById('wiki__editbar'); // insert the form before the main buttons 107 $form->insertElement($pos, $html); 108 109 return true; 110 } 111 112 /** 113 * Create the form to edit schemadata 114 * 115 * @param string $tablename 116 * @return string The HTML for this schema's form 117 */ 118 protected function createForm($tablename) { 119 global $ID; 120 global $REV; 121 $schema = new SchemaData($tablename, $ID, $REV); 122 $schemadata = $schema->getData(); 123 124 $html = "<h3>$tablename</h3>"; 125 foreach($schemadata as $field) { 126 $label = $field->getColumn()->getLabel(); 127 $trans = hsc($field->getColumn()->getTranslatedLabel()); 128 $name = "Schema[$tablename][$label]"; 129 $input = $field->getValueEditor($name); 130 $element = "<label>$trans $input</label><br />"; 131 $html .= $element; 132 } 133 134 return $html; 135 } 136 137} 138 139// vim:ts=4:sw=4:et: 140