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\CSVSerialImporter; 15use dokuwiki\plugin\struct\meta\Schema; 16use dokuwiki\plugin\struct\meta\SchemaBuilder; 17use dokuwiki\plugin\struct\meta\SchemaEditor; 18use dokuwiki\plugin\struct\meta\SchemaImporter; 19use dokuwiki\plugin\struct\meta\StructException; 20 21class admin_plugin_struct_schemas extends DokuWiki_Admin_Plugin 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 $datatype = $INPUT->str('importtype'); 87 if ($datatype === CSVExporter::DATATYPE_PAGE) { 88 $csvImporter = new CSVPageImporter($table, $_FILES['csvfile']['tmp_name'], $datatype); 89 } elseif ($datatype === CSVExporter::DATATYPE_SERIAL) { 90 $csvImporter = new CSVSerialImporter($table, $_FILES['csvfile']['tmp_name'], $datatype); 91 } else { 92 $csvImporter = new CSVImporter($table, $_FILES['csvfile']['tmp_name'], $datatype); 93 } 94 $csvImporter->import(); 95 msg($this->getLang('admin_csvdone'), 1); 96 } catch (StructException $e) { 97 msg(hsc($e->getMessage()), -1); 98 } 99 } 100 } 101 102 // export CSV 103 if ($table && $INPUT->bool('exportcsv')) { 104 header('Content-Type: text/csv'); 105 header('Content-Disposition: attachment; filename="' . $table . '.csv";'); 106 new CSVExporter($table, $INPUT->str('exporttype')); 107 exit(); 108 } 109 110 // delete 111 if ($table && $INPUT->bool('delete')) { 112 if ($table != $INPUT->str('confirm')) { 113 msg($this->getLang('del_fail'), -1); 114 } else { 115 try { 116 $schema = new Schema($table); 117 $schema->delete(); 118 msg($this->getLang('del_ok'), 1); 119 touch(action_plugin_struct_cache::getSchemaRefreshFile()); 120 send_redirect(wl($ID, array('do' => 'admin', 'page' => 'struct_schemas'), true, '&')); 121 } catch (StructException $e) { 122 msg(hsc($e->getMessage()), -1); 123 } 124 } 125 } 126 127 // clear 128 if ($table && $INPUT->bool('clear')) { 129 if ($table != $INPUT->str('confirm_clear')) { 130 msg($this->getLang('clear_fail'), -1); 131 } else { 132 try { 133 $schema = new Schema($table); 134 $schema->clear(); 135 msg($this->getLang('clear_ok'), 1); 136 touch(action_plugin_struct_cache::getSchemaRefreshFile()); 137 send_redirect(wl($ID, array('do' => 'admin', 'page' => 'struct_schemas'), true, '&')); 138 } catch (StructException $e) { 139 msg(hsc($e->getMessage()), -1); 140 } 141 } 142 } 143 } 144 145 /** 146 * Render HTML output, e.g. helpful text and a form 147 */ 148 public function html() 149 { 150 global $INPUT; 151 152 $table = Schema::cleanTableName($INPUT->str('table')); 153 if ($table) { 154 $schema = new Schema($table, 0); 155 156 echo $this->locale_xhtml('editor_edit'); 157 echo '<h2>' . sprintf($this->getLang('edithl'), hsc($table)) . '</h2>'; 158 159 echo '<ul class="tabs" id="plugin__struct_tabs">'; 160 /** @noinspection HtmlUnknownAnchorTarget */ 161 echo '<li class="active"><a href="#plugin__struct_editor">' . $this->getLang('tab_edit') . '</a></li>'; 162 /** @noinspection HtmlUnknownAnchorTarget */ 163 echo '<li><a href="#plugin__struct_json">' . $this->getLang('tab_export') . '</a></li>'; 164 /** @noinspection HtmlUnknownAnchorTarget */ 165 echo '<li><a href="#plugin__struct_delete">' . $this->getLang('tab_delete') . '</a></li>'; 166 echo '</ul>'; 167 echo '<div class="panelHeader"></div>'; 168 169 $editor = new SchemaEditor($schema); 170 echo $editor->getEditor(); 171 echo $this->htmlJson($schema); 172 echo $this->htmlDelete($schema); 173 } else { 174 echo $this->locale_xhtml('editor_intro'); 175 echo $this->htmlNewschema(); 176 } 177 } 178 179 /** 180 * Form for handling import/export from/to JSON and CSV 181 * 182 * @param Schema $schema 183 * @return string 184 */ 185 protected function htmlJson(Schema $schema) 186 { 187 $form = new Form(array('enctype' => 'multipart/form-data', 'id' => 'plugin__struct_json')); 188 $form->setHiddenField('do', 'admin'); 189 $form->setHiddenField('page', 'struct_schemas'); 190 $form->setHiddenField('table', $schema->getTable()); 191 192 // schemas 193 $form->addFieldsetOpen($this->getLang('export')); 194 $form->addButton('export', $this->getLang('btn_export')); 195 $form->addFieldsetClose(); 196 197 $form->addFieldsetOpen($this->getLang('import')); 198 $form->addElement(new \dokuwiki\Form\InputElement('file', 'schemafile'))->attr('accept', '.json'); 199 $form->addButton('import', $this->getLang('btn_import')); 200 $form->addHTML('<p>' . $this->getLang('import_warning') . '</p>'); 201 $form->addFieldsetClose(); 202 203 // data 204 $form->addFieldsetOpen($this->getLang('admin_csvexport')); 205 $form->addTagOpen('legend'); 206 $form->addHTML($this->getLang('admin_csvexport_datatype')); 207 $form->addTagClose('legend'); 208 $form->addRadioButton('exporttype', $this->getLang('admin_csv_page')) 209 ->val(CSVExporter::DATATYPE_PAGE) 210 ->attr('checked', 'checked')->addClass('edit block'); 211 $form->addRadioButton('exporttype', $this->getLang('admin_csv_lookup')) 212 ->val(CSVExporter::DATATYPE_GLOBAL) 213 ->addClass('edit block'); 214 $form->addRadioButton('exporttype', $this->getLang('admin_csv_serial')) 215 ->val(CSVExporter::DATATYPE_SERIAL) 216 ->addClass('edit block'); 217 $form->addHTML('<br>'); 218 $form->addButton('exportcsv', $this->getLang('btn_export')); 219 $form->addFieldsetClose(); 220 221 $form->addFieldsetOpen($this->getLang('admin_csvimport')); 222 $form->addTagOpen('legend'); 223 $form->addHTML($this->getLang('admin_csvimport_datatype')); 224 $form->addTagClose('legend'); 225 $form->addRadioButton('importtype', $this->getLang('admin_csv_page')) 226 ->val(CSVExporter::DATATYPE_PAGE) 227 ->attr('checked', 'checked') 228 ->addClass('edit block'); 229 $form->addRadioButton('importtype', $this->getLang('admin_csv_lookup')) 230 ->val(CSVExporter::DATATYPE_GLOBAL) 231 ->addClass('edit block'); 232 $form->addRadioButton('importtype', $this->getLang('admin_csv_serial')) 233 ->val(CSVExporter::DATATYPE_SERIAL) 234 ->addClass('edit block'); 235 $form->addHTML('<br>'); 236 $form->addElement(new \dokuwiki\Form\InputElement('file', 'csvfile'))->attr('accept', '.csv'); 237 $form->addButton('importcsv', $this->getLang('btn_import')); 238 $form->addCheckbox('createPage', 'Create missing pages')->addClass('block edit'); 239 $form->addHTML( 240 '<p><a href="https://www.dokuwiki.org/plugin:struct:csvimport">' . 241 $this->getLang('admin_csvhelp') . '</a></p>' 242 ); 243 $form->addFieldsetClose(); 244 245 return $form->toHTML(); 246 } 247 248 /** 249 * Form for deleting schemas 250 * 251 * @param Schema $schema 252 * @return string 253 */ 254 protected function htmlDelete(Schema $schema) 255 { 256 $form = new Form(array('id' => 'plugin__struct_delete')); 257 $form->setHiddenField('do', 'admin'); 258 $form->setHiddenField('page', 'struct_schemas'); 259 $form->setHiddenField('table', $schema->getTable()); 260 261 $form->addFieldsetOpen($this->getLang('btn_delete')); 262 $form->addHTML($this->locale_xhtml('delete_intro')); 263 $form->addTextInput('confirm', $this->getLang('del_confirm')); 264 $form->addButton('delete', $this->getLang('btn_delete')); 265 $form->addFieldsetClose(); 266 267 $form->addFieldsetOpen($this->getLang('btn_clear')); 268 $form->addHTML($this->locale_xhtml('clear_intro')); 269 $form->addTextInput('confirm_clear', $this->getLang('clear_confirm')); 270 $form->addButton('clear', $this->getLang('btn_clear')); 271 $form->addFieldsetClose(); 272 273 return $form->toHTML(); 274 } 275 276 /** 277 * Form to add a new schema 278 * 279 * @return string 280 */ 281 protected function htmlNewschema() 282 { 283 $form = new Form(); 284 $form->addClass('struct_newschema'); 285 $form->addFieldsetOpen($this->getLang('create')); 286 $form->setHiddenField('do', 'admin'); 287 $form->setHiddenField('page', 'struct_schemas'); 288 $form->addTextInput('table', $this->getLang('schemaname')); 289 $form->addButton('', $this->getLang('save')); 290 $form->addHTML('<p>' . $this->getLang('createhint') . '</p>'); // FIXME is that true? we probably could 291 $form->addFieldsetClose(); 292 return $form->toHTML(); 293 } 294 295 /** 296 * Adds all available schemas to the Table of Contents 297 * 298 * @return array 299 */ 300 public function getTOC() 301 { 302 global $ID; 303 304 $toc = array(); 305 $link = wl( 306 $ID, 307 array( 308 'do' => 'admin', 309 'page' => 'struct_assignments' 310 ) 311 ); 312 $toc[] = html_mktocitem($link, $this->getLang('menu_assignments'), 0, ''); 313 $slink = wl( 314 $ID, 315 array( 316 'do' => 'admin', 317 'page' => 'struct_schemas' 318 ) 319 ); 320 $toc[] = html_mktocitem($slink, $this->getLang('menu'), 0, ''); 321 322 $tables = Schema::getAll(); 323 if ($tables) { 324 foreach ($tables as $table) { 325 $link = wl( 326 $ID, 327 array( 328 'do' => 'admin', 329 'page' => 'struct_schemas', 330 'table' => $table 331 ) 332 ); 333 334 $toc[] = html_mktocitem($link, hsc($table), 1, ''); 335 } 336 } 337 338 return $toc; 339 } 340} 341 342// vim:ts=4:sw=4:et: 343