1<?php 2 3namespace dokuwiki\plugin\struct\meta; 4 5use dokuwiki\Form\Form; 6use dokuwiki\plugin\struct\types\Text; 7 8/** 9 * Class SchemaEditor 10 * 11 * Provides the editing interface for a given Schema as used in the admin backend. The actual modifying of the 12 * schema happens in the SchemaBuilder class. 13 * 14 * @package dokuwiki\plugin\struct\meta 15 */ 16class SchemaEditor 17{ 18 /** @var Schema the schema that is edited */ 19 protected $schema; 20 21 /** @var \DokuWiki_Plugin */ 22 protected $hlp; 23 24 /** 25 * SchemaEditor constructor. 26 * @param Schema $schema 27 */ 28 public function __construct(Schema $schema) 29 { 30 $this->schema = $schema; 31 $this->hlp = plugin_load('helper', 'struct_config'); 32 } 33 34 /** 35 * Returns the Admin Form to edit the schema 36 * 37 * This data is processed by the SchemaBuilder class 38 * 39 * @return string the HTML for the editor form 40 * @see SchemaBuilder 41 */ 42 public function getEditor() 43 { 44 $form = new Form(array('method' => 'POST', 'id' => 'plugin__struct_editor')); 45 $form->setHiddenField('do', 'admin'); 46 $form->setHiddenField('page', 'struct_schemas'); 47 $form->setHiddenField('table', $this->schema->getTable()); 48 $form->setHiddenField('schema[id]', $this->schema->getId()); 49 50 $form->addHTML('<table class="inline">'); 51 $form->addHTML("<tr> 52 <th>{$this->hlp->getLang('editor_sort')}</th> 53 <th>{$this->hlp->getLang('editor_label')}</th> 54 <th>{$this->hlp->getLang('editor_multi')}</th> 55 <th>{$this->hlp->getLang('editor_conf')}</th> 56 <th>{$this->hlp->getLang('editor_type')}</th> 57 <th>{$this->hlp->getLang('editor_enabled')}</th> 58 </tr>"); 59 60 61 foreach ($this->schema->getColumns() as $key => $col) { 62 $form->addHTML($this->adminColumn($col->getColref(), $col)); 63 } 64 65 // FIXME new one needs to be added dynamically, this is just for testing 66 $form->addHTML($this->adminColumn('new1', new Column($this->schema->getMaxsort() + 10, new Text()), 'new')); 67 68 $form->addHTML('</table>'); 69 70 $form->addFieldsetOpen(); 71 $config = json_encode($this->schema->getConfig(), JSON_PRETTY_PRINT); 72 $form->addHTML( 73 '<textarea name="schema[config]" id="schemaConfig" cols="45" rows="10" class="config">' . 74 hsc($config) . 75 '</textarea>' 76 ); 77 $form->addFieldsetClose(); 78 79 80 $form->addButton('save', 'Save')->attr('type', 'submit'); 81 return $form->toHTML() . $this->initJSONEditor(); 82 } 83 84 /** 85 * Gives the code to attach the JSON editor to the config field 86 * 87 * We do not use the "normal" way, because this is rarely used code and there's no need to always load it. 88 * @return string 89 */ 90 protected function initJSONEditor() 91 { 92 $html = ''; 93 $html .= '<link href="' . DOKU_BASE . 94 'lib/plugins/struct/jsoneditor/jsoneditor.min.css" rel="stylesheet" type="text/css">'; 95 $html .= '<link href="' . DOKU_BASE . 96 'lib/plugins/struct/jsoneditor/setup.css" rel="stylesheet" type="text/css">'; 97 $html .= '<script src="' . DOKU_BASE . 98 'lib/plugins/struct/jsoneditor/jsoneditor-minimalist.min.js" defer="defer"></script>'; 99 $html .= '<script src="' . DOKU_BASE . 100 'lib/plugins/struct/jsoneditor/setup.js" defer="defer"></script>'; 101 return $html; 102 } 103 104 /** 105 * Returns the HTML to edit a single column definition of the schema 106 * 107 * @param string $column_id 108 * @param Column $col 109 * @param string $key The key to use in the form 110 * @return string 111 * @todo this should probably be reused for adding new columns via AJAX later? 112 */ 113 protected function adminColumn($column_id, Column $col, $key = 'cols') 114 { 115 $base = 'schema[' . $key . '][' . $column_id . ']'; // base name for all fields 116 117 $class = $col->isEnabled() ? '' : 'disabled'; 118 119 $html = "<tr class=\"$class\">"; 120 121 $html .= '<td class="sort">'; 122 $html .= '<input type="text" name="' . $base . '[sort]" value="' . hsc($col->getSort()) . '" size="3">'; 123 $html .= '</td>'; 124 125 $html .= '<td class="label">'; 126 $html .= '<input type="text" name="' . $base . '[label]" value="' . hsc($col->getType()->getLabel()) . '">'; 127 $html .= '</td>'; 128 129 $html .= '<td class="ismulti">'; 130 $checked = $col->getType()->isMulti() ? 'checked="checked"' : ''; 131 $html .= '<input type="checkbox" name="' . $base . '[ismulti]" value="1" ' . $checked . '>'; 132 $html .= '</td>'; 133 134 $html .= '<td class="config">'; 135 $config = json_encode($col->getType()->getConfig(), JSON_PRETTY_PRINT); 136 $html .= '<textarea name="' . $base . '[config]" cols="45" rows="10" class="config">' . 137 hsc($config) . 138 '</textarea>'; 139 $html .= '</td>'; 140 141 $types = array_keys(Column::allTypes()); 142 $html .= '<td class="class">'; 143 $html .= '<select name="' . $base . '[class]">'; 144 foreach ($types as $type) { 145 $selected = ($col->getType()->getClass() == $type) ? 'selected="selected"' : ''; 146 $html .= '<option value="' . hsc($type) . '" ' . $selected . '>' . hsc($type) . '</option>'; 147 } 148 $html .= '</select>'; 149 $html .= '</td>'; 150 151 152 $html .= '<td class="isenabled">'; 153 $checked = $col->isEnabled() ? 'checked="checked"' : ''; 154 $html .= '<input type="checkbox" name="' . $base . '[isenabled]" value="1" ' . $checked . '>'; 155 $html .= '</td>'; 156 157 $html .= '</tr>'; 158 159 return $html; 160 } 161} 162