051c69b5a8f6dce4f542d04eac8badb8cb1249b1
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Tree / Pagetree / Commands.php
1 <?php
2 namespace TYPO3\CMS\Backend\Tree\Pagetree;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2011 TYPO3 Tree Team <http://forge.typo3.org/projects/typo3v4-extjstrees>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Page Tree and Context Menu Commands
31 *
32 * @author Stefan Galinski <stefan.galinski@gmail.com>
33 */
34 class Commands {
35
36 /**
37 * @var boolean|null
38 */
39 static protected $useNavTitle = NULL;
40
41 /**
42 * @var boolean|null
43 */
44 static protected $addIdAsPrefix = NULL;
45
46 /**
47 * @var boolean|null
48 */
49 static protected $addDomainName = NULL;
50
51 /**
52 * @var array|null
53 */
54 static protected $backgroundColors = NULL;
55
56 /**
57 * @var integer|null
58 */
59 static protected $titleLength = NULL;
60
61 /**
62 * Visibly the page
63 *
64 * @param \TYPO3\CMS\Backend\Tree\Pagetree\PagetreeNode $node
65 * @return void
66 */
67 static public function visiblyNode(\TYPO3\CMS\Backend\Tree\Pagetree\PagetreeNode $node) {
68 $data['pages'][$node->getWorkspaceId()]['hidden'] = 0;
69 self::processTceCmdAndDataMap(array(), $data);
70 }
71
72 /**
73 * Hide the page
74 *
75 * @param \TYPO3\CMS\Backend\Tree\Pagetree\PagetreeNode $node
76 * @return void
77 */
78 static public function disableNode(\TYPO3\CMS\Backend\Tree\Pagetree\PagetreeNode $node) {
79 $data['pages'][$node->getWorkspaceId()]['hidden'] = 1;
80 self::processTceCmdAndDataMap(array(), $data);
81 }
82
83 /**
84 * Delete the page
85 *
86 * @param \TYPO3\CMS\Backend\Tree\Pagetree\PagetreeNode $node
87 * @return void
88 */
89 static public function deleteNode(\TYPO3\CMS\Backend\Tree\Pagetree\PagetreeNode $node) {
90 $cmd['pages'][$node->getId()]['delete'] = 1;
91 self::processTceCmdAndDataMap($cmd);
92 }
93
94 /**
95 * Restore the page
96 *
97 * @param \TYPO3\CMS\Backend\Tree\Pagetree\PagetreeNode $node
98 * @param integer $targetId
99 * @return void
100 */
101 static public function restoreNode(\TYPO3\CMS\Backend\Tree\Pagetree\PagetreeNode $node, $targetId) {
102 $cmd['pages'][$node->getId()]['undelete'] = 1;
103 self::processTceCmdAndDataMap($cmd);
104 if ($node->getId() !== $targetId) {
105 self::moveNode($node, $targetId);
106 }
107 }
108
109 /**
110 * Updates the node label
111 *
112 * @param \TYPO3\CMS\Backend\Tree\Pagetree\PagetreeNode $node
113 * @param string $updatedLabel
114 * @return void
115 */
116 static public function updateNodeLabel(\TYPO3\CMS\Backend\Tree\Pagetree\PagetreeNode $node, $updatedLabel) {
117 $data['pages'][$node->getWorkspaceId()][$node->getTextSourceField()] = $updatedLabel;
118 self::processTceCmdAndDataMap(array(), $data);
119 }
120
121 /**
122 * Copies a page and returns the id of the new page
123 *
124 * Node: Use a negative target id to specify a sibling target else the parent is used
125 *
126 * @param \TYPO3\CMS\Backend\Tree\Pagetree\PagetreeNode $sourceNode
127 * @param integer $targetId
128 * @return integer
129 */
130 static public function copyNode(\TYPO3\CMS\Backend\Tree\Pagetree\PagetreeNode $sourceNode, $targetId) {
131 $cmd['pages'][$sourceNode->getId()]['copy'] = $targetId;
132 $returnValue = self::processTceCmdAndDataMap($cmd);
133 return $returnValue['pages'][$sourceNode->getId()];
134 }
135
136 /**
137 * Moves a page
138 *
139 * Node: Use a negative target id to specify a sibling target else the parent is used
140 *
141 * @param \TYPO3\CMS\Backend\Tree\Pagetree\PagetreeNode $sourceNode
142 * @param integer $targetId
143 * @return void
144 */
145 static public function moveNode(\TYPO3\CMS\Backend\Tree\Pagetree\PagetreeNode $sourceNode, $targetId) {
146 $cmd['pages'][$sourceNode->getId()]['move'] = $targetId;
147 self::processTceCmdAndDataMap($cmd);
148 }
149
150 /**
151 * Creates a page of the given doktype and returns the id of the created page
152 *
153 * @param \TYPO3\CMS\Backend\Tree\Pagetree\PagetreeNode $parentNode
154 * @param integer $targetId
155 * @param integer $pageType
156 * @return integer
157 */
158 static public function createNode(\TYPO3\CMS\Backend\Tree\Pagetree\PagetreeNode $parentNode, $targetId, $pageType) {
159 $placeholder = 'NEW12345';
160 $pid = intval($parentNode->getWorkspaceId());
161 $targetId = intval($targetId);
162
163 // Use page TsConfig as default page initialization
164 $pageTs = \TYPO3\CMS\Backend\Utility\BackendUtility::getPagesTSconfig($pid);
165 if (array_key_exists('TCAdefaults.', $pageTs) && array_key_exists('pages.', $pageTs['TCAdefaults.'])) {
166 $data['pages'][$placeholder] = $pageTs['TCAdefaults.']['pages.'];
167 } else {
168 $data['pages'][$placeholder] = array();
169 }
170
171 $data['pages'][$placeholder]['pid'] = $pid;
172 $data['pages'][$placeholder]['doktype'] = $pageType;
173 $data['pages'][$placeholder]['title'] = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:tree.defaultPageTitle', TRUE);
174 $newPageId = self::processTceCmdAndDataMap(array(), $data);
175 $node = self::getNode($newPageId[$placeholder]);
176 if ($pid !== $targetId) {
177 self::moveNode($node, $targetId);
178 }
179
180 return $newPageId[$placeholder];
181 }
182
183 /**
184 * Process TCEMAIN commands and data maps
185 *
186 * Command Map:
187 * Used for moving, recover, remove and some more operations.
188 *
189 * Data Map:
190 * Used for creating and updating records,
191 *
192 * This API contains all necessary access checks.
193 *
194 * @param array $cmd
195 * @param array $data
196 * @return array
197 * @throws \RuntimeException if an error happened while the TCE processing
198 */
199 static protected function processTceCmdAndDataMap(array $cmd, array $data = array()) {
200 /** @var $tce \TYPO3\CMS\Core\DataHandling\DataHandler */
201 $tce = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\DataHandling\\DataHandler');
202 $tce->stripslashes_values = 0;
203 $tce->start($data, $cmd);
204 $tce->copyTree = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($GLOBALS['BE_USER']->uc['copyLevels'], 0, 100);
205 if (count($cmd)) {
206 $tce->process_cmdmap();
207 $returnValues = $tce->copyMappingArray_merged;
208 } elseif (count($data)) {
209 $tce->process_datamap();
210 $returnValues = $tce->substNEWwithIDs;
211 } else {
212 $returnValues = array();
213 }
214 // check errors
215 if (count($tce->errorLog)) {
216 throw new \RuntimeException(implode(chr(10), $tce->errorLog), 1333754629);
217 }
218 return $returnValues;
219 }
220
221 /**
222 * Returns a node from the given node id
223 *
224 * @param integer $nodeId
225 * @param boolean $unsetMovePointers
226 * @return \TYPO3\CMS\Backend\Tree\Pagetree\PagetreeNode
227 */
228 static public function getNode($nodeId, $unsetMovePointers = TRUE) {
229 $record = self::getNodeRecord($nodeId, $unsetMovePointers);
230 return self::getNewNode($record);
231 }
232
233 /**
234 * Returns the mount point path for a temporary mount or the given id
235 *
236 * @param integer $uid
237 * @return string
238 */
239 static public function getMountPointPath($uid = -1) {
240 if ($uid === -1) {
241 $uid = intval($GLOBALS['BE_USER']->uc['pageTree_temporaryMountPoint']);
242 }
243 if ($uid <= 0) {
244 return '';
245 }
246 if (self::$useNavTitle === NULL) {
247 self::$useNavTitle = $GLOBALS['BE_USER']->getTSConfigVal('options.pageTree.showNavTitle');
248 }
249 $rootline = array_reverse(\TYPO3\CMS\Backend\Utility\BackendUtility::BEgetRootLine($uid));
250 array_shift($rootline);
251 $path = array();
252 foreach ($rootline as $rootlineElement) {
253 $record = self::getNodeRecord($rootlineElement['uid']);
254 $text = $record['title'];
255 if (self::$useNavTitle && trim($record['nav_title']) !== '') {
256 $text = $record['nav_title'];
257 }
258 $path[] = htmlspecialchars($text);
259 }
260 return '/' . implode('/', $path);
261 }
262
263 /**
264 * Returns a node record from a given id
265 *
266 * @param integer $nodeId
267 * @param boolean $unsetMovePointers
268 * @return array
269 */
270 static public function getNodeRecord($nodeId, $unsetMovePointers = TRUE) {
271 $record = \TYPO3\CMS\Backend\Utility\BackendUtility::getRecordWSOL('pages', $nodeId, '*', '', TRUE, $unsetMovePointers);
272 return $record;
273 }
274
275 /**
276 * Returns the first configured domain name for a page
277 *
278 * @param integer $uid
279 * @return string
280 */
281 static public function getDomainName($uid) {
282 $whereClause = 'pid=' . intval($uid) . \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause('sys_domain') . \TYPO3\CMS\Backend\Utility\BackendUtility::BEenableFields('sys_domain');
283 $domain = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('domainName', 'sys_domain', $whereClause, '', 'sorting');
284 return is_array($domain) ? htmlspecialchars($domain['domainName']) : '';
285 }
286
287 /**
288 * Creates a node with the given record information
289 *
290 * @param array $record
291 * @param integer $mountPoint
292 * @return \TYPO3\CMS\Backend\Tree\Pagetree\PagetreeNode
293 */
294 static public function getNewNode($record, $mountPoint = 0) {
295 if (self::$titleLength === NULL) {
296 self::$useNavTitle = $GLOBALS['BE_USER']->getTSConfigVal('options.pageTree.showNavTitle');
297 self::$addIdAsPrefix = $GLOBALS['BE_USER']->getTSConfigVal('options.pageTree.showPageIdWithTitle');
298 self::$addDomainName = $GLOBALS['BE_USER']->getTSConfigVal('options.pageTree.showDomainNameWithTitle');
299 self::$backgroundColors = $GLOBALS['BE_USER']->getTSConfigProp('options.pageTree.backgroundColor');
300 self::$titleLength = intval($GLOBALS['BE_USER']->uc['titleLen']);
301 }
302 /** @var $subNode \TYPO3\CMS\Backend\Tree\Pagetree\PagetreeNode */
303 $subNode = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Tree\\Pagetree\\PagetreeNode');
304 $subNode->setRecord($record);
305 $subNode->setCls($record['_CSSCLASS']);
306 $subNode->setType('pages');
307 $subNode->setId($record['uid']);
308 $subNode->setMountPoint($mountPoint);
309 $subNode->setWorkspaceId($record['_ORIG_uid'] ? $record['_ORIG_uid'] : $record['uid']);
310 $subNode->setBackgroundColor(self::$backgroundColors[$record['uid']]);
311 $field = 'title';
312 $text = $record['title'];
313 if (self::$useNavTitle && trim($record['nav_title']) !== '') {
314 $field = 'nav_title';
315 $text = $record['nav_title'];
316 }
317 if (trim($text) === '') {
318 $visibleText = '[' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:labels.no_title', TRUE) . ']';
319 } else {
320 $visibleText = $text;
321 }
322 $visibleText = \TYPO3\CMS\Core\Utility\GeneralUtility::fixed_lgd_cs($visibleText, self::$titleLength);
323 $suffix = '';
324 if (self::$addDomainName) {
325 $domain = self::getDomainName($record['uid']);
326 $suffix = $domain !== '' ? ' [' . $domain . ']' : '';
327 }
328 $qtip = str_replace(' - ', '<br />', htmlspecialchars(\TYPO3\CMS\Backend\Utility\BackendUtility::titleAttribForPages($record, '', FALSE)));
329 $prefix = '';
330 $lockInfo = \TYPO3\CMS\Backend\Utility\BackendUtility::isRecordLocked('pages', $record['uid']);
331 if (is_array($lockInfo)) {
332 $qtip .= '<br />' . htmlspecialchars($lockInfo['msg']);
333 $prefix .= \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('status-warning-in-use', array(
334 'class' => 'typo3-pagetree-status'
335 ));
336 }
337 // Call stats information hook
338 $stat = '';
339 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['recStatInfoHooks'])) {
340 $_params = array('pages', $record['uid']);
341 $fakeThis = NULL;
342 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['recStatInfoHooks'] as $_funcRef) {
343 $stat .= \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($_funcRef, $_params, $fakeThis);
344 }
345 }
346 $prefix .= htmlspecialchars(self::$addIdAsPrefix ? '[' . $record['uid'] . '] ' : '');
347 $subNode->setEditableText($text);
348 $subNode->setText(htmlspecialchars($visibleText), $field, $prefix, htmlspecialchars($suffix) . $stat);
349 $subNode->setQTip($qtip);
350 if ($record['uid'] !== 0) {
351 $spriteIconCode = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForRecord('pages', $record);
352 } else {
353 $spriteIconCode = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('apps-pagetree-root');
354 }
355 $subNode->setSpriteIconCode($spriteIconCode);
356 if (!$subNode->canCreateNewPages() || intval($record['t3ver_state']) === 2) {
357 $subNode->setIsDropTarget(FALSE);
358 }
359 if (!$subNode->canBeEdited() || !$subNode->canBeRemoved() || intval($record['t3ver_state']) === 2) {
360 $subNode->setDraggable(FALSE);
361 }
362 return $subNode;
363 }
364
365 }
366
367
368 ?>