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')); 38 $form->setHiddenField('do', 'admin'); 39 $form->setHiddenField('page', 'struct'); 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(); 56 } 57 58 /** 59 * Returns the HTML to edit a single column definition of the schema 60 * 61 * @param string $column_id 62 * @param Column $col 63 * @param string $key The key to use in the form 64 * @return string 65 * @todo this should probably be reused for adding new columns via AJAX later? 66 */ 67 protected function adminColumn($column_id, Column $col, $key='cols') { 68 $base = 'schema['.$key.'][' . $column_id . ']'; // base name for all fields 69 70 $html = '<tr>'; 71 72 $html .= '<td>'; 73 $html .= '<input type="text" name="' . $base . '[sort]" value="' . hsc($col->getSort()) . '" size="3">'; 74 $html .= '</td>'; 75 76 $html .= '<td>'; 77 $html .= '<input type="text" name="' . $base . '[label]" value="' . hsc($col->getType()->getLabel()) . '">'; 78 $html .= '</td>'; 79 80 $html .= '<td>'; 81 $checked = $col->getType()->isMulti() ? 'checked="checked"' : ''; 82 $html .= '<input type="checkbox" name="' . $base . '[ismulti]" value="1" ' . $checked . '>'; 83 $html .= '</td>'; 84 85 $html .= '<td>'; 86 $config = json_encode($col->getType()->getConfig(), JSON_PRETTY_PRINT); 87 $html .= '<textarea name="' . $base . '[config]" cols="45" rows="10">' . hsc($config) . '</textarea>'; 88 $html .= '</td>'; 89 90 $types = Column::allTypes(); 91 $html .= '<td>'; 92 $html .= '<select name="' . $base . '[class]">'; 93 foreach($types as $type) { 94 $selected = ($col->getType()->getClass() == $type) ? 'selected="selected"' : ''; 95 $html .= '<option value="' . hsc($type) . '" ' . $selected . '>' . hsc($type) . '</option>'; 96 } 97 $html .= '</select>'; 98 $html .= '</td>'; 99 100 $html .= '</tr>'; 101 102 return $html; 103 } 104 105} 106