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