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