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 global $config_cascade; 42 $config_file_path = end($config_cascade['main']['local']); 43 44 // form submit 45 $table = Schema::cleanTableName($INPUT->str('table')); 46 if($table && $INPUT->bool('save') && checkSecurityToken()) { 47 $builder = new SchemaBuilder($table, $INPUT->arr('schema')); 48 if(!$builder->build()) { 49 msg('something went wrong while saving', -1); 50 } 51 touch($config_file_path); 52 } 53 // export 54 if($table && $INPUT->bool('export')) { 55 $builder = new Schema($table); 56 header('Content-Type: application/json'); 57 header("Content-Disposition: attachment; filename=$table.struct.json"); 58 echo $builder->toJSON(); 59 exit; 60 } 61 // import 62 if($table && $INPUT->bool('import')) { 63 if(isset($_FILES['schemafile']['tmp_name'])) { 64 $json = io_readFile($_FILES['schemafile']['tmp_name'], false); 65 if(!$json) { 66 msg('Something went wrong with the upload', -1); 67 } else { 68 $builder = new SchemaImporter($table, $json); 69 if(!$builder->build()) { 70 msg('something went wrong while saving', -1); 71 } 72 touch($config_file_path); 73 } 74 } 75 } 76 // delete 77 if($table && $INPUT->bool('delete')) { 78 if($table != $INPUT->str('confirm')) { 79 msg($this->getLang('del_fail'), -1); 80 } else { 81 try { 82 $schema = new Schema($table); 83 $schema->delete(); 84 msg($this->getLang('del_ok'), 1); 85 touch($config_file_path); 86 send_redirect(wl($ID, array('do' => 'admin', 'page' => 'struct_schemas'), true, '&')); 87 } catch(StructException $e) { 88 msg(hsc($e->getMessage()), -1); 89 } 90 } 91 } 92 93 } 94 95 /** 96 * Render HTML output, e.g. helpful text and a form 97 */ 98 public function html() { 99 global $INPUT; 100 101 $table = Schema::cleanTableName($INPUT->str('table')); 102 if($table) { 103 $schema = new Schema($table, 0, $INPUT->bool('lookup')); 104 if($schema->isLookup()) { 105 $hl = 'edithl lookup'; 106 } else { 107 $hl = 'edithl page'; 108 } 109 110 echo $this->locale_xhtml('editor_edit'); 111 echo '<h2>' . sprintf($this->getLang($hl), hsc($table)) . '</h2>'; 112 113 echo '<ul class="tabs" id="plugin__struct_tabs">'; 114 /** @noinspection HtmlUnknownAnchorTarget */ 115 echo '<li class="active"><a href="#plugin__struct_editor">' . $this->getLang('tab_edit') . '</a></li>'; 116 /** @noinspection HtmlUnknownAnchorTarget */ 117 echo '<li><a href="#plugin__struct_json">' . $this->getLang('tab_export') . '</a></li>'; 118 /** @noinspection HtmlUnknownAnchorTarget */ 119 echo '<li><a href="#plugin__struct_delete">' . $this->getLang('tab_delete') . '</a></li>'; 120 echo '</ul>'; 121 echo '<div class="panelHeader"></div>'; 122 123 $editor = new SchemaEditor($schema); 124 echo $editor->getEditor(); 125 echo $this->html_json(); 126 echo $this->html_delete(); 127 128 } else { 129 echo $this->locale_xhtml('editor_intro'); 130 echo $this->html_newschema(); 131 } 132 } 133 134 /** 135 * Form for handling import/export from/to JSON 136 * @return string 137 */ 138 protected function html_json() { 139 global $INPUT; 140 $table = Schema::cleanTableName($INPUT->str('table')); 141 142 $form = new Form(array('enctype' => 'multipart/form-data', 'id' => 'plugin__struct_json')); 143 $form->setHiddenField('do', 'admin'); 144 $form->setHiddenField('page', 'struct_schemas'); 145 $form->setHiddenField('table', $table); 146 147 $form->addFieldsetOpen($this->getLang('export')); 148 $form->addButton('export', $this->getLang('btn_export')); 149 $form->addFieldsetClose(); 150 151 $form->addFieldsetOpen($this->getLang('import')); 152 $form->addElement(new \dokuwiki\Form\InputElement('file', 'schemafile')); 153 $form->addButton('import', $this->getLang('btn_import')); 154 $form->addHTML('<p>' . $this->getLang('import_warning') . '</p>'); 155 $form->addFieldsetClose(); 156 return $form->toHTML(); 157 } 158 159 /** 160 * Form for deleting schemas 161 * @return string 162 */ 163 protected function html_delete() { 164 global $INPUT; 165 $table = Schema::cleanTableName($INPUT->str('table')); 166 167 $form = new Form(array('id' => 'plugin__struct_delete')); 168 $form->setHiddenField('do', 'admin'); 169 $form->setHiddenField('page', 'struct_schemas'); 170 $form->setHiddenField('table', $table); 171 172 $form->addHTML($this->locale_xhtml('delete_intro')); 173 174 $form->addFieldsetOpen($this->getLang('tab_delete')); 175 $form->addTextInput('confirm', $this->getLang('del_confirm')); 176 $form->addButton('delete', $this->getLang('btn_delete')); 177 $form->addFieldsetClose(); 178 return $form->toHTML(); 179 } 180 181 /** 182 * Form to add a new schema 183 * 184 * @return string 185 */ 186 protected function html_newschema() { 187 $form = new Form(); 188 $form->addClass('struct_newschema'); 189 $form->addFieldsetOpen($this->getLang('create')); 190 $form->setHiddenField('do', 'admin'); 191 $form->setHiddenField('page', 'struct_schemas'); 192 $form->addTextInput('table', $this->getLang('schemaname')); 193 $form->addRadioButton('lookup', $this->getLang('page schema'))->val('0')->attr('checked', 'checked'); 194 $form->addRadioButton('lookup', $this->getLang('lookup schema'))->val('1'); 195 $form->addButton('', $this->getLang('save')); 196 $form->addHTML('<p>' . $this->getLang('createhint') . '</p>'); // FIXME is that true? we probably could 197 $form->addFieldsetClose(); 198 return $form->toHTML(); 199 } 200 201 /** 202 * Adds all available schemas to the Table of Contents 203 * 204 * @return array 205 */ 206 public function getTOC() { 207 global $ID; 208 209 $toc = array(); 210 $link = wl( 211 $ID, array( 212 'do' => 'admin', 213 'page' => 'struct_assignments' 214 ) 215 ); 216 $toc[] = html_mktocitem($link, $this->getLang('menu_assignments'), 0, ''); 217 $slink = wl( 218 $ID, array( 219 'do' => 'admin', 220 'page' => 'struct_schemas' 221 ) 222 ); 223 $toc[] = html_mktocitem($slink, $this->getLang('menu'), 0, ''); 224 225 $tables = Schema::getAll('page'); 226 if($tables) { 227 $toc[] = html_mktocitem($slink, $this->getLang('page schema'), 1, ''); 228 foreach($tables as $table) { 229 $link = wl( 230 $ID, array( 231 'do' => 'admin', 232 'page' => 'struct_schemas', 233 'table' => $table 234 ) 235 ); 236 237 $toc[] = html_mktocitem($link, hsc($table), 2, ''); 238 } 239 } 240 241 $tables = Schema::getAll('lookup'); 242 if($tables) { 243 $toc[] = html_mktocitem($slink, $this->getLang('lookup schema'), 1, ''); 244 foreach($tables as $table) { 245 $link = wl( 246 $ID, array( 247 'do' => 'admin', 248 'page' => 'struct_schemas', 249 'table' => $table 250 ) 251 ); 252 253 $toc[] = html_mktocitem($link, hsc($table), 2, ''); 254 } 255 } 256 257 return $toc; 258 } 259 260} 261 262// vim:ts=4:sw=4:et: 263