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; 10*a0b3799eSAndreas Gohruse dokuwiki\plugin\struct\meta\CSVImporter; 11ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Schema; 12ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaBuilder; 13ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaEditor; 14ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaImporter; 15ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\StructException; 1687fdbc6bSMichael Große 17d5a1a6dcSAndreas Gohr// must be run within Dokuwiki 1887fdbc6bSMichael Großeif(!defined('DOKU_INC')) die(); 1987fdbc6bSMichael Große 2087fdbc6bSMichael Großeclass admin_plugin_struct_schemas extends DokuWiki_Admin_Plugin { 2187fdbc6bSMichael Große 2287fdbc6bSMichael Große /** 2387fdbc6bSMichael Große * @return int sort number in admin menu 2487fdbc6bSMichael Große */ 2587fdbc6bSMichael Große public function getMenuSort() { 2687fdbc6bSMichael Große return 500; 2787fdbc6bSMichael Große } 2887fdbc6bSMichael Große 2987fdbc6bSMichael Große /** 3087fdbc6bSMichael Große * @return bool true if only access for superuser, false is for superusers and moderators 3187fdbc6bSMichael Große */ 3287fdbc6bSMichael Große public function forAdminOnly() { 334d220607SAndreas Gohr return false; 3487fdbc6bSMichael Große } 3587fdbc6bSMichael Große 3687fdbc6bSMichael Große /** 3787fdbc6bSMichael Große * Should carry out any processing required by the plugin. 3887fdbc6bSMichael Große */ 3987fdbc6bSMichael Große public function handle() { 4087fdbc6bSMichael Große global $INPUT; 41d5a1a6dcSAndreas Gohr global $ID; 42e33460e2SMichael Grosse global $config_cascade; 43e33460e2SMichael Grosse $config_file_path = end($config_cascade['main']['local']); 4487fdbc6bSMichael Große 458ddf87afSAndreas Gohr // form submit 4687fdbc6bSMichael Große $table = Schema::cleanTableName($INPUT->str('table')); 4787fdbc6bSMichael Große if($table && $INPUT->bool('save') && checkSecurityToken()) { 48d5a1a6dcSAndreas Gohr $builder = new SchemaBuilder($table, $INPUT->arr('schema')); 4987fdbc6bSMichael Große if(!$builder->build()) { 5087fdbc6bSMichael Große msg('something went wrong while saving', -1); 5187fdbc6bSMichael Große } 52ae5c46faSAndreas Gohr touch(action_plugin_struct_cache::getSchemaRefreshFile()); 5387fdbc6bSMichael Große } 548ddf87afSAndreas Gohr // export 55d486d6d7SAndreas Gohr if($table && $INPUT->bool('export')) { 56d5a1a6dcSAndreas Gohr $builder = new Schema($table); 57d486d6d7SAndreas Gohr header('Content-Type: application/json'); 58d486d6d7SAndreas Gohr header("Content-Disposition: attachment; filename=$table.struct.json"); 59d486d6d7SAndreas Gohr echo $builder->toJSON(); 60d486d6d7SAndreas Gohr exit; 61d486d6d7SAndreas Gohr } 628ddf87afSAndreas Gohr // import 638ddf87afSAndreas Gohr if($table && $INPUT->bool('import')) { 648ddf87afSAndreas Gohr if(isset($_FILES['schemafile']['tmp_name'])) { 658ddf87afSAndreas Gohr $json = io_readFile($_FILES['schemafile']['tmp_name'], false); 668ddf87afSAndreas Gohr if(!$json) { 678ddf87afSAndreas Gohr msg('Something went wrong with the upload', -1); 688ddf87afSAndreas Gohr } else { 690845722bSAndreas Gohr $builder = new SchemaImporter($table, $json, $INPUT->bool('lookup')); 708ddf87afSAndreas Gohr if(!$builder->build()) { 718ddf87afSAndreas Gohr msg('something went wrong while saving', -1); 728ddf87afSAndreas Gohr } 73ae5c46faSAndreas Gohr touch(action_plugin_struct_cache::getSchemaRefreshFile()); 748ddf87afSAndreas Gohr } 758ddf87afSAndreas Gohr } 768ddf87afSAndreas Gohr } 77*a0b3799eSAndreas Gohr 78*a0b3799eSAndreas Gohr // import CSV 79*a0b3799eSAndreas Gohr if($table && $INPUT->bool('importcsv')) { 80*a0b3799eSAndreas Gohr if(isset($_FILES['csvfile']['tmp_name'])) { 81*a0b3799eSAndreas Gohr try { 82*a0b3799eSAndreas Gohr new CSVImporter($table, $_FILES['csvfile']['tmp_name']); 83*a0b3799eSAndreas Gohr msg('CSV imported', 1); 84*a0b3799eSAndreas Gohr } catch(StructException $e) { 85*a0b3799eSAndreas Gohr msg(hsc($e->getMessage()), -1); 86*a0b3799eSAndreas Gohr } 87*a0b3799eSAndreas Gohr } 88*a0b3799eSAndreas Gohr } 89*a0b3799eSAndreas Gohr 90d5a1a6dcSAndreas Gohr // delete 91d5a1a6dcSAndreas Gohr if($table && $INPUT->bool('delete')) { 92d5a1a6dcSAndreas Gohr if($table != $INPUT->str('confirm')) { 93d5a1a6dcSAndreas Gohr msg($this->getLang('del_fail'), -1); 94d5a1a6dcSAndreas Gohr } else { 95d5a1a6dcSAndreas Gohr try { 96d5a1a6dcSAndreas Gohr $schema = new Schema($table); 97d5a1a6dcSAndreas Gohr $schema->delete(); 98d5a1a6dcSAndreas Gohr msg($this->getLang('del_ok'), 1); 99ae5c46faSAndreas Gohr touch(action_plugin_struct_cache::getSchemaRefreshFile()); 100d5a1a6dcSAndreas Gohr send_redirect(wl($ID, array('do' => 'admin', 'page' => 'struct_schemas'), true, '&')); 101d5a1a6dcSAndreas Gohr } catch(StructException $e) { 102d5a1a6dcSAndreas Gohr msg(hsc($e->getMessage()), -1); 103d5a1a6dcSAndreas Gohr } 104d5a1a6dcSAndreas Gohr } 105d5a1a6dcSAndreas Gohr } 106d5a1a6dcSAndreas Gohr 10787fdbc6bSMichael Große } 10887fdbc6bSMichael Große 10987fdbc6bSMichael Große /** 11087fdbc6bSMichael Große * Render HTML output, e.g. helpful text and a form 11187fdbc6bSMichael Große */ 11287fdbc6bSMichael Große public function html() { 11387fdbc6bSMichael Große global $INPUT; 11487fdbc6bSMichael Große 11587fdbc6bSMichael Große $table = Schema::cleanTableName($INPUT->str('table')); 11687fdbc6bSMichael Große if($table) { 1177c080d69SAndreas Gohr $schema = new Schema($table, 0, $INPUT->bool('lookup')); 1187c080d69SAndreas Gohr if($schema->isLookup()) { 1197c080d69SAndreas Gohr $hl = 'edithl lookup'; 1207c080d69SAndreas Gohr } else { 1217c080d69SAndreas Gohr $hl = 'edithl page'; 1227c080d69SAndreas Gohr } 1237c080d69SAndreas Gohr 1246af24d3eSAndreas Gohr echo $this->locale_xhtml('editor_edit'); 1257c080d69SAndreas Gohr echo '<h2>' . sprintf($this->getLang($hl), hsc($table)) . '</h2>'; 1268ddf87afSAndreas Gohr 1278ddf87afSAndreas Gohr echo '<ul class="tabs" id="plugin__struct_tabs">'; 1288ddf87afSAndreas Gohr /** @noinspection HtmlUnknownAnchorTarget */ 1298ddf87afSAndreas Gohr echo '<li class="active"><a href="#plugin__struct_editor">' . $this->getLang('tab_edit') . '</a></li>'; 1308ddf87afSAndreas Gohr /** @noinspection HtmlUnknownAnchorTarget */ 1318ddf87afSAndreas Gohr echo '<li><a href="#plugin__struct_json">' . $this->getLang('tab_export') . '</a></li>'; 132d5a1a6dcSAndreas Gohr /** @noinspection HtmlUnknownAnchorTarget */ 133d5a1a6dcSAndreas Gohr echo '<li><a href="#plugin__struct_delete">' . $this->getLang('tab_delete') . '</a></li>'; 1348ddf87afSAndreas Gohr echo '</ul>'; 1358ddf87afSAndreas Gohr echo '<div class="panelHeader"></div>'; 1368ddf87afSAndreas Gohr 1377c080d69SAndreas Gohr $editor = new SchemaEditor($schema); 13887fdbc6bSMichael Große echo $editor->getEditor(); 1390845722bSAndreas Gohr echo $this->html_json($schema); 1400845722bSAndreas Gohr echo $this->html_delete($schema); 141d486d6d7SAndreas Gohr 14287fdbc6bSMichael Große } else { 1436af24d3eSAndreas Gohr echo $this->locale_xhtml('editor_intro'); 1448ddf87afSAndreas Gohr echo $this->html_newschema(); 14587fdbc6bSMichael Große } 14687fdbc6bSMichael Große } 14787fdbc6bSMichael Große 14887fdbc6bSMichael Große /** 1498ddf87afSAndreas Gohr * Form for handling import/export from/to JSON 1500845722bSAndreas Gohr * 1510845722bSAndreas Gohr * @param Schema $schema 1528ddf87afSAndreas Gohr * @return string 1538ddf87afSAndreas Gohr */ 1540845722bSAndreas Gohr protected function html_json(Schema $schema) { 1558ddf87afSAndreas Gohr $form = new Form(array('enctype' => 'multipart/form-data', 'id' => 'plugin__struct_json')); 1568ddf87afSAndreas Gohr $form->setHiddenField('do', 'admin'); 1578ddf87afSAndreas Gohr $form->setHiddenField('page', 'struct_schemas'); 1580845722bSAndreas Gohr $form->setHiddenField('table', $schema->getTable()); 1590845722bSAndreas Gohr $form->setHiddenField('lookup', $schema->isLookup()); 1608ddf87afSAndreas Gohr 1618ddf87afSAndreas Gohr $form->addFieldsetOpen($this->getLang('export')); 1628ddf87afSAndreas Gohr $form->addButton('export', $this->getLang('btn_export')); 1638ddf87afSAndreas Gohr $form->addFieldsetClose(); 1648ddf87afSAndreas Gohr 1658ddf87afSAndreas Gohr $form->addFieldsetOpen($this->getLang('import')); 1668ddf87afSAndreas Gohr $form->addElement(new \dokuwiki\Form\InputElement('file', 'schemafile')); 1678ddf87afSAndreas Gohr $form->addButton('import', $this->getLang('btn_import')); 1688ddf87afSAndreas Gohr $form->addHTML('<p>' . $this->getLang('import_warning') . '</p>'); 1698ddf87afSAndreas Gohr $form->addFieldsetClose(); 170*a0b3799eSAndreas Gohr 171*a0b3799eSAndreas Gohr if($schema->isLookup()) { 172*a0b3799eSAndreas Gohr $form->addFieldsetOpen($this->getLang('csvimport')); 173*a0b3799eSAndreas Gohr $form->addElement(new \dokuwiki\Form\InputElement('file', 'csvfile')); 174*a0b3799eSAndreas Gohr $form->addButton('importcsv', $this->getLang('btn_import')); 175*a0b3799eSAndreas Gohr $form->addHTML('<p><a href="https://www.dokuwiki.org/plugin:struct:csvimport">' . $this->getLang('csv_help_link') . '</a></p>'); 176*a0b3799eSAndreas Gohr $form->addFieldsetClose(); 177*a0b3799eSAndreas Gohr } 178*a0b3799eSAndreas Gohr 1798ddf87afSAndreas Gohr return $form->toHTML(); 1808ddf87afSAndreas Gohr } 1818ddf87afSAndreas Gohr 1828ddf87afSAndreas Gohr /** 183d5a1a6dcSAndreas Gohr * Form for deleting schemas 1840845722bSAndreas Gohr * 1850845722bSAndreas Gohr * @param Schema $schema 186d5a1a6dcSAndreas Gohr * @return string 187d5a1a6dcSAndreas Gohr */ 1880845722bSAndreas Gohr protected function html_delete(Schema $schema) { 189d5a1a6dcSAndreas Gohr $form = new Form(array('id' => 'plugin__struct_delete')); 190d5a1a6dcSAndreas Gohr $form->setHiddenField('do', 'admin'); 191d5a1a6dcSAndreas Gohr $form->setHiddenField('page', 'struct_schemas'); 1920845722bSAndreas Gohr $form->setHiddenField('table', $schema->getTable()); 193d5a1a6dcSAndreas Gohr 194d5a1a6dcSAndreas Gohr $form->addHTML($this->locale_xhtml('delete_intro')); 195d5a1a6dcSAndreas Gohr 196d5a1a6dcSAndreas Gohr $form->addFieldsetOpen($this->getLang('tab_delete')); 197d5a1a6dcSAndreas Gohr $form->addTextInput('confirm', $this->getLang('del_confirm')); 198d5a1a6dcSAndreas Gohr $form->addButton('delete', $this->getLang('btn_delete')); 199d5a1a6dcSAndreas Gohr $form->addFieldsetClose(); 200d5a1a6dcSAndreas Gohr return $form->toHTML(); 201d5a1a6dcSAndreas Gohr } 202d5a1a6dcSAndreas Gohr 203d5a1a6dcSAndreas Gohr /** 20487fdbc6bSMichael Große * Form to add a new schema 2058ddf87afSAndreas Gohr * 2068ddf87afSAndreas Gohr * @return string 20787fdbc6bSMichael Große */ 20887fdbc6bSMichael Große protected function html_newschema() { 20987fdbc6bSMichael Große $form = new Form(); 2104e427bd5SAndreas Gohr $form->addClass('struct_newschema'); 21187fdbc6bSMichael Große $form->addFieldsetOpen($this->getLang('create')); 21287fdbc6bSMichael Große $form->setHiddenField('do', 'admin'); 213dbffe06eSAndreas Gohr $form->setHiddenField('page', 'struct_schemas'); 21487fdbc6bSMichael Große $form->addTextInput('table', $this->getLang('schemaname')); 2154e427bd5SAndreas Gohr $form->addRadioButton('lookup', $this->getLang('page schema'))->val('0')->attr('checked', 'checked'); 2164e427bd5SAndreas Gohr $form->addRadioButton('lookup', $this->getLang('lookup schema'))->val('1'); 21787fdbc6bSMichael Große $form->addButton('', $this->getLang('save')); 21887fdbc6bSMichael Große $form->addHTML('<p>' . $this->getLang('createhint') . '</p>'); // FIXME is that true? we probably could 21987fdbc6bSMichael Große $form->addFieldsetClose(); 2208ddf87afSAndreas Gohr return $form->toHTML(); 22187fdbc6bSMichael Große } 22287fdbc6bSMichael Große 22387fdbc6bSMichael Große /** 22487fdbc6bSMichael Große * Adds all available schemas to the Table of Contents 22587fdbc6bSMichael Große * 22687fdbc6bSMichael Große * @return array 22787fdbc6bSMichael Große */ 22887fdbc6bSMichael Große public function getTOC() { 22987fdbc6bSMichael Große global $ID; 23087fdbc6bSMichael Große 23187fdbc6bSMichael Große $toc = array(); 2328ddf87afSAndreas Gohr $link = wl( 2338ddf87afSAndreas Gohr $ID, array( 23487fdbc6bSMichael Große 'do' => 'admin', 235dbffe06eSAndreas Gohr 'page' => 'struct_assignments' 2368ddf87afSAndreas Gohr ) 2378ddf87afSAndreas Gohr ); 238dbffe06eSAndreas Gohr $toc[] = html_mktocitem($link, $this->getLang('menu_assignments'), 0, ''); 2397c080d69SAndreas Gohr $slink = wl( 2408ddf87afSAndreas Gohr $ID, array( 241dbffe06eSAndreas Gohr 'do' => 'admin', 242dbffe06eSAndreas Gohr 'page' => 'struct_schemas' 2438ddf87afSAndreas Gohr ) 2448ddf87afSAndreas Gohr ); 2457c080d69SAndreas Gohr $toc[] = html_mktocitem($slink, $this->getLang('menu'), 0, ''); 24687fdbc6bSMichael Große 2477c080d69SAndreas Gohr $tables = Schema::getAll('page'); 2487c080d69SAndreas Gohr if($tables) { 2497c080d69SAndreas Gohr $toc[] = html_mktocitem($slink, $this->getLang('page schema'), 1, ''); 250097f4a53SAndreas Gohr foreach($tables as $table) { 2518ddf87afSAndreas Gohr $link = wl( 2528ddf87afSAndreas Gohr $ID, array( 25387fdbc6bSMichael Große 'do' => 'admin', 254dbffe06eSAndreas Gohr 'page' => 'struct_schemas', 255097f4a53SAndreas Gohr 'table' => $table 2568ddf87afSAndreas Gohr ) 2578ddf87afSAndreas Gohr ); 25887fdbc6bSMichael Große 2597c080d69SAndreas Gohr $toc[] = html_mktocitem($link, hsc($table), 2, ''); 26087fdbc6bSMichael Große } 2617c080d69SAndreas Gohr } 2627c080d69SAndreas Gohr 2637c080d69SAndreas Gohr $tables = Schema::getAll('lookup'); 2647c080d69SAndreas Gohr if($tables) { 2657c080d69SAndreas Gohr $toc[] = html_mktocitem($slink, $this->getLang('lookup schema'), 1, ''); 2667c080d69SAndreas Gohr foreach($tables as $table) { 2677c080d69SAndreas Gohr $link = wl( 2687c080d69SAndreas Gohr $ID, array( 2697c080d69SAndreas Gohr 'do' => 'admin', 2707c080d69SAndreas Gohr 'page' => 'struct_schemas', 2717c080d69SAndreas Gohr 'table' => $table 2727c080d69SAndreas Gohr ) 2737c080d69SAndreas Gohr ); 2747c080d69SAndreas Gohr 2757c080d69SAndreas Gohr $toc[] = html_mktocitem($link, hsc($table), 2, ''); 2767c080d69SAndreas Gohr } 2777c080d69SAndreas Gohr } 2787c080d69SAndreas Gohr 27987fdbc6bSMichael Große return $toc; 28087fdbc6bSMichael Große } 28187fdbc6bSMichael Große 282*a0b3799eSAndreas Gohr 283*a0b3799eSAndreas Gohr 28487fdbc6bSMichael Große} 28587fdbc6bSMichael Große 28687fdbc6bSMichael Große// vim:ts=4:sw=4:et: 287