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