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\SchemaData; 13 14class action_plugin_struct_entry extends DokuWiki_Action_Plugin { 15 16 17 /** @var helper_plugin_sqlite */ 18 protected $sqlite; 19 20 /** 21 * Registers a callback function for a given event 22 * 23 * @param Doku_Event_Handler $controller DokuWiki's event controller object 24 * @return void 25 */ 26 public function register(Doku_Event_Handler $controller) { 27 28 $controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'handle_editform'); 29 $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_pagesave'); 30 31 } 32 33 /** 34 * Save values of Schemas but do not interfere with saving the page. 35 * 36 * @param Doku_Event $event event object by reference 37 * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 38 * handler was registered] 39 * @return bool 40 */ 41 public function handle_pagesave(Doku_Event &$event, $param) { 42 global $ID, $INPUT; 43 if (act_clean($event->data) !== "save") return false; 44 45 /** @var \helper_plugin_struct_db $helper */ 46 $helper = plugin_load('helper', 'struct_db'); 47 $this->sqlite = $helper->getDB(); 48 49 $res = $this->sqlite->query("SELECT tbl FROM schema_assignments WHERE assign = ?",array($ID,)); 50 if (!$this->sqlite->res2count($res)) return false; 51 52 $tables = array_map( 53 function ($value) { 54 return $value['tbl']; 55 }, 56 $this->sqlite->res2arr($res) 57 ); 58 $this->sqlite->res_close($res); 59 60 $structData = $INPUT->arr('Schema'); 61 $timestamp = $INPUT->int('date'); 62 63 foreach ($tables as $table) { 64 $schema = new SchemaData($table, $ID, $timestamp); 65 $schemaData = $structData[$table]; 66 foreach ($schema->getColumns() as $col) { 67 $type = $col->getType(); 68 $label = $type->getLabel(); 69 if ($type->isMulti() && !is_array($schemaData[$label])) { 70 $schemaData[$label] = $type->splitValues($schemaData[$label]); 71 } 72 } 73 $schema->saveData($schemaData); 74 } 75 return false; 76 } 77 78 /* 79 * Enhance the editing form with structural data editing 80 * 81 * @param Doku_Event $event event object by reference 82 * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 83 * handler was registered] 84 * @return bool 85 */ 86 public function handle_editform(Doku_Event $event, $param) { 87 global $ID; 88 /** @var \helper_plugin_struct_db $helper */ 89 $helper = plugin_load('helper', 'struct_db'); 90 $this->sqlite = $helper->getDB(); 91 92 $res = $this->sqlite->query("SELECT tbl FROM schema_assignments WHERE assign = ?", array($ID,)); 93 if(!$this->sqlite->res2count($res)) return false; 94 95 $tables = array_map( 96 function ($value) { 97 return $value['tbl']; 98 }, 99 $this->sqlite->res2arr($res) 100 ); 101 $this->sqlite->res_close($res); 102 103 $html = ''; 104 foreach($tables as $table) { 105 $html .= $this->createForm($table); 106 } 107 108 /** @var Doku_Form $form */ 109 $form = $event->data; 110 $html = "<div class=\"struct\">$html</div>"; 111 $pos = $form->findElementById('wiki__editbar'); // insert the form before the main buttons 112 $form->insertElement($pos, $html); 113 114 return true; 115 } 116 117 /** 118 * Create the form to edit schemadata 119 * 120 * @param string $tablename 121 * @return string The HTML for this schema's form 122 */ 123 protected function createForm($tablename) { 124 global $ID; 125 global $REV; 126 $schema = new SchemaData($tablename, $ID, $REV); 127 $schemadata = $schema->getData(); 128 129 $html = "<h3>$tablename</h3>"; 130 $cols = $schema->getColumns(false); 131 foreach ($cols as $index => $col) { 132 $type = $col->getType(); 133 $label = $type->getLabel(); 134 $name = "Schema[$tablename][$label]"; 135 if($type->isMulti()) { 136 $input = $type->multiValueEditor($name, $schemadata[$label]); 137 } else { 138 $input = $type->valueEditor($name, $schemadata[$label]); 139 } 140 $element = "<label>$label $input</label><br />"; 141 $html .= $element; 142 } 143 144 return $html; 145 } 146 147} 148 149// vim:ts=4:sw=4:et: 150