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