Cleanup: Fixed formatting issues in t3lib PHP files
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_frontendedit.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2008-2009 Jeff Segars <jeff@webempoweredchurch.org>
6 * (c) 2008-2009 David Slayback <dave@webempoweredchurch.org>
7 * All rights reserved
8 *
9 * This script is part of the TYPO3 project. The TYPO3 project is
10 * free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
17 * A copy is found in the textfile GPL.txt and important notices to the license
18 * from the author is found in LICENSE.txt distributed with these scripts.
19 *
20 *
21 * This script is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
28 /**
29 * Controller class for frontend editing.
30 *
31 * $Id$
32 *
33 * @author Jeff Segars <jeff@webempoweredchurch.org>
34 * @author David Slayback <dave@webempoweredchurch.org>
35 * @package TYPO3
36 * @subpackage t3lib
37 */
38 class t3lib_frontendedit {
39
40 /**
41 * TCEmain object.
42 *
43 * @var t3lib_tcemain
44 */
45 protected $tce;
46
47 /**
48 * Creates and initializes the TCEmain object.
49 *
50 * @return void
51 */
52 public function __construct() {
53 $this->tce = t3lib_div::makeInstance('t3lib_TCEmain');
54 $this->tce->stripslashes_values=0;
55 }
56
57 /**
58 * Initializes configuration options.
59 *
60 * @return void
61 */
62 public function initConfigOptions() {
63 $this->TSFE_EDIT = t3lib_div::_POST('TSFE_EDIT');
64
65 // Include classes for editing IF editing module in Admin Panel is open
66 if ($GLOBALS['BE_USER']->isFrontendEditingActive()) {
67 $GLOBALS['TSFE']->includeTCA();
68 if ($this->isEditAction()) {
69 $this->editAction();
70 }
71 }
72 }
73
74 /**
75 * 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.
76 * With the "edit panel" the user will see buttons with links to editing, moving, hiding, deleting the element
77 * This function is used for the cObject EDITPANEL and the stdWrap property ".editPanel"
78 *
79 * @param string 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.
80 * @param array TypoScript configuration properties for the editPanel
81 * @param string 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"
82 * @param array Alternative data array to use. Default is $this->data
83 * @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.
84 * @link http://typo3.org/doc.0.html?&tx_extrepmgm_pi1[extUid]=270&tx_extrepmgm_pi1[tocEl]=375&cHash=7d8915d508
85 */
86 public function displayEditPanel($content, array $conf, $currentRecord, array $dataArray) {
87 if ($conf['newRecordFromTable']) {
88 $currentRecord = $conf['newRecordFromTable'] . ':NEW';
89 $conf['allow'] = 'new';
90 }
91
92 list($table, $uid) = explode(':', $currentRecord);
93
94 // Page ID for new records, 0 if not specified
95 $newRecordPid = intval($conf['newRecordInPid']);
96 if (!$conf['onlyCurrentPid'] || $dataArray['pid'] == $GLOBALS['TSFE']->id) {
97 if ($table=='pages') {
98 $newUid = $uid;
99 } else {
100 if ($conf['newRecordFromTable']) {
101 $newUid = $GLOBALS['TSFE']->id;
102 if ($newRecordPid) {
103 $newUid = $newRecordPid;
104 }
105 } else {
106 $newUid = -1 * $uid;
107 }
108 }
109 }
110
111 if ($GLOBALS['TSFE']->displayEditIcons && $table && $this->allowedToEdit($table, $dataArray, $conf) && $this->allowedToEditLanguage($table, $dataArray)) {
112 $editClass = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/classes/class.frontendedit.php']['edit'];
113 if ($editClass) {
114 $edit = &t3lib_div::getUserObj($editClass, false);
115 if (is_object($edit)) {
116 $allowedActions = $this->getAllowedEditActions($table, $conf, $dataArray['pid']);
117 $content = $edit->editPanel($content, $conf, $currentRecord, $dataArray, $table, $allowedActions, $newUid, $this->getHiddenFields($dataArray));
118 }
119 }
120 }
121
122 return $content;
123 }
124
125 /**
126 * 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.
127 * This implements TYPO3 context sensitive editing facilities. Only backend users will have access (if properly configured as well).
128 *
129 * @param string The content to which the edit icons should be appended
130 * @param string 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
131 * @param array TypoScript properties for configuring the edit icons.
132 * @param string 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"
133 * @param array Alternative data array to use. Default is $this->data
134 * @param string Additional URL parameters for the link pointing to alt_doc.php
135 * @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.
136 */
137
138 public function displayEditIcons($content, $params, array $conf=array(), $currentRecord = '', array $dataArray = array(), $addUrlParamStr = '') {
139 // Check incoming params:
140 list($currentRecordTable, $currentRecordUID) = explode(':', $currentRecord);
141 list($fieldList, $table) = array_reverse(t3lib_div::trimExplode(':', $params, 1)); // Reverse the array because table is optional
142 if (!$table) {
143 $table = $currentRecordTable;
144 } elseif ($table != $currentRecordTable) {
145 return $content; // If the table is set as the first parameter, and does not match the table of the current record, then just return.
146 }
147
148 $editUid = $dataArray['_LOCALIZED_UID'] ? $dataArray['_LOCALIZED_UID'] : $currentRecordUID;
149
150 // Edit icons imply that the editing action is generally allowed, assuming page and content element permissions permit it.
151 if (!array_key_exists('allow', $conf)) {
152 $conf['allow'] = 'edit';
153 }
154
155 if ($GLOBALS['TSFE']->displayFieldEditIcons && $table && $this->allowedToEdit($table, $dataArray, $conf) && $fieldList && $this->allowedToEditLanguage($table, $dataArray)) {
156 $editClass = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/classes/class.frontendedit.php']['edit'];
157 if ($editClass) {
158 $edit = &t3lib_div::getUserObj($editClass);
159 if (is_object($edit)) {
160 $content = $edit->editIcons($content, $params, $conf, $currentRecord, $dataArray, $addURLParamStr, $table, $editUid, $fieldList);
161 }
162 }
163 }
164
165 return $content;
166 }
167
168 /*****************************************************
169 *
170 * Frontend Editing
171 *
172 ****************************************************/
173
174 /**
175 * Returns true if an edit-action is sent from the Admin Panel
176 *
177 * @return boolean
178 * @see index_ts.php
179 */
180 public function isEditAction() {
181 if (is_array($this->TSFE_EDIT)) {
182 if ($this->TSFE_EDIT['cancel']) {
183 unset($this->TSFE_EDIT['cmd']);
184 } else {
185 $cmd = (string) $this->TSFE_EDIT['cmd'];
186 if (($cmd != 'edit' || (is_array($this->TSFE_EDIT['data']) && ($this->TSFE_EDIT['doSave'] || $this->TSFE_EDIT['update'] || $this->TSFE_EDIT['update_close']))) && $cmd != 'new') {
187 // $cmd can be a command like "hide" or "move". If $cmd is "edit" or "new" it's an indication to show the formfields. But if data is sent with update-flag then $cmd = edit is accepted because edit may be sent because of .keepGoing flag.
188 return true;
189 }
190 }
191 }
192 return false;
193 }
194
195 /**
196 * Returns true if an edit form is shown on the page.
197 * Used from index_ts.php where a true return-value will result in classes etc. being included.
198 *
199 * @return boolean
200 * @see index_ts.php
201 */
202 public function isEditFormShown() {
203 if (is_array($this->TSFE_EDIT)) {
204 $cmd = (string) $this->TSFE_EDIT['cmd'];
205 if ($cmd == 'edit' || $cmd == 'new') {
206 return true;
207 }
208 }
209 }
210
211 /**
212 * Management of the on-page frontend editing forms and edit panels.
213 * Basically taking in the data and commands and passes them on to the proper classes as they should be.
214 *
215 * @return void
216 * @throws UnexpectedValueException if TSFE_EDIT[cmd] is not a valid command
217 * @see index_ts.php
218 */
219 public function editAction() {
220 // Commands:
221 list($table, $uid) = explode(':', $this->TSFE_EDIT['record']);
222 $cmd = $this->TSFE_EDIT['cmd'];
223
224 // Look for some TSFE_EDIT data that indicates we should save.
225 if (($this->TSFE_EDIT['doSave'] || $this->TSFE_EDIT['update'] || $this->TSFE_EDIT['update_close']) && is_array($this->TSFE_EDIT['data'])) {
226 $cmd = 'save';
227 }
228
229 if (($cmd == 'save') || ($cmd && $table && $uid && isset($GLOBALS['TCA'][$table]))) {
230 // Hook for defining custom editing actions. Naming is incorrect, but preserves compatibility.
231 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['extEditAction'])) {
232 $_params = array();
233 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['extEditAction'] as $_funcRef) {
234 t3lib_div::callUserFunction($_funcRef, $_params, $this);
235 }
236 }
237
238 // Perform the requested editing command.
239 $cmdAction = 'do' . ucwords($cmd);
240 if (is_callable(array($this, $cmdAction))) {
241 $this->$cmdAction($table, $uid);
242 } else {
243 throw new UnexpectedValueException(
244 'The specified frontend edit command (' . $cmd . ') is not valid.',
245 1225818120
246 );
247 }
248 }
249 }
250
251 /**
252 * Hides a specific record.
253 *
254 * @param string The table name for the record to hide.
255 * @param integer The UID for the record to hide.
256 * @return void
257 */
258 public function doHide($table, $uid) {
259 $hideField = $GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['disabled'];
260 if ($hideField) {
261 $recData = array();
262 $recData[$table][$uid][$hideField] = 1;
263 $this->tce->start($recData, array());
264 $this->tce->process_datamap();
265 }
266 }
267
268 /**
269 * Unhides (shows) a specific record.
270 *
271 * @param string The table name for the record to unhide.
272 * @param integer The UID for the record to unhide.
273 * @return void
274 */
275 public function doUnhide($table, $uid) {
276 $hideField = $GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['disabled'];
277 if ($hideField) {
278 $recData = array();
279 $recData[$table][$uid][$hideField] = 0;
280 $this->tce->start($recData, array());
281 $this->tce->process_datamap();
282 }
283 }
284
285 /**
286 * Moves a record up.
287 *
288 * @param string The table name for the record to move.
289 * @param integer The UID for the record to hide.
290 * @return void
291 */
292 public function doUp($table, $uid) {
293 $this->move($table, $uid, 'up');
294 }
295
296 /**
297 * Moves a record down.
298 *
299 * @param string The table name for the record to move.
300 * @param integer The UID for the record to move.
301 * @return void
302 */
303 public function doDown($table, $uid) {
304 $this->move($table, $uid, 'down');
305 }
306
307 /**
308 * Moves a record after a given element. Used for drag.
309 *
310 * @param string The table name for the record to move.
311 * @param integer The UID for the record to move.
312 * @return void
313 */
314 public function doMoveAfter($table, $uid) {
315 $afterUID = $GLOBALS['BE_USER']->frontendEdit->TSFE_EDIT['moveAfter'];
316 $this->move($table, $uid, '', $afterUID);
317 }
318
319 /**
320 * Moves a record
321 *
322 * @param string The table name for the record to move.
323 * @param integer The UID for the record to move.
324 * @param string The direction to move, either 'up' or 'down'.
325 * @param integer The UID of record to move after. This is specified for dragging only.
326 * @return void
327 */
328 protected function move($table, $uid, $direction='', $afterUID=0) {
329 $cmdData = array();
330 $sortField = $GLOBALS['TCA'][$table]['ctrl']['sortby'];
331 if ($sortField) {
332 // Get self:
333 $fields = array_unique(t3lib_div::trimExplode(',', $GLOBALS['TCA'][$table]['ctrl']['copyAfterDuplFields'] . ',uid,pid,' . $sortField, true));
334 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(implode(',', $fields), $table, 'uid=' . $uid);
335 if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
336 // record before or after
337 $preview = $this->extGetFeAdminValue('preview');
338 $copyAfterFieldsQuery = '';
339 if ($preview) {
340 $ignore = array('starttime'=>1, 'endtime'=>1, 'disabled'=>1, 'fe_group'=>1);
341 }
342 if ($GLOBALS['TCA'][$table]['ctrl']['copyAfterDuplFields']) {
343 $cAFields = t3lib_div::trimExplode(',', $GLOBALS['TCA'][$table]['ctrl']['copyAfterDuplFields'], true);
344 foreach($cAFields as $fieldName) {
345 $copyAfterFieldsQuery .= ' AND ' . $fieldName . '="' . $row[$fieldName] . '"';
346 }
347 }
348 if (!empty($direction)) {
349 if ($direction == 'up') {
350 $operator = '<';
351 $order = 'DESC';
352 } else {
353 $operator = '>';
354 $order = 'ASC';
355 }
356 $sortCheck = ' AND ' . $sortField . $operator . intval($row[$sortField]);
357 }
358 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
359 'uid,pid',
360 $table,
361 'pid=' . intval($row['pid']) .
362 $sortCheck .
363 $copyAfterFieldsQuery .
364 $GLOBALS['TSFE']->sys_page->enableFields($table, '', $ignore),
365 '',
366 $sortField . ' ' . $order,
367 '2'
368 );
369 if ($row2 = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
370 if ($afterUID) {
371 $cmdData[$table][$uid]['move'] = -$afterUID;
372 }
373 elseif ($direction == 'down') {
374 $cmdData[$table][$uid]['move'] = -$row2['uid'];
375 }
376 elseif ($row3 = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) { // Must take the second record above...
377 $cmdData[$table][$uid]['move'] = -$row3['uid'];
378 }
379 else { // ... and if that does not exist, use pid
380 $cmdData[$table][$uid]['move'] = $row['pid'];
381 }
382 } elseif ($direction == 'up') {
383 $cmdData[$table][$uid]['move'] = $row['pid'];
384 }
385 }
386 if (!empty($cmdData)) {
387 $this->tce->start(array(), $cmdData);
388 $this->tce->process_cmdmap();
389 }
390 }
391 }
392
393 /**
394 * Deletes a specific record.
395 *
396 * @param string The table name for the record to delete.
397 * @param integer The UID for the record to delete.
398 * @return void
399 */
400 public function doDelete($table, $uid) {
401 $cmdData[$table][$uid]['delete'] = 1;
402 if (count($cmdData)) {
403 $this->tce->start(array(), $cmdData);
404 $this->tce->process_cmdmap();
405 }
406 }
407
408 /**
409 * Saves a record based on its data array.
410 *
411 * @param string The table name for the record to save.
412 * @param integer The UID for the record to save.
413 * @return void
414 */
415 public function doSave($table, $uid) {
416 $data = $this->TSFE_EDIT['data'];
417
418 if (!empty($data)) {
419 $this->tce->start($data, array());
420 $this->tce->process_uploads($_FILES);
421 $this->tce->process_datamap();
422
423 // Save the new UID back into TSFE_EDIT
424 $newUID = $this->tce->substNEWwithIDs['NEW'];
425 if ($newUID) {
426 $GLOBALS['BE_USER']->frontendEdit->TSFE_EDIT['newUID'] = $newUID;
427 }
428 }
429 }
430
431 /**
432 * Stub for closing a record. No real functionality needed since content
433 * element rendering will take care of everything.
434 *
435 * @param string The table name for the record to close.
436 * @param integer The UID for the record to close.
437 * @return void
438 */
439 public function doClose($table, $uid) {
440 // Do nothing.
441 }
442
443 /**
444 * Checks whether the user has access to edit the language for the
445 * requested record.
446 *
447 * @param string The name of the table.
448 * @param array The record.
449 * @return boolean
450 */
451 protected function allowedToEditLanguage($table, array $currentRecord) {
452 // If no access right to record languages, return immediately
453 if ($table === 'pages') {
454 $lang = $GLOBALS['TSFE']->sys_language_uid;
455 } elseif ($table === 'tt_content') {
456 $lang = $GLOBALS['TSFE']->sys_language_content;
457 } elseif ($GLOBALS['TCA'][$table]['ctrl']['languageField']) {
458 $lang = $currentRecord[$GLOBALS['TCA'][$table]['ctrl']['languageField']];
459 } else {
460 $lang = -1;
461 }
462
463 if ($GLOBALS['BE_USER']->checkLanguageAccess($lang)) {
464 $languageAccess = true;
465 } else {
466 $languageAccess = false;
467 }
468
469 return $languageAccess;
470 }
471
472 /**
473 * Checks whether the user is allowed to edit the requested table.
474 *
475 * @param string The name of the table.
476 * @param array The data array.
477 * @param array The configuration array for the edit panel.
478 * @return boolean
479 */
480 protected function allowedToEdit($table, array $dataArray, array $conf) {
481
482 // Unless permissions specifically allow it, editing is not allowed.
483 $mayEdit = false;
484
485 if ($table=='pages') {
486 // 2 = permission to edit the page
487 if ($GLOBALS['BE_USER']->isAdmin() || $GLOBALS['BE_USER']->doesUserHaveAccess($dataArray, 2)) {
488 $mayEdit = true;
489 }
490 } else {
491 // 16 = permission to edit content on the page
492 if ($GLOBALS['BE_USER']->isAdmin() || $GLOBALS['BE_USER']->doesUserHaveAccess(t3lib_BEfunc::getRecord('pages', $dataArray['pid']), 16)) {
493 $mayEdit = true;
494 }
495 }
496
497 if (!$conf['onlyCurrentPid'] || ($dataArray['pid'] == $GLOBALS['TSFE']->id)) {
498 // Permissions:
499 $types = t3lib_div::trimExplode(',', t3lib_div::strtolower($conf['allow']),1);
500 $allow = array_flip($types);
501
502 $perms = $GLOBALS['BE_USER']->calcPerms($GLOBALS['TSFE']->page);
503 if ($table == 'pages') {
504 $allow = $this->getAllowedEditActions($table, $conf, $dataArray['pid'], $allow);
505
506 // Can only display editbox if there are options in the menu
507 if (count($allow)) {
508 $mayEdit = true;
509 }
510 } else {
511 $mayEdit = count($allow) && ($perms & 16);
512 }
513 }
514
515 return $mayEdit;
516 }
517
518 /**
519 * Takes an array of generally allowed actions and filters that list based on page and content permissions.
520 *
521 * @param string The name of the table.
522 * @param array The configuration array.
523 * @param integer The PID where editing will occur.
524 * @param string Comma-separated list of actions that are allowed in general.
525 * @return array
526 */
527 protected function getAllowedEditActions($table, array $conf, $pid, $allow = '') {
528
529 if (!$allow) {
530 $types = t3lib_div::trimExplode(',', t3lib_div::strtolower($conf['allow']), true);
531 $allow = array_flip($types);
532 }
533
534 if (!$conf['onlyCurrentPid'] || $pid == $GLOBALS['TSFE']->id) {
535 // Permissions:
536 $types = t3lib_div::trimExplode(',', t3lib_div::strtolower($conf['allow']), true);
537 $allow = array_flip($types);
538
539 $perms = $GLOBALS['BE_USER']->calcPerms($GLOBALS['TSFE']->page);
540 if ($table=='pages') {
541 // rootpage!
542 if (count($GLOBALS['TSFE']->config['rootLine']) == 1) {
543 unset($allow['move']);
544 unset($allow['hide']);
545 unset($allow['delete']);
546 }
547 if (!($perms & 2)){
548 unset($allow['edit']);
549 unset($allow['move']);
550 unset($allow['hide']);
551 }
552 if (!($perms & 4)) {
553 unset($allow['delete']);
554 }
555 if (!($perms&8)) {
556 unset($allow['new']);
557 }
558 }
559 }
560
561 return $allow;
562 }
563
564 /**
565 * Adds any extra Javascript includes needed for Front-end editing
566 *
567 * @param none
568 * @return string
569 */
570 public function getJavascriptIncludes() {
571 // No extra JS includes needed
572 return '';
573 }
574
575 /**
576 * Gets the hidden fields (array key=field name, value=field value) to be used in the edit panel for a particular content element.
577 * In the normal case, no hidden fields are needed but special controllers such as TemplaVoila need to track flexform pointers, etc.
578 *
579 * @param array The data array for a specific content element.
580 * @return array
581 */
582 public function getHiddenFields(array $dataArray) {
583 // No special hidden fields needed.
584 return array();
585 }
586 }
587
588 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_frontendedit.php']) {
589 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_frontendedit.php']);
590 }
591
592 ?>