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