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ße// must be run within Dokuwiki 1087fdbc6bSMichael Großeuse dokuwiki\Form\Form; 1187fdbc6bSMichael Großeuse plugin\struct\meta\Schema; 1287fdbc6bSMichael Großeuse plugin\struct\meta\SchemaEditor; 1387fdbc6bSMichael Große 1487fdbc6bSMichael Großeif(!defined('DOKU_INC')) die(); 1587fdbc6bSMichael Große 1687fdbc6bSMichael Großeclass admin_plugin_struct_schemas extends DokuWiki_Admin_Plugin { 1787fdbc6bSMichael Große 1887fdbc6bSMichael Große /** 1987fdbc6bSMichael Große * @return int sort number in admin menu 2087fdbc6bSMichael Große */ 2187fdbc6bSMichael Große public function getMenuSort() { 2287fdbc6bSMichael Große return 500; 2387fdbc6bSMichael Große } 2487fdbc6bSMichael Große 2587fdbc6bSMichael Große /** 2687fdbc6bSMichael Große * @return bool true if only access for superuser, false is for superusers and moderators 2787fdbc6bSMichael Große */ 2887fdbc6bSMichael Große public function forAdminOnly() { 294d220607SAndreas Gohr return false; 3087fdbc6bSMichael Große } 3187fdbc6bSMichael Große 3287fdbc6bSMichael Große /** 3387fdbc6bSMichael Große * Should carry out any processing required by the plugin. 3487fdbc6bSMichael Große */ 3587fdbc6bSMichael Große public function handle() { 3687fdbc6bSMichael Große global $INPUT; 3787fdbc6bSMichael Große 3887fdbc6bSMichael Große $table = Schema::cleanTableName($INPUT->str('table')); 3987fdbc6bSMichael Große if($table && $INPUT->bool('save') && checkSecurityToken()) { 4087fdbc6bSMichael Große $builder = new \plugin\struct\meta\SchemaBuilder($table, $INPUT->arr('schema')); 4187fdbc6bSMichael Große if(!$builder->build()) { 4287fdbc6bSMichael Große msg('something went wrong while saving', -1); 4387fdbc6bSMichael Große } 4487fdbc6bSMichael Große } 45*d486d6d7SAndreas Gohr if($table && $INPUT->bool('export')) { 46*d486d6d7SAndreas Gohr $builder = new \plugin\struct\meta\Schema($table); 47*d486d6d7SAndreas Gohr header('Content-Type: application/json'); 48*d486d6d7SAndreas Gohr header("Content-Disposition: attachment; filename=$table.struct.json"); 49*d486d6d7SAndreas Gohr echo $builder->toJSON(); 50*d486d6d7SAndreas Gohr exit; 51*d486d6d7SAndreas Gohr } 5287fdbc6bSMichael Große } 5387fdbc6bSMichael Große 5487fdbc6bSMichael Große /** 5587fdbc6bSMichael Große * Render HTML output, e.g. helpful text and a form 5687fdbc6bSMichael Große */ 5787fdbc6bSMichael Große public function html() { 5887fdbc6bSMichael Große global $INPUT; 59*d486d6d7SAndreas Gohr global $ID; 6087fdbc6bSMichael Große 6187fdbc6bSMichael Große $table = Schema::cleanTableName($INPUT->str('table')); 6287fdbc6bSMichael Große if($table) { 636af24d3eSAndreas Gohr echo $this->locale_xhtml('editor_edit'); 6487fdbc6bSMichael Große 656af24d3eSAndreas Gohr echo '<h2>'.sprintf($this->getLang('edithl'), hsc($table)).'</h2>'; 6687fdbc6bSMichael Große $editor = new SchemaEditor(new Schema($table)); 6787fdbc6bSMichael Große echo $editor->getEditor(); 68*d486d6d7SAndreas Gohr 69*d486d6d7SAndreas Gohr echo '<p>'; 70*d486d6d7SAndreas Gohr $export = wl($ID, array('do' => 'admin', 'page' => 'struct_schemas', 'table' => $table, 'export' => 1)); 71*d486d6d7SAndreas Gohr echo '<a href="' . $export . '">' . $this->getLang('export') . '</a>'; 72*d486d6d7SAndreas Gohr echo '</p>'; 7387fdbc6bSMichael Große } else { 746af24d3eSAndreas Gohr echo $this->locale_xhtml('editor_intro'); 7587fdbc6bSMichael Große $this->html_newschema(); 7687fdbc6bSMichael Große } 7787fdbc6bSMichael Große } 7887fdbc6bSMichael Große 7987fdbc6bSMichael Große /** 8087fdbc6bSMichael Große * Form to add a new schema 8187fdbc6bSMichael Große */ 8287fdbc6bSMichael Große protected function html_newschema() { 8387fdbc6bSMichael Große $form = new Form(); 8487fdbc6bSMichael Große $form->addFieldsetOpen($this->getLang('create')); 8587fdbc6bSMichael Große $form->setHiddenField('do', 'admin'); 86dbffe06eSAndreas Gohr $form->setHiddenField('page', 'struct_schemas'); 8787fdbc6bSMichael Große $form->addTextInput('table', $this->getLang('schemaname')); 8887fdbc6bSMichael Große $form->addButton('', $this->getLang('save')); 8987fdbc6bSMichael Große $form->addHTML('<p>'.$this->getLang('createhint').'</p>'); // FIXME is that true? we probably could 9087fdbc6bSMichael Große $form->addFieldsetClose(); 9187fdbc6bSMichael Große echo $form->toHTML(); 9287fdbc6bSMichael Große } 9387fdbc6bSMichael Große 9487fdbc6bSMichael Große /** 9587fdbc6bSMichael Große * Adds all available schemas to the Table of Contents 9687fdbc6bSMichael Große * 9787fdbc6bSMichael Große * @return array 9887fdbc6bSMichael Große */ 9987fdbc6bSMichael Große public function getTOC() { 10087fdbc6bSMichael Große global $ID; 10187fdbc6bSMichael Große 10287fdbc6bSMichael Große $toc = array(); 10387fdbc6bSMichael Große $link = wl($ID, array( 10487fdbc6bSMichael Große 'do' => 'admin', 105dbffe06eSAndreas Gohr 'page' => 'struct_assignments' 106dbffe06eSAndreas Gohr )); 107dbffe06eSAndreas Gohr $toc[] = html_mktocitem($link, $this->getLang('menu_assignments'), 0, ''); 108dbffe06eSAndreas Gohr $link = wl($ID, array( 109dbffe06eSAndreas Gohr 'do' => 'admin', 110dbffe06eSAndreas Gohr 'page' => 'struct_schemas' 11187fdbc6bSMichael Große )); 11287fdbc6bSMichael Große $toc[] = html_mktocitem($link, $this->getLang('menu'), 0, ''); 11387fdbc6bSMichael Große 114097f4a53SAndreas Gohr $tables = Schema::getAll(); 115097f4a53SAndreas Gohr foreach($tables as $table) { 11687fdbc6bSMichael Große $link = wl($ID, array( 11787fdbc6bSMichael Große 'do' => 'admin', 118dbffe06eSAndreas Gohr 'page' => 'struct_schemas', 119097f4a53SAndreas Gohr 'table' => $table 12087fdbc6bSMichael Große )); 12187fdbc6bSMichael Große 122097f4a53SAndreas Gohr $toc[] = html_mktocitem($link, hsc($table), 1, ''); 12387fdbc6bSMichael Große } 12487fdbc6bSMichael Große return $toc; 12587fdbc6bSMichael Große } 12687fdbc6bSMichael Große 12787fdbc6bSMichael Große} 12887fdbc6bSMichael Große 12987fdbc6bSMichael Große// vim:ts=4:sw=4:et: 130