Merged new PageTree (version for TYPO3 4.5 Beta 3)
[Packages/TYPO3.CMS.git] / typo3 / sysext / pagetree / classes / class.tx_pagetree_commands.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 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 tx_pagetree
34 */
35 final class tx_pagetree_Commands {
36 /**
37 * Visibly the page
38 *
39 * @param tx_pagetree_Node $nodeData
40 * @return void
41 */
42 public static function visiblyNode(tx_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 tx_pagetree_Node $nodeData
51 * @return void
52 */
53 public static function disableNode(tx_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 tx_pagetree_Node $nodeData
62 * @return void
63 */
64 public static function deleteNode(tx_pagetree_Node $node) {
65 $cmd['pages'][$node->getId()]['delete'] = 1;
66 self::processTceCmdAndDataMap($cmd);
67 }
68
69 /**
70 * Restore the page
71 *
72 * @param tx_pagetree_Node $nodeData
73 * @param int $targetId
74 * @return void
75 */
76 public static function restoreNode(tx_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 tx_pagetree_Node $nodeData
89 * @param string $updatedLabel
90 * @return void
91 */
92 public static function updateNodeLabel(tx_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 tx_pagetree_Node $sourceNode
103 * @param int $targetId
104 * @return int
105 */
106 public static function copyNode(tx_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 tx_pagetree_Node $sourceNode
119 * @param int $targetId
120 * @return void
121 */
122 public static function moveNode(tx_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 tx_pagetree_Node $parentNode
131 * @param int $targetId
132 * @param int $pageType
133 * @return int
134 */
135 public static function createNode(tx_pagetree_Node $parentNode, $targetId, $pageType) {
136 $placeholder = 'NEW12345';
137 $data['pages'][$placeholder] = array(
138 'pid' => $parentNode->getWorkspaceId(),
139 'doktype' => $pageType,
140 'title' => $GLOBALS['LANG']->sL('LLL:EXT:pagetree/locallang_pagetree.xml:defaultTitle', TRUE),
141 );
142 $newPageId = self::processTceCmdAndDataMap(array(), $data);
143 $node = self::getNode($newPageId[$placeholder]);
144
145 if ($parentNode->getWorkspaceId() !== $targetId) {
146 self::moveNode($node, $targetId);
147 }
148
149 return $newPageId[$placeholder];
150 }
151
152 /**
153 * Process TCEMAIN commands and data maps
154 *
155 * Command Map:
156 * Used for moving, recover, remove and some more operations.
157 *
158 * Data Map:
159 * Used for creating and updating records,
160 *
161 * This API contains all necessary access checks.
162 *
163 * @param array $cmd
164 * @param array $data
165 * @throws Exception if an error happened while the TCE processing
166 * @return array
167 */
168 protected static function processTceCmdAndDataMap(array $cmd, array $data = array()) {
169 /** @var $tce t3lib_TCEmain */
170 $tce = t3lib_div::makeInstance('t3lib_TCEmain');
171 $tce->stripslashes_values = 0;
172 $tce->start($data, $cmd);
173 $tce->copyTree = t3lib_div::intInRange($GLOBALS['BE_USER']->uc['copyLevels'], 0, 100);
174
175 if (count($cmd)) {
176 $tce->process_cmdmap();
177 $returnValues = $tce->copyMappingArray_merged;
178 } elseif (count($data)) {
179 $tce->process_datamap();
180 $returnValues = $tce->substNEWwithIDs;
181 }
182
183 // check errors
184 if (count($tce->errorLog)) {
185 throw new Exception(implode(chr(10), $tce->errorLog));
186 }
187
188 return $returnValues;
189 }
190
191 /**
192 * Returns a node from the given node id
193 *
194 * @param int $nodeId
195 * @param boolean $unsetMovePointers
196 * @return tx_pagetree_Node
197 */
198 public static function getNode($nodeId, $unsetMovePointers = TRUE) {
199 $record = self::getNodeRecord($nodeId, $unsetMovePointers);
200 return self::getNewNode($record);
201 }
202
203 /**
204 * Returns the mount point path
205 *
206 * @static
207 * @return void
208 */
209 public static function getMountPointPath() {
210 $temporaryMountPoint = intval($GLOBALS['BE_USER']->uc['pageTree_temporaryMountPoint']);
211 if (!$temporaryMountPoint) {
212 return '';
213 }
214
215 $useNavTitle = $GLOBALS['BE_USER']->getTSConfigVal('options.pageTree.showNavTitle');
216 $rootline = array_reverse(t3lib_BEfunc::BEgetRootLine($temporaryMountPoint));
217 array_shift($rootline);
218
219 $path = array();
220 foreach ($rootline as $rootlineElement) {
221 $record = tx_pagetree_Commands::getNodeRecord($rootlineElement['uid']);
222
223 $text = $record['title'];
224 if ($useNavTitle && trim($record['nav_title']) !== '') {
225 $text = $record['nav_title'];
226 }
227
228 $path[] = $text;
229 }
230
231 return '/' . implode('/', $path);
232 }
233
234 /**
235 * Returns a node record from a given id
236 *
237 * @param int $nodeId
238 * @param boolean $unsetMovePointers
239 * @return array
240 */
241 public static function getNodeRecord($nodeId, $unsetMovePointers = TRUE) {
242 $record = t3lib_BEfunc::getRecordWSOL('pages', $nodeId, '*', '', TRUE, $unsetMovePointers);
243 return $record;
244 }
245
246 /**
247 * Returns the first configured domain name for a page
248 *
249 * @static
250 * @param integer $uid
251 * @return string
252 */
253 public static function getDomainName($uid) {
254 $whereClause = $GLOBALS['TYPO3_DB']->quoteStr(
255 'pid=' . intval($uid) . t3lib_BEfunc::deleteClause('sys_domain') .
256 t3lib_BEfunc::BEenableFields('sys_domain'),
257 'sys_domain'
258 );
259
260 $domain = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
261 'domainName',
262 'sys_domain',
263 $whereClause,
264 '',
265 'sorting'
266 );
267
268 return htmlspecialchars($domain['domainName']);
269 }
270
271 /**
272 * Creates a node with the given record information's
273 *
274 * @param array $record
275 * @return tx_pagetree_Node
276 */
277 public static function getNewNode($record, $mountPoint = 0) {
278 $useNavTitle = $GLOBALS['BE_USER']->getTSConfigVal('options.pageTree.showNavTitle');
279 $addIdAsPrefix = $GLOBALS['BE_USER']->getTSConfigVal('options.pageTree.showPageIdWithTitle');
280 $addDomainName = $GLOBALS['BE_USER']->getTSConfigVal('options.pageTree.showDomainNameWithTitle');
281
282 /** @var $subNode tx_pagetree_Node */
283 $subNode = t3lib_div::makeInstance('tx_pagetree_Node');
284 $subNode->setRecord($record);
285 $subNode->setCls($record['_CSSCLASS']);
286 $subNode->setQTip('ID: ' . $record['uid']);
287 $subNode->setType('pages');
288
289 $subNode->setId($record['uid']);
290 $subNode->setMountPoint($mountPoint);
291 $subNode->setWorkspaceId(($record['_ORIG_uid'] ? $record['_ORIG_uid'] : $record['uid']));
292
293 $field = 'title';
294 $text = $record['title'];
295 if ($useNavTitle && trim($record['nav_title']) !== '') {
296 $field = 'nav_title';
297 $text = $record['nav_title'];
298 }
299
300 $suffix = '';
301 if ($addDomainName) {
302 $domain = self::getDomainName($record['uid']);
303 $suffix = ($domain !== '' ? ' [' . $domain . ']' : '');
304 }
305
306 $prefix = ($addIdAsPrefix ? '[' . $record['uid'] . '] ' : '');
307 $subNode->setText($text, $field, $prefix, $suffix);
308 $subNode->setEditableText($text);
309
310 if ($record['uid'] !== 0) {
311 $spriteIconCode = t3lib_iconWorks::getSpriteIconForRecord('pages', $record);
312 } else {
313 $spriteIconCode = t3lib_iconWorks::getSpriteIcon('apps-pagetree-root');
314 }
315 $subNode->setSpriteIconCode($spriteIconCode);
316
317 if (!$subNode->canCreateNewPages() || intval($record['t3ver_state']) === 2) {
318 $subNode->setIsDropTarget(FALSE);
319 }
320
321 if (!$subNode->canBeEdited() || !$subNode->canBeRemoved() || intval($record['t3ver_state']) === 2) {
322 $subNode->setDraggable(FALSE);
323 }
324
325 return $subNode;
326 }
327 }
328
329 ?>