[TASK] Optimize invocation of cObj::checkIf
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / ContentObject / Menu / ImageMenuContentObject.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 /**
18 * ImageMap based menus
19 *
20 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
21 */
22 class ImageMenuContentObject extends \TYPO3\CMS\Frontend\ContentObject\Menu\AbstractMenuContentObject {
23
24 /**
25 * Calls procesItemStates() so that the common configuration for the menu items are resolved into individual configuration per item.
26 * Calls makeImageMap() to generate the image map image-file
27 *
28 * @return void
29 * @see AbstractMenuContentObject::procesItemStates(), makeImageMap()
30 * @todo Define visibility
31 */
32 public function generate() {
33 $splitCount = count($this->menuArr);
34 if ($splitCount) {
35 list($NOconf) = $this->procesItemStates($splitCount);
36 }
37 if ($this->mconf['debugItemConf']) {
38 echo '<h3>$NOconf:</h3>';
39 debug($NOconf);
40 }
41 $this->makeImageMap($NOconf);
42 }
43
44 /**
45 * Will traverse input array with configuration per-item and create corresponding GIF files for the menu.
46 * The data of the files are stored in $this->result
47 *
48 * @param array $conf Array with configuration for each item.
49 * @return void
50 * @access private
51 * @see generate()
52 * @todo Define visibility
53 */
54 public function makeImageMap($conf) {
55 if (!is_array($conf)) {
56 $conf = array();
57 }
58 if (is_array($this->mconf['main.'])) {
59 $gifCreator = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\Imaging\\GifBuilder');
60 $gifCreator->init();
61 $itemsConf = $conf;
62 $conf = $this->mconf['main.'];
63 if (is_array($conf)) {
64 $gifObjCount = 0;
65 $sKeyArray = \TYPO3\CMS\Core\TypoScript\TemplateService::sortedKeyList($conf);
66 $gifObjCount = (int)end($sKeyArray);
67 $lastOriginal = $gifObjCount;
68 // Now we add graphical objects to the gifbuilder-setup
69 $waArr = array();
70 foreach ($itemsConf as $key => $val) {
71 if (is_array($val)) {
72 $gifObjCount++;
73 $waArr[$key]['free'] = $gifObjCount;
74 $sKeyArray = \TYPO3\CMS\Core\TypoScript\TemplateService::sortedKeyList($val);
75 foreach ($sKeyArray as $theKey) {
76 $theValue = $val[$theKey];
77 if ((int)$theKey && ($theValArr = $val[$theKey . '.'])) {
78 $cObjData = $this->menuArr[$key] ?: array();
79 $gifObjCount++;
80 if ($theValue == 'TEXT') {
81 $waArr[$key]['textNum'] = $gifObjCount;
82 $gifCreator->data = $cObjData;
83 $theValArr = $gifCreator->checkTextObj($theValArr);
84 // if this is not done it seems that imageMaps will be rendered wrong!!
85 unset($theValArr['text.']);
86 // check links
87 $LD = $this->menuTypoLink($this->menuArr[$key], $this->mconf['target'], '', '', array(), '', $this->mconf['forceTypeValue']);
88 // If access restricted pages should be shown in menus, change the link of such pages to link to a redirection page:
89 $this->changeLinksForAccessRestrictedPages($LD, $this->menuArr[$key], $this->mconf['target'], $this->mconf['forceTypeValue']);
90 // Overriding URL / Target if set to do so:
91 if ($this->menuArr[$key]['_OVERRIDE_HREF']) {
92 $LD['totalURL'] = $this->menuArr[$key]['_OVERRIDE_HREF'];
93 if ($this->menuArr[$key]['_OVERRIDE_TARGET']) {
94 $LD['target'] = $this->menuArr[$key]['_OVERRIDE_TARGET'];
95 }
96 }
97 // Setting target/url for Image Map:
98 if ($theValArr['imgMap.']['url'] == '') {
99 $theValArr['imgMap.']['url'] = $LD['totalURL'];
100 }
101 if ($theValArr['imgMap.']['target'] == '') {
102 $theValArr['imgMap.']['target'] = $LD['target'];
103 }
104 if (is_array($theValArr['imgMap.']['altText.'])) {
105 $cObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer');
106 $cObj->start($cObjData, 'pages');
107 if (isset($theValArr['imgMap.']['altText.'])) {
108 $theValArr['imgMap.']['altText'] = $cObj->stdWrap($theValArr['imgMap.']['altText'], $theValArr['imgMap.']['altText.']);
109 }
110 unset($theValArr['imgMap.']['altText.']);
111 }
112 if (is_array($theValArr['imgMap.']['titleText.'])) {
113 $cObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer');
114 $cObj->start($cObjData, 'pages');
115 if (isset($theValArr['imgMap.']['titleText.'])) {
116 $theValArr['imgMap.']['titleText'] = $cObj->stdWrap($theValArr['imgMap.']['titleText'], $theValArr['imgMap.']['titleText.']);
117 }
118 unset($theValArr['imgMap.']['titleText.']);
119 }
120 }
121 // This code goes one level in if the object is an image. If 'file' and/or 'mask' appears to be GIFBUILDER-objects, they are both searched for TEXT objects, and if a textobj is found, it's checked with the currently loaded record!!
122 if ($theValue == 'IMAGE') {
123 if ($theValArr['file'] == 'GIFBUILDER') {
124 $temp_sKeyArray = \TYPO3\CMS\Core\TypoScript\TemplateService::sortedKeyList($theValArr['file.']);
125 foreach ($temp_sKeyArray as $temp_theKey) {
126 if ($theValArr['mask.'][$temp_theKey] == 'TEXT') {
127 $gifCreator->data = $this->menuArr[$key] ?: array();
128 $theValArr['mask.'][$temp_theKey . '.'] = $gifCreator->checkTextObj($theValArr['mask.'][$temp_theKey . '.']);
129 // If this is not done it seems that imageMaps will be rendered wrong!!
130 unset($theValArr['mask.'][$temp_theKey . '.']['text.']);
131 }
132 }
133 }
134 if ($theValArr['mask'] == 'GIFBUILDER') {
135 $temp_sKeyArray = \TYPO3\CMS\Core\TypoScript\TemplateService::sortedKeyList($theValArr['mask.']);
136 foreach ($temp_sKeyArray as $temp_theKey) {
137 if ($theValArr['mask.'][$temp_theKey] == 'TEXT') {
138 $gifCreator->data = $this->menuArr[$key] ?: array();
139 $theValArr['mask.'][$temp_theKey . '.'] = $gifCreator->checkTextObj($theValArr['mask.'][$temp_theKey . '.']);
140 // if this is not done it seems that imageMaps will be rendered wrong!!
141 unset($theValArr['mask.'][$temp_theKey . '.']['text.']);
142 }
143 }
144 }
145 }
146 // Checks if disabled is set...
147 $setObjFlag = 1;
148 if ($theValArr['if.']) {
149 /** @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $cObj */
150 $cObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer');
151 $cObj->start($cObjData, 'pages');
152 if (!empty($theValArr['if.']) && !$cObj->checkIf($theValArr['if.'])) {
153 $setObjFlag = 0;
154 }
155 unset($theValArr['if.']);
156 }
157 // Set the object!
158 if ($setObjFlag) {
159 $conf[$gifObjCount] = $theValue;
160 $conf[$gifObjCount . '.'] = $theValArr;
161 }
162 }
163 }
164 }
165 }
166 $gifCreator->start($conf, $GLOBALS['TSFE']->page);
167 // calculations
168 foreach ($waArr as $key => $val) {
169 if ($dConf[$key] = $itemsConf[$key]['distrib']) {
170 $textBB = $gifCreator->objBB[$val['textNum']];
171 $dConf[$key] = str_replace(
172 array('textX', 'textY'),
173 array($textBB[0], $textBB[1]),
174 $dConf[$key]
175 );
176 $dConf[$key] = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $gifCreator->calcOffset($dConf[$key]));
177 }
178 }
179 $workArea = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $gifCreator->calcOffset($this->mconf['dWorkArea']));
180 foreach ($waArr as $key => $val) {
181 $index = $val['free'];
182 $gifCreator->setup[$index] = 'WORKAREA';
183 $workArea[2] = $dConf[$key][2] ?: $dConf[$key][0];
184 $workArea[3] = $dConf[$key][3] ?: $dConf[$key][1];
185 $gifCreator->setup[$index . '.']['set'] = implode(',', $workArea);
186 $workArea[0] += $dConf[$key][0];
187 $workArea[1] += $dConf[$key][1];
188 }
189 if ($this->mconf['debugRenumberedObject']) {
190 echo '<h3>Renumbered GIFBUILDER object:</h3>';
191 debug($gifCreator->setup);
192 }
193 $gifCreator->createTempSubDir('menu/');
194 $gifFileName = $gifCreator->fileName('menu/');
195 // Gets the ImageMap from the cache...
196 $imgHash = md5($gifFileName);
197 $imgMap = $this->sys_page->getHash($imgHash);
198 // File exists
199 if ($imgMap && file_exists($gifFileName)) {
200 $info = @getimagesize($gifFileName);
201 $w = $info[0];
202 $h = $info[1];
203 } else {
204 // file is generated
205 $gifCreator->make();
206 $w = $gifCreator->w;
207 $h = $gifCreator->h;
208 $gifCreator->output($gifFileName);
209 $gifCreator->destroy();
210 $imgMap = $gifCreator->map;
211 $this->sys_page->storeHash($imgHash, $imgMap, 'MENUIMAGEMAP');
212 }
213 $imgMap .= $this->mconf['imgMapExtras'];
214 $gifFileName = \TYPO3\CMS\Core\Utility\GeneralUtility::png_to_gif_by_imagemagick($gifFileName);
215 $this->result = array('output_file' => $gifFileName, 'output_w' => $w, 'output_h' => $h, 'imgMap' => $imgMap);
216 }
217 }
218 }
219
220 /**
221 * Returns the HTML for the image map menu.
222 * If ->result is TRUE it will create the HTML for the image map menu.
223 *
224 * @return string The HTML for the menu
225 * @todo Define visibility
226 */
227 public function writeMenu() {
228 if ($this->result) {
229 $res = &$this->result;
230 // shortMD5 260900
231 $menuName = 'menu_' . \TYPO3\CMS\Core\Utility\GeneralUtility::shortMD5($res['imgMap']);
232 $result = '<img src="' . $GLOBALS['TSFE']->absRefPrefix . $res['output_file'] . '" width="' . $res['output_w'] . '" height="' . $res['output_h'] . '" usemap="#' . $menuName . '" border="0" ' . $this->mconf['params'];
233 // Adding alt attribute if not set.
234 if (!strstr($result, 'alt="')) {
235 $result .= ' alt="Menu Image Map"';
236 }
237 $result .= ' /><map name="' . $menuName . '" id="' . $menuName . '">' . $res['imgMap'] . '</map>';
238 $GLOBALS['TSFE']->imagesOnPage[] = $res['output_file'];
239 return $this->tmpl->wrap($result, $this->mconf['wrap']);
240 }
241 }
242
243 }