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