1*ae697e1fSAndreas Gohr<?php 2*ae697e1fSAndreas Gohr 3*ae697e1fSAndreas Gohrnamespace plugin\struct\meta; 4*ae697e1fSAndreas Gohr 5*ae697e1fSAndreas Gohruse dokuwiki\Form\Form; 6*ae697e1fSAndreas Gohruse plugin\struct\types\Text; 7*ae697e1fSAndreas Gohr 8*ae697e1fSAndreas Gohr/** 9*ae697e1fSAndreas Gohr * Class SchemaEditor 10*ae697e1fSAndreas Gohr * 11*ae697e1fSAndreas Gohr * Provides the editing interface for a given Schema as used in the admin backend. The actual modifying of the 12*ae697e1fSAndreas Gohr * schema happens in the SchemaBuilder class. 13*ae697e1fSAndreas Gohr * 14*ae697e1fSAndreas Gohr * @package plugin\struct\meta 15*ae697e1fSAndreas Gohr */ 16*ae697e1fSAndreas Gohrclass SchemaEditor { 17*ae697e1fSAndreas Gohr /** @var Schema the schema that is edited */ 18*ae697e1fSAndreas Gohr protected $schema; 19*ae697e1fSAndreas Gohr 20*ae697e1fSAndreas Gohr /** 21*ae697e1fSAndreas Gohr * SchemaEditor constructor. 22*ae697e1fSAndreas Gohr * @param Schema $schema 23*ae697e1fSAndreas Gohr */ 24*ae697e1fSAndreas Gohr public function __construct(Schema $schema) { 25*ae697e1fSAndreas Gohr $this->schema = $schema; 26*ae697e1fSAndreas Gohr } 27*ae697e1fSAndreas Gohr 28*ae697e1fSAndreas Gohr /** 29*ae697e1fSAndreas Gohr * Returns the Admin Form to edit the schema 30*ae697e1fSAndreas Gohr * 31*ae697e1fSAndreas Gohr * This data is processed by the SchemaBuilder class 32*ae697e1fSAndreas Gohr * 33*ae697e1fSAndreas Gohr * @return string the HTML for the editor form 34*ae697e1fSAndreas Gohr * @see SchemaBuilder 35*ae697e1fSAndreas Gohr */ 36*ae697e1fSAndreas Gohr public function getEditor() { 37*ae697e1fSAndreas Gohr $form = new Form(array('method' => 'POST')); 38*ae697e1fSAndreas Gohr $form->setHiddenField('do', 'admin'); 39*ae697e1fSAndreas Gohr $form->setHiddenField('page', 'struct'); 40*ae697e1fSAndreas Gohr $form->setHiddenField('table', $this->schema->getTable()); 41*ae697e1fSAndreas Gohr $form->setHiddenField('schema[id]', $this->schema->getId()); 42*ae697e1fSAndreas Gohr 43*ae697e1fSAndreas Gohr $form->addHTML('<table class="inline">'); 44*ae697e1fSAndreas Gohr $form->addHTML('<tr><th>Sort</th><th>Label</th><th>Multi-Input?</th><th>Configuration</th><th>Type</th></tr>'); // FIXME localize 45*ae697e1fSAndreas Gohr 46*ae697e1fSAndreas Gohr foreach($this->schema->getColumns() as $key => $obj) { 47*ae697e1fSAndreas Gohr $form->addHTML($this->adminColumn($key, $obj)); 48*ae697e1fSAndreas Gohr } 49*ae697e1fSAndreas Gohr 50*ae697e1fSAndreas Gohr // FIXME new one needs to be added dynamically, this is just for testing 51*ae697e1fSAndreas Gohr $form->addHTML($this->adminColumn('new1', new Column($this->schema->getMaxsort()+10, new Text()), 'new')); 52*ae697e1fSAndreas Gohr 53*ae697e1fSAndreas Gohr $form->addHTML('</table>'); 54*ae697e1fSAndreas Gohr $form->addButton('save', 'Save')->attr('type','submit'); 55*ae697e1fSAndreas Gohr return $form->toHTML(); 56*ae697e1fSAndreas Gohr } 57*ae697e1fSAndreas Gohr 58*ae697e1fSAndreas Gohr /** 59*ae697e1fSAndreas Gohr * Returns the HTML to edit a single column definition of the schema 60*ae697e1fSAndreas Gohr * 61*ae697e1fSAndreas Gohr * @param string $column_id 62*ae697e1fSAndreas Gohr * @param Column $col 63*ae697e1fSAndreas Gohr * @param string $key The key to use in the form 64*ae697e1fSAndreas Gohr * @return string 65*ae697e1fSAndreas Gohr * @todo this should probably be reused for adding new columns via AJAX later? 66*ae697e1fSAndreas Gohr */ 67*ae697e1fSAndreas Gohr protected function adminColumn($column_id, Column $col, $key='cols') { 68*ae697e1fSAndreas Gohr $base = 'schema['.$key.'][' . $column_id . ']'; // base name for all fields 69*ae697e1fSAndreas Gohr 70*ae697e1fSAndreas Gohr $html = '<tr>'; 71*ae697e1fSAndreas Gohr 72*ae697e1fSAndreas Gohr $html .= '<td>'; 73*ae697e1fSAndreas Gohr $html .= '<input type="text" name="' . $base . '[sort]" value="' . hsc($col->getSort()) . '" size="3">'; 74*ae697e1fSAndreas Gohr $html .= '</td>'; 75*ae697e1fSAndreas Gohr 76*ae697e1fSAndreas Gohr $html .= '<td>'; 77*ae697e1fSAndreas Gohr $html .= '<input type="text" name="' . $base . '[label]" value="' . hsc($col->getType()->getLabel()) . '">'; 78*ae697e1fSAndreas Gohr $html .= '</td>'; 79*ae697e1fSAndreas Gohr 80*ae697e1fSAndreas Gohr $html .= '<td>'; 81*ae697e1fSAndreas Gohr $checked = $col->getType()->isMulti() ? 'checked="checked"' : ''; 82*ae697e1fSAndreas Gohr $html .= '<input type="checkbox" name="' . $base . '[ismulti]" value="1" ' . $checked . '>'; 83*ae697e1fSAndreas Gohr $html .= '</td>'; 84*ae697e1fSAndreas Gohr 85*ae697e1fSAndreas Gohr $html .= '<td>'; 86*ae697e1fSAndreas Gohr $config = json_encode($col->getType()->getConfig(), JSON_PRETTY_PRINT); 87*ae697e1fSAndreas Gohr $html .= '<textarea name="' . $base . '[config]" cols="45" rows="10">' . hsc($config) . '</textarea>'; 88*ae697e1fSAndreas Gohr $html .= '</td>'; 89*ae697e1fSAndreas Gohr 90*ae697e1fSAndreas Gohr $types = Column::allTypes(); 91*ae697e1fSAndreas Gohr $html .= '<td>'; 92*ae697e1fSAndreas Gohr $html .= '<select name="' . $base . '[class]">'; 93*ae697e1fSAndreas Gohr foreach($types as $type) { 94*ae697e1fSAndreas Gohr $selected = ($col->getType()->getClass() == $type) ? 'selected="selected"' : ''; 95*ae697e1fSAndreas Gohr $html .= '<option value="' . hsc($type) . '" ' . $selected . '>' . hsc($type) . '</option>'; 96*ae697e1fSAndreas Gohr } 97*ae697e1fSAndreas Gohr $html .= '</select>'; 98*ae697e1fSAndreas Gohr $html .= '</td>'; 99*ae697e1fSAndreas Gohr 100*ae697e1fSAndreas Gohr $html .= '</tr>'; 101*ae697e1fSAndreas Gohr 102*ae697e1fSAndreas Gohr return $html; 103*ae697e1fSAndreas Gohr } 104*ae697e1fSAndreas Gohr 105*ae697e1fSAndreas Gohr} 106