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