ee15e693530139347f9828b7aad09e9fbb94adae
[Packages/TYPO3.CMS.git] / typo3 / sysext / feedit / Classes / FrontendEditPanel.php
1 <?php
2 namespace TYPO3\CMS\Feedit;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use TYPO3\CMS\Backend\FrontendBackendUserAuthentication;
17 use TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Database\DatabaseConnection;
19 use TYPO3\CMS\Core\Imaging\Icon;
20 use TYPO3\CMS\Core\Imaging\IconFactory;
21 use TYPO3\CMS\Core\Type\Bitmask\JsConfirmation;
22 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\Core\Utility\MathUtility;
25 use TYPO3\CMS\Core\Utility\PathUtility;
26 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
27 use TYPO3\CMS\Frontend\View\AdminPanelView;
28
29 /**
30 * View class for the edit panels in frontend editing.
31 */
32 class FrontendEditPanel
33 {
34 /**
35 * The Content Object Renderer
36 *
37 * @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
38 */
39 protected $cObj;
40
41 /**
42 * Property for accessing TypoScriptFrontendController centrally
43 *
44 * @var TypoScriptFrontendController
45 */
46 protected $frontendController;
47
48 /**
49 * Property for accessing DatabaseConnection centrally
50 *
51 * @var DatabaseConnection
52 */
53 protected $databaseConnection;
54
55 /**
56 * @var FrontendBackendUserAuthentication
57 */
58 protected $backendUser;
59
60 /**
61 * @var \TYPO3\CMS\Core\Imaging\IconFactory
62 */
63 protected $iconFactory;
64
65 /**
66 * Constructor for the edit panel
67 *
68 * @param DatabaseConnection $databaseConnection
69 * @param TypoScriptFrontendController $frontendController
70 * @param FrontendBackendUserAuthentication $backendUser
71 */
72 public function __construct(DatabaseConnection $databaseConnection = null, TypoScriptFrontendController $frontendController = null, FrontendBackendUserAuthentication $backendUser = null)
73 {
74 $this->databaseConnection = $databaseConnection ?: $GLOBALS['TYPO3_DB'];
75 $this->frontendController = $frontendController ?: $GLOBALS['TSFE'];
76 $this->backendUser = $backendUser ?: $GLOBALS['BE_USER'];
77 $this->cObj = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class);
78 $this->cObj->start([]);
79 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
80 }
81
82 /**
83 * Generates the "edit panels" which can be shown for a page or records on a page when the Admin Panel is enabled for a backend users surfing the frontend.
84 * With the "edit panel" the user will see buttons with links to editing, moving, hiding, deleting the element
85 * This function is used for the cObject EDITPANEL and the stdWrap property ".editPanel"
86 *
87 * @param string $content A content string containing the content related to the edit panel. For cObject "EDITPANEL" this is empty but not so for the stdWrap property. The edit panel is appended to this string and returned.
88 * @param array $conf TypoScript configuration properties for the editPanel
89 * @param string $currentRecord The "table:uid" of the record being shown. If empty string then $this->currentRecord is used. For new records (set by $conf['newRecordFromTable']) it's auto-generated to "[tablename]:NEW
90 * @param array $dataArr Alternative data array to use. Default is $this->data
91 * @param string $table
92 * @param array $allow
93 * @param int $newUID
94 * @param array $hiddenFields
95 * @return string The input content string with the editPanel appended. This function returns only an edit panel appended to the content string if a backend user is logged in (and has the correct permissions). Otherwise the content string is directly returned.
96 */
97 public function editPanel($content, array $conf, $currentRecord = '', array $dataArr = [], $table = '', array $allow = [], $newUID = 0, array $hiddenFields = [])
98 {
99 $hiddenFieldString = $command = '';
100
101 // Special content is about to be shown, so the cache must be disabled.
102 $this->frontendController->set_no_cache('Frontend edit panel is shown', true);
103
104 $formName = 'TSFE_EDIT_FORM_' . substr($this->frontendController->uniqueHash(), 0, 4);
105 $formTag = '<form name="' . $formName . '" id ="' . $formName . '" action="' . htmlspecialchars(GeneralUtility::getIndpEnv('REQUEST_URI')) . '" method="post" enctype="multipart/form-data" onsubmit="return TBE_EDITOR.checkSubmit(1);">';
106 $sortField = $GLOBALS['TCA'][$table]['ctrl']['sortby'];
107 $labelField = $GLOBALS['TCA'][$table]['ctrl']['label'];
108 $hideField = $GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['disabled'];
109
110 $panel = '';
111 if (isset($allow['toolbar']) && $this->backendUser->adminPanel instanceof AdminPanelView) {
112 $panel .= $this->backendUser->adminPanel->ext_makeToolBar();
113 }
114 if (isset($allow['edit'])) {
115 $icon = '<span title="' . $this->backendUser->extGetLL('p_editRecord') . '">' . $this->iconFactory->getIcon('actions-document-open', Icon::SIZE_SMALL)->render() . '</span>';
116 $panel .= $this->editPanelLinkWrap($icon, $formName, 'edit', $dataArr['_LOCALIZED_UID'] ? $table . ':' . $dataArr['_LOCALIZED_UID'] : $currentRecord);
117 }
118 // Hiding in workspaces because implementation is incomplete
119 if (isset($allow['move']) && $sortField && $this->backendUser->workspace === 0) {
120 $icon = '<span title="' . $this->backendUser->extGetLL('p_moveUp') . '">' . $this->iconFactory->getIcon('actions-move-up', Icon::SIZE_SMALL)->render() . '</span>';
121 $panel .= $this->editPanelLinkWrap($icon, $formName, 'up');
122 $icon = '<span title="' . $this->backendUser->extGetLL('p_moveDown') . '">' . $this->iconFactory->getIcon('actions-move-down', Icon::SIZE_SMALL)->render() . '</span>';
123 $panel .= $this->editPanelLinkWrap($icon, $formName, 'down');
124 }
125 // Hiding in workspaces because implementation is incomplete
126 // Hiding for localizations because it is unknown what should be the function in that case
127 if (isset($allow['hide']) && $hideField && $this->backendUser->workspace === 0 && !$dataArr['_LOCALIZED_UID']) {
128 if ($dataArr[$hideField]) {
129 $icon = $this->iconFactory->getIcon('actions-edit-unhide', Icon::SIZE_SMALL)->render();
130 $panel .= $this->editPanelLinkWrap($icon, $formName, 'unhide');
131 } else {
132 $icon = $this->iconFactory->getIcon('actions-edit-hide', Icon::SIZE_SMALL)->render();
133 $panel .= $this->editPanelLinkWrap($icon, $formName, 'hide', '', $this->backendUser->extGetLL('p_hideConfirm'));
134 }
135 }
136 if (isset($allow['new'])) {
137 if ($table === 'pages') {
138 $icon = '<span title="' . $this->backendUser->extGetLL('p_newSubpage') . '">'
139 . $this->iconFactory->getIcon('actions-page-new', Icon::SIZE_SMALL)->render()
140 . '</span>';
141 $panel .= $this->editPanelLinkWrap($icon, $formName, 'new', $currentRecord, '');
142 } else {
143 $icon = '<span title="' . $this->backendUser->extGetLL('p_newRecordAfter') . '">'
144 . $this->iconFactory->getIcon('actions-document-new', Icon::SIZE_SMALL)->render()
145 . '</span>';
146 $panel .= $this->editPanelLinkWrap($icon, $formName, 'new', $currentRecord, '', $newUID);
147 }
148 }
149 // Hiding in workspaces because implementation is incomplete
150 // Hiding for localizations because it is unknown what should be the function in that case
151 if (isset($allow['delete']) && $this->backendUser->workspace === 0 && !$dataArr['_LOCALIZED_UID']) {
152 $icon = '<span title="' . $this->backendUser->extGetLL('p_delete') . '">'
153 . $this->iconFactory->getIcon('actions-edit-delete', Icon::SIZE_SMALL)->render()
154 . '</span>';
155 $panel .= $this->editPanelLinkWrap($icon, $formName, 'delete', '', $this->backendUser->extGetLL('p_deleteConfirm'));
156 }
157 // Final
158 $labelTxt = $this->cObj->stdWrap($conf['label'], $conf['label.']);
159 foreach ((array)$hiddenFields as $name => $value) {
160 $hiddenFieldString .= '<input type="hidden" name="TSFE_EDIT[' . htmlspecialchars($name) . ']" value="' . htmlspecialchars($value) . '"/>' . LF;
161 }
162
163 $panel = '<!-- BE_USER Edit Panel: -->
164 ' . $formTag . $hiddenFieldString . '
165 <input type="hidden" name="TSFE_EDIT[cmd]" value="" />
166 <input type="hidden" name="TSFE_EDIT[record]" value="' . $currentRecord . '" />
167 <div class="typo3-editPanel">'
168 . '<div class="btn-group">'
169 . $panel
170 . '</div>' .
171 ($labelTxt ? '<div class="typo3-editPanel-label">' . sprintf($labelTxt, htmlspecialchars(GeneralUtility::fixed_lgd_cs($dataArr[$labelField], 50))) . '</div>' : '') . '
172 </div>
173 </form>';
174
175 // Wrap the panel
176 if ($conf['innerWrap']) {
177 $panel = $this->cObj->wrap($panel, $conf['innerWrap']);
178 }
179 if ($conf['innerWrap.']) {
180 $panel = $this->cObj->stdWrap($panel, $conf['innerWrap.']);
181 }
182
183 // Wrap the complete panel
184 if ($conf['outerWrap']) {
185 $panel = $this->cObj->wrap($panel, $conf['outerWrap']);
186 }
187 if ($conf['outerWrap.']) {
188 $panel = $this->cObj->stdWrap($panel, $conf['outerWrap.']);
189 }
190 if ($conf['printBeforeContent']) {
191 $finalOut = $panel . $content;
192 } else {
193 $finalOut = $content . $panel;
194 }
195
196 $hidden = $this->isDisabled($table, $dataArr) ? ' typo3-feedit-element-hidden' : '';
197 $outerWrapConfig = isset($conf['stdWrap.'])
198 ? $conf['stdWrap.']
199 : ['wrap' => '<div class="typo3-feedit-element' . $hidden . '">|</div>'];
200 $finalOut = $this->cObj->stdWrap($finalOut, $outerWrapConfig);
201
202 return $finalOut;
203 }
204
205 /**
206 * Adds an edit icon to the content string. The edit icon links to EditDocumentController with proper parameters for editing the table/fields of the context.
207 * This implements TYPO3 context sensitive editing facilities. Only backend users will have access (if properly configured as well).
208 *
209 * @param string $content The content to which the edit icons should be appended
210 * @param string $params The parameters defining which table and fields to edit. Syntax is [tablename]:[fieldname],[fieldname],[fieldname],... OR [fieldname],[fieldname],[fieldname],... (basically "[tablename]:" is optional, default table is the one of the "current record" used in the function). The fieldlist is sent as "&columnsOnly=" parameter to EditDocumentController
211 * @param array $conf TypoScript properties for configuring the edit icons.
212 * @param string $currentRecord The "table:uid" of the record being shown. If empty string then $this->currentRecord is used. For new records (set by $conf['newRecordFromTable']) it's auto-generated to "[tablename]:NEW
213 * @param array $dataArr Alternative data array to use. Default is $this->data
214 * @param string $addUrlParamStr Additional URL parameters for the link pointing to EditDocumentController
215 * @param string $table
216 * @param int $editUid
217 * @param string $fieldList
218 * @return string The input content string, possibly with edit icons added (not necessarily in the end but just after the last string of normal content.
219 */
220 public function editIcons($content, $params, array $conf = [], $currentRecord = '', array $dataArr = [], $addUrlParamStr = '', $table, $editUid, $fieldList)
221 {
222 // Special content is about to be shown, so the cache must be disabled.
223 $this->frontendController->set_no_cache('Display frontend edit icons', true);
224 $iconTitle = $this->cObj->stdWrap($conf['iconTitle'], $conf['iconTitle.']);
225 $iconImg = '<span title="' . htmlspecialchars($iconTitle, ENT_COMPAT, 'UTF-8', false) . '" class="frontEndEditIcons" style="' . ($conf['styleAttribute'] ? htmlspecialchars($conf['styleAttribute']) : '') . '">'
226 . $this->iconFactory->getIcon('actions-document-open', Icon::SIZE_SMALL)->render()
227 . '</span>';
228 $nV = GeneralUtility::_GP('ADMCMD_view') ? 1 : 0;
229
230 $url = BackendUtility::getModuleUrl(
231 'record_edit',
232 [
233 'edit[' . $table . '][' . $editUid . ']' => 'edit',
234 'columnsOnly' => $fieldList,
235 'noView' => $nV,
236 'feEdit' => 1
237 ]
238 ) . $addUrlParamStr;
239 $icon = $this->editPanelLinkWrap_doWrap($iconImg, $url, 'content-link');
240 if ($conf['beforeLastTag'] < 0) {
241 $content = $icon . $content;
242 } elseif ($conf['beforeLastTag'] > 0) {
243 $cBuf = rtrim($content);
244 $secureCount = 30;
245 while ($secureCount && substr($cBuf, -1) == '>' && substr($cBuf, -4) != '</a>') {
246 $cBuf = rtrim(preg_replace('/<[^<]*>$/', '', $cBuf));
247 $secureCount--;
248 }
249 $content = strlen($cBuf) && $secureCount ? substr($content, 0, strlen($cBuf)) . $icon . substr($content, strlen($cBuf)) : ($content = $icon . $content);
250 } else {
251 $content .= $icon;
252 }
253 return $content;
254 }
255
256 /**
257 * Helper function for editPanel() which wraps icons in the panel in a link with the action of the panel.
258 * The links are for some of them not simple hyperlinks but onclick-actions which submits a little form which the panel is wrapped in.
259 *
260 * @param string $string The string to wrap in a link, typ. and image used as button in the edit panel.
261 * @param string $formName The name of the form wrapping the edit panel.
262 * @param string $cmd The command of the link. There is a predefined list available: edit, new, up, down etc.
263 * @param string $currentRecord The "table:uid" of the record being processed by the panel.
264 * @param string $confirm Text string with confirmation message; If set a confirm box will be displayed before carrying out the action (if Yes is pressed)
265 * @param int|string $nPid "New pid" - for new records
266 * @return string A <a> tag wrapped string.
267 */
268 protected function editPanelLinkWrap($string, $formName, $cmd, $currentRecord = '', $confirm = '', $nPid = '')
269 {
270 $nV = GeneralUtility::_GP('ADMCMD_view') ? 1 : 0;
271 if ($cmd == 'edit') {
272 $rParts = explode(':', $currentRecord);
273 $out = $this->editPanelLinkWrap_doWrap($string, BackendUtility::getModuleUrl('record_edit', ['edit[' . $rParts[0] . '][' . $rParts[1] . ']' => 'edit', 'noView' => $nV, 'feEdit' => 1]), $currentRecord);
274 } elseif ($cmd == 'new') {
275 $rParts = explode(':', $currentRecord);
276 if ($rParts[0] == 'pages') {
277 $out = $this->editPanelLinkWrap_doWrap($string, BackendUtility::getModuleUrl('db_new', ['id' => $rParts[1], 'pagesOnly' => 1]), $currentRecord);
278 } else {
279 if (!(int)$nPid) {
280 $nPid = MathUtility::canBeInterpretedAsInteger($rParts[1]) ? -$rParts[1] : $this->frontendController->id;
281 }
282 $out = $this->editPanelLinkWrap_doWrap($string, BackendUtility::getModuleUrl('record_edit', ['edit[' . $rParts[0] . '][' . $nPid . ']' => 'new', 'noView' => $nV]), $currentRecord);
283 }
284 } else {
285 if ($confirm && $this->backendUser->jsConfirmation(JsConfirmation::FE_EDIT)) {
286 // Gets htmlspecialchared later
287 $cf1 = 'if (confirm(' . GeneralUtility::quoteJSvalue($confirm, true) . ')) {';
288 $cf2 = '}';
289 } else {
290 $cf1 = ($cf2 = '');
291 }
292 $out = '<a href="#" class="btn btn-default btn-sm" onclick="' . htmlspecialchars(($cf1 . 'document.' . $formName . '[\'TSFE_EDIT[cmd]\'].value=\'' . $cmd . '\'; document.' . $formName . '.submit();' . $cf2 . ' return false;')) . '">' . $string . '</a>';
293 }
294 return $out;
295 }
296
297 /**
298 * Creates a link to a script (eg. EditDocumentController or NewRecordController) which either opens in the current frame OR in a pop-up window.
299 *
300 * @param string $string The string to wrap in a link, typ. and image used as button in the edit panel.
301 * @param string $url The URL of the link. Should be absolute if supposed to work with <base> path set.
302 * @param string $additionalClasses Additional CSS classes
303 * @return string A <a> tag wrapped string.
304 * @see editPanelLinkWrap()
305 */
306 protected function editPanelLinkWrap_doWrap($string, $url, $additionalClasses = '')
307 {
308 $width = MathUtility::forceIntegerInRange($this->backendUser->getTSConfigVal('options.feedit.popupWidth'), 690, 5000, 690);
309 $height = MathUtility::forceIntegerInRange($this->backendUser->getTSConfigVal('options.feedit.popupHeight'), 500, 5000, 500);
310 $onclick = 'vHWin=window.open(' . GeneralUtility::quoteJSvalue($url . '&returnUrl=' . PathUtility::getAbsoluteWebPath(ExtensionManagementUtility::siteRelPath('backend') . 'Resources/Private/Templates/Close.html')) . ',\'FEquickEditWindow\',\'width=' . $width . ',height=' . $height . ',status=0,menubar=0,scrollbars=1,resizable=1\');vHWin.focus();return false;';
311 return '<a href="#" class="btn btn-default btn-sm ' . htmlspecialchars($additionalClasses) . '" onclick="' . htmlspecialchars($onclick) . '" class="frontEndEditIconLinks">' . $string . '</a>';
312 }
313
314 /**
315 * Returns TRUE if the input table/row would be hidden in the frontend, according to the current time and simulate user group
316 *
317 * @param string $table The table name
318 * @param array $row The data record
319 * @return bool
320 */
321 protected function isDisabled($table, array $row)
322 {
323 $status = false;
324 if (
325 $GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['disabled'] &&
326 $row[$GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['disabled']] ||
327 $GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['fe_group'] &&
328 $this->frontendController->simUserGroup &&
329 $row[$GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['fe_group']] == $this->frontendController->simUserGroup ||
330 $GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['starttime'] &&
331 $row[$GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['starttime']] > $GLOBALS['EXEC_TIME'] ||
332 $GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['endtime'] &&
333 $row[$GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['endtime']] &&
334 $row[$GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['endtime']] < $GLOBALS['EXEC_TIME']
335 ) {
336 $status = true;
337 }
338
339 return $status;
340 }
341 }