1<?php 2/** 3 * DokuWiki Plugin struct (Admin Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Andreas Gohr, Michael Große <dokuwiki@cosmocode.de> 7 */ 8 9use dokuwiki\Form\Form; 10use dokuwiki\plugin\struct\meta\Schema; 11use dokuwiki\plugin\struct\meta\SchemaBuilder; 12use dokuwiki\plugin\struct\meta\SchemaEditor; 13use dokuwiki\plugin\struct\meta\SchemaImporter; 14use dokuwiki\plugin\struct\meta\StructException; 15 16// must be run within Dokuwiki 17if(!defined('DOKU_INC')) die(); 18 19class admin_plugin_struct_schemas extends DokuWiki_Admin_Plugin { 20 21 /** 22 * @return int sort number in admin menu 23 */ 24 public function getMenuSort() { 25 return 500; 26 } 27 28 /** 29 * @return bool true if only access for superuser, false is for superusers and moderators 30 */ 31 public function forAdminOnly() { 32 return false; 33 } 34 35 /** 36 * Should carry out any processing required by the plugin. 37 */ 38 public function handle() { 39 global $INPUT; 40 global $ID; 41 42 // form submit 43 $table = Schema::cleanTableName($INPUT->str('table')); 44 if($table && $INPUT->bool('save') && checkSecurityToken()) { 45 $builder = new SchemaBuilder($table, $INPUT->arr('schema')); 46 if(!$builder->build()) { 47 msg('something went wrong while saving', -1); 48 } 49 touch(DOKU_CONF . 'local.php'); 50 } 51 // export 52 if($table && $INPUT->bool('export')) { 53 $builder = new Schema($table); 54 header('Content-Type: application/json'); 55 header("Content-Disposition: attachment; filename=$table.struct.json"); 56 echo $builder->toJSON(); 57 exit; 58 } 59 // import 60 if($table && $INPUT->bool('import')) { 61 if(isset($_FILES['schemafile']['tmp_name'])) { 62 $json = io_readFile($_FILES['schemafile']['tmp_name'], false); 63 if(!$json) { 64 msg('Something went wrong with the upload', -1); 65 } else { 66 $builder = new SchemaImporter($table, $json); 67 if(!$builder->build()) { 68 msg('something went wrong while saving', -1); 69 } 70 touch(DOKU_CONF . 'local.php'); 71 } 72 } 73 } 74 // delete 75 if($table && $INPUT->bool('delete')) { 76 if($table != $INPUT->str('confirm')) { 77 msg($this->getLang('del_fail'), -1); 78 } else { 79 try { 80 $schema = new Schema($table); 81 $schema->delete(); 82 msg($this->getLang('del_ok'), 1); 83 touch(DOKU_CONF . 'local.php'); 84 send_redirect(wl($ID, array('do' => 'admin', 'page' => 'struct_schemas'), true, '&')); 85 } catch(StructException $e) { 86 msg(hsc($e->getMessage()), -1); 87 } 88 } 89 } 90 91 } 92 93 /** 94 * Render HTML output, e.g. helpful text and a form 95 */ 96 public function html() { 97 global $INPUT; 98 99 $table = Schema::cleanTableName($INPUT->str('table')); 100 if($table) { 101 echo $this->locale_xhtml('editor_edit'); 102 echo '<h2>' . sprintf($this->getLang('edithl'), hsc($table)) . '</h2>'; 103 104 echo '<ul class="tabs" id="plugin__struct_tabs">'; 105 /** @noinspection HtmlUnknownAnchorTarget */ 106 echo '<li class="active"><a href="#plugin__struct_editor">' . $this->getLang('tab_edit') . '</a></li>'; 107 /** @noinspection HtmlUnknownAnchorTarget */ 108 echo '<li><a href="#plugin__struct_json">' . $this->getLang('tab_export') . '</a></li>'; 109 /** @noinspection HtmlUnknownAnchorTarget */ 110 echo '<li><a href="#plugin__struct_delete">' . $this->getLang('tab_delete') . '</a></li>'; 111 echo '</ul>'; 112 echo '<div class="panelHeader"></div>'; 113 114 $editor = new SchemaEditor(new Schema($table)); 115 echo $editor->getEditor(); 116 echo $this->html_json(); 117 echo $this->html_delete(); 118 119 } else { 120 echo $this->locale_xhtml('editor_intro'); 121 echo $this->html_newschema(); 122 } 123 } 124 125 /** 126 * Form for handling import/export from/to JSON 127 * @return string 128 */ 129 protected function html_json() { 130 global $INPUT; 131 $table = Schema::cleanTableName($INPUT->str('table')); 132 133 $form = new Form(array('enctype' => 'multipart/form-data', 'id' => 'plugin__struct_json')); 134 $form->setHiddenField('do', 'admin'); 135 $form->setHiddenField('page', 'struct_schemas'); 136 $form->setHiddenField('table', $table); 137 138 $form->addFieldsetOpen($this->getLang('export')); 139 $form->addButton('export', $this->getLang('btn_export')); 140 $form->addFieldsetClose(); 141 142 $form->addFieldsetOpen($this->getLang('import')); 143 $form->addElement(new \dokuwiki\Form\InputElement('file', 'schemafile')); 144 $form->addButton('import', $this->getLang('btn_import')); 145 $form->addHTML('<p>' . $this->getLang('import_warning') . '</p>'); 146 $form->addFieldsetClose(); 147 return $form->toHTML(); 148 } 149 150 /** 151 * Form for deleting schemas 152 * @return string 153 */ 154 protected function html_delete() { 155 global $INPUT; 156 $table = Schema::cleanTableName($INPUT->str('table')); 157 158 $form = new Form(array('id' => 'plugin__struct_delete')); 159 $form->setHiddenField('do', 'admin'); 160 $form->setHiddenField('page', 'struct_schemas'); 161 $form->setHiddenField('table', $table); 162 163 $form->addHTML($this->locale_xhtml('delete_intro')); 164 165 $form->addFieldsetOpen($this->getLang('tab_delete')); 166 $form->addTextInput('confirm', $this->getLang('del_confirm')); 167 $form->addButton('delete', $this->getLang('btn_delete')); 168 $form->addFieldsetClose(); 169 return $form->toHTML(); 170 } 171 172 /** 173 * Form to add a new schema 174 * 175 * @return string 176 */ 177 protected function html_newschema() { 178 $form = new Form(); 179 $form->addFieldsetOpen($this->getLang('create')); 180 $form->setHiddenField('do', 'admin'); 181 $form->setHiddenField('page', 'struct_schemas'); 182 $form->addTextInput('table', $this->getLang('schemaname')); 183 $form->addButton('', $this->getLang('save')); 184 $form->addHTML('<p>' . $this->getLang('createhint') . '</p>'); // FIXME is that true? we probably could 185 $form->addFieldsetClose(); 186 return $form->toHTML(); 187 } 188 189 /** 190 * Adds all available schemas to the Table of Contents 191 * 192 * @return array 193 */ 194 public function getTOC() { 195 global $ID; 196 197 $toc = array(); 198 $link = wl( 199 $ID, array( 200 'do' => 'admin', 201 'page' => 'struct_assignments' 202 ) 203 ); 204 $toc[] = html_mktocitem($link, $this->getLang('menu_assignments'), 0, ''); 205 $link = wl( 206 $ID, array( 207 'do' => 'admin', 208 'page' => 'struct_schemas' 209 ) 210 ); 211 $toc[] = html_mktocitem($link, $this->getLang('menu'), 0, ''); 212 213 $tables = Schema::getAll(); 214 foreach($tables as $table) { 215 $link = wl( 216 $ID, array( 217 'do' => 'admin', 218 'page' => 'struct_schemas', 219 'table' => $table 220 ) 221 ); 222 223 $toc[] = html_mktocitem($link, hsc($table), 1, ''); 224 } 225 return $toc; 226 } 227 228} 229 230// vim:ts=4:sw=4:et: 231