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