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