1<?php 2 3namespace dokuwiki\Ui; 4 5use dokuwiki\Extension\Event; 6use dokuwiki\Form\Form; 7 8/** 9 * DokuWiki Page Editor 10 * 11 * @package dokuwiki\Ui 12 */ 13class Editor extends Ui 14{ 15 /** 16 * Display the Edit Window 17 * preprocess edit form data 18 * 19 * @author Andreas Gohr <andi@splitbrain.org> 20 * 21 * @triggers HTML_EDIT_FORMSELECTION 22 * @return void 23 */ 24 public function show() 25 { 26 global $INPUT; 27 global $ID; 28 global $REV; 29 global $DATE; 30 global $PRE; 31 global $SUF; 32 global $INFO; 33 global $SUM; 34 global $lang; 35 global $conf; 36 global $TEXT; 37 38 global $license; 39 40 if ($INPUT->has('changecheck')) { 41 $check = $INPUT->str('changecheck'); 42 } elseif (!$INFO['exists']) { 43 // $TEXT has been loaded from page template 44 $check = md5(''); 45 } else { 46 $check = md5($TEXT); 47 } 48 $mod = (md5($TEXT) !== $check); 49 50 $wr = $INFO['writable'] && !$INFO['locked']; 51 52 // intro locale text (edit, rditrev, or read) 53 if ($wr) { 54 $intro = ($REV) ? 'editrev' : 'edit'; 55 } else { 56 // check pseudo action 'source' 57 if (!actionOK('source')) { 58 msg('Command disabled: source', -1); 59 return; 60 } 61 $intro = 'read'; 62 } 63 64 // create the Editor form 65 $form = new Form(['id' => 'dw__editform']); 66 $form->setHiddenField('id', $ID); 67 $form->setHiddenField('rev', $REV); 68 $form->setHiddenField('date', $DATE); 69 $form->setHiddenField('prefix', $PRE .'.'); 70 $form->setHiddenField('suffix', $SUF); 71 $form->setHiddenField('changecheck', $check); 72 73 // prepare data for HTML_EDIT_FORMSELECTION event 74 $data = array( 75 'form' => $form, 76 'wr' => $wr, 77 'media_manager' => true, 78 'target' => ($INPUT->has('target') && $wr) ? $INPUT->str('target') : 'section', 79 'intro_locale' => $intro, 80 ); 81 82 if ($data['target'] !== 'section') { 83 // Only emit event if page is writable, section edit data is valid and 84 // edit target is not section. 85 Event::createAndTrigger('HTML_EDIT_FORMSELECTION', $data, [$this,'addTextarea'], true); 86 } else { 87 $this->addTextarea($data); 88 } 89 90 $form->setHiddenField('target', $data['target']); 91 92 if ($INPUT->has('hid')) { 93 $form->setHiddenField('hid', $INPUT->str('hid')); 94 } 95 if ($INPUT->has('codeblockOffset')) { 96 $form->setHiddenField('codeblockOffset', $INPUT->str('codeblockOffset')); 97 } 98 99 $form->addTagOpen('div')->id('wiki__editbar')->addClass('editBar'); 100 101 $form->addTagOpen('div')->id('size__ctl'); 102 $form->addTagClose('div'); 103 104 if ($wr) { 105 // add edit buttons: save, preview, cancel 106 $form->addTagOpen('div')->addClass('editButtons'); 107 $form->addButton('do[save]', $lang['btn_save'])->attr('type', 'submit') 108 ->attrs(['accesskey' => 's', 'tabindex' => '4']) 109 ->id('edbtn__save'); 110 $form->addButton('do[preview]', $lang['btn_preview'])->attr('type', 'submit') 111 ->attrs(['accesskey' => 'p', 'tabindex' => '5']) 112 ->id('edbtn__preview'); 113 $form->addButton('do[cancel]', $lang['btn_cancel'])->attr('type', 'submit') 114 ->attrs(['tabindex' => '6']); 115 $form->addTagClose('div'); // close div editButtons class 116 117 // add a textbox for edit summary 118 $form->addTagOpen('div')->addClass('summary'); 119 $input = $form->addTextInput('summary', $lang['summary']) 120 ->attrs(['size' => '50', 'tabindex' => '2']) 121 ->id('edit__summary')->addClass('edit') 122 ->val($SUM); 123 $input->getLabel()->attr('class', 'nowrap'); 124 125 // adds a checkbox for minor edits for logged in users 126 if ($conf['useacl'] && $INPUT->server->str('REMOTE_USER')) { 127 $form->addHTML(' '); 128 $form->addCheckbox('minor', $lang['minoredit'])->id('edit__minoredit')->addClass('nowrap')->val('1'); 129 } 130 $form->addTagClose('div'); // close div summary class 131 } 132 133 $form->addTagClose('div'); // close div editBar class 134 135 // license note 136 if ($wr && $conf['license']) { 137 $form->addTagOpen('div')->addClass('license'); 138 $form->addHTML($lang['licenseok'] 139 .' <a href="'.$license[$conf['license']]['url'].'" rel="license" class="urlextern"' 140 . ($conf['target']['extern']) ? ' target="'.$conf['target']['extern'].'">' : '>' 141 . $license[$conf['license']]['name'].'</a>' 142 ); 143 $form->addTagClose('div'); 144 } 145 146 // start editor html output 147 if ($wr) { 148 // sets changed to true when previewed 149 echo '<script>/*<![CDATA[*/'.'textChanged = '. ($mod ? 'true' : 'false') .'/*!]]>*/</script>'; 150 } 151 152 // print intro locale text (edit, rditrev, or read.txt) 153 if (isset($data['intro_locale'])) { 154 echo p_locale_xhtml($data['intro_locale']); 155 } 156 157 echo '<div class="editBox" role="application">'; 158 159 echo '<div class="toolbar group">'; 160 echo '<div id="tool__bar" class="tool__bar">'; 161 if ($wr && $data['media_manager']) { 162 echo '<a href="'.DOKU_BASE.'lib/exe/mediamanager.php?ns='.$INFO['namespace'].'" target="_blank">'; 163 echo $lang['mediaselect']; 164 echo '</a>'; 165 } 166 echo '</div>'; 167 echo '</div>'; 168 169 echo '<div id="draft__status" class="draft__status">'; 170 $draft = new \dokuwiki\Draft($ID, $INFO['client']); 171 if ($draft->isDraftAvailable()) { 172 echo $draft->getDraftMessage(); 173 } 174 echo '</div>'; 175 176 echo $form->toHTML('Edit'); 177 178 echo '</div>'; // close div editBox class 179 } 180 181 /** 182 * Display the default edit form (textarea) 183 * 184 * the default action for HTML_EDIT_FORMSELECTION. 185 * 186 * @param mixed[] $data 187 */ 188 public function addTextarea(&$data) 189 { 190 global $TEXT; 191 192 if ($data['target'] !== 'section') { 193 msg('No editor for edit target '. hsc($data['target']) .' found.', -1); 194 } 195 196 // set textarea attributes 197 $attr = array('tabindex' => '1'); 198 if (!$data['wr']) $attr['readonly'] = 'readonly'; 199 $attr['dir'] = 'auto'; 200 $attr['cols'] = '80'; 201 $attr['rows'] = '10'; 202 203 $data['form']->addTextarea('wikitext','')->attrs($attr)->val($TEXT) 204 ->id('wiki__text')->addClass('edit'); 205 } 206 207} 208