1ae697e1fSAndreas Gohr<?php 2ae697e1fSAndreas Gohr 3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\meta; 4ae697e1fSAndreas Gohr 57234bfb1Ssplitbrainuse dokuwiki\Extension\Plugin; 6ae697e1fSAndreas Gohruse dokuwiki\Form\Form; 7ba766201SAndreas Gohruse dokuwiki\plugin\struct\types\Text; 8ae697e1fSAndreas Gohr 9ae697e1fSAndreas Gohr/** 10ae697e1fSAndreas Gohr * Class SchemaEditor 11ae697e1fSAndreas Gohr * 12ae697e1fSAndreas Gohr * Provides the editing interface for a given Schema as used in the admin backend. The actual modifying of the 13ae697e1fSAndreas Gohr * schema happens in the SchemaBuilder class. 14ae697e1fSAndreas Gohr * 15ba766201SAndreas Gohr * @package dokuwiki\plugin\struct\meta 16ae697e1fSAndreas Gohr */ 17d6d97f60SAnna Dabrowskaclass SchemaEditor 18d6d97f60SAnna Dabrowska{ 19ae697e1fSAndreas Gohr /** @var Schema the schema that is edited */ 20ae697e1fSAndreas Gohr protected $schema; 21ae697e1fSAndreas Gohr 227234bfb1Ssplitbrain /** @var Plugin */ 236af24d3eSAndreas Gohr protected $hlp; 246af24d3eSAndreas Gohr 25ae697e1fSAndreas Gohr /** 26ae697e1fSAndreas Gohr * SchemaEditor constructor. 27ae697e1fSAndreas Gohr * @param Schema $schema 28ae697e1fSAndreas Gohr */ 29d6d97f60SAnna Dabrowska public function __construct(Schema $schema) 30d6d97f60SAnna Dabrowska { 31ae697e1fSAndreas Gohr $this->schema = $schema; 326af24d3eSAndreas Gohr $this->hlp = plugin_load('helper', 'struct_config'); 33ae697e1fSAndreas Gohr } 34ae697e1fSAndreas Gohr 35ae697e1fSAndreas Gohr /** 36ae697e1fSAndreas Gohr * Returns the Admin Form to edit the schema 37ae697e1fSAndreas Gohr * 38ae697e1fSAndreas Gohr * This data is processed by the SchemaBuilder class 39ae697e1fSAndreas Gohr * 40ae697e1fSAndreas Gohr * @return string the HTML for the editor form 41ae697e1fSAndreas Gohr * @see SchemaBuilder 42ae697e1fSAndreas Gohr */ 43d6d97f60SAnna Dabrowska public function getEditor() 44d6d97f60SAnna Dabrowska { 457234bfb1Ssplitbrain $form = new Form(['method' => 'POST', 'id' => 'plugin__struct_editor']); 46ae697e1fSAndreas Gohr $form->setHiddenField('do', 'admin'); 47dbffe06eSAndreas Gohr $form->setHiddenField('page', 'struct_schemas'); 48ae697e1fSAndreas Gohr $form->setHiddenField('table', $this->schema->getTable()); 49ae697e1fSAndreas Gohr $form->setHiddenField('schema[id]', $this->schema->getId()); 50ae697e1fSAndreas Gohr 51ae697e1fSAndreas Gohr $form->addHTML('<table class="inline">'); 526af24d3eSAndreas Gohr $form->addHTML("<tr> 536af24d3eSAndreas Gohr <th>{$this->hlp->getLang('editor_sort')}</th> 546af24d3eSAndreas Gohr <th>{$this->hlp->getLang('editor_label')}</th> 556af24d3eSAndreas Gohr <th>{$this->hlp->getLang('editor_multi')}</th> 566af24d3eSAndreas Gohr <th>{$this->hlp->getLang('editor_conf')}</th> 576af24d3eSAndreas Gohr <th>{$this->hlp->getLang('editor_type')}</th> 5826147f8cSAndreas Gohr <th>{$this->hlp->getLang('editor_enabled')}</th> 596af24d3eSAndreas Gohr </tr>"); 606af24d3eSAndreas Gohr 61ae697e1fSAndreas Gohr 627234bfb1Ssplitbrain foreach ($this->schema->getColumns() as $col) { 637fb68f9eSAndreas Gohr $form->addHTML($this->adminColumn($col->getColref(), $col)); 64ae697e1fSAndreas Gohr } 65ae697e1fSAndreas Gohr 66*dc2a4c37SAndreas Gohr // Add one new field at the end 67ae697e1fSAndreas Gohr $form->addHTML($this->adminColumn('new1', new Column($this->schema->getMaxsort() + 10, new Text()), 'new')); 68ae697e1fSAndreas Gohr 69ae697e1fSAndreas Gohr $form->addHTML('</table>'); 70e2c90eebSAndreas Gohr 71e2c90eebSAndreas Gohr $form->addFieldsetOpen(); 727234bfb1Ssplitbrain 73127d6bacSMichael Große $config = json_encode($this->schema->getConfig(), JSON_PRETTY_PRINT); 7417a3a578SAndreas Gohr $form->addHTML( 7517a3a578SAndreas Gohr '<textarea name="schema[config]" id="schemaConfig" cols="45" rows="10" class="config">' . 7617a3a578SAndreas Gohr hsc($config) . 7717a3a578SAndreas Gohr '</textarea>' 7817a3a578SAndreas Gohr ); 79e2c90eebSAndreas Gohr $form->addFieldsetClose(); 80e2c90eebSAndreas Gohr 81e2c90eebSAndreas Gohr 82ae697e1fSAndreas Gohr $form->addButton('save', 'Save')->attr('type', 'submit'); 83a57a64a5SAndreas Gohr return $form->toHTML() . $this->initJSONEditor(); 84a57a64a5SAndreas Gohr } 85a57a64a5SAndreas Gohr 86a57a64a5SAndreas Gohr /** 87a57a64a5SAndreas Gohr * Gives the code to attach the JSON editor to the config field 88a57a64a5SAndreas Gohr * 89a57a64a5SAndreas Gohr * We do not use the "normal" way, because this is rarely used code and there's no need to always load it. 90a57a64a5SAndreas Gohr * @return string 91a57a64a5SAndreas Gohr */ 92d6d97f60SAnna Dabrowska protected function initJSONEditor() 93d6d97f60SAnna Dabrowska { 94a57a64a5SAndreas Gohr $html = ''; 9517a3a578SAndreas Gohr $html .= '<link href="' . DOKU_BASE . 9617a3a578SAndreas Gohr 'lib/plugins/struct/jsoneditor/jsoneditor.min.css" rel="stylesheet" type="text/css">'; 9717a3a578SAndreas Gohr $html .= '<link href="' . DOKU_BASE . 9817a3a578SAndreas Gohr 'lib/plugins/struct/jsoneditor/setup.css" rel="stylesheet" type="text/css">'; 9917a3a578SAndreas Gohr $html .= '<script src="' . DOKU_BASE . 10017a3a578SAndreas Gohr 'lib/plugins/struct/jsoneditor/jsoneditor-minimalist.min.js" defer="defer"></script>'; 10117a3a578SAndreas Gohr $html .= '<script src="' . DOKU_BASE . 10217a3a578SAndreas Gohr 'lib/plugins/struct/jsoneditor/setup.js" defer="defer"></script>'; 103a57a64a5SAndreas Gohr return $html; 104ae697e1fSAndreas Gohr } 105ae697e1fSAndreas Gohr 106ae697e1fSAndreas Gohr /** 107ae697e1fSAndreas Gohr * Returns the HTML to edit a single column definition of the schema 108ae697e1fSAndreas Gohr * 109ae697e1fSAndreas Gohr * @param string $column_id 110ae697e1fSAndreas Gohr * @param Column $col 111ae697e1fSAndreas Gohr * @param string $key The key to use in the form 112ae697e1fSAndreas Gohr * @return string 113ae697e1fSAndreas Gohr * @todo this should probably be reused for adding new columns via AJAX later? 114ae697e1fSAndreas Gohr */ 115d6d97f60SAnna Dabrowska protected function adminColumn($column_id, Column $col, $key = 'cols') 116d6d97f60SAnna Dabrowska { 117ae697e1fSAndreas Gohr $base = 'schema[' . $key . '][' . $column_id . ']'; // base name for all fields 118ae697e1fSAndreas Gohr 119fd81b928SAndreas Gohr $class = $col->isEnabled() ? '' : 'disabled'; 120*dc2a4c37SAndreas Gohr if($key === 'new') $class .= ' new'; 121ae697e1fSAndreas Gohr 122fd81b928SAndreas Gohr $html = "<tr class=\"$class\">"; 123fd81b928SAndreas Gohr 124fd81b928SAndreas Gohr $html .= '<td class="sort">'; 125ae697e1fSAndreas Gohr $html .= '<input type="text" name="' . $base . '[sort]" value="' . hsc($col->getSort()) . '" size="3">'; 126ae697e1fSAndreas Gohr $html .= '</td>'; 127ae697e1fSAndreas Gohr 128fd81b928SAndreas Gohr $html .= '<td class="label">'; 129ae697e1fSAndreas Gohr $html .= '<input type="text" name="' . $base . '[label]" value="' . hsc($col->getType()->getLabel()) . '">'; 130ae697e1fSAndreas Gohr $html .= '</td>'; 131ae697e1fSAndreas Gohr 132fd81b928SAndreas Gohr $html .= '<td class="ismulti">'; 133ae697e1fSAndreas Gohr $checked = $col->getType()->isMulti() ? 'checked="checked"' : ''; 134ae697e1fSAndreas Gohr $html .= '<input type="checkbox" name="' . $base . '[ismulti]" value="1" ' . $checked . '>'; 135ae697e1fSAndreas Gohr $html .= '</td>'; 136ae697e1fSAndreas Gohr 137fd81b928SAndreas Gohr $html .= '<td class="config">'; 138ae697e1fSAndreas Gohr $config = json_encode($col->getType()->getConfig(), JSON_PRETTY_PRINT); 13917a3a578SAndreas Gohr $html .= '<textarea name="' . $base . '[config]" cols="45" rows="10" class="config">' . 14017a3a578SAndreas Gohr hsc($config) . 14117a3a578SAndreas Gohr '</textarea>'; 142ae697e1fSAndreas Gohr $html .= '</td>'; 143ae697e1fSAndreas Gohr 144636c8abaSAndreas Gohr $types = array_keys(Column::allTypes()); 145fd81b928SAndreas Gohr $html .= '<td class="class">'; 146ae697e1fSAndreas Gohr $html .= '<select name="' . $base . '[class]">'; 147ae697e1fSAndreas Gohr foreach ($types as $type) { 148ae697e1fSAndreas Gohr $selected = ($col->getType()->getClass() == $type) ? 'selected="selected"' : ''; 149ae697e1fSAndreas Gohr $html .= '<option value="' . hsc($type) . '" ' . $selected . '>' . hsc($type) . '</option>'; 150ae697e1fSAndreas Gohr } 151ae697e1fSAndreas Gohr $html .= '</select>'; 152ae697e1fSAndreas Gohr $html .= '</td>'; 153ae697e1fSAndreas Gohr 15426147f8cSAndreas Gohr 155fd81b928SAndreas Gohr $html .= '<td class="isenabled">'; 15626147f8cSAndreas Gohr $checked = $col->isEnabled() ? 'checked="checked"' : ''; 15726147f8cSAndreas Gohr $html .= '<input type="checkbox" name="' . $base . '[isenabled]" value="1" ' . $checked . '>'; 15826147f8cSAndreas Gohr $html .= '</td>'; 15926147f8cSAndreas Gohr 160ae697e1fSAndreas Gohr $html .= '</tr>'; 161ae697e1fSAndreas Gohr 162ae697e1fSAndreas Gohr return $html; 163ae697e1fSAndreas Gohr } 164ae697e1fSAndreas Gohr} 165