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