cc40c30522c30fc6d09ce9088a270bd88c8e02e3
[Packages/TYPO3.CMS.git] / t3lib / tree / extdirect / class.t3lib_tree_extdirect_node.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 * Node for the usage with ExtDirect and ExtJS
30 *
31 * @author Stefan Galinski <stefan.galinski@gmail.com>
32 * @package TYPO3
33 * @subpackage t3lib
34 */
35 class t3lib_tree_extdirect_Node extends t3lib_tree_Node {
36 /**
37 * Node type
38 *
39 * @var string
40 */
41 protected $type = '';
42
43 /**
44 * Leaf Node Indicator
45 *
46 * @var bool
47 */
48 protected $leaf = TRUE;
49
50 /**
51 * Indicator if the node is expanded
52 *
53 * @var bool
54 */
55 protected $expanded = FALSE;
56
57 /**
58 * Indicator if the node can be expanded
59 *
60 * @var bool
61 */
62 protected $expandable = FALSE;
63
64 /**
65 * Indicator if the node is draggable
66 *
67 * @var bool
68 */
69 protected $draggable = TRUE;
70
71 /**
72 * Indicator if the node is allowed as a drop target
73 *
74 * @var bool
75 */
76 protected $isDropTarget = TRUE;
77
78 /**
79 * Label
80 *
81 * @var string
82 */
83 protected $text = '';
84
85 /**
86 * Editable Label text
87 *
88 * @var string
89 */
90 protected $editableText = '';
91
92 /**
93 * Prefix text of the label
94 *
95 * @var string
96 */
97 protected $prefix = '';
98
99 /**
100 * Suffix text of the label
101 *
102 * @var string
103 */
104 protected $suffix = '';
105
106 /**
107 * CSS Class
108 *
109 * @var string
110 */
111 protected $cls = '';
112
113 /**
114 * Quick Tip
115 *
116 * @var string
117 */
118 protected $qtip = '';
119
120 /**
121 * Sprite Icon HTML
122 *
123 * @var string
124 */
125 protected $spriteIconCode = '';
126
127 /**
128 * Text source field (title, nav_title, ...)
129 *
130 * @var string
131 */
132 protected $t3TextSourceField = '';
133
134 /**
135 * Indicator if the copy mode is activated
136 *
137 * @var bool
138 */
139 protected $t3InCopyMode = FALSE;
140
141 /**
142 * Indicator if the cut mode is activated
143 *
144 * @var bool
145 */
146 protected $t3InCutMode = FALSE;
147
148 /**
149 * Database record (not serialized or merged into the result array!)
150 *
151 * @var array
152 */
153 protected $record = array();
154
155 /**
156 * Context Info
157 *
158 * @var array
159 */
160 protected $contextInfo = array();
161
162 /**
163 * Indicator if the label is editable
164 *
165 * @var bool
166 */
167 protected $labelIsEditable = TRUE;
168
169 /**
170 * Indicator if the node can have children's
171 *
172 * @var bool
173 */
174 protected $allowChildren = TRUE;
175
176 /**
177 * Set's the node type
178 *
179 * @param string $type
180 * @return void
181 */
182 public function setType($type) {
183 $this->type = $type;
184 }
185
186 /**
187 * Returns the node type
188 *
189 * @return string
190 */
191 public function getType() {
192 return $this->type;
193 }
194
195 /**
196 * Sets the leaf node indicator
197 *
198 * @param bool $isLeaf
199 * @return void
200 */
201 public function setLeaf($isLeaf) {
202 $this->leaf = ($isLeaf == TRUE);
203 }
204
205 /**
206 * Returns if the node is a leaf node
207 *
208 * @return bool
209 */
210 public function isLeafNode() {
211 return $this->leaf;
212 }
213
214 /**
215 * Sets the expandable indicator
216 *
217 * @param bool $expandable
218 * @return void
219 */
220 public function setExpandable($expandable) {
221 $this->expandable = ($expandable == TRUE);
222 }
223
224 /**
225 * Returns the expandable indicator
226 *
227 * @return bool
228 */
229 public function isExpandable() {
230 return $this->expandable;
231 }
232
233 /**
234 * Sets the expanded indicator
235 *
236 * @param bool $expanded
237 * @return void
238 */
239 public function setExpanded($expanded) {
240 $this->expanded = ($expanded == TRUE);
241 }
242
243 /**
244 * Returns the expanded indicator
245 *
246 * @return bool
247 */
248 public function isExpanded() {
249 if ($this->isLeafNode()) {
250 return TRUE;
251 }
252
253 return $this->expanded;
254 }
255
256 /**
257 * Sets the draggable indicator
258 *
259 * @param bool $draggable
260 * @return void
261 */
262 public function setDraggable($draggable) {
263 $this->draggable = ($draggable == TRUE);
264 }
265
266 /**
267 * Returns the draggable indicator
268 *
269 * @return bool
270 */
271 public function isDraggable() {
272 return $this->draggable;
273 }
274
275 /**
276 * Sets the indicator if the node can be a drop target
277 *
278 * @param bool $isDropTarget
279 * @return void
280 */
281 public function setIsDropTarget($isDropTarget) {
282 $this->isDropTarget = ($isDropTarget == TRUE);
283 }
284
285 /**
286 * Returns the indicator if the node is a drop target
287 *
288 * @return bool
289 */
290 public function isDropTarget() {
291 return $this->isDropTarget;
292 }
293
294 /**
295 * Sets the label of the node with the source field and the prefix
296 *
297 * @param string $text
298 * @param string $textSourceField
299 * @param string $prefix
300 * @param string $suffix
301 * @return void
302 */
303 public function setText($text, $textSourceField = 'title', $prefix = '', $suffix = '') {
304 $this->text = $text;
305 $this->t3TextSourceField = $textSourceField;
306 $this->prefix = $prefix;
307 $this->suffix = $suffix;
308 }
309
310 /**
311 * Returns the label
312 *
313 * @return string
314 */
315 public function getText() {
316 return $this->text;
317 }
318
319 /**
320 * Sets the editable text
321 *
322 * @param string $editableText
323 * @return void
324 */
325 public function setEditableText($editableText) {
326 $this->editableText = $editableText;
327 }
328
329 /**
330 * Returns the editable text
331 *
332 * @return string
333 */
334 public function getEditableText() {
335 return $this->editableText;
336 }
337
338 /**
339 * Returns the source field of the label
340 *
341 * @return string
342 */
343 public function getTextSourceField() {
344 return $this->t3TextSourceField;
345 }
346
347 /**
348 * Sets the paste copy indicator
349 *
350 * @param boolean $inCopyMode
351 * @return void
352 */
353 public function setInCopyMode($inCopyMode) {
354 $this->t3InCopyMode = ($inCopyMode == TRUE);
355 }
356
357 /**
358 * Returns the copy mode indicator
359 *
360 * @return bool
361 */
362 public function isInCopyMode() {
363 return $this->t3InCopyMode;
364 }
365
366 /**
367 * Sets the paste cut indicator
368 *
369 * @param boolean $inCutMode
370 * @return void
371 */
372 public function setInCutMode($inCutMode) {
373 $this->t3InCutMode = ($inCutMode == TRUE);
374 }
375
376 /**
377 * Returns the cut mode indicator
378 *
379 * @return bool
380 */
381 public function isInCutMode() {
382 return $this->t3InCutMode;
383 }
384
385 /**
386 * Returns the prefix text of the label
387 *
388 * @return string
389 */
390 public function getPrefix() {
391 return $this->prefix;
392 }
393
394 /**
395 * Returns the suffix text of the label
396 *
397 * @return string
398 */
399 public function getSuffix() {
400 return $this->suffix;
401 }
402
403 /**
404 * Sets the css class(es)
405 *
406 * @param string $class
407 * @return void
408 */
409 public function setCls($class) {
410 $this->cls = $class;
411 }
412
413 /**
414 * Returns the css class(es)
415 *
416 * @return string
417 */
418 public function getCls() {
419 return $this->cls;
420 }
421
422 /**
423 * Sets the quick tip
424 *
425 * @param string $qtip
426 * @return void
427 */
428 public function setQTip($qtip) {
429 $this->qtip = $qtip;
430 }
431
432 /**
433 * Returns the quick tip
434 *
435 * @return string
436 */
437 public function getQTip() {
438 return $this->qtip;
439 }
440
441 /**
442 * Sets the sprite icon code
443 *
444 * @param string $spriteIcon
445 * @return void
446 */
447 public function setSpriteIconCode($spriteIcon) {
448 $this->spriteIconCode = $spriteIcon;
449 }
450
451 /**
452 * Returns the sprite icon code
453 *
454 * @return string
455 */
456 public function getSpriteIconCode() {
457 return $this->spriteIconCode;
458 }
459
460 /**
461 * Sets the indicator if the label is editable
462 *
463 * @param bool $labelIsEditable
464 * @return void
465 */
466 public function setLabelIsEditable($labelIsEditable) {
467 $this->labelIsEditable = ($labelIsEditable == TRUE);
468 }
469
470 /**
471 * Returns the editable label indicator
472 *
473 * @return bool
474 */
475 public function isLabelEditable() {
476 return $this->labelIsEditable;
477 }
478
479 /**
480 * Sets the database record array
481 *
482 * @param array $record
483 * @return void
484 */
485 public function setRecord($record) {
486 $this->record = (array) $record;
487 }
488
489 /**
490 * Returns the database record array
491 *
492 * @return array
493 */
494 public function getRecord() {
495 return $this->record;
496 }
497
498 /**
499 * Sets the context info
500 *
501 * @param array $contextInfo
502 * @return void
503 */
504 public function setContextInfo($contextInfo) {
505 $this->contextInfo = (array) $contextInfo;
506 }
507
508 /**
509 * Returns the context info
510 *
511 * @return array
512 */
513 public function getContextInfo() {
514 return (array) $this->contextInfo;
515 }
516
517 /**
518 * Sets the child nodes collection
519 *
520 * @param t3lib_tree_NodeCollection $childNodes
521 * @return void
522 */
523 public function setChildNodes(t3lib_tree_NodeCollection $childNodes) {
524 parent::setChildNodes($childNodes);
525
526 if ($childNodes->count()) {
527 $this->setLeaf(FALSE);
528 }
529 }
530
531 /**
532 * Sets the indicator if the node can have child nodes
533 *
534 * @param boolean $allowChildren
535 * @return void
536 */
537 public function setAllowChildren($allowChildren) {
538 $this->allowChildren = ($allowChildren == TRUE);
539 }
540
541 /**
542 * Checks if the node can have child nodes
543 *
544 * @return bool
545 */
546 public function canHaveChildren() {
547 return $this->allowChildren;
548 }
549
550 /**
551 * Returns the node in an array representation that can be used for serialization
552 *
553 * @return array
554 */
555 public function toArray() {
556 $arrayRepresentation = array(
557 'serializeClassName' => get_class($this),
558 'id' => $this->getId(),
559 'type' => $this->getType(),
560 'editableText' => $this->getEditableText(),
561 'text' => $this->getPrefix() . $this->getText() . $this->getSuffix(),
562 'cls' => $this->getCls(),
563 'prefix' => $this->getPrefix(),
564 'suffix' => $this->getSuffix(),
565 'qtip' => $this->getQTip(),
566 'expanded' => $this->isExpanded(),
567 'expandable' => $this->isExpandable(),
568 'draggable' => $this->isDraggable(),
569 'isTarget' => $this->isDropTarget(),
570 'spriteIconCode' => $this->getSpriteIconCode(),
571 't3TextSourceField' => $this->getTextSourceField(),
572 't3InCopyMode' => $this->isInCopyMode(),
573 't3InCutMode' => $this->isInCutMode(),
574 't3ContextInfo' => $this->getContextInfo(),
575 'editable' => $this->isLabelEditable(),
576 'allowChildren' => $this->canHaveChildren(),
577 );
578
579 // only set the leaf attribute if the node has children's,
580 // otherwise you cannot add child's to real leaf nodes
581 if (!$this->isLeafNode()) {
582 $arrayRepresentation['leaf'] = FALSE;
583 }
584
585 // Suhosin(?) or some other strange environment thingy prevents
586 // the direct copy of an array into an index of the same array
587 $copy = $arrayRepresentation;
588 $arrayRepresentation['nodeData'] = $copy;
589
590 if ($this->hasChildNodes()) {
591 $arrayRepresentation['children'] = $this->childNodes->toArray();
592 }
593
594 return $arrayRepresentation;
595 }
596
597 /**
598 * Sets data of the node by a given data array
599 *
600 * @param array $data
601 * @return void
602 */
603 public function dataFromArray($data) {
604 parent::dataFromArray($data);
605
606 $this->setType($data['type']);
607 $this->setText($data['label'], $data['t3TextSourceField'], $data['prefix'], $data['suffix']);
608 $this->setEditableText($data['editableText']);
609 $this->setCls($data['cls']);
610 $this->setQTip($data['qtip']);
611 $this->setExpanded($data['expanded']);
612 $this->setExpandable($data['expandable']);
613 $this->setDraggable($data['draggable']);
614 $this->setIsDropTarget($data['isTarget']);
615 $this->setSpriteIconCode($data['spriteIconCode']);
616 $this->setInCopyMode($data['t3InCopyMode']);
617 $this->setInCutMode($data['t3InCutMode']);
618 $this->setContextInfo($data['t3ContextInfo']);
619 $this->setLabelIsEditable($data['editable']);
620 $this->setAllowChildren($data['allowChildren']);
621
622 // only set the leaf attribute if it's applied
623 // otherwise you cannot insert nodes into this one
624 if (isset($data['leaf'])) {
625 $this->setLeaf(FALSE);
626 }
627 }
628 }
629
630 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/tree/extdirect/class.t3lib_tree_extdirect_node.php'])) {
631 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/tree/extdirect/class.t3lib_tree_extdirect_node.php']);
632 }
633
634 ?>