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