1ae697e1fSAndreas Gohr<?php 2ae697e1fSAndreas Gohr 3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\meta; 4ae697e1fSAndreas Gohr 5ae697e1fSAndreas Gohruse dokuwiki\Form\Form; 6ba766201SAndreas Gohruse dokuwiki\plugin\struct\types\Text; 7ae697e1fSAndreas Gohr 8ae697e1fSAndreas Gohr/** 9ae697e1fSAndreas Gohr * Class SchemaEditor 10ae697e1fSAndreas Gohr * 11ae697e1fSAndreas Gohr * Provides the editing interface for a given Schema as used in the admin backend. The actual modifying of the 12ae697e1fSAndreas Gohr * schema happens in the SchemaBuilder class. 13ae697e1fSAndreas Gohr * 14ba766201SAndreas Gohr * @package dokuwiki\plugin\struct\meta 15ae697e1fSAndreas Gohr */ 16d6d97f60SAnna Dabrowskaclass SchemaEditor 17d6d97f60SAnna Dabrowska{ 18ae697e1fSAndreas Gohr /** @var Schema the schema that is edited */ 19ae697e1fSAndreas Gohr protected $schema; 20ae697e1fSAndreas Gohr 216af24d3eSAndreas Gohr /** @var \DokuWiki_Plugin */ 226af24d3eSAndreas Gohr protected $hlp; 236af24d3eSAndreas Gohr 24ae697e1fSAndreas Gohr /** 25ae697e1fSAndreas Gohr * SchemaEditor constructor. 26ae697e1fSAndreas Gohr * @param Schema $schema 27ae697e1fSAndreas Gohr */ 28d6d97f60SAnna Dabrowska public function __construct(Schema $schema) 29d6d97f60SAnna Dabrowska { 30ae697e1fSAndreas Gohr $this->schema = $schema; 316af24d3eSAndreas Gohr $this->hlp = plugin_load('helper', 'struct_config'); 32ae697e1fSAndreas Gohr } 33ae697e1fSAndreas Gohr 34ae697e1fSAndreas Gohr /** 35ae697e1fSAndreas Gohr * Returns the Admin Form to edit the schema 36ae697e1fSAndreas Gohr * 37ae697e1fSAndreas Gohr * This data is processed by the SchemaBuilder class 38ae697e1fSAndreas Gohr * 39ae697e1fSAndreas Gohr * @return string the HTML for the editor form 40ae697e1fSAndreas Gohr * @see SchemaBuilder 41ae697e1fSAndreas Gohr */ 42d6d97f60SAnna Dabrowska public function getEditor() 43d6d97f60SAnna Dabrowska { 445a1eab78SAndreas Gohr $form = new Form(array('method' => 'POST', 'id' => 'plugin__struct_editor')); 45ae697e1fSAndreas Gohr $form->setHiddenField('do', 'admin'); 46dbffe06eSAndreas Gohr $form->setHiddenField('page', 'struct_schemas'); 47ae697e1fSAndreas Gohr $form->setHiddenField('table', $this->schema->getTable()); 48ae697e1fSAndreas Gohr $form->setHiddenField('schema[id]', $this->schema->getId()); 49ae697e1fSAndreas Gohr 50ae697e1fSAndreas Gohr $form->addHTML('<table class="inline">'); 516af24d3eSAndreas Gohr $form->addHTML("<tr> 526af24d3eSAndreas Gohr <th>{$this->hlp->getLang('editor_sort')}</th> 536af24d3eSAndreas Gohr <th>{$this->hlp->getLang('editor_label')}</th> 546af24d3eSAndreas Gohr <th>{$this->hlp->getLang('editor_multi')}</th> 556af24d3eSAndreas Gohr <th>{$this->hlp->getLang('editor_conf')}</th> 566af24d3eSAndreas Gohr <th>{$this->hlp->getLang('editor_type')}</th> 5726147f8cSAndreas Gohr <th>{$this->hlp->getLang('editor_enabled')}</th> 586af24d3eSAndreas Gohr </tr>"); 596af24d3eSAndreas Gohr 60ae697e1fSAndreas Gohr 617fb68f9eSAndreas Gohr foreach ($this->schema->getColumns() as $key => $col) { 627fb68f9eSAndreas Gohr $form->addHTML($this->adminColumn($col->getColref(), $col)); 63ae697e1fSAndreas Gohr } 64ae697e1fSAndreas Gohr 65ae697e1fSAndreas Gohr // FIXME new one needs to be added dynamically, this is just for testing 66ae697e1fSAndreas Gohr $form->addHTML($this->adminColumn('new1', new Column($this->schema->getMaxsort() + 10, new Text()), 'new')); 67ae697e1fSAndreas Gohr 68ae697e1fSAndreas Gohr $form->addHTML('</table>'); 69e2c90eebSAndreas Gohr 70e2c90eebSAndreas Gohr $form->addFieldsetOpen(); 71127d6bacSMichael Große $config = json_encode($this->schema->getConfig(), JSON_PRETTY_PRINT); 72*17a3a578SAndreas Gohr $form->addHTML( 73*17a3a578SAndreas Gohr '<textarea name="schema[config]" id="schemaConfig" cols="45" rows="10" class="config">' . 74*17a3a578SAndreas Gohr hsc($config) . 75*17a3a578SAndreas Gohr '</textarea>' 76*17a3a578SAndreas Gohr ); 77e2c90eebSAndreas Gohr $form->addFieldsetClose(); 78e2c90eebSAndreas Gohr 79e2c90eebSAndreas Gohr 80ae697e1fSAndreas Gohr $form->addButton('save', 'Save')->attr('type', 'submit'); 81a57a64a5SAndreas Gohr return $form->toHTML() . $this->initJSONEditor(); 82a57a64a5SAndreas Gohr } 83a57a64a5SAndreas Gohr 84a57a64a5SAndreas Gohr /** 85a57a64a5SAndreas Gohr * Gives the code to attach the JSON editor to the config field 86a57a64a5SAndreas Gohr * 87a57a64a5SAndreas Gohr * We do not use the "normal" way, because this is rarely used code and there's no need to always load it. 88a57a64a5SAndreas Gohr * @return string 89a57a64a5SAndreas Gohr */ 90d6d97f60SAnna Dabrowska protected function initJSONEditor() 91d6d97f60SAnna Dabrowska { 92a57a64a5SAndreas Gohr $html = ''; 93*17a3a578SAndreas Gohr $html .= '<link href="' . DOKU_BASE . 94*17a3a578SAndreas Gohr 'lib/plugins/struct/jsoneditor/jsoneditor.min.css" rel="stylesheet" type="text/css">'; 95*17a3a578SAndreas Gohr $html .= '<link href="' . DOKU_BASE . 96*17a3a578SAndreas Gohr 'lib/plugins/struct/jsoneditor/setup.css" rel="stylesheet" type="text/css">'; 97*17a3a578SAndreas Gohr $html .= '<script src="' . DOKU_BASE . 98*17a3a578SAndreas Gohr 'lib/plugins/struct/jsoneditor/jsoneditor-minimalist.min.js" defer="defer"></script>'; 99*17a3a578SAndreas Gohr $html .= '<script src="' . DOKU_BASE . 100*17a3a578SAndreas Gohr 'lib/plugins/struct/jsoneditor/setup.js" defer="defer"></script>'; 101a57a64a5SAndreas Gohr return $html; 102ae697e1fSAndreas Gohr } 103ae697e1fSAndreas Gohr 104ae697e1fSAndreas Gohr /** 105ae697e1fSAndreas Gohr * Returns the HTML to edit a single column definition of the schema 106ae697e1fSAndreas Gohr * 107ae697e1fSAndreas Gohr * @param string $column_id 108ae697e1fSAndreas Gohr * @param Column $col 109ae697e1fSAndreas Gohr * @param string $key The key to use in the form 110ae697e1fSAndreas Gohr * @return string 111ae697e1fSAndreas Gohr * @todo this should probably be reused for adding new columns via AJAX later? 112ae697e1fSAndreas Gohr */ 113d6d97f60SAnna Dabrowska protected function adminColumn($column_id, Column $col, $key = 'cols') 114d6d97f60SAnna Dabrowska { 115ae697e1fSAndreas Gohr $base = 'schema[' . $key . '][' . $column_id . ']'; // base name for all fields 116ae697e1fSAndreas Gohr 117fd81b928SAndreas Gohr $class = $col->isEnabled() ? '' : 'disabled'; 118ae697e1fSAndreas Gohr 119fd81b928SAndreas Gohr $html = "<tr class=\"$class\">"; 120fd81b928SAndreas Gohr 121fd81b928SAndreas Gohr $html .= '<td class="sort">'; 122ae697e1fSAndreas Gohr $html .= '<input type="text" name="' . $base . '[sort]" value="' . hsc($col->getSort()) . '" size="3">'; 123ae697e1fSAndreas Gohr $html .= '</td>'; 124ae697e1fSAndreas Gohr 125fd81b928SAndreas Gohr $html .= '<td class="label">'; 126ae697e1fSAndreas Gohr $html .= '<input type="text" name="' . $base . '[label]" value="' . hsc($col->getType()->getLabel()) . '">'; 127ae697e1fSAndreas Gohr $html .= '</td>'; 128ae697e1fSAndreas Gohr 129fd81b928SAndreas Gohr $html .= '<td class="ismulti">'; 130ae697e1fSAndreas Gohr $checked = $col->getType()->isMulti() ? 'checked="checked"' : ''; 131ae697e1fSAndreas Gohr $html .= '<input type="checkbox" name="' . $base . '[ismulti]" value="1" ' . $checked . '>'; 132ae697e1fSAndreas Gohr $html .= '</td>'; 133ae697e1fSAndreas Gohr 134fd81b928SAndreas Gohr $html .= '<td class="config">'; 135ae697e1fSAndreas Gohr $config = json_encode($col->getType()->getConfig(), JSON_PRETTY_PRINT); 136*17a3a578SAndreas Gohr $html .= '<textarea name="' . $base . '[config]" cols="45" rows="10" class="config">' . 137*17a3a578SAndreas Gohr hsc($config) . 138*17a3a578SAndreas Gohr '</textarea>'; 139ae697e1fSAndreas Gohr $html .= '</td>'; 140ae697e1fSAndreas Gohr 141636c8abaSAndreas Gohr $types = array_keys(Column::allTypes()); 142fd81b928SAndreas Gohr $html .= '<td class="class">'; 143ae697e1fSAndreas Gohr $html .= '<select name="' . $base . '[class]">'; 144ae697e1fSAndreas Gohr foreach ($types as $type) { 145ae697e1fSAndreas Gohr $selected = ($col->getType()->getClass() == $type) ? 'selected="selected"' : ''; 146ae697e1fSAndreas Gohr $html .= '<option value="' . hsc($type) . '" ' . $selected . '>' . hsc($type) . '</option>'; 147ae697e1fSAndreas Gohr } 148ae697e1fSAndreas Gohr $html .= '</select>'; 149ae697e1fSAndreas Gohr $html .= '</td>'; 150ae697e1fSAndreas Gohr 15126147f8cSAndreas Gohr 152fd81b928SAndreas Gohr $html .= '<td class="isenabled">'; 15326147f8cSAndreas Gohr $checked = $col->isEnabled() ? 'checked="checked"' : ''; 15426147f8cSAndreas Gohr $html .= '<input type="checkbox" name="' . $base . '[isenabled]" value="1" ' . $checked . '>'; 15526147f8cSAndreas Gohr $html .= '</td>'; 15626147f8cSAndreas Gohr 157ae697e1fSAndreas Gohr $html .= '</tr>'; 158ae697e1fSAndreas Gohr 159ae697e1fSAndreas Gohr return $html; 160ae697e1fSAndreas Gohr } 161ae697e1fSAndreas Gohr} 162