69c3923a09ac566da5faf825c2b8c30978557086
[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 * Visibly the page
38 *
39 * @param t3lib_tree_pagetree_Node $nodeData
40 * @return void
41 */
42 public static function visiblyNode(t3lib_tree_pagetree_Node $node) {
43 $data['pages'][$node->getWorkspaceId()]['hidden'] = 0;
44 self::processTceCmdAndDataMap(array(), $data);
45 }
46
47 /**
48 * Hide the page
49 *
50 * @param t3lib_tree_pagetree_Node $nodeData
51 * @return void
52 */
53 public static function disableNode(t3lib_tree_pagetree_Node $node) {
54 $data['pages'][$node->getWorkspaceId()]['hidden'] = 1;
55 self::processTceCmdAndDataMap(array(), $data);
56 }
57
58 /**
59 * Delete the page
60 *
61 * @param t3lib_tree_pagetree_Node $nodeData
62 * @return void
63 */
64 public static function deleteNode(t3lib_tree_pagetree_Node $node) {
65 $cmd['pages'][$node->getId()]['delete'] = 1;
66 self::processTceCmdAndDataMap($cmd);
67 }
68
69 /**
70 * Restore the page
71 *
72 * @param t3lib_tree_pagetree_Node $nodeData
73 * @param int $targetId
74 * @return void
75 */
76 public static function restoreNode(t3lib_tree_pagetree_Node $node, $targetId) {
77 $cmd['pages'][$node->getId()]['undelete'] = 1;
78 self::processTceCmdAndDataMap($cmd);
79
80 if ($node->getId() !== $targetId) {
81 self::moveNode($node, $targetId);
82 }
83 }
84
85 /**
86 * Updates the node label
87 *
88 * @param t3lib_tree_pagetree_Node $nodeData
89 * @param string $updatedLabel
90 * @return void
91 */
92 public static function updateNodeLabel(t3lib_tree_pagetree_Node $node, $updatedLabel) {
93 $data['pages'][$node->getWorkspaceId()][$node->getTextSourceField()] = $updatedLabel;
94 self::processTceCmdAndDataMap(array(), $data);
95 }
96
97 /**
98 * Copies a page and returns the id of the new page
99 *
100 * Node: Use a negative target id to specify a sibling target else the parent is used
101 *
102 * @param t3lib_tree_pagetree_Node $sourceNode
103 * @param int $targetId
104 * @return int
105 */
106 public static function copyNode(t3lib_tree_pagetree_Node $sourceNode, $targetId) {
107 $cmd['pages'][$sourceNode->getId()]['copy'] = $targetId;
108 $returnValue = self::processTceCmdAndDataMap($cmd);
109
110 return $returnValue['pages'][$sourceNode->getId()];
111 }
112
113 /**
114 * Moves a page
115 *
116 * Node: Use a negative target id to specify a sibling target else the parent is used
117 *
118 * @param t3lib_tree_pagetree_Node $sourceNode
119 * @param int $targetId
120 * @return void
121 */
122 public static function moveNode(t3lib_tree_pagetree_Node $sourceNode, $targetId) {
123 $cmd['pages'][$sourceNode->getId()]['move'] = $targetId;
124 self::processTceCmdAndDataMap($cmd);
125 }
126
127 /**
128 * Creates a page of the given doktype and returns the id of the created page
129 *
130 * @param t3lib_tree_pagetree_Node $parentNode
131 * @param int $targetId
132 * @param int $pageType
133 * @return int
134 */
135 public static function createNode(t3lib_tree_pagetree_Node $parentNode, $targetId, $pageType) {
136 $placeholder = 'NEW12345';
137 $pid = $parentNode->getWorkspaceId();
138
139 // Use page TsConfig as default page initialization
140 $pageTs = t3lib_BEfunc::getPagesTSconfig($pid);
141 if (array_key_exists('TCAdefaults.', $pageTs) && array_key_exists('pages.', $pageTs['TCAdefaults.'])) {
142 $data['pages'][$placeholder] = $pageTs['TCAdefaults.']['pages.'];
143 } else {
144 $data['pages'][$placeholder] = array();
145 }
146
147 $data['pages'][$placeholder]['pid'] = $pid;
148 $data['pages'][$placeholder]['doktype'] = $pageType;
149 $data['pages'][$placeholder]['title'] = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:tree.defaultPageTitle', TRUE);
150
151 $newPageId = self::processTceCmdAndDataMap(array(), $data);
152 $node = self::getNode($newPageId[$placeholder]);
153
154 if ($parentNode->getWorkspaceId() !== $targetId) {
155 self::moveNode($node, $targetId);
156 }
157
158 return $newPageId[$placeholder];
159 }
160
161 /**
162 * Process TCEMAIN commands and data maps
163 *
164 * Command Map:
165 * Used for moving, recover, remove and some more operations.
166 *
167 * Data Map:
168 * Used for creating and updating records,
169 *
170 * This API contains all necessary access checks.
171 *
172 * @param array $cmd
173 * @param array $data
174 * @throws Exception if an error happened while the TCE processing
175 * @return array
176 */
177 protected static function processTceCmdAndDataMap(array $cmd, array $data = array()) {
178 /** @var $tce t3lib_TCEmain */
179 $tce = t3lib_div::makeInstance('t3lib_TCEmain');
180 $tce->stripslashes_values = 0;
181 $tce->start($data, $cmd);
182 $tce->copyTree = t3lib_utility_Math::forceIntegerInRange($GLOBALS['BE_USER']->uc['copyLevels'], 0, 100);
183
184 if (count($cmd)) {
185 $tce->process_cmdmap();
186 $returnValues = $tce->copyMappingArray_merged;
187 } elseif (count($data)) {
188 $tce->process_datamap();
189 $returnValues = $tce->substNEWwithIDs;
190 }
191
192 // check errors
193 if (count($tce->errorLog)) {
194 throw new Exception(implode(chr(10), $tce->errorLog));
195 }
196
197 return $returnValues;
198 }
199
200 /**
201 * Returns a node from the given node id
202 *
203 * @param int $nodeId
204 * @param boolean $unsetMovePointers
205 * @return t3lib_tree_pagetree_Node
206 */
207 public static function getNode($nodeId, $unsetMovePointers = TRUE) {
208 $record = self::getNodeRecord($nodeId, $unsetMovePointers);
209 return self::getNewNode($record);
210 }
211
212 /**
213 * Returns the mount point path for a temporary mount or the given id
214 *
215 * @static
216 * @param int $uid
217 * @return void
218 */
219 public static function getMountPointPath($uid = -1) {
220 if ($uid === -1) {
221 $uid = intval($GLOBALS['BE_USER']->uc['pageTree_temporaryMountPoint']);
222 }
223
224 if ($uid <= 0) {
225 return '';
226 }
227
228 $useNavTitle = $GLOBALS['BE_USER']->getTSConfigVal('options.pageTree.showNavTitle');
229 $rootline = array_reverse(t3lib_BEfunc::BEgetRootLine($uid));
230 array_shift($rootline);
231
232 $path = array();
233 foreach ($rootline as $rootlineElement) {
234 $record = t3lib_tree_pagetree_Commands::getNodeRecord($rootlineElement['uid']);
235
236 $text = $record['title'];
237 if ($useNavTitle && trim($record['nav_title']) !== '') {
238 $text = $record['nav_title'];
239 }
240
241 $path[] = $text;
242 }
243
244 return htmlspecialchars('/' . implode('/', $path));
245 }
246
247 /**
248 * Returns a node record from a given id
249 *
250 * @param int $nodeId
251 * @param boolean $unsetMovePointers
252 * @return array
253 */
254 public static function getNodeRecord($nodeId, $unsetMovePointers = TRUE) {
255 $record = t3lib_BEfunc::getRecordWSOL('pages', $nodeId, '*', '', TRUE, $unsetMovePointers);
256 return $record;
257 }
258
259 /**
260 * Returns the first configured domain name for a page
261 *
262 * @static
263 * @param integer $uid
264 * @return string
265 */
266 public static function getDomainName($uid) {
267 $whereClause = $GLOBALS['TYPO3_DB']->quoteStr(
268 'pid=' . intval($uid) . t3lib_BEfunc::deleteClause('sys_domain') .
269 t3lib_BEfunc::BEenableFields('sys_domain'),
270 'sys_domain'
271 );
272
273 $domain = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
274 'domainName',
275 'sys_domain',
276 $whereClause,
277 '',
278 'sorting'
279 );
280
281 return htmlspecialchars($domain['domainName']);
282 }
283
284 /**
285 * Creates a node with the given record information's
286 *
287 * @param array $record
288 * @return t3lib_tree_pagetree_Node
289 */
290 public static function getNewNode($record, $mountPoint = 0) {
291 $useNavTitle = $GLOBALS['BE_USER']->getTSConfigVal('options.pageTree.showNavTitle');
292 $addIdAsPrefix = $GLOBALS['BE_USER']->getTSConfigVal('options.pageTree.showPageIdWithTitle');
293 $addDomainName = $GLOBALS['BE_USER']->getTSConfigVal('options.pageTree.showDomainNameWithTitle');
294 $titleLength = intval($GLOBALS['BE_USER']->uc['titleLen']);
295
296 /** @var $subNode t3lib_tree_pagetree_Node */
297 $subNode = t3lib_div::makeInstance('t3lib_tree_pagetree_Node');
298 $subNode->setRecord($record);
299 $subNode->setCls($record['_CSSCLASS']);
300 $subNode->setType('pages');
301
302 $subNode->setId($record['uid']);
303 $subNode->setMountPoint($mountPoint);
304 $subNode->setWorkspaceId(($record['_ORIG_uid'] ? $record['_ORIG_uid'] : $record['uid']));
305
306 $field = 'title';
307 $text = $record['title'];
308 if ($useNavTitle && trim($record['nav_title']) !== '') {
309 $field = 'nav_title';
310 $text = $record['nav_title'];
311 }
312
313 if (trim($text) === '') {
314 $visibleText = '[' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xml:labels.no_title', TRUE) . ']';
315 } else {
316 $visibleText = $text;
317 }
318 $visibleText = t3lib_div::fixed_lgd_cs($visibleText, $titleLength);
319
320 $suffix = '';
321 if ($addDomainName) {
322 $domain = self::getDomainName($record['uid']);
323 $suffix = ($domain !== '' ? ' [' . $domain . ']' : '');
324 }
325
326 $qtip = str_replace(' - ', '<br />', htmlspecialchars(
327 t3lib_BEfunc::titleAttribForPages($record, '', FALSE))
328 );
329
330 $prefix = '';
331 $lockInfo = t3lib_BEfunc::isRecordLocked('pages', $record['uid']);
332 if (is_array($lockInfo)) {
333 $qtip .= '<br />' . htmlspecialchars($lockInfo['msg']);
334 $prefix .= t3lib_iconWorks::getSpriteIcon(
335 'status-warning-in-use',
336 array(
337 'class' => 'typo3-pagetree-status'
338 )
339 );
340 }
341
342 // Call stats information hook
343 $stat = '';
344 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['recStatInfoHooks'])) {
345 $_params = array('pages', $record['uid']);
346 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['recStatInfoHooks'] as $_funcRef) {
347 $stat .= t3lib_div::callUserFunction($_funcRef, $_params, $this);
348 }
349 }
350
351 $prefix .= htmlspecialchars($addIdAsPrefix ? '[' . $record['uid'] . '] ' : '');
352 $subNode->setEditableText($text);
353 $subNode->setText(
354 htmlspecialchars($visibleText),
355 $field,
356 $prefix,
357 htmlspecialchars($suffix) . $stat
358 );
359
360 $subNode->setQTip($qtip);
361
362 if ($record['uid'] !== 0) {
363 $spriteIconCode = t3lib_iconWorks::getSpriteIconForRecord('pages', $record);
364 } else {
365 $spriteIconCode = t3lib_iconWorks::getSpriteIcon('apps-pagetree-root');
366 }
367 $subNode->setSpriteIconCode($spriteIconCode);
368
369 if (!$subNode->canCreateNewPages() || intval($record['t3ver_state']) === 2) {
370 $subNode->setIsDropTarget(FALSE);
371 }
372
373 if (!$subNode->canBeEdited() || !$subNode->canBeRemoved() || intval($record['t3ver_state']) === 2) {
374 $subNode->setDraggable(FALSE);
375 }
376
377 return $subNode;
378 }
379 }
380
381 ?>