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'); 2904641d56SMichael Große $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_pagesave'); 30549a0837SAndreas Gohr 31549a0837SAndreas Gohr } 32549a0837SAndreas Gohr 33549a0837SAndreas Gohr /** 3404641d56SMichael Große * Save values of Schemas but do not interfere with saving the page. 3504641d56SMichael Große * 3604641d56SMichael Große * @param Doku_Event $event event object by reference 3704641d56SMichael Große * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 3804641d56SMichael Große * handler was registered] 3904641d56SMichael Große * @return bool 4004641d56SMichael Große */ 4104641d56SMichael Große public function handle_pagesave(Doku_Event &$event, $param) { 42*3ece9074SMichael Große global $ID, $INPUT; 4304641d56SMichael Große if (act_clean($event->data) !== "save") return false; 4404641d56SMichael Große 4504641d56SMichael Große /** @var \helper_plugin_struct_db $helper */ 4604641d56SMichael Große $helper = plugin_load('helper', 'struct_db'); 4704641d56SMichael Große $this->sqlite = $helper->getDB(); 4804641d56SMichael Große 4904641d56SMichael Große $res = $this->sqlite->query("SELECT tbl FROM schema_assignments WHERE assign = ?",array($ID,)); 5004641d56SMichael Große if (!$this->sqlite->res2count($res)) return false; 5104641d56SMichael Große 52*3ece9074SMichael Große $tables = array_map( 53*3ece9074SMichael Große function ($value) { 54*3ece9074SMichael Große return $value['tbl']; 55*3ece9074SMichael Große }, 56*3ece9074SMichael Große $this->sqlite->res2arr($res) 57*3ece9074SMichael Große ); 5804641d56SMichael Große $this->sqlite->res_close($res); 5904641d56SMichael Große 6004641d56SMichael Große $structData = $INPUT->arr('Schema'); 6104641d56SMichael Große $timestamp = $INPUT->int('date'); 6204641d56SMichael Große 6304641d56SMichael Große foreach ($tables as $table) { 6404641d56SMichael Große $schema = new SchemaData($table, $ID, $timestamp); 6504641d56SMichael Große $schemaData = $structData[$table]; 6604641d56SMichael Große foreach ($schema->getColumns() as $col) { 6704641d56SMichael Große if ($col->getType()->isMulti()) { 68*3ece9074SMichael Große $label = $col->getType()->getLabel(); 69*3ece9074SMichael Große $schemaData[$label] = explode(',',$schemaData[$label]); 70*3ece9074SMichael Große $schemaData[$label] = array_map( 71*3ece9074SMichael Große function($value) { 72*3ece9074SMichael Große return trim($value); 73*3ece9074SMichael Große }, 74*3ece9074SMichael Große $schemaData[$label] 75*3ece9074SMichael Große ); 7604641d56SMichael Große } 7704641d56SMichael Große } 7804641d56SMichael Große $schema->saveData($schemaData); 7904641d56SMichael Große } 8004641d56SMichael Große return false; 8104641d56SMichael Große } 8204641d56SMichael Große 83016bf382SMichael Große /* 84f36fda9dSAndreas Gohr * Enhance the editing form with structural data editing 85549a0837SAndreas Gohr * 86549a0837SAndreas Gohr * @param Doku_Event $event event object by reference 87549a0837SAndreas Gohr * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 88549a0837SAndreas Gohr * handler was registered] 89c2fd0bf0SMichael Große * @return bool 90549a0837SAndreas Gohr */ 91c07703d4SAndreas Gohr public function handle_editform(Doku_Event $event, $param) { 9265598e4aSAndreas Gohr global $ID; 93c2fd0bf0SMichael Große /** @var \helper_plugin_struct_db $helper */ 94c2fd0bf0SMichael Große $helper = plugin_load('helper', 'struct_db'); 95c2fd0bf0SMichael Große $this->sqlite = $helper->getDB(); 96c2fd0bf0SMichael Große 97c2fd0bf0SMichael Große $res = $this->sqlite->query("SELECT tbl FROM schema_assignments WHERE assign = ?", array($ID,)); 98c2fd0bf0SMichael Große if(!$this->sqlite->res2count($res)) return false; 99c2fd0bf0SMichael Große 10065598e4aSAndreas Gohr $tables = array_map( 10165598e4aSAndreas Gohr function ($value) { 10265598e4aSAndreas Gohr return $value['tbl']; 10365598e4aSAndreas Gohr }, 10465598e4aSAndreas Gohr $this->sqlite->res2arr($res) 10565598e4aSAndreas Gohr ); 106*3ece9074SMichael Große $this->sqlite->res_close($res); 107c2fd0bf0SMichael Große 10865598e4aSAndreas Gohr $html = ''; 109c2fd0bf0SMichael Große foreach($tables as $table) { 11065598e4aSAndreas Gohr $html .= $this->createForm($table); 111c2fd0bf0SMichael Große } 112c2fd0bf0SMichael Große 11365598e4aSAndreas Gohr /** @var Doku_Form $form */ 11465598e4aSAndreas Gohr $form = $event->data; 11565598e4aSAndreas Gohr $html = "<div class=\"struct\">$html</div>"; 11665598e4aSAndreas Gohr $pos = $form->findElementById('wiki__editbar'); // insert the form before the main buttons 11765598e4aSAndreas Gohr $form->insertElement($pos, $html); 11865598e4aSAndreas Gohr 119c2fd0bf0SMichael Große return true; 120c2fd0bf0SMichael Große } 121c2fd0bf0SMichael Große 122c2fd0bf0SMichael Große /** 12365598e4aSAndreas Gohr * Create the form to edit schemadata 124f36fda9dSAndreas Gohr * 125c2fd0bf0SMichael Große * @param string $tablename 12665598e4aSAndreas Gohr * @return string The HTML for this schema's form 127c2fd0bf0SMichael Große */ 12865598e4aSAndreas Gohr protected function createForm($tablename) { 129c2fd0bf0SMichael Große global $ID; 13083beda18SAndreas Gohr global $REV; 13183beda18SAndreas Gohr $schema = new SchemaData($tablename, $ID, $REV); 132c2fd0bf0SMichael Große $schemadata = $schema->getData(); 133c2fd0bf0SMichael Große 13465598e4aSAndreas Gohr $html = "<h3>$tablename</h3>"; 135750a393bSAndreas Gohr $cols = $schema->getColumns(false); 136c2fd0bf0SMichael Große foreach ($cols as $index => $col) { 137c2fd0bf0SMichael Große $type = $col->getType(); 138c2fd0bf0SMichael Große $label = $type->getLabel(); 139c2fd0bf0SMichael Große $name = "Schema[$tablename][$label]"; 140c2fd0bf0SMichael Große $input = $type->valueEditor($name, $schemadata[$label]); 141c2fd0bf0SMichael Große $element = "<label>$label $input</label><br />"; 14265598e4aSAndreas Gohr $html .= $element; 143c2fd0bf0SMichael Große } 14465598e4aSAndreas Gohr 14565598e4aSAndreas Gohr return $html; 146549a0837SAndreas Gohr } 147549a0837SAndreas Gohr 148549a0837SAndreas Gohr} 149549a0837SAndreas Gohr 150549a0837SAndreas Gohr// vim:ts=4:sw=4:et: 151