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; 10f36cc634SAndreas Gohruse dokuwiki\plugin\struct\meta\CSVExporter; 11a0b3799eSAndreas Gohruse dokuwiki\plugin\struct\meta\CSVImporter; 12ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Schema; 13ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaBuilder; 14ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaEditor; 15ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaImporter; 16ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\StructException; 1787fdbc6bSMichael Große 18d5a1a6dcSAndreas Gohr// must be run within Dokuwiki 1987fdbc6bSMichael Großeif(!defined('DOKU_INC')) die(); 2087fdbc6bSMichael Große 2187fdbc6bSMichael Großeclass admin_plugin_struct_schemas extends DokuWiki_Admin_Plugin { 2287fdbc6bSMichael Große 2387fdbc6bSMichael Große /** 2487fdbc6bSMichael Große * @return int sort number in admin menu 2587fdbc6bSMichael Große */ 2687fdbc6bSMichael Große public function getMenuSort() { 2787fdbc6bSMichael Große return 500; 2887fdbc6bSMichael Große } 2987fdbc6bSMichael Große 3087fdbc6bSMichael Große /** 3187fdbc6bSMichael Große * @return bool true if only access for superuser, false is for superusers and moderators 3287fdbc6bSMichael Große */ 3387fdbc6bSMichael Große public function forAdminOnly() { 344d220607SAndreas Gohr return false; 3587fdbc6bSMichael Große } 3687fdbc6bSMichael Große 3787fdbc6bSMichael Große /** 3887fdbc6bSMichael Große * Should carry out any processing required by the plugin. 3987fdbc6bSMichael Große */ 4087fdbc6bSMichael Große public function handle() { 4187fdbc6bSMichael Große global $INPUT; 42d5a1a6dcSAndreas Gohr global $ID; 43e33460e2SMichael Grosse global $config_cascade; 44e33460e2SMichael Grosse $config_file_path = end($config_cascade['main']['local']); 4587fdbc6bSMichael Große 468ddf87afSAndreas Gohr // form submit 4787fdbc6bSMichael Große $table = Schema::cleanTableName($INPUT->str('table')); 4887fdbc6bSMichael Große if($table && $INPUT->bool('save') && checkSecurityToken()) { 49d5a1a6dcSAndreas Gohr $builder = new SchemaBuilder($table, $INPUT->arr('schema')); 5087fdbc6bSMichael Große if(!$builder->build()) { 5187fdbc6bSMichael Große msg('something went wrong while saving', -1); 5287fdbc6bSMichael Große } 53ae5c46faSAndreas Gohr touch(action_plugin_struct_cache::getSchemaRefreshFile()); 5487fdbc6bSMichael Große } 558ddf87afSAndreas Gohr // export 56d486d6d7SAndreas Gohr if($table && $INPUT->bool('export')) { 57d5a1a6dcSAndreas Gohr $builder = new Schema($table); 58d486d6d7SAndreas Gohr header('Content-Type: application/json'); 59d486d6d7SAndreas Gohr header("Content-Disposition: attachment; filename=$table.struct.json"); 60d486d6d7SAndreas Gohr echo $builder->toJSON(); 61d486d6d7SAndreas Gohr exit; 62d486d6d7SAndreas Gohr } 638ddf87afSAndreas Gohr // import 648ddf87afSAndreas Gohr if($table && $INPUT->bool('import')) { 658ddf87afSAndreas Gohr if(isset($_FILES['schemafile']['tmp_name'])) { 668ddf87afSAndreas Gohr $json = io_readFile($_FILES['schemafile']['tmp_name'], false); 678ddf87afSAndreas Gohr if(!$json) { 688ddf87afSAndreas Gohr msg('Something went wrong with the upload', -1); 698ddf87afSAndreas Gohr } else { 700845722bSAndreas Gohr $builder = new SchemaImporter($table, $json, $INPUT->bool('lookup')); 718ddf87afSAndreas Gohr if(!$builder->build()) { 728ddf87afSAndreas Gohr msg('something went wrong while saving', -1); 738ddf87afSAndreas Gohr } 74ae5c46faSAndreas Gohr touch(action_plugin_struct_cache::getSchemaRefreshFile()); 758ddf87afSAndreas Gohr } 768ddf87afSAndreas Gohr } 778ddf87afSAndreas Gohr } 78a0b3799eSAndreas Gohr 79a0b3799eSAndreas Gohr // import CSV 80a0b3799eSAndreas Gohr if($table && $INPUT->bool('importcsv')) { 81a0b3799eSAndreas Gohr if(isset($_FILES['csvfile']['tmp_name'])) { 82a0b3799eSAndreas Gohr try { 83a0b3799eSAndreas Gohr new CSVImporter($table, $_FILES['csvfile']['tmp_name']); 846d2df532SAndreas Gohr msg($this->getLang('admin_csvdone'), 1); 85a0b3799eSAndreas Gohr } catch(StructException $e) { 86a0b3799eSAndreas Gohr msg(hsc($e->getMessage()), -1); 87a0b3799eSAndreas Gohr } 88a0b3799eSAndreas Gohr } 89a0b3799eSAndreas Gohr } 90a0b3799eSAndreas Gohr 91f36cc634SAndreas Gohr // export CSV 92f36cc634SAndreas Gohr if($table && $INPUT->bool('exportcsv')) { 93f36cc634SAndreas Gohr header('Content-Type: text/csv'); 94f36cc634SAndreas Gohr header('Content-Disposition: attachment; filename="' . $table . '.csv";'); 95f36cc634SAndreas Gohr new CSVExporter($table); 96f36cc634SAndreas Gohr exit(); 97f36cc634SAndreas Gohr } 98f36cc634SAndreas Gohr 99d5a1a6dcSAndreas Gohr // delete 100d5a1a6dcSAndreas Gohr if($table && $INPUT->bool('delete')) { 101d5a1a6dcSAndreas Gohr if($table != $INPUT->str('confirm')) { 102d5a1a6dcSAndreas Gohr msg($this->getLang('del_fail'), -1); 103d5a1a6dcSAndreas Gohr } else { 104d5a1a6dcSAndreas Gohr try { 105d5a1a6dcSAndreas Gohr $schema = new Schema($table); 106d5a1a6dcSAndreas Gohr $schema->delete(); 107d5a1a6dcSAndreas Gohr msg($this->getLang('del_ok'), 1); 108ae5c46faSAndreas Gohr touch(action_plugin_struct_cache::getSchemaRefreshFile()); 109d5a1a6dcSAndreas Gohr send_redirect(wl($ID, array('do' => 'admin', 'page' => 'struct_schemas'), true, '&')); 110d5a1a6dcSAndreas Gohr } catch(StructException $e) { 111d5a1a6dcSAndreas Gohr msg(hsc($e->getMessage()), -1); 112d5a1a6dcSAndreas Gohr } 113d5a1a6dcSAndreas Gohr } 114d5a1a6dcSAndreas Gohr } 115d5a1a6dcSAndreas Gohr 116*79c83e06SMichael Große // clear 117*79c83e06SMichael Große if($table && $INPUT->bool('clear')) { 118*79c83e06SMichael Große if($table != $INPUT->str('confirm_clear')) { 119*79c83e06SMichael Große msg($this->getLang('clear_fail'), -1); 120*79c83e06SMichael Große } else { 121*79c83e06SMichael Große try { 122*79c83e06SMichael Große $schema = new Schema($table); 123*79c83e06SMichael Große $schema->clear(); 124*79c83e06SMichael Große msg($this->getLang('clear_ok'), 1); 125*79c83e06SMichael Große touch(action_plugin_struct_cache::getSchemaRefreshFile()); 126*79c83e06SMichael Große send_redirect(wl($ID, array('do' => 'admin', 'page' => 'struct_schemas'), true, '&')); 127*79c83e06SMichael Große } catch(StructException $e) { 128*79c83e06SMichael Große msg(hsc($e->getMessage()), -1); 129*79c83e06SMichael Große } 130*79c83e06SMichael Große } 131*79c83e06SMichael Große } 132*79c83e06SMichael Große 13387fdbc6bSMichael Große } 13487fdbc6bSMichael Große 13587fdbc6bSMichael Große /** 13687fdbc6bSMichael Große * Render HTML output, e.g. helpful text and a form 13787fdbc6bSMichael Große */ 13887fdbc6bSMichael Große public function html() { 13987fdbc6bSMichael Große global $INPUT; 14087fdbc6bSMichael Große 14187fdbc6bSMichael Große $table = Schema::cleanTableName($INPUT->str('table')); 14287fdbc6bSMichael Große if($table) { 1437c080d69SAndreas Gohr $schema = new Schema($table, 0, $INPUT->bool('lookup')); 1447c080d69SAndreas Gohr if($schema->isLookup()) { 1457c080d69SAndreas Gohr $hl = 'edithl lookup'; 1467c080d69SAndreas Gohr } else { 1477c080d69SAndreas Gohr $hl = 'edithl page'; 1487c080d69SAndreas Gohr } 1497c080d69SAndreas Gohr 1506af24d3eSAndreas Gohr echo $this->locale_xhtml('editor_edit'); 1517c080d69SAndreas Gohr echo '<h2>' . sprintf($this->getLang($hl), hsc($table)) . '</h2>'; 1528ddf87afSAndreas Gohr 1538ddf87afSAndreas Gohr echo '<ul class="tabs" id="plugin__struct_tabs">'; 1548ddf87afSAndreas Gohr /** @noinspection HtmlUnknownAnchorTarget */ 1558ddf87afSAndreas Gohr echo '<li class="active"><a href="#plugin__struct_editor">' . $this->getLang('tab_edit') . '</a></li>'; 1568ddf87afSAndreas Gohr /** @noinspection HtmlUnknownAnchorTarget */ 1578ddf87afSAndreas Gohr echo '<li><a href="#plugin__struct_json">' . $this->getLang('tab_export') . '</a></li>'; 158d5a1a6dcSAndreas Gohr /** @noinspection HtmlUnknownAnchorTarget */ 159d5a1a6dcSAndreas Gohr echo '<li><a href="#plugin__struct_delete">' . $this->getLang('tab_delete') . '</a></li>'; 1608ddf87afSAndreas Gohr echo '</ul>'; 1618ddf87afSAndreas Gohr echo '<div class="panelHeader"></div>'; 1628ddf87afSAndreas Gohr 1637c080d69SAndreas Gohr $editor = new SchemaEditor($schema); 16487fdbc6bSMichael Große echo $editor->getEditor(); 1650845722bSAndreas Gohr echo $this->html_json($schema); 1660845722bSAndreas Gohr echo $this->html_delete($schema); 167d486d6d7SAndreas Gohr 16887fdbc6bSMichael Große } else { 1696af24d3eSAndreas Gohr echo $this->locale_xhtml('editor_intro'); 1708ddf87afSAndreas Gohr echo $this->html_newschema(); 17187fdbc6bSMichael Große } 17287fdbc6bSMichael Große } 17387fdbc6bSMichael Große 17487fdbc6bSMichael Große /** 1758ddf87afSAndreas Gohr * Form for handling import/export from/to JSON 1760845722bSAndreas Gohr * 1770845722bSAndreas Gohr * @param Schema $schema 1788ddf87afSAndreas Gohr * @return string 1798ddf87afSAndreas Gohr */ 1800845722bSAndreas Gohr protected function html_json(Schema $schema) { 1818ddf87afSAndreas Gohr $form = new Form(array('enctype' => 'multipart/form-data', 'id' => 'plugin__struct_json')); 1828ddf87afSAndreas Gohr $form->setHiddenField('do', 'admin'); 1838ddf87afSAndreas Gohr $form->setHiddenField('page', 'struct_schemas'); 1840845722bSAndreas Gohr $form->setHiddenField('table', $schema->getTable()); 1850845722bSAndreas Gohr $form->setHiddenField('lookup', $schema->isLookup()); 1868ddf87afSAndreas Gohr 1878ddf87afSAndreas Gohr $form->addFieldsetOpen($this->getLang('export')); 1888ddf87afSAndreas Gohr $form->addButton('export', $this->getLang('btn_export')); 1898ddf87afSAndreas Gohr $form->addFieldsetClose(); 1908ddf87afSAndreas Gohr 1918ddf87afSAndreas Gohr $form->addFieldsetOpen($this->getLang('import')); 1928ddf87afSAndreas Gohr $form->addElement(new \dokuwiki\Form\InputElement('file', 'schemafile')); 1938ddf87afSAndreas Gohr $form->addButton('import', $this->getLang('btn_import')); 1948ddf87afSAndreas Gohr $form->addHTML('<p>' . $this->getLang('import_warning') . '</p>'); 1958ddf87afSAndreas Gohr $form->addFieldsetClose(); 196a0b3799eSAndreas Gohr 1976d2df532SAndreas Gohr $form->addFieldsetOpen($this->getLang('admin_csvexport')); 198f36cc634SAndreas Gohr $form->addButton('exportcsv', $this->getLang('btn_export')); 199f36cc634SAndreas Gohr $form->addFieldsetClose(); 200f36cc634SAndreas Gohr 201a0b3799eSAndreas Gohr if($schema->isLookup()) { 2026d2df532SAndreas Gohr $form->addFieldsetOpen($this->getLang('admin_csvimport')); 203a0b3799eSAndreas Gohr $form->addElement(new \dokuwiki\Form\InputElement('file', 'csvfile')); 204a0b3799eSAndreas Gohr $form->addButton('importcsv', $this->getLang('btn_import')); 2056d2df532SAndreas Gohr $form->addHTML('<p><a href="https://www.dokuwiki.org/plugin:struct:csvimport">' . $this->getLang('admin_csvhelp') . '</a></p>'); 206a0b3799eSAndreas Gohr $form->addFieldsetClose(); 207a0b3799eSAndreas Gohr } 208a0b3799eSAndreas Gohr 2098ddf87afSAndreas Gohr return $form->toHTML(); 2108ddf87afSAndreas Gohr } 2118ddf87afSAndreas Gohr 2128ddf87afSAndreas Gohr /** 213d5a1a6dcSAndreas Gohr * Form for deleting schemas 2140845722bSAndreas Gohr * 2150845722bSAndreas Gohr * @param Schema $schema 216d5a1a6dcSAndreas Gohr * @return string 217d5a1a6dcSAndreas Gohr */ 2180845722bSAndreas Gohr protected function html_delete(Schema $schema) { 219d5a1a6dcSAndreas Gohr $form = new Form(array('id' => 'plugin__struct_delete')); 220d5a1a6dcSAndreas Gohr $form->setHiddenField('do', 'admin'); 221d5a1a6dcSAndreas Gohr $form->setHiddenField('page', 'struct_schemas'); 2220845722bSAndreas Gohr $form->setHiddenField('table', $schema->getTable()); 223d5a1a6dcSAndreas Gohr 224*79c83e06SMichael Große $form->addFieldsetOpen($this->getLang('btn_delete')); 225d5a1a6dcSAndreas Gohr $form->addHTML($this->locale_xhtml('delete_intro')); 226d5a1a6dcSAndreas Gohr $form->addTextInput('confirm', $this->getLang('del_confirm')); 227d5a1a6dcSAndreas Gohr $form->addButton('delete', $this->getLang('btn_delete')); 228d5a1a6dcSAndreas Gohr $form->addFieldsetClose(); 229*79c83e06SMichael Große 230*79c83e06SMichael Große $form->addFieldsetOpen($this->getLang('btn_clear')); 231*79c83e06SMichael Große $form->addHTML($this->locale_xhtml('clear_intro')); 232*79c83e06SMichael Große $form->addTextInput('confirm_clear', $this->getLang('clear_confirm')); 233*79c83e06SMichael Große $form->addButton('clear', $this->getLang('btn_clear')); 234*79c83e06SMichael Große $form->addFieldsetClose(); 235*79c83e06SMichael Große 236d5a1a6dcSAndreas Gohr return $form->toHTML(); 237d5a1a6dcSAndreas Gohr } 238d5a1a6dcSAndreas Gohr 239d5a1a6dcSAndreas Gohr /** 24087fdbc6bSMichael Große * Form to add a new schema 2418ddf87afSAndreas Gohr * 2428ddf87afSAndreas Gohr * @return string 24387fdbc6bSMichael Große */ 24487fdbc6bSMichael Große protected function html_newschema() { 24587fdbc6bSMichael Große $form = new Form(); 2464e427bd5SAndreas Gohr $form->addClass('struct_newschema'); 24787fdbc6bSMichael Große $form->addFieldsetOpen($this->getLang('create')); 24887fdbc6bSMichael Große $form->setHiddenField('do', 'admin'); 249dbffe06eSAndreas Gohr $form->setHiddenField('page', 'struct_schemas'); 25087fdbc6bSMichael Große $form->addTextInput('table', $this->getLang('schemaname')); 2514e427bd5SAndreas Gohr $form->addRadioButton('lookup', $this->getLang('page schema'))->val('0')->attr('checked', 'checked'); 2524e427bd5SAndreas Gohr $form->addRadioButton('lookup', $this->getLang('lookup schema'))->val('1'); 25387fdbc6bSMichael Große $form->addButton('', $this->getLang('save')); 25487fdbc6bSMichael Große $form->addHTML('<p>' . $this->getLang('createhint') . '</p>'); // FIXME is that true? we probably could 25587fdbc6bSMichael Große $form->addFieldsetClose(); 2568ddf87afSAndreas Gohr return $form->toHTML(); 25787fdbc6bSMichael Große } 25887fdbc6bSMichael Große 25987fdbc6bSMichael Große /** 26087fdbc6bSMichael Große * Adds all available schemas to the Table of Contents 26187fdbc6bSMichael Große * 26287fdbc6bSMichael Große * @return array 26387fdbc6bSMichael Große */ 26487fdbc6bSMichael Große public function getTOC() { 26587fdbc6bSMichael Große global $ID; 26687fdbc6bSMichael Große 26787fdbc6bSMichael Große $toc = array(); 2688ddf87afSAndreas Gohr $link = wl( 2698ddf87afSAndreas Gohr $ID, array( 27087fdbc6bSMichael Große 'do' => 'admin', 271dbffe06eSAndreas Gohr 'page' => 'struct_assignments' 2728ddf87afSAndreas Gohr ) 2738ddf87afSAndreas Gohr ); 274dbffe06eSAndreas Gohr $toc[] = html_mktocitem($link, $this->getLang('menu_assignments'), 0, ''); 2757c080d69SAndreas Gohr $slink = wl( 2768ddf87afSAndreas Gohr $ID, array( 277dbffe06eSAndreas Gohr 'do' => 'admin', 278dbffe06eSAndreas Gohr 'page' => 'struct_schemas' 2798ddf87afSAndreas Gohr ) 2808ddf87afSAndreas Gohr ); 2817c080d69SAndreas Gohr $toc[] = html_mktocitem($slink, $this->getLang('menu'), 0, ''); 28287fdbc6bSMichael Große 2837c080d69SAndreas Gohr $tables = Schema::getAll('page'); 2847c080d69SAndreas Gohr if($tables) { 2857c080d69SAndreas Gohr $toc[] = html_mktocitem($slink, $this->getLang('page schema'), 1, ''); 286097f4a53SAndreas Gohr foreach($tables as $table) { 2878ddf87afSAndreas Gohr $link = wl( 2888ddf87afSAndreas Gohr $ID, array( 28987fdbc6bSMichael Große 'do' => 'admin', 290dbffe06eSAndreas Gohr 'page' => 'struct_schemas', 291097f4a53SAndreas Gohr 'table' => $table 2928ddf87afSAndreas Gohr ) 2938ddf87afSAndreas Gohr ); 29487fdbc6bSMichael Große 2957c080d69SAndreas Gohr $toc[] = html_mktocitem($link, hsc($table), 2, ''); 29687fdbc6bSMichael Große } 2977c080d69SAndreas Gohr } 2987c080d69SAndreas Gohr 2997c080d69SAndreas Gohr $tables = Schema::getAll('lookup'); 3007c080d69SAndreas Gohr if($tables) { 3017c080d69SAndreas Gohr $toc[] = html_mktocitem($slink, $this->getLang('lookup schema'), 1, ''); 3027c080d69SAndreas Gohr foreach($tables as $table) { 3037c080d69SAndreas Gohr $link = wl( 3047c080d69SAndreas Gohr $ID, array( 3057c080d69SAndreas Gohr 'do' => 'admin', 3067c080d69SAndreas Gohr 'page' => 'struct_schemas', 3077c080d69SAndreas Gohr 'table' => $table 3087c080d69SAndreas Gohr ) 3097c080d69SAndreas Gohr ); 3107c080d69SAndreas Gohr 3117c080d69SAndreas Gohr $toc[] = html_mktocitem($link, hsc($table), 2, ''); 3127c080d69SAndreas Gohr } 3137c080d69SAndreas Gohr } 3147c080d69SAndreas Gohr 31587fdbc6bSMichael Große return $toc; 31687fdbc6bSMichael Große } 31787fdbc6bSMichael Große 318a0b3799eSAndreas Gohr 319a0b3799eSAndreas Gohr 32087fdbc6bSMichael Große} 32187fdbc6bSMichael Große 32287fdbc6bSMichael Große// vim:ts=4:sw=4:et: 323