187fdbc6bSMichael Große<?php 2*d6d97f60SAnna Dabrowska 387fdbc6bSMichael Große/** 487fdbc6bSMichael Große * DokuWiki Plugin struct (Admin Component) 587fdbc6bSMichael Große * 687fdbc6bSMichael Große * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 787fdbc6bSMichael Große * @author Andreas Gohr, Michael Große <dokuwiki@cosmocode.de> 887fdbc6bSMichael Große */ 987fdbc6bSMichael Große 1087fdbc6bSMichael Großeuse dokuwiki\Form\Form; 11f36cc634SAndreas Gohruse dokuwiki\plugin\struct\meta\CSVExporter; 121fc2361fSSzymon Olewniczakuse dokuwiki\plugin\struct\meta\CSVLookupImporter; 131fc2361fSSzymon Olewniczakuse dokuwiki\plugin\struct\meta\CSVPageImporter; 14ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Schema; 15ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaBuilder; 16ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaEditor; 17ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaImporter; 18ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\StructException; 1987fdbc6bSMichael Große 20d5a1a6dcSAndreas Gohr// must be run within Dokuwiki 2187fdbc6bSMichael Großeif (!defined('DOKU_INC')) die(); 2287fdbc6bSMichael Große 23*d6d97f60SAnna Dabrowskaclass admin_plugin_struct_schemas extends DokuWiki_Admin_Plugin 24*d6d97f60SAnna Dabrowska{ 2587fdbc6bSMichael Große 2687fdbc6bSMichael Große /** 2787fdbc6bSMichael Große * @return int sort number in admin menu 2887fdbc6bSMichael Große */ 29*d6d97f60SAnna Dabrowska public function getMenuSort() 30*d6d97f60SAnna Dabrowska { 3187fdbc6bSMichael Große return 500; 3287fdbc6bSMichael Große } 3387fdbc6bSMichael Große 3487fdbc6bSMichael Große /** 3587fdbc6bSMichael Große * @return bool true if only access for superuser, false is for superusers and moderators 3687fdbc6bSMichael Große */ 37*d6d97f60SAnna Dabrowska public function forAdminOnly() 38*d6d97f60SAnna Dabrowska { 394d220607SAndreas Gohr return false; 4087fdbc6bSMichael Große } 4187fdbc6bSMichael Große 4287fdbc6bSMichael Große /** 4387fdbc6bSMichael Große * Should carry out any processing required by the plugin. 4487fdbc6bSMichael Große */ 45*d6d97f60SAnna Dabrowska public function handle() 46*d6d97f60SAnna Dabrowska { 4787fdbc6bSMichael Große global $INPUT; 48d5a1a6dcSAndreas Gohr global $ID; 49e33460e2SMichael Grosse global $config_cascade; 50e33460e2SMichael Grosse $config_file_path = end($config_cascade['main']['local']); 5187fdbc6bSMichael Große 528ddf87afSAndreas Gohr // form submit 5387fdbc6bSMichael Große $table = Schema::cleanTableName($INPUT->str('table')); 5487fdbc6bSMichael Große if ($table && $INPUT->bool('save') && checkSecurityToken()) { 55d5a1a6dcSAndreas Gohr $builder = new SchemaBuilder($table, $INPUT->arr('schema')); 5687fdbc6bSMichael Große if (!$builder->build()) { 5787fdbc6bSMichael Große msg('something went wrong while saving', -1); 5887fdbc6bSMichael Große } 59ae5c46faSAndreas Gohr touch(action_plugin_struct_cache::getSchemaRefreshFile()); 6087fdbc6bSMichael Große } 618ddf87afSAndreas Gohr // export 62d486d6d7SAndreas Gohr if ($table && $INPUT->bool('export')) { 63d5a1a6dcSAndreas Gohr $builder = new Schema($table); 64d486d6d7SAndreas Gohr header('Content-Type: application/json'); 65d486d6d7SAndreas Gohr header("Content-Disposition: attachment; filename=$table.struct.json"); 66d486d6d7SAndreas Gohr echo $builder->toJSON(); 67d486d6d7SAndreas Gohr exit; 68d486d6d7SAndreas Gohr } 698ddf87afSAndreas Gohr // import 708ddf87afSAndreas Gohr if ($table && $INPUT->bool('import')) { 718ddf87afSAndreas Gohr if (isset($_FILES['schemafile']['tmp_name'])) { 728ddf87afSAndreas Gohr $json = io_readFile($_FILES['schemafile']['tmp_name'], false); 738ddf87afSAndreas Gohr if (!$json) { 748ddf87afSAndreas Gohr msg('Something went wrong with the upload', -1); 758ddf87afSAndreas Gohr } else { 760845722bSAndreas Gohr $builder = new SchemaImporter($table, $json, $INPUT->bool('lookup')); 778ddf87afSAndreas Gohr if (!$builder->build()) { 788ddf87afSAndreas Gohr msg('something went wrong while saving', -1); 798ddf87afSAndreas Gohr } 80ae5c46faSAndreas Gohr touch(action_plugin_struct_cache::getSchemaRefreshFile()); 818ddf87afSAndreas Gohr } 828ddf87afSAndreas Gohr } 838ddf87afSAndreas Gohr } 84a0b3799eSAndreas Gohr 85a0b3799eSAndreas Gohr // import CSV 86a0b3799eSAndreas Gohr if ($table && $INPUT->bool('importcsv')) { 87a0b3799eSAndreas Gohr if (isset($_FILES['csvfile']['tmp_name'])) { 88a0b3799eSAndreas Gohr try { 891fc2361fSSzymon Olewniczak if ($INPUT->bool('lookup')) { 901fc2361fSSzymon Olewniczak $csvImporter = new CSVLookupImporter($table, $_FILES['csvfile']['tmp_name']); 911fc2361fSSzymon Olewniczak } else { 921fc2361fSSzymon Olewniczak $csvImporter = new CSVPageImporter($table, $_FILES['csvfile']['tmp_name']); 931fc2361fSSzymon Olewniczak } 941fc2361fSSzymon Olewniczak $csvImporter->import(); 951fc2361fSSzymon Olewniczak 966d2df532SAndreas Gohr msg($this->getLang('admin_csvdone'), 1); 97a0b3799eSAndreas Gohr } catch (StructException $e) { 98a0b3799eSAndreas Gohr msg(hsc($e->getMessage()), -1); 99a0b3799eSAndreas Gohr } 100a0b3799eSAndreas Gohr } 101a0b3799eSAndreas Gohr } 102a0b3799eSAndreas Gohr 103f36cc634SAndreas Gohr // export CSV 104f36cc634SAndreas Gohr if ($table && $INPUT->bool('exportcsv')) { 105f36cc634SAndreas Gohr header('Content-Type: text/csv'); 106f36cc634SAndreas Gohr header('Content-Disposition: attachment; filename="' . $table . '.csv";'); 107f36cc634SAndreas Gohr new CSVExporter($table); 108f36cc634SAndreas Gohr exit(); 109f36cc634SAndreas Gohr } 110f36cc634SAndreas Gohr 111d5a1a6dcSAndreas Gohr // delete 112d5a1a6dcSAndreas Gohr if ($table && $INPUT->bool('delete')) { 113d5a1a6dcSAndreas Gohr if ($table != $INPUT->str('confirm')) { 114d5a1a6dcSAndreas Gohr msg($this->getLang('del_fail'), -1); 115d5a1a6dcSAndreas Gohr } else { 116d5a1a6dcSAndreas Gohr try { 117d5a1a6dcSAndreas Gohr $schema = new Schema($table); 118d5a1a6dcSAndreas Gohr $schema->delete(); 119d5a1a6dcSAndreas Gohr msg($this->getLang('del_ok'), 1); 120ae5c46faSAndreas Gohr touch(action_plugin_struct_cache::getSchemaRefreshFile()); 121d5a1a6dcSAndreas Gohr send_redirect(wl($ID, array('do' => 'admin', 'page' => 'struct_schemas'), true, '&')); 122d5a1a6dcSAndreas Gohr } catch (StructException $e) { 123d5a1a6dcSAndreas Gohr msg(hsc($e->getMessage()), -1); 124d5a1a6dcSAndreas Gohr } 125d5a1a6dcSAndreas Gohr } 126d5a1a6dcSAndreas Gohr } 127d5a1a6dcSAndreas Gohr 12879c83e06SMichael Große // clear 12979c83e06SMichael Große if ($table && $INPUT->bool('clear')) { 13079c83e06SMichael Große if ($table != $INPUT->str('confirm_clear')) { 13179c83e06SMichael Große msg($this->getLang('clear_fail'), -1); 13279c83e06SMichael Große } else { 13379c83e06SMichael Große try { 13479c83e06SMichael Große $schema = new Schema($table); 13579c83e06SMichael Große $schema->clear(); 13679c83e06SMichael Große msg($this->getLang('clear_ok'), 1); 13779c83e06SMichael Große touch(action_plugin_struct_cache::getSchemaRefreshFile()); 13879c83e06SMichael Große send_redirect(wl($ID, array('do' => 'admin', 'page' => 'struct_schemas'), true, '&')); 13979c83e06SMichael Große } catch (StructException $e) { 14079c83e06SMichael Große msg(hsc($e->getMessage()), -1); 14179c83e06SMichael Große } 14279c83e06SMichael Große } 14379c83e06SMichael Große } 14487fdbc6bSMichael Große } 14587fdbc6bSMichael Große 14687fdbc6bSMichael Große /** 14787fdbc6bSMichael Große * Render HTML output, e.g. helpful text and a form 14887fdbc6bSMichael Große */ 149*d6d97f60SAnna Dabrowska public function html() 150*d6d97f60SAnna Dabrowska { 15187fdbc6bSMichael Große global $INPUT; 15287fdbc6bSMichael Große 15387fdbc6bSMichael Große $table = Schema::cleanTableName($INPUT->str('table')); 15487fdbc6bSMichael Große if ($table) { 1550ceefd5cSAnna Dabrowska $schema = new Schema($table, 0); 1567c080d69SAndreas Gohr 1576af24d3eSAndreas Gohr echo $this->locale_xhtml('editor_edit'); 1580ceefd5cSAnna Dabrowska echo '<h2>' . sprintf($this->getLang('edithl'), hsc($table)) . '</h2>'; 1598ddf87afSAndreas Gohr 1608ddf87afSAndreas Gohr echo '<ul class="tabs" id="plugin__struct_tabs">'; 1618ddf87afSAndreas Gohr /** @noinspection HtmlUnknownAnchorTarget */ 1628ddf87afSAndreas Gohr echo '<li class="active"><a href="#plugin__struct_editor">' . $this->getLang('tab_edit') . '</a></li>'; 1638ddf87afSAndreas Gohr /** @noinspection HtmlUnknownAnchorTarget */ 1648ddf87afSAndreas Gohr echo '<li><a href="#plugin__struct_json">' . $this->getLang('tab_export') . '</a></li>'; 165d5a1a6dcSAndreas Gohr /** @noinspection HtmlUnknownAnchorTarget */ 166d5a1a6dcSAndreas Gohr echo '<li><a href="#plugin__struct_delete">' . $this->getLang('tab_delete') . '</a></li>'; 1678ddf87afSAndreas Gohr echo '</ul>'; 1688ddf87afSAndreas Gohr echo '<div class="panelHeader"></div>'; 1698ddf87afSAndreas Gohr 1707c080d69SAndreas Gohr $editor = new SchemaEditor($schema); 17187fdbc6bSMichael Große echo $editor->getEditor(); 1720845722bSAndreas Gohr echo $this->html_json($schema); 1730845722bSAndreas Gohr echo $this->html_delete($schema); 17487fdbc6bSMichael Große } else { 1756af24d3eSAndreas Gohr echo $this->locale_xhtml('editor_intro'); 1768ddf87afSAndreas Gohr echo $this->html_newschema(); 17787fdbc6bSMichael Große } 17887fdbc6bSMichael Große } 17987fdbc6bSMichael Große 18087fdbc6bSMichael Große /** 1818ddf87afSAndreas Gohr * Form for handling import/export from/to JSON 1820845722bSAndreas Gohr * 1830845722bSAndreas Gohr * @param Schema $schema 1848ddf87afSAndreas Gohr * @return string 1858ddf87afSAndreas Gohr */ 186*d6d97f60SAnna Dabrowska protected function html_json(Schema $schema) 187*d6d97f60SAnna Dabrowska { 1888ddf87afSAndreas Gohr $form = new Form(array('enctype' => 'multipart/form-data', 'id' => 'plugin__struct_json')); 1898ddf87afSAndreas Gohr $form->setHiddenField('do', 'admin'); 1908ddf87afSAndreas Gohr $form->setHiddenField('page', 'struct_schemas'); 1910845722bSAndreas Gohr $form->setHiddenField('table', $schema->getTable()); 1928ddf87afSAndreas Gohr 1938ddf87afSAndreas Gohr $form->addFieldsetOpen($this->getLang('export')); 1948ddf87afSAndreas Gohr $form->addButton('export', $this->getLang('btn_export')); 1958ddf87afSAndreas Gohr $form->addFieldsetClose(); 1968ddf87afSAndreas Gohr 1978ddf87afSAndreas Gohr $form->addFieldsetOpen($this->getLang('import')); 1980bd068beSMichael Große $form->addElement(new \dokuwiki\Form\InputElement('file', 'schemafile'))->attr('accept', '.json'); 1998ddf87afSAndreas Gohr $form->addButton('import', $this->getLang('btn_import')); 2008ddf87afSAndreas Gohr $form->addHTML('<p>' . $this->getLang('import_warning') . '</p>'); 2018ddf87afSAndreas Gohr $form->addFieldsetClose(); 202a0b3799eSAndreas Gohr 2036d2df532SAndreas Gohr $form->addFieldsetOpen($this->getLang('admin_csvexport')); 204f36cc634SAndreas Gohr $form->addButton('exportcsv', $this->getLang('btn_export')); 205f36cc634SAndreas Gohr $form->addFieldsetClose(); 206f36cc634SAndreas Gohr 2076d2df532SAndreas Gohr $form->addFieldsetOpen($this->getLang('admin_csvimport')); 2080bd068beSMichael Große $form->addElement(new \dokuwiki\Form\InputElement('file', 'csvfile'))->attr('accept', '.csv'); 209a0b3799eSAndreas Gohr $form->addButton('importcsv', $this->getLang('btn_import')); 210a22f2592SMichael Große $form->addCheckbox('createPage', 'Create missing pages')->addClass('block edit'); 2116d2df532SAndreas Gohr $form->addHTML('<p><a href="https://www.dokuwiki.org/plugin:struct:csvimport">' . $this->getLang('admin_csvhelp') . '</a></p>'); 212a0b3799eSAndreas Gohr $form->addFieldsetClose(); 213a0b3799eSAndreas Gohr 2148ddf87afSAndreas Gohr return $form->toHTML(); 2158ddf87afSAndreas Gohr } 2168ddf87afSAndreas Gohr 2178ddf87afSAndreas Gohr /** 218d5a1a6dcSAndreas Gohr * Form for deleting schemas 2190845722bSAndreas Gohr * 2200845722bSAndreas Gohr * @param Schema $schema 221d5a1a6dcSAndreas Gohr * @return string 222d5a1a6dcSAndreas Gohr */ 223*d6d97f60SAnna Dabrowska protected function html_delete(Schema $schema) 224*d6d97f60SAnna Dabrowska { 225d5a1a6dcSAndreas Gohr $form = new Form(array('id' => 'plugin__struct_delete')); 226d5a1a6dcSAndreas Gohr $form->setHiddenField('do', 'admin'); 227d5a1a6dcSAndreas Gohr $form->setHiddenField('page', 'struct_schemas'); 2280845722bSAndreas Gohr $form->setHiddenField('table', $schema->getTable()); 229d5a1a6dcSAndreas Gohr 23079c83e06SMichael Große $form->addFieldsetOpen($this->getLang('btn_delete')); 231d5a1a6dcSAndreas Gohr $form->addHTML($this->locale_xhtml('delete_intro')); 232d5a1a6dcSAndreas Gohr $form->addTextInput('confirm', $this->getLang('del_confirm')); 233d5a1a6dcSAndreas Gohr $form->addButton('delete', $this->getLang('btn_delete')); 234d5a1a6dcSAndreas Gohr $form->addFieldsetClose(); 23579c83e06SMichael Große 23679c83e06SMichael Große $form->addFieldsetOpen($this->getLang('btn_clear')); 23779c83e06SMichael Große $form->addHTML($this->locale_xhtml('clear_intro')); 23879c83e06SMichael Große $form->addTextInput('confirm_clear', $this->getLang('clear_confirm')); 23979c83e06SMichael Große $form->addButton('clear', $this->getLang('btn_clear')); 24079c83e06SMichael Große $form->addFieldsetClose(); 24179c83e06SMichael Große 242d5a1a6dcSAndreas Gohr return $form->toHTML(); 243d5a1a6dcSAndreas Gohr } 244d5a1a6dcSAndreas Gohr 245d5a1a6dcSAndreas Gohr /** 24687fdbc6bSMichael Große * Form to add a new schema 2478ddf87afSAndreas Gohr * 2488ddf87afSAndreas Gohr * @return string 24987fdbc6bSMichael Große */ 250*d6d97f60SAnna Dabrowska protected function html_newschema() 251*d6d97f60SAnna Dabrowska { 25287fdbc6bSMichael Große $form = new Form(); 2534e427bd5SAndreas Gohr $form->addClass('struct_newschema'); 25487fdbc6bSMichael Große $form->addFieldsetOpen($this->getLang('create')); 25587fdbc6bSMichael Große $form->setHiddenField('do', 'admin'); 256dbffe06eSAndreas Gohr $form->setHiddenField('page', 'struct_schemas'); 25787fdbc6bSMichael Große $form->addTextInput('table', $this->getLang('schemaname')); 25887fdbc6bSMichael Große $form->addButton('', $this->getLang('save')); 25987fdbc6bSMichael Große $form->addHTML('<p>' . $this->getLang('createhint') . '</p>'); // FIXME is that true? we probably could 26087fdbc6bSMichael Große $form->addFieldsetClose(); 2618ddf87afSAndreas Gohr return $form->toHTML(); 26287fdbc6bSMichael Große } 26387fdbc6bSMichael Große 26487fdbc6bSMichael Große /** 26587fdbc6bSMichael Große * Adds all available schemas to the Table of Contents 26687fdbc6bSMichael Große * 26787fdbc6bSMichael Große * @return array 26887fdbc6bSMichael Große */ 269*d6d97f60SAnna Dabrowska public function getTOC() 270*d6d97f60SAnna Dabrowska { 27187fdbc6bSMichael Große global $ID; 27287fdbc6bSMichael Große 27387fdbc6bSMichael Große $toc = array(); 2748ddf87afSAndreas Gohr $link = wl( 275*d6d97f60SAnna Dabrowska $ID, 276*d6d97f60SAnna Dabrowska array( 27787fdbc6bSMichael Große 'do' => 'admin', 278dbffe06eSAndreas Gohr 'page' => 'struct_assignments' 2798ddf87afSAndreas Gohr ) 2808ddf87afSAndreas Gohr ); 281dbffe06eSAndreas Gohr $toc[] = html_mktocitem($link, $this->getLang('menu_assignments'), 0, ''); 2827c080d69SAndreas Gohr $slink = wl( 283*d6d97f60SAnna Dabrowska $ID, 284*d6d97f60SAnna Dabrowska array( 285dbffe06eSAndreas Gohr 'do' => 'admin', 286dbffe06eSAndreas Gohr 'page' => 'struct_schemas' 2878ddf87afSAndreas Gohr ) 2888ddf87afSAndreas Gohr ); 2897c080d69SAndreas Gohr $toc[] = html_mktocitem($slink, $this->getLang('menu'), 0, ''); 29087fdbc6bSMichael Große 2910ceefd5cSAnna Dabrowska $tables = Schema::getAll(); 2927c080d69SAndreas Gohr if ($tables) { 293097f4a53SAndreas Gohr foreach ($tables as $table) { 2948ddf87afSAndreas Gohr $link = wl( 295*d6d97f60SAnna Dabrowska $ID, 296*d6d97f60SAnna Dabrowska array( 29787fdbc6bSMichael Große 'do' => 'admin', 298dbffe06eSAndreas Gohr 'page' => 'struct_schemas', 299097f4a53SAndreas Gohr 'table' => $table 3008ddf87afSAndreas Gohr ) 3018ddf87afSAndreas Gohr ); 30287fdbc6bSMichael Große 3030ceefd5cSAnna Dabrowska $toc[] = html_mktocitem($link, hsc($table), 1, ''); 3047c080d69SAndreas Gohr } 3057c080d69SAndreas Gohr } 3067c080d69SAndreas Gohr 30787fdbc6bSMichael Große return $toc; 30887fdbc6bSMichael Große } 30987fdbc6bSMichael Große} 31087fdbc6bSMichael Große 31187fdbc6bSMichael Große// vim:ts=4:sw=4:et: 312