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