[TASK] Update copyright year to 2013
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / ContentObject / Menu / JavaScriptMenuContentObject.php
1 <?php
2 namespace TYPO3\CMS\Frontend\ContentObject\Menu;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 1999-2013 Kasper Skårhøj (kasperYYYY@typo3.com)
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 /**
31 * JavaScript/Selectorbox based menus
32 *
33 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
34 */
35 class JavaScriptMenuContentObject extends \TYPO3\CMS\Frontend\ContentObject\Menu\AbstractMenuContentObject {
36
37 /**
38 * Dummy. Should do nothing, because we don't use the result-array here!
39 *
40 * @return void
41 * @todo Define visibility
42 */
43 public function generate() {
44
45 }
46
47 /**
48 * Creates the HTML (mixture of a <form> and a JavaScript section) for the JavaScript menu (basically an array of selector boxes with onchange handlers)
49 *
50 * @return string The HTML code for the menu
51 * @todo Define visibility
52 */
53 public function writeMenu() {
54 if ($this->id) {
55 // Making levels:
56 $levels = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($this->mconf['levels'], 1, 5);
57 $this->levels = $levels;
58 $uniqueParam = \TYPO3\CMS\Core\Utility\GeneralUtility::shortMD5(microtime(), 5);
59 $this->JSVarName = 'eid' . $uniqueParam;
60 $this->JSMenuName = $this->mconf['menuName'] ? $this->mconf['menuName'] : 'JSmenu' . $uniqueParam;
61 $JScode = '
62 var ' . $this->JSMenuName . ' = new JSmenu(' . $levels . ', \'' . $this->JSMenuName . 'Form\');';
63 for ($a = 1; $a <= $levels; $a++) {
64 $JScode .= '
65 var ' . $this->JSVarName . $a . '=0;';
66 }
67 $JScode .= $this->generate_level($levels, 1, $this->id, $this->menuArr, $this->MP_array) . LF;
68 $GLOBALS['TSFE']->additionalHeaderData['JSMenuCode'] = '<script type="text/javascript" src="' . $GLOBALS['TSFE']->absRefPrefix . 't3lib/jsfunc.menu.js"></script>';
69 $GLOBALS['TSFE']->JSCode .= $JScode;
70 // Printing:
71 $allFormCode = '';
72 for ($a = 1; $a <= $this->levels; $a++) {
73 $formCode = '';
74 $levelConf = $this->mconf[$a . '.'];
75 $length = $levelConf['width'] ? $levelConf['width'] : 14;
76 $lenghtStr = '';
77 for ($b = 0; $b < $length; $b++) {
78 $lenghtStr .= '_';
79 }
80 $height = $levelConf['elements'] ? $levelConf['elements'] : 5;
81 $formCode .= '<select name="selector' . $a . '" onchange="' . $this->JSMenuName . '.act(' . $a . ');"' . ($levelConf['additionalParams'] ? ' ' . $levelConf['additionalParams'] : '') . '>';
82 for ($b = 0; $b < $height; $b++) {
83 $formCode .= '<option value="0">';
84 if ($b == 0) {
85 $formCode .= $lenghtStr;
86 }
87 $formCode .= '</option>';
88 }
89 $formCode .= '</select>';
90 $allFormCode .= $this->tmpl->wrap($formCode, $levelConf['wrap']);
91 }
92 $formCode = $this->tmpl->wrap($allFormCode, $this->mconf['wrap']);
93 $formCode = '<form action="" method="post" style="margin: 0 0 0 0;" name="' . $this->JSMenuName . 'Form">' . $formCode . '</form>';
94 $formCode .= '<script type="text/javascript"> /*<![CDATA[*/ ' . $this->JSMenuName . '.writeOut(1,' . $this->JSMenuName . '.openID,1); /*]]>*/ </script>';
95 return $this->tmpl->wrap($formCode, $this->mconf['wrapAfterTags']);
96 }
97 }
98
99 /**
100 * Generates a number of lines of JavaScript code for a menu level.
101 * Calls itself recursively for additional levels.
102 *
103 * @param integer $levels Number of levels to generate
104 * @param integer $count Current level being generated - and if this number is less than $levels it will call itself recursively with $count incremented
105 * @param integer $pid Page id of the starting point.
106 * @param array $menuItemArray $this->menuArr passed along
107 * @param array $MP_array Previous MP vars
108 * @return string JavaScript code lines.
109 * @access private
110 * @todo Define visibility
111 */
112 public function generate_level($levels, $count, $pid, $menuItemArray = '', $MP_array = array()) {
113 $levelConf = $this->mconf[$count . '.'];
114 // Translate PID to a mount page, if any:
115 $mount_info = $this->sys_page->getMountPointInfo($pid);
116 if (is_array($mount_info)) {
117 $MP_array[] = $mount_info['MPvar'];
118 $pid = $mount_info['mount_pid'];
119 }
120 // UIDs to ban:
121 $banUidArray = $this->getBannedUids();
122 // Initializing variables:
123 $var = $this->JSVarName;
124 $menuName = $this->JSMenuName;
125 $parent = $count == 1 ? 0 : $var . ($count - 1);
126 $prev = 0;
127 $c = 0;
128 $codeLines = '';
129 $menuItems = is_array($menuItemArray) ? $menuItemArray : $this->sys_page->getMenu($pid);
130 foreach ($menuItems as $uid => $data) {
131 // $data['_MP_PARAM'] contains MP param for overlay mount points (MPs with "substitute this page" set)
132 // if present: add param to copy of MP array (copy used for that submenu branch only)
133 $MP_array_sub = $MP_array;
134 if (array_key_exists('_MP_PARAM', $data) && $data['_MP_PARAM']) {
135 $MP_array_sub[] = $data['_MP_PARAM'];
136 }
137 // Set "&MP=" var:
138 $MP_var = implode(',', $MP_array_sub);
139 $MP_params = $MP_var ? '&MP=' . rawurlencode($MP_var) : '';
140 // If item is a spacer, $spacer is set
141 $spacer = \TYPO3\CMS\Core\Utility\GeneralUtility::inList($this->spacerIDList, $data['doktype']) ? 1 : 0;
142 // If the spacer-function is not enabled, spacers will not enter the $menuArr
143 if ($this->mconf['SPC'] || !$spacer) {
144 // Page may not be 'not_in_menu' or 'Backend User Section' + not in banned uid's
145 if (!\TYPO3\CMS\Core\Utility\GeneralUtility::inList($this->doktypeExcludeList, $data['doktype']) && (!$data['nav_hide'] || $this->conf['includeNotInMenu']) && !\TYPO3\CMS\Core\Utility\GeneralUtility::inArray($banUidArray, $uid)) {
146 if ($count < $levels) {
147 $addLines = $this->generate_level($levels, $count + 1, $data['uid'], '', $MP_array_sub);
148 } else {
149 $addLines = '';
150 }
151 $title = $data['title'];
152 $url = '';
153 $target = '';
154 if (!$addLines && !$levelConf['noLink'] || $levelConf['alwaysLink']) {
155 $LD = $this->menuTypoLink($data, $this->mconf['target'], '', '', array(), $MP_params, $this->mconf['forceTypeValue']);
156 // If access restricted pages should be shown in menus, change the link of such pages to link to a redirection page:
157 $this->changeLinksForAccessRestrictedPages($LD, $data, $this->mconf['target'], $this->mconf['forceTypeValue']);
158 $url = $GLOBALS['TSFE']->baseUrlWrap($LD['totalURL']);
159 $target = $LD['target'];
160 }
161 $codeLines .= LF . $var . $count . '=' . $menuName . '.add(' . $parent . ',' . $prev . ',0,' . \TYPO3\CMS\Core\Utility\GeneralUtility::quoteJSvalue($title, TRUE) . ',' . \TYPO3\CMS\Core\Utility\GeneralUtility::quoteJSvalue($url, TRUE) . ',' . \TYPO3\CMS\Core\Utility\GeneralUtility::quoteJSvalue($target, TRUE) . ');';
162 // If the active one should be chosen...
163 $active = $levelConf['showActive'] && $this->isActive($data['uid'], $MP_var);
164 // If the first item should be shown
165 $first = !$c && $levelConf['showFirst'];
166 // do it...
167 if ($active || $first) {
168 if ($count == 1) {
169 $codeLines .= LF . $menuName . '.openID = ' . $var . $count . ';';
170 } else {
171 $codeLines .= LF . $menuName . '.entry[' . $parent . '].openID = ' . $var . $count . ';';
172 }
173 }
174 // Add submenu...
175 $codeLines .= $addLines;
176 $prev = $var . $count;
177 $c++;
178 }
179 }
180 }
181 if ($this->mconf['firstLabelGeneral'] && !$levelConf['firstLabel']) {
182 $levelConf['firstLabel'] = $this->mconf['firstLabelGeneral'];
183 }
184 if ($levelConf['firstLabel'] && $codeLines) {
185 $codeLines .= LF . $menuName . '.defTopTitle[' . $count . '] = ' . \TYPO3\CMS\Core\Utility\GeneralUtility::quoteJSvalue($levelConf['firstLabel'], TRUE) . ';';
186 }
187 return $codeLines;
188 }
189
190 }
191
192
193 ?>