187fdbc6bSMichael Große<?php 287fdbc6bSMichael Große/** 387fdbc6bSMichael Große * DokuWiki Plugin struct (Admin Component) 487fdbc6bSMichael Große * 587fdbc6bSMichael Große * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 687fdbc6bSMichael Große * @author Andreas Gohr, Michael Große <dokuwiki@cosmocode.de> 787fdbc6bSMichael Große */ 887fdbc6bSMichael Große 987fdbc6bSMichael Großeuse dokuwiki\Form\Form; 10ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Schema; 11ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaBuilder; 12ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaEditor; 13ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaImporter; 14ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\StructException; 1587fdbc6bSMichael Große 16d5a1a6dcSAndreas Gohr// must be run within Dokuwiki 1787fdbc6bSMichael Großeif(!defined('DOKU_INC')) die(); 1887fdbc6bSMichael Große 1987fdbc6bSMichael Großeclass admin_plugin_struct_schemas extends DokuWiki_Admin_Plugin { 2087fdbc6bSMichael Große 2187fdbc6bSMichael Große /** 2287fdbc6bSMichael Große * @return int sort number in admin menu 2387fdbc6bSMichael Große */ 2487fdbc6bSMichael Große public function getMenuSort() { 2587fdbc6bSMichael Große return 500; 2687fdbc6bSMichael Große } 2787fdbc6bSMichael Große 2887fdbc6bSMichael Große /** 2987fdbc6bSMichael Große * @return bool true if only access for superuser, false is for superusers and moderators 3087fdbc6bSMichael Große */ 3187fdbc6bSMichael Große public function forAdminOnly() { 324d220607SAndreas Gohr return false; 3387fdbc6bSMichael Große } 3487fdbc6bSMichael Große 3587fdbc6bSMichael Große /** 3687fdbc6bSMichael Große * Should carry out any processing required by the plugin. 3787fdbc6bSMichael Große */ 3887fdbc6bSMichael Große public function handle() { 3987fdbc6bSMichael Große global $INPUT; 40d5a1a6dcSAndreas Gohr global $ID; 41*e33460e2SMichael Grosse global $config_cascade; 42*e33460e2SMichael Grosse $config_file_path = end($config_cascade['main']['local']); 4387fdbc6bSMichael Große 448ddf87afSAndreas Gohr // form submit 4587fdbc6bSMichael Große $table = Schema::cleanTableName($INPUT->str('table')); 4687fdbc6bSMichael Große if($table && $INPUT->bool('save') && checkSecurityToken()) { 47d5a1a6dcSAndreas Gohr $builder = new SchemaBuilder($table, $INPUT->arr('schema')); 4887fdbc6bSMichael Große if(!$builder->build()) { 4987fdbc6bSMichael Große msg('something went wrong while saving', -1); 5087fdbc6bSMichael Große } 51*e33460e2SMichael Grosse touch($config_file_path); 5287fdbc6bSMichael Große } 538ddf87afSAndreas Gohr // export 54d486d6d7SAndreas Gohr if($table && $INPUT->bool('export')) { 55d5a1a6dcSAndreas Gohr $builder = new Schema($table); 56d486d6d7SAndreas Gohr header('Content-Type: application/json'); 57d486d6d7SAndreas Gohr header("Content-Disposition: attachment; filename=$table.struct.json"); 58d486d6d7SAndreas Gohr echo $builder->toJSON(); 59d486d6d7SAndreas Gohr exit; 60d486d6d7SAndreas Gohr } 618ddf87afSAndreas Gohr // import 628ddf87afSAndreas Gohr if($table && $INPUT->bool('import')) { 638ddf87afSAndreas Gohr if(isset($_FILES['schemafile']['tmp_name'])) { 648ddf87afSAndreas Gohr $json = io_readFile($_FILES['schemafile']['tmp_name'], false); 658ddf87afSAndreas Gohr if(!$json) { 668ddf87afSAndreas Gohr msg('Something went wrong with the upload', -1); 678ddf87afSAndreas Gohr } else { 68d5a1a6dcSAndreas Gohr $builder = new SchemaImporter($table, $json); 698ddf87afSAndreas Gohr if(!$builder->build()) { 708ddf87afSAndreas Gohr msg('something went wrong while saving', -1); 718ddf87afSAndreas Gohr } 72*e33460e2SMichael Grosse touch($config_file_path); 738ddf87afSAndreas Gohr } 748ddf87afSAndreas Gohr } 758ddf87afSAndreas Gohr } 76d5a1a6dcSAndreas Gohr // delete 77d5a1a6dcSAndreas Gohr if($table && $INPUT->bool('delete')) { 78d5a1a6dcSAndreas Gohr if($table != $INPUT->str('confirm')) { 79d5a1a6dcSAndreas Gohr msg($this->getLang('del_fail'), -1); 80d5a1a6dcSAndreas Gohr } else { 81d5a1a6dcSAndreas Gohr try { 82d5a1a6dcSAndreas Gohr $schema = new Schema($table); 83d5a1a6dcSAndreas Gohr $schema->delete(); 84d5a1a6dcSAndreas Gohr msg($this->getLang('del_ok'), 1); 85*e33460e2SMichael Grosse touch($config_file_path); 86d5a1a6dcSAndreas Gohr send_redirect(wl($ID, array('do' => 'admin', 'page' => 'struct_schemas'), true, '&')); 87d5a1a6dcSAndreas Gohr } catch(StructException $e) { 88d5a1a6dcSAndreas Gohr msg(hsc($e->getMessage()), -1); 89d5a1a6dcSAndreas Gohr } 90d5a1a6dcSAndreas Gohr } 91d5a1a6dcSAndreas Gohr } 92d5a1a6dcSAndreas Gohr 9387fdbc6bSMichael Große } 9487fdbc6bSMichael Große 9587fdbc6bSMichael Große /** 9687fdbc6bSMichael Große * Render HTML output, e.g. helpful text and a form 9787fdbc6bSMichael Große */ 9887fdbc6bSMichael Große public function html() { 9987fdbc6bSMichael Große global $INPUT; 10087fdbc6bSMichael Große 10187fdbc6bSMichael Große $table = Schema::cleanTableName($INPUT->str('table')); 10287fdbc6bSMichael Große if($table) { 1036af24d3eSAndreas Gohr echo $this->locale_xhtml('editor_edit'); 1046af24d3eSAndreas Gohr echo '<h2>' . sprintf($this->getLang('edithl'), hsc($table)) . '</h2>'; 1058ddf87afSAndreas Gohr 1068ddf87afSAndreas Gohr echo '<ul class="tabs" id="plugin__struct_tabs">'; 1078ddf87afSAndreas Gohr /** @noinspection HtmlUnknownAnchorTarget */ 1088ddf87afSAndreas Gohr echo '<li class="active"><a href="#plugin__struct_editor">' . $this->getLang('tab_edit') . '</a></li>'; 1098ddf87afSAndreas Gohr /** @noinspection HtmlUnknownAnchorTarget */ 1108ddf87afSAndreas Gohr echo '<li><a href="#plugin__struct_json">' . $this->getLang('tab_export') . '</a></li>'; 111d5a1a6dcSAndreas Gohr /** @noinspection HtmlUnknownAnchorTarget */ 112d5a1a6dcSAndreas Gohr echo '<li><a href="#plugin__struct_delete">' . $this->getLang('tab_delete') . '</a></li>'; 1138ddf87afSAndreas Gohr echo '</ul>'; 1148ddf87afSAndreas Gohr echo '<div class="panelHeader"></div>'; 1158ddf87afSAndreas Gohr 11687fdbc6bSMichael Große $editor = new SchemaEditor(new Schema($table)); 11787fdbc6bSMichael Große echo $editor->getEditor(); 1188ddf87afSAndreas Gohr echo $this->html_json(); 119d5a1a6dcSAndreas Gohr echo $this->html_delete(); 120d486d6d7SAndreas Gohr 12187fdbc6bSMichael Große } else { 1226af24d3eSAndreas Gohr echo $this->locale_xhtml('editor_intro'); 1238ddf87afSAndreas Gohr echo $this->html_newschema(); 12487fdbc6bSMichael Große } 12587fdbc6bSMichael Große } 12687fdbc6bSMichael Große 12787fdbc6bSMichael Große /** 1288ddf87afSAndreas Gohr * Form for handling import/export from/to JSON 1298ddf87afSAndreas Gohr * @return string 1308ddf87afSAndreas Gohr */ 1318ddf87afSAndreas Gohr protected function html_json() { 1328ddf87afSAndreas Gohr global $INPUT; 1338ddf87afSAndreas Gohr $table = Schema::cleanTableName($INPUT->str('table')); 1348ddf87afSAndreas Gohr 1358ddf87afSAndreas Gohr $form = new Form(array('enctype' => 'multipart/form-data', 'id' => 'plugin__struct_json')); 1368ddf87afSAndreas Gohr $form->setHiddenField('do', 'admin'); 1378ddf87afSAndreas Gohr $form->setHiddenField('page', 'struct_schemas'); 1388ddf87afSAndreas Gohr $form->setHiddenField('table', $table); 1398ddf87afSAndreas Gohr 1408ddf87afSAndreas Gohr $form->addFieldsetOpen($this->getLang('export')); 1418ddf87afSAndreas Gohr $form->addButton('export', $this->getLang('btn_export')); 1428ddf87afSAndreas Gohr $form->addFieldsetClose(); 1438ddf87afSAndreas Gohr 1448ddf87afSAndreas Gohr $form->addFieldsetOpen($this->getLang('import')); 1458ddf87afSAndreas Gohr $form->addElement(new \dokuwiki\Form\InputElement('file', 'schemafile')); 1468ddf87afSAndreas Gohr $form->addButton('import', $this->getLang('btn_import')); 1478ddf87afSAndreas Gohr $form->addHTML('<p>' . $this->getLang('import_warning') . '</p>'); 1488ddf87afSAndreas Gohr $form->addFieldsetClose(); 1498ddf87afSAndreas Gohr return $form->toHTML(); 1508ddf87afSAndreas Gohr } 1518ddf87afSAndreas Gohr 1528ddf87afSAndreas Gohr /** 153d5a1a6dcSAndreas Gohr * Form for deleting schemas 154d5a1a6dcSAndreas Gohr * @return string 155d5a1a6dcSAndreas Gohr */ 156d5a1a6dcSAndreas Gohr protected function html_delete() { 157d5a1a6dcSAndreas Gohr global $INPUT; 158d5a1a6dcSAndreas Gohr $table = Schema::cleanTableName($INPUT->str('table')); 159d5a1a6dcSAndreas Gohr 160d5a1a6dcSAndreas Gohr $form = new Form(array('id' => 'plugin__struct_delete')); 161d5a1a6dcSAndreas Gohr $form->setHiddenField('do', 'admin'); 162d5a1a6dcSAndreas Gohr $form->setHiddenField('page', 'struct_schemas'); 163d5a1a6dcSAndreas Gohr $form->setHiddenField('table', $table); 164d5a1a6dcSAndreas Gohr 165d5a1a6dcSAndreas Gohr $form->addHTML($this->locale_xhtml('delete_intro')); 166d5a1a6dcSAndreas Gohr 167d5a1a6dcSAndreas Gohr $form->addFieldsetOpen($this->getLang('tab_delete')); 168d5a1a6dcSAndreas Gohr $form->addTextInput('confirm', $this->getLang('del_confirm')); 169d5a1a6dcSAndreas Gohr $form->addButton('delete', $this->getLang('btn_delete')); 170d5a1a6dcSAndreas Gohr $form->addFieldsetClose(); 171d5a1a6dcSAndreas Gohr return $form->toHTML(); 172d5a1a6dcSAndreas Gohr } 173d5a1a6dcSAndreas Gohr 174d5a1a6dcSAndreas Gohr /** 17587fdbc6bSMichael Große * Form to add a new schema 1768ddf87afSAndreas Gohr * 1778ddf87afSAndreas Gohr * @return string 17887fdbc6bSMichael Große */ 17987fdbc6bSMichael Große protected function html_newschema() { 18087fdbc6bSMichael Große $form = new Form(); 18187fdbc6bSMichael Große $form->addFieldsetOpen($this->getLang('create')); 18287fdbc6bSMichael Große $form->setHiddenField('do', 'admin'); 183dbffe06eSAndreas Gohr $form->setHiddenField('page', 'struct_schemas'); 18487fdbc6bSMichael Große $form->addTextInput('table', $this->getLang('schemaname')); 18587fdbc6bSMichael Große $form->addButton('', $this->getLang('save')); 18687fdbc6bSMichael Große $form->addHTML('<p>' . $this->getLang('createhint') . '</p>'); // FIXME is that true? we probably could 18787fdbc6bSMichael Große $form->addFieldsetClose(); 1888ddf87afSAndreas Gohr return $form->toHTML(); 18987fdbc6bSMichael Große } 19087fdbc6bSMichael Große 19187fdbc6bSMichael Große /** 19287fdbc6bSMichael Große * Adds all available schemas to the Table of Contents 19387fdbc6bSMichael Große * 19487fdbc6bSMichael Große * @return array 19587fdbc6bSMichael Große */ 19687fdbc6bSMichael Große public function getTOC() { 19787fdbc6bSMichael Große global $ID; 19887fdbc6bSMichael Große 19987fdbc6bSMichael Große $toc = array(); 2008ddf87afSAndreas Gohr $link = wl( 2018ddf87afSAndreas Gohr $ID, array( 20287fdbc6bSMichael Große 'do' => 'admin', 203dbffe06eSAndreas Gohr 'page' => 'struct_assignments' 2048ddf87afSAndreas Gohr ) 2058ddf87afSAndreas Gohr ); 206dbffe06eSAndreas Gohr $toc[] = html_mktocitem($link, $this->getLang('menu_assignments'), 0, ''); 2078ddf87afSAndreas Gohr $link = wl( 2088ddf87afSAndreas Gohr $ID, array( 209dbffe06eSAndreas Gohr 'do' => 'admin', 210dbffe06eSAndreas Gohr 'page' => 'struct_schemas' 2118ddf87afSAndreas Gohr ) 2128ddf87afSAndreas Gohr ); 21387fdbc6bSMichael Große $toc[] = html_mktocitem($link, $this->getLang('menu'), 0, ''); 21487fdbc6bSMichael Große 215097f4a53SAndreas Gohr $tables = Schema::getAll(); 216097f4a53SAndreas Gohr foreach($tables as $table) { 2178ddf87afSAndreas Gohr $link = wl( 2188ddf87afSAndreas Gohr $ID, array( 21987fdbc6bSMichael Große 'do' => 'admin', 220dbffe06eSAndreas Gohr 'page' => 'struct_schemas', 221097f4a53SAndreas Gohr 'table' => $table 2228ddf87afSAndreas Gohr ) 2238ddf87afSAndreas Gohr ); 22487fdbc6bSMichael Große 225097f4a53SAndreas Gohr $toc[] = html_mktocitem($link, hsc($table), 1, ''); 22687fdbc6bSMichael Große } 22787fdbc6bSMichael Große return $toc; 22887fdbc6bSMichael Große } 22987fdbc6bSMichael Große 23087fdbc6bSMichael Große} 23187fdbc6bSMichael Große 23287fdbc6bSMichael Große// vim:ts=4:sw=4:et: 233