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