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