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($this->getLang('admin_csvdone'), 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('admin_csvexport')); 181 $form->addButton('exportcsv', $this->getLang('btn_export')); 182 $form->addFieldsetClose(); 183 184 $form->addFieldsetOpen($this->getLang('admin_csvimport')); 185 $form->addElement(new \dokuwiki\Form\InputElement('file', 'csvfile')); 186 $form->addButton('importcsv', $this->getLang('btn_import')); 187 $form->addHTML('<p><a href="https://www.dokuwiki.org/plugin:struct:csvimport">' . $this->getLang('admin_csvhelp') . '</a></p>'); 188 $form->addFieldsetClose(); 189 190 return $form->toHTML(); 191 } 192 193 /** 194 * Form for deleting schemas 195 * 196 * @param Schema $schema 197 * @return string 198 */ 199 protected function html_delete(Schema $schema) { 200 $form = new Form(array('id' => 'plugin__struct_delete')); 201 $form->setHiddenField('do', 'admin'); 202 $form->setHiddenField('page', 'struct_schemas'); 203 $form->setHiddenField('table', $schema->getTable()); 204 205 $form->addHTML($this->locale_xhtml('delete_intro')); 206 207 $form->addFieldsetOpen($this->getLang('tab_delete')); 208 $form->addTextInput('confirm', $this->getLang('del_confirm')); 209 $form->addButton('delete', $this->getLang('btn_delete')); 210 $form->addFieldsetClose(); 211 return $form->toHTML(); 212 } 213 214 /** 215 * Form to add a new schema 216 * 217 * @return string 218 */ 219 protected function html_newschema() { 220 $form = new Form(); 221 $form->addClass('struct_newschema'); 222 $form->addFieldsetOpen($this->getLang('create')); 223 $form->setHiddenField('do', 'admin'); 224 $form->setHiddenField('page', 'struct_schemas'); 225 $form->addTextInput('table', $this->getLang('schemaname')); 226 $form->addRadioButton('lookup', $this->getLang('page schema'))->val('0')->attr('checked', 'checked'); 227 $form->addRadioButton('lookup', $this->getLang('lookup schema'))->val('1'); 228 $form->addButton('', $this->getLang('save')); 229 $form->addHTML('<p>' . $this->getLang('createhint') . '</p>'); // FIXME is that true? we probably could 230 $form->addFieldsetClose(); 231 return $form->toHTML(); 232 } 233 234 /** 235 * Adds all available schemas to the Table of Contents 236 * 237 * @return array 238 */ 239 public function getTOC() { 240 global $ID; 241 242 $toc = array(); 243 $link = wl( 244 $ID, array( 245 'do' => 'admin', 246 'page' => 'struct_assignments' 247 ) 248 ); 249 $toc[] = html_mktocitem($link, $this->getLang('menu_assignments'), 0, ''); 250 $slink = wl( 251 $ID, array( 252 'do' => 'admin', 253 'page' => 'struct_schemas' 254 ) 255 ); 256 $toc[] = html_mktocitem($slink, $this->getLang('menu'), 0, ''); 257 258 $tables = Schema::getAll('page'); 259 if($tables) { 260 $toc[] = html_mktocitem($slink, $this->getLang('page schema'), 1, ''); 261 foreach($tables as $table) { 262 $link = wl( 263 $ID, array( 264 'do' => 'admin', 265 'page' => 'struct_schemas', 266 'table' => $table 267 ) 268 ); 269 270 $toc[] = html_mktocitem($link, hsc($table), 2, ''); 271 } 272 } 273 274 $tables = Schema::getAll('lookup'); 275 if($tables) { 276 $toc[] = html_mktocitem($slink, $this->getLang('lookup schema'), 1, ''); 277 foreach($tables as $table) { 278 $link = wl( 279 $ID, array( 280 'do' => 'admin', 281 'page' => 'struct_schemas', 282 'table' => $table 283 ) 284 ); 285 286 $toc[] = html_mktocitem($link, hsc($table), 2, ''); 287 } 288 } 289 290 return $toc; 291 } 292 293 294 295} 296 297// vim:ts=4:sw=4:et: 298