1ae697e1fSAndreas Gohr<?php 2ae697e1fSAndreas Gohr 3*ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\meta; 4ae697e1fSAndreas Gohr 5ae697e1fSAndreas Gohruse dokuwiki\Form\Form; 6*ba766201SAndreas Gohruse dokuwiki\plugin\struct\types\Text; 7ae697e1fSAndreas Gohr 845560cc7SAndreas Gohrif(!defined('JSON_PRETTY_PRINT')) define('JSON_PRETTY_PRINT', 0); // PHP 5.3 compatibility 945560cc7SAndreas Gohr 10ae697e1fSAndreas Gohr/** 11ae697e1fSAndreas Gohr * Class SchemaEditor 12ae697e1fSAndreas Gohr * 13ae697e1fSAndreas Gohr * Provides the editing interface for a given Schema as used in the admin backend. The actual modifying of the 14ae697e1fSAndreas Gohr * schema happens in the SchemaBuilder class. 15ae697e1fSAndreas Gohr * 16*ba766201SAndreas Gohr * @package dokuwiki\plugin\struct\meta 17ae697e1fSAndreas Gohr */ 18ae697e1fSAndreas Gohrclass SchemaEditor { 19ae697e1fSAndreas Gohr /** @var Schema the schema that is edited */ 20ae697e1fSAndreas Gohr protected $schema; 21ae697e1fSAndreas Gohr 226af24d3eSAndreas Gohr /** @var \DokuWiki_Plugin */ 236af24d3eSAndreas Gohr protected $hlp; 246af24d3eSAndreas Gohr 25ae697e1fSAndreas Gohr /** 26ae697e1fSAndreas Gohr * SchemaEditor constructor. 27ae697e1fSAndreas Gohr * @param Schema $schema 28ae697e1fSAndreas Gohr */ 29ae697e1fSAndreas Gohr public function __construct(Schema $schema) { 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 */ 42ae697e1fSAndreas Gohr public function getEditor() { 435a1eab78SAndreas Gohr $form = new Form(array('method' => 'POST', 'id'=>'plugin__struct_editor')); 44ae697e1fSAndreas Gohr $form->setHiddenField('do', 'admin'); 45dbffe06eSAndreas Gohr $form->setHiddenField('page', 'struct_schemas'); 46ae697e1fSAndreas Gohr $form->setHiddenField('table', $this->schema->getTable()); 47ae697e1fSAndreas Gohr $form->setHiddenField('schema[id]', $this->schema->getId()); 48ae697e1fSAndreas Gohr 49ae697e1fSAndreas Gohr $form->addHTML('<table class="inline">'); 506af24d3eSAndreas Gohr $form->addHTML("<tr> 516af24d3eSAndreas Gohr <th>{$this->hlp->getLang('editor_sort')}</th> 526af24d3eSAndreas Gohr <th>{$this->hlp->getLang('editor_label')}</th> 536af24d3eSAndreas Gohr <th>{$this->hlp->getLang('editor_multi')}</th> 546af24d3eSAndreas Gohr <th>{$this->hlp->getLang('editor_conf')}</th> 556af24d3eSAndreas Gohr <th>{$this->hlp->getLang('editor_type')}</th> 5626147f8cSAndreas Gohr <th>{$this->hlp->getLang('editor_enabled')}</th> 576af24d3eSAndreas Gohr </tr>"); 586af24d3eSAndreas Gohr 59ae697e1fSAndreas Gohr 607fb68f9eSAndreas Gohr foreach($this->schema->getColumns() as $key => $col) { 617fb68f9eSAndreas Gohr $form->addHTML($this->adminColumn($col->getColref(), $col)); 62ae697e1fSAndreas Gohr } 63ae697e1fSAndreas Gohr 64ae697e1fSAndreas Gohr // FIXME new one needs to be added dynamically, this is just for testing 65ae697e1fSAndreas Gohr $form->addHTML($this->adminColumn('new1', new Column($this->schema->getMaxsort()+10, new Text()), 'new')); 66ae697e1fSAndreas Gohr 67ae697e1fSAndreas Gohr $form->addHTML('</table>'); 68ae697e1fSAndreas Gohr $form->addButton('save', 'Save')->attr('type','submit'); 69a57a64a5SAndreas Gohr return $form->toHTML() . $this->initJSONEditor(); 70a57a64a5SAndreas Gohr } 71a57a64a5SAndreas Gohr 72a57a64a5SAndreas Gohr /** 73a57a64a5SAndreas Gohr * Gives the code to attach the JSON editor to the config field 74a57a64a5SAndreas Gohr * 75a57a64a5SAndreas Gohr * We do not use the "normal" way, because this is rarely used code and there's no need to always load it. 76a57a64a5SAndreas Gohr * @return string 77a57a64a5SAndreas Gohr */ 78a57a64a5SAndreas Gohr protected function initJSONEditor() { 79a57a64a5SAndreas Gohr $html = ''; 80a57a64a5SAndreas Gohr $html .= '<link href="'.DOKU_BASE.'lib/plugins/struct/jsoneditor/jsoneditor.min.css" rel="stylesheet" type="text/css">'; 81a57a64a5SAndreas Gohr $html .= '<link href="'.DOKU_BASE.'lib/plugins/struct/jsoneditor/setup.css" rel="stylesheet" type="text/css">'; 82a57a64a5SAndreas Gohr $html .= '<script src="'.DOKU_BASE.'lib/plugins/struct/jsoneditor/jsoneditor-minimalist.min.js"></script>'; 83a57a64a5SAndreas Gohr $html .= '<script src="'.DOKU_BASE.'lib/plugins/struct/jsoneditor/setup.js"></script>'; 84a57a64a5SAndreas Gohr return $html; 85ae697e1fSAndreas Gohr } 86ae697e1fSAndreas Gohr 87ae697e1fSAndreas Gohr /** 88ae697e1fSAndreas Gohr * Returns the HTML to edit a single column definition of the schema 89ae697e1fSAndreas Gohr * 90ae697e1fSAndreas Gohr * @param string $column_id 91ae697e1fSAndreas Gohr * @param Column $col 92ae697e1fSAndreas Gohr * @param string $key The key to use in the form 93ae697e1fSAndreas Gohr * @return string 94ae697e1fSAndreas Gohr * @todo this should probably be reused for adding new columns via AJAX later? 95ae697e1fSAndreas Gohr */ 96ae697e1fSAndreas Gohr protected function adminColumn($column_id, Column $col, $key='cols') { 97ae697e1fSAndreas Gohr $base = 'schema['.$key.'][' . $column_id . ']'; // base name for all fields 98ae697e1fSAndreas Gohr 99fd81b928SAndreas Gohr $class = $col->isEnabled() ? '' : 'disabled'; 100ae697e1fSAndreas Gohr 101fd81b928SAndreas Gohr $html = "<tr class=\"$class\">"; 102fd81b928SAndreas Gohr 103fd81b928SAndreas Gohr $html .= '<td class="sort">'; 104ae697e1fSAndreas Gohr $html .= '<input type="text" name="' . $base . '[sort]" value="' . hsc($col->getSort()) . '" size="3">'; 105ae697e1fSAndreas Gohr $html .= '</td>'; 106ae697e1fSAndreas Gohr 107fd81b928SAndreas Gohr $html .= '<td class="label">'; 108ae697e1fSAndreas Gohr $html .= '<input type="text" name="' . $base . '[label]" value="' . hsc($col->getType()->getLabel()) . '">'; 109ae697e1fSAndreas Gohr $html .= '</td>'; 110ae697e1fSAndreas Gohr 111fd81b928SAndreas Gohr $html .= '<td class="ismulti">'; 112ae697e1fSAndreas Gohr $checked = $col->getType()->isMulti() ? 'checked="checked"' : ''; 113ae697e1fSAndreas Gohr $html .= '<input type="checkbox" name="' . $base . '[ismulti]" value="1" ' . $checked . '>'; 114ae697e1fSAndreas Gohr $html .= '</td>'; 115ae697e1fSAndreas Gohr 116fd81b928SAndreas Gohr $html .= '<td class="config">'; 117ae697e1fSAndreas Gohr $config = json_encode($col->getType()->getConfig(), JSON_PRETTY_PRINT); 118a57a64a5SAndreas Gohr $html .= '<textarea name="' . $base . '[config]" cols="45" rows="10" class="config">' . hsc($config) . '</textarea>'; 119ae697e1fSAndreas Gohr $html .= '</td>'; 120ae697e1fSAndreas Gohr 121ae697e1fSAndreas Gohr $types = Column::allTypes(); 122fd81b928SAndreas Gohr $html .= '<td class="class">'; 123ae697e1fSAndreas Gohr $html .= '<select name="' . $base . '[class]">'; 124ae697e1fSAndreas Gohr foreach($types as $type) { 125ae697e1fSAndreas Gohr $selected = ($col->getType()->getClass() == $type) ? 'selected="selected"' : ''; 126ae697e1fSAndreas Gohr $html .= '<option value="' . hsc($type) . '" ' . $selected . '>' . hsc($type) . '</option>'; 127ae697e1fSAndreas Gohr } 128ae697e1fSAndreas Gohr $html .= '</select>'; 129ae697e1fSAndreas Gohr $html .= '</td>'; 130ae697e1fSAndreas Gohr 13126147f8cSAndreas Gohr 132fd81b928SAndreas Gohr $html .= '<td class="isenabled">'; 13326147f8cSAndreas Gohr $checked = $col->isEnabled() ? 'checked="checked"' : ''; 13426147f8cSAndreas Gohr $html .= '<input type="checkbox" name="' . $base . '[isenabled]" value="1" ' . $checked . '>'; 13526147f8cSAndreas Gohr $html .= '</td>'; 13626147f8cSAndreas Gohr 137ae697e1fSAndreas Gohr $html .= '</tr>'; 138ae697e1fSAndreas Gohr 139ae697e1fSAndreas Gohr return $html; 140ae697e1fSAndreas Gohr } 141ae697e1fSAndreas Gohr 142ae697e1fSAndreas Gohr} 143