1<?php 2 3/** 4 * DokuWiki Plugin struct (Admin Component) 5 * 6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 7 * @author Andreas Gohr, Michael Große <dokuwiki@cosmocode.de> 8 */ 9 10use dokuwiki\Form\Form; 11use dokuwiki\plugin\struct\meta\CSVExporter; 12use dokuwiki\plugin\struct\meta\CSVImporter; 13use dokuwiki\plugin\struct\meta\CSVPageImporter; 14use dokuwiki\plugin\struct\meta\Schema; 15use dokuwiki\plugin\struct\meta\SchemaBuilder; 16use dokuwiki\plugin\struct\meta\SchemaEditor; 17use dokuwiki\plugin\struct\meta\SchemaImporter; 18use dokuwiki\plugin\struct\meta\StructException; 19 20class admin_plugin_struct_schemas extends DokuWiki_Admin_Plugin 21{ 22 23 /** 24 * @return int sort number in admin menu 25 */ 26 public function getMenuSort() 27 { 28 return 500; 29 } 30 31 /** 32 * @return bool true if only access for superuser, false is for superusers and moderators 33 */ 34 public function forAdminOnly() 35 { 36 return false; 37 } 38 39 /** 40 * Should carry out any processing required by the plugin. 41 */ 42 public function handle() 43 { 44 global $INPUT; 45 global $ID; 46 global $config_cascade; 47 $config_file_path = end($config_cascade['main']['local']); 48 49 // form submit 50 $table = Schema::cleanTableName($INPUT->str('table')); 51 if ($table && $INPUT->bool('save') && checkSecurityToken()) { 52 $builder = new SchemaBuilder($table, $INPUT->arr('schema')); 53 if (!$builder->build()) { 54 msg('something went wrong while saving', -1); 55 } 56 touch(action_plugin_struct_cache::getSchemaRefreshFile()); 57 } 58 // export 59 if ($table && $INPUT->bool('export')) { 60 $builder = new Schema($table); 61 header('Content-Type: application/json'); 62 header("Content-Disposition: attachment; filename=$table.struct.json"); 63 echo $builder->toJSON(); 64 exit; 65 } 66 // import 67 if ($table && $INPUT->bool('import')) { 68 if (isset($_FILES['schemafile']['tmp_name'])) { 69 $json = io_readFile($_FILES['schemafile']['tmp_name'], false); 70 if (!$json) { 71 msg('Something went wrong with the upload', -1); 72 } else { 73 $builder = new SchemaImporter($table, $json); 74 if (!$builder->build()) { 75 msg('something went wrong while saving', -1); 76 } 77 touch(action_plugin_struct_cache::getSchemaRefreshFile()); 78 } 79 } 80 } 81 82 // import CSV 83 if ($table && $INPUT->bool('importcsv')) { 84 if (isset($_FILES['csvfile']['tmp_name'])) { 85 try { 86 // FIXME 87 $datatype = $INPUT->str('importtype'); 88 if ($datatype === 'page') { 89 $csvImporter = new CSVPageImporter($table, $_FILES['csvfile']['tmp_name'], $datatype); 90 } else { 91 $csvImporter = new CSVImporter($table, $_FILES['csvfile']['tmp_name'], $datatype); 92 } 93 $csvImporter->import(); 94 msg($this->getLang('admin_csvdone'), 1); 95 } catch (StructException $e) { 96 msg(hsc($e->getMessage()), -1); 97 } 98 } 99 } 100 101 // export CSV 102 if ($table && $INPUT->bool('exportcsv')) { 103 header('Content-Type: text/csv'); 104 header('Content-Disposition: attachment; filename="' . $table . '.csv";'); 105 new CSVExporter($table, $INPUT->str('exporttype')); 106 exit(); 107 } 108 109 // delete 110 if ($table && $INPUT->bool('delete')) { 111 if ($table != $INPUT->str('confirm')) { 112 msg($this->getLang('del_fail'), -1); 113 } else { 114 try { 115 $schema = new Schema($table); 116 $schema->delete(); 117 msg($this->getLang('del_ok'), 1); 118 touch(action_plugin_struct_cache::getSchemaRefreshFile()); 119 send_redirect(wl($ID, array('do' => 'admin', 'page' => 'struct_schemas'), true, '&')); 120 } catch (StructException $e) { 121 msg(hsc($e->getMessage()), -1); 122 } 123 } 124 } 125 126 // clear 127 if ($table && $INPUT->bool('clear')) { 128 if ($table != $INPUT->str('confirm_clear')) { 129 msg($this->getLang('clear_fail'), -1); 130 } else { 131 try { 132 $schema = new Schema($table); 133 $schema->clear(); 134 msg($this->getLang('clear_ok'), 1); 135 touch(action_plugin_struct_cache::getSchemaRefreshFile()); 136 send_redirect(wl($ID, array('do' => 'admin', 'page' => 'struct_schemas'), true, '&')); 137 } catch (StructException $e) { 138 msg(hsc($e->getMessage()), -1); 139 } 140 } 141 } 142 } 143 144 /** 145 * Render HTML output, e.g. helpful text and a form 146 */ 147 public function html() 148 { 149 global $INPUT; 150 151 $table = Schema::cleanTableName($INPUT->str('table')); 152 if ($table) { 153 $schema = new Schema($table, 0); 154 155 echo $this->locale_xhtml('editor_edit'); 156 echo '<h2>' . sprintf($this->getLang('edithl'), hsc($table)) . '</h2>'; 157 158 echo '<ul class="tabs" id="plugin__struct_tabs">'; 159 /** @noinspection HtmlUnknownAnchorTarget */ 160 echo '<li class="active"><a href="#plugin__struct_editor">' . $this->getLang('tab_edit') . '</a></li>'; 161 /** @noinspection HtmlUnknownAnchorTarget */ 162 echo '<li><a href="#plugin__struct_json">' . $this->getLang('tab_export') . '</a></li>'; 163 /** @noinspection HtmlUnknownAnchorTarget */ 164 echo '<li><a href="#plugin__struct_delete">' . $this->getLang('tab_delete') . '</a></li>'; 165 echo '</ul>'; 166 echo '<div class="panelHeader"></div>'; 167 168 $editor = new SchemaEditor($schema); 169 echo $editor->getEditor(); 170 echo $this->htmlJson($schema); 171 echo $this->htmlDelete($schema); 172 } else { 173 echo $this->locale_xhtml('editor_intro'); 174 echo $this->htmlNewschema(); 175 } 176 } 177 178 /** 179 * Form for handling import/export from/to JSON and CSV 180 * 181 * @param Schema $schema 182 * @return string 183 */ 184 protected function htmlJson(Schema $schema) 185 { 186 $form = new Form(array('enctype' => 'multipart/form-data', 'id' => 'plugin__struct_json')); 187 $form->setHiddenField('do', 'admin'); 188 $form->setHiddenField('page', 'struct_schemas'); 189 $form->setHiddenField('table', $schema->getTable()); 190 191 // schemas 192 $form->addFieldsetOpen($this->getLang('export')); 193 $form->addButton('export', $this->getLang('btn_export')); 194 $form->addFieldsetClose(); 195 196 $form->addFieldsetOpen($this->getLang('import')); 197 $form->addElement(new \dokuwiki\Form\InputElement('file', 'schemafile'))->attr('accept', '.json'); 198 $form->addButton('import', $this->getLang('btn_import')); 199 $form->addHTML('<p>' . $this->getLang('import_warning') . '</p>'); 200 $form->addFieldsetClose(); 201 202 // data 203 $form->addFieldsetOpen($this->getLang('admin_csvexport')); 204 $form->addTagOpen('legend'); 205 $form->addHTML($this->getLang('admin_csvexport_datatype')); 206 $form->addTagClose('legend'); 207 $form->addRadioButton('exporttype', $this->getLang('admin_csv_page')) 208 ->val('page') 209 ->attr('checked', 'checked')->addClass('edit block'); 210 $form->addRadioButton('exporttype', $this->getLang('admin_csv_lookup')) 211 ->val('lookup') 212 ->addClass('edit block'); 213 $form->addRadioButton('exporttype', $this->getLang('admin_csv_serial')) 214 ->val('serial') 215 ->addClass('edit block'); 216 $form->addHTML('<br>'); 217 $form->addButton('exportcsv', $this->getLang('btn_export')); 218 $form->addFieldsetClose(); 219 220 $form->addFieldsetOpen($this->getLang('admin_csvimport')); 221 $form->addTagOpen('legend'); 222 $form->addHTML($this->getLang('admin_csvimport_datatype')); 223 $form->addTagClose('legend'); 224 $form->addRadioButton('importtype', $this->getLang('admin_csv_page')) 225 ->val('page') 226 ->attr('checked', 'checked') 227 ->addClass('edit block'); 228 $form->addRadioButton('importtype', $this->getLang('admin_csv_lookup')) 229 ->val('lookup') 230 ->addClass('edit block'); 231 $form->addRadioButton('importtype', $this->getLang('admin_csv_serial')) 232 ->val('serial') 233 ->addClass('edit block'); 234 $form->addHTML('<br>'); 235 $form->addElement(new \dokuwiki\Form\InputElement('file', 'csvfile'))->attr('accept', '.csv'); 236 $form->addButton('importcsv', $this->getLang('btn_import')); 237 $form->addCheckbox('createPage', 'Create missing pages')->addClass('block edit'); 238 $form->addHTML('<p><a href="https://www.dokuwiki.org/plugin:struct:csvimport">' . $this->getLang('admin_csvhelp') . '</a></p>'); 239 $form->addFieldsetClose(); 240 241 return $form->toHTML(); 242 } 243 244 /** 245 * Form for deleting schemas 246 * 247 * @param Schema $schema 248 * @return string 249 */ 250 protected function htmlDelete(Schema $schema) 251 { 252 $form = new Form(array('id' => 'plugin__struct_delete')); 253 $form->setHiddenField('do', 'admin'); 254 $form->setHiddenField('page', 'struct_schemas'); 255 $form->setHiddenField('table', $schema->getTable()); 256 257 $form->addFieldsetOpen($this->getLang('btn_delete')); 258 $form->addHTML($this->locale_xhtml('delete_intro')); 259 $form->addTextInput('confirm', $this->getLang('del_confirm')); 260 $form->addButton('delete', $this->getLang('btn_delete')); 261 $form->addFieldsetClose(); 262 263 $form->addFieldsetOpen($this->getLang('btn_clear')); 264 $form->addHTML($this->locale_xhtml('clear_intro')); 265 $form->addTextInput('confirm_clear', $this->getLang('clear_confirm')); 266 $form->addButton('clear', $this->getLang('btn_clear')); 267 $form->addFieldsetClose(); 268 269 return $form->toHTML(); 270 } 271 272 /** 273 * Form to add a new schema 274 * 275 * @return string 276 */ 277 protected function htmlNewschema() 278 { 279 $form = new Form(); 280 $form->addClass('struct_newschema'); 281 $form->addFieldsetOpen($this->getLang('create')); 282 $form->setHiddenField('do', 'admin'); 283 $form->setHiddenField('page', 'struct_schemas'); 284 $form->addTextInput('table', $this->getLang('schemaname')); 285 $form->addButton('', $this->getLang('save')); 286 $form->addHTML('<p>' . $this->getLang('createhint') . '</p>'); // FIXME is that true? we probably could 287 $form->addFieldsetClose(); 288 return $form->toHTML(); 289 } 290 291 /** 292 * Adds all available schemas to the Table of Contents 293 * 294 * @return array 295 */ 296 public function getTOC() 297 { 298 global $ID; 299 300 $toc = array(); 301 $link = wl( 302 $ID, 303 array( 304 'do' => 'admin', 305 'page' => 'struct_assignments' 306 ) 307 ); 308 $toc[] = html_mktocitem($link, $this->getLang('menu_assignments'), 0, ''); 309 $slink = wl( 310 $ID, 311 array( 312 'do' => 'admin', 313 'page' => 'struct_schemas' 314 ) 315 ); 316 $toc[] = html_mktocitem($slink, $this->getLang('menu'), 0, ''); 317 318 $tables = Schema::getAll(); 319 if ($tables) { 320 foreach ($tables as $table) { 321 $link = wl( 322 $ID, 323 array( 324 'do' => 'admin', 325 'page' => 'struct_schemas', 326 'table' => $table 327 ) 328 ); 329 330 $toc[] = html_mktocitem($link, hsc($table), 1, ''); 331 } 332 } 333 334 return $toc; 335 } 336} 337 338// vim:ts=4:sw=4:et: 339