1549a0837SAndreas Gohr<?php 2549a0837SAndreas Gohr/** 3549a0837SAndreas Gohr * DokuWiki Plugin struct (Action Component) 4549a0837SAndreas Gohr * 5549a0837SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6549a0837SAndreas Gohr * @author Andreas Gohr, Michael Große <dokuwiki@cosmocode.de> 7549a0837SAndreas Gohr */ 8549a0837SAndreas Gohr 9549a0837SAndreas Gohr// must be run within Dokuwiki 10549a0837SAndreas Gohrif(!defined('DOKU_INC')) die(); 11549a0837SAndreas Gohr 12c2fd0bf0SMichael Großeuse plugin\struct\meta\SchemaData; 13c2fd0bf0SMichael Große 14549a0837SAndreas Gohrclass action_plugin_struct_entry extends DokuWiki_Action_Plugin { 15549a0837SAndreas Gohr 16c2fd0bf0SMichael Große 17c2fd0bf0SMichael Große /** @var helper_plugin_sqlite */ 18c2fd0bf0SMichael Große protected $sqlite; 19c2fd0bf0SMichael Große 20549a0837SAndreas Gohr /** 21549a0837SAndreas Gohr * Registers a callback function for a given event 22549a0837SAndreas Gohr * 23549a0837SAndreas Gohr * @param Doku_Event_Handler $controller DokuWiki's event controller object 24549a0837SAndreas Gohr * @return void 25549a0837SAndreas Gohr */ 26549a0837SAndreas Gohr public function register(Doku_Event_Handler $controller) { 27549a0837SAndreas Gohr 28c2fd0bf0SMichael Große $controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'handle_editform'); 29*04641d56SMichael Große $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_pagesave'); 30549a0837SAndreas Gohr 31549a0837SAndreas Gohr } 32549a0837SAndreas Gohr 33549a0837SAndreas Gohr /** 34*04641d56SMichael Große * Save values of Schemas but do not interfere with saving the page. 35*04641d56SMichael Große * 36*04641d56SMichael Große * @param Doku_Event $event event object by reference 37*04641d56SMichael Große * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 38*04641d56SMichael Große * handler was registered] 39*04641d56SMichael Große * @return bool 40*04641d56SMichael Große */ 41*04641d56SMichael Große public function handle_pagesave(Doku_Event &$event, $param) { 42*04641d56SMichael Große if (act_clean($event->data) !== "save") return false; 43*04641d56SMichael Große 44*04641d56SMichael Große /** @var \helper_plugin_struct_db $helper */ 45*04641d56SMichael Große $helper = plugin_load('helper', 'struct_db'); 46*04641d56SMichael Große $this->sqlite = $helper->getDB(); 47*04641d56SMichael Große 48*04641d56SMichael Große global $ID, $INPUT; 49*04641d56SMichael Große 50*04641d56SMichael Große $res = $this->sqlite->query("SELECT tbl FROM schema_assignments WHERE assign = ?",array($ID,)); 51*04641d56SMichael Große if (!$this->sqlite->res2count($res)) return false; 52*04641d56SMichael Große 53*04641d56SMichael Große $tables = array_map(function ($value){return $value['tbl'];},$this->sqlite->res2arr($res)); 54*04641d56SMichael Große $this->sqlite->res_close($res); 55*04641d56SMichael Große 56*04641d56SMichael Große $structData = $INPUT->arr('Schema'); 57*04641d56SMichael Große $timestamp = $INPUT->int('date'); 58*04641d56SMichael Große 59*04641d56SMichael Große foreach ($tables as $table) { 60*04641d56SMichael Große $schema = new SchemaData($table, $ID, $timestamp); 61*04641d56SMichael Große $schemaData = $structData[$table]; 62*04641d56SMichael Große foreach ($schema->getColumns() as $col) { 63*04641d56SMichael Große if ($col->getType()->isMulti()) { 64*04641d56SMichael Große $schemaData[$col->getType()->getLabel()] = explode(',',$schemaData[$col->getType()->getLabel()]); 65*04641d56SMichael Große $schemaData[$col->getType()->getLabel()] = array_map(function($value){return trim($value);}, $schemaData[$col->getType()->getLabel()]); 66*04641d56SMichael Große } 67*04641d56SMichael Große } 68*04641d56SMichael Große $schema->saveData($schemaData); 69*04641d56SMichael Große } 70*04641d56SMichael Große return false; 71*04641d56SMichael Große } 72*04641d56SMichael Große 73*04641d56SMichael Große /** 74549a0837SAndreas Gohr * [Custom event handler which performs action] 75549a0837SAndreas Gohr * 76549a0837SAndreas Gohr * @param Doku_Event $event event object by reference 77549a0837SAndreas Gohr * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 78549a0837SAndreas Gohr * handler was registered] 79c2fd0bf0SMichael Große * @return bool 80549a0837SAndreas Gohr */ 81c2fd0bf0SMichael Große public function handle_editform(Doku_Event &$event, $param) { 82c2fd0bf0SMichael Große 83c2fd0bf0SMichael Große /** @var \helper_plugin_struct_db $helper */ 84c2fd0bf0SMichael Große $helper = plugin_load('helper', 'struct_db'); 85c2fd0bf0SMichael Große $this->sqlite = $helper->getDB(); 86c2fd0bf0SMichael Große 87c2fd0bf0SMichael Große global $ID; 88c2fd0bf0SMichael Große 89c2fd0bf0SMichael Große $res = $this->sqlite->query("SELECT tbl FROM schema_assignments WHERE assign = ?",array($ID,)); 90c2fd0bf0SMichael Große if (!$this->sqlite->res2count($res)) return false; 91c2fd0bf0SMichael Große 92c2fd0bf0SMichael Große $tables = array_map(function ($value){return $value['tbl'];},$this->sqlite->res2arr($res)); 93*04641d56SMichael Große $this->sqlite->res_close($res); 94c2fd0bf0SMichael Große 95c2fd0bf0SMichael Große foreach ($tables as $table) { 96c2fd0bf0SMichael Große $this->createForm($table, $event->data); 97c2fd0bf0SMichael Große } 98c2fd0bf0SMichael Große 99c2fd0bf0SMichael Große return true; 100c2fd0bf0SMichael Große } 101c2fd0bf0SMichael Große 102c2fd0bf0SMichael Große /** 103c2fd0bf0SMichael Große * @param string $tablename 104c2fd0bf0SMichael Große * @param Doku_Form $data 105c2fd0bf0SMichael Große */ 106c2fd0bf0SMichael Große private function createForm($tablename, &$data) { 107c2fd0bf0SMichael Große global $ID; 108c2fd0bf0SMichael Große $schema = new SchemaData($tablename, $ID, 0); 109c2fd0bf0SMichael Große $schemadata = $schema->getData(); 110c2fd0bf0SMichael Große 111c2fd0bf0SMichael Große $data->insertElement(4, "<h3>$tablename</h3>"); 112c2fd0bf0SMichael Große $cols = $schema->getColumns(); 113c2fd0bf0SMichael Große usort($cols, function($a, $b){if ($a->getSort()<$b->getSort())return -1;return 1;}); 114c2fd0bf0SMichael Große 115c2fd0bf0SMichael Große foreach ($cols as $index => $col) { 116c2fd0bf0SMichael Große $type = $col->getType(); 117c2fd0bf0SMichael Große $label = $type->getLabel(); 118c2fd0bf0SMichael Große $name = "Schema[$tablename][$label]"; 119c2fd0bf0SMichael Große $input = $type->valueEditor($name, $schemadata[$label]); 120c2fd0bf0SMichael Große $element = "<label>$label $input</label><br />"; 121c2fd0bf0SMichael Große $data->insertElement(5 + $index, $element); 122c2fd0bf0SMichael Große } 123549a0837SAndreas Gohr } 124549a0837SAndreas Gohr 125549a0837SAndreas Gohr} 126549a0837SAndreas Gohr 127549a0837SAndreas Gohr// vim:ts=4:sw=4:et: 128