class.tx_ttnews_imageMarkerHook.php 8.71 KB
Newer Older
1 2
<?php
/***************************************************************
sgalinsk's avatar
sgalinsk committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *  Copyright notice
 *
 *  (c) 2010 Peter Klein (pmk@io.dk)
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *  A copy is found in the textfile GPL.txt and important notices to the license
 *  from the author is found in LICENSE.txt distributed with these scripts.
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/
26 27 28 29 30 31 32 33

/**
 * This class contains a hook class for modifying the tt_news image markers
 * It just add a "IMAGE_NUM_CURRENT" register value, so that pmkshadowbox
 * can use navigation on tt_news images.
 *
 * @author Peter Klein <pmk@io.dk>
 */
jok's avatar
jok committed
34
class tx_ttnews_imageMarker extends \TYPO3\CMS\Frontend\Plugin\AbstractPlugin {
sgalinsk's avatar
sgalinsk committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
	/**
	 * Same as class name
	 *
	 * @var string
	 */
	var $prefixId = 'tx_ttnews_imageMarkerHook';

	/**
	 * Path to this script relative to the extension dir.
	 *
	 * @var string
	 */
	var $scriptRelPath = 'class.tx_ttnews_imageMarkerHook.php';

	/**
	 * The extension key.
	 *
	 * @var string
	 */
	var $extKey = 'tt_news';
55

sgalinsk's avatar
sgalinsk committed
56 57 58 59 60 61 62
	/**
	 * @param array $parentMarkerArray
	 * @param array $row
	 * @param array $lConf
	 * @param object $tt_news
	 * @return array
	 */
63 64 65
	function extraItemMarkerProcessor($parentMarkerArray, $row, $lConf, $tt_news) {
		$tt_news->pi_setPiVarDefaults();
		$this->pi_loadLL();
sgalinsk's avatar
sgalinsk committed
66
		$this->conf = & $tt_news->conf;
67 68 69 70 71 72

		if ($tt_news->config['FFimgH'] || $tt_news->config['FFimgW']) {
			$lConf['image.']['file.']['maxW'] = $tt_news->config['FFimgW'];
			$lConf['image.']['file.']['maxH'] = $tt_news->config['FFimgH'];
		}

sgalinsk's avatar
sgalinsk committed
73
		$this->makeImageMarkers($row, $lConf, $tt_news, $parentMarkerArray);
74 75 76 77

		return $parentMarkerArray;
	}

sgalinsk's avatar
sgalinsk committed
78 79 80 81 82 83 84 85
	/**
	 * @param array $row
	 * @param array $lConf
	 * @param object $tt_news
	 * @param array $parentMarkerArray
	 * @return void
	 */
	function makeImageMarkers($row, $lConf, $tt_news, &$parentMarkerArray) {
86
		$imageNum = isset($lConf['imageCount']) ? $lConf['imageCount'] : 1;
jok's avatar
jok committed
87
		$imageNum = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($imageNum, 0, 100);
88
		$theImgCode = '';
jok's avatar
jok committed
89
		$imgs = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $row['image'], 1);
90 91 92 93 94 95 96 97 98
		$imgsCaptions = explode(chr(10), $row['imagecaption']);
		$imgsAltTexts = explode(chr(10), $row['imagealttext']);
		$imgsTitleTexts = explode(chr(10), $row['imagetitletext']);

		$textRenderObj = $tt_news->theCode;

		reset($imgs);

		if ($textRenderObj == 'displaySingle' || $textRenderObj == 'SINGLE') {
sgalinsk's avatar
sgalinsk committed
99 100 101
			$parentMarkerArray = $this->getSingleViewImages(
				$lConf, $imgs, $imgsCaptions, $imgsAltTexts, $imgsTitleTexts, $imageNum, $parentMarkerArray, $tt_news
			);
102 103 104 105 106
		} else {

			$imageMode = (strpos($textRenderObj, 'LATEST') ? $lConf['latestImageMode'] : $lConf['listImageMode']);

			$suf = '';
sgalinsk's avatar
sgalinsk committed
107
			if (is_numeric(substr($lConf['image.']['file.']['maxW'], -1))) { // 'm' or 'c' not set by TS
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
				if ($imageMode) {
					switch ($imageMode) {
						case 'resize2max' :
							$suf = 'm';
							break;
						case 'crop' :
							$suf = 'c';
							break;
						case 'resize' :
							$suf = '';
							break;
					}
				}
			}

sgalinsk's avatar
sgalinsk committed
123 124
			// only insert width/height if it is not given by TS and width/height is empty
			if ($lConf['image.']['file.']['maxW'] && !$lConf['image.']['file.']['width']) {
125 126 127
				$lConf['image.']['file.']['width'] = $lConf['image.']['file.']['maxW'] . $suf;
				unset($lConf['image.']['file.']['maxW']);
			}
sgalinsk's avatar
sgalinsk committed
128
			if ($lConf['image.']['file.']['maxH'] && !$lConf['image.']['file.']['height']) {
129 130 131 132 133 134
				$lConf['image.']['file.']['height'] = $lConf['image.']['file.']['maxH'] . $suf;
				unset($lConf['image.']['file.']['maxH']);
			}

			$cc = 0;
			foreach ($imgs as $val) {
sgalinsk's avatar
sgalinsk committed
135
				if ($cc == $imageNum) {
136
					break;
sgalinsk's avatar
sgalinsk committed
137
				}
138 139 140 141 142 143
				if ($val) {
					$lConf['image.']['altText'] = $imgsAltTexts[$cc];
					$lConf['image.']['titleText'] = $imgsTitleTexts[$cc];
					$lConf['image.']['file'] = 'uploads/pics/' . $val;
					$GLOBALS['TSFE']->register['IMAGE_NUM_CURRENT'] = $cc;

sgalinsk's avatar
sgalinsk committed
144 145 146
					$theImgCode .= $tt_news->local_cObj->IMAGE($lConf['image.']) . $tt_news->local_cObj->stdWrap(
							$imgsCaptions[$cc], $lConf['caption_stdWrap.']
						);
147 148 149 150 151
				}
				$cc++;
			}

			if ($cc) {
sgalinsk's avatar
sgalinsk committed
152 153 154
				$parentMarkerArray['###NEWS_IMAGE###'] = $tt_news->local_cObj->wrap(
					$theImgCode, $lConf['imageWrapIfAny']
				);
155
			} else {
sgalinsk's avatar
sgalinsk committed
156 157 158
				$parentMarkerArray['###NEWS_IMAGE###'] = $tt_news->local_cObj->stdWrap(
					$parentMarkerArray['###NEWS_IMAGE###'], $lConf['image.']['noImage_stdWrap.']
				);
159 160 161 162 163 164 165
			}
		}
	}

	/**
	 * Fills the image markers for the SINGLE view with data. Supports Optionssplit for some parameters
	 *
sgalinsk's avatar
sgalinsk committed
166 167 168 169 170 171 172 173 174
	 * @param array $lConf
	 * @param array $imgs
	 * @param array $imgsCaptions
	 * @param array $imgsAltTexts
	 * @param array $imgsTitleTexts
	 * @param int $imageNum
	 * @param array $markerArray
	 * @param object $tt_news
	 * @return array
175
	 */
sgalinsk's avatar
sgalinsk committed
176 177 178
	function getSingleViewImages(
		$lConf, $imgs, $imgsCaptions, $imgsAltTexts, $imgsTitleTexts, $imageNum, $markerArray, $tt_news
	) {
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
		$marker = 'NEWS_IMAGE';
		$sViewSplitLConf = array();
		$tmpMarkers = array();
		$iC = count($imgs);

		// remove first img from image array in single view if the TSvar firstImageIsPreview is set
		if (($iC > 1 && $tt_news->config['firstImageIsPreview']) || ($iC >= 1 && $tt_news->config['forceFirstImageIsPreview'])) {
			array_shift($imgs);
			array_shift($imgsCaptions);
			array_shift($imgsAltTexts);
			array_shift($imgsTitleTexts);
			$iC--;
		}

		if ($iC > $imageNum) {
			$iC = $imageNum;
		}

		// get img array parts for single view pages
		if ($tt_news->piVars[$tt_news->config['singleViewPointerName']]) {
			$spage = $tt_news->piVars[$tt_news->config['singleViewPointerName']];
			$astart = $imageNum * $spage;
			$imgs = array_slice($imgs, $astart, $imageNum);
			$imgsCaptions = array_slice($imgsCaptions, $astart, $imageNum);
			$imgsAltTexts = array_slice($imgsAltTexts, $astart, $imageNum);
			$imgsTitleTexts = array_slice($imgsTitleTexts, $astart, $imageNum);
		}

sgalinsk's avatar
sgalinsk committed
207
		$osCount = 0;
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
		if ($tt_news->conf['enableOptionSplit']) {
			if ($lConf['imageMarkerOptionSplit']) {
				$ostmp = explode('|*|', $lConf['imageMarkerOptionSplit']);
				$osCount = count($ostmp);
			}
			$sViewSplitLConf = $tt_news->processOptionSplit($lConf, $iC);
		}

		// reset markers for optionSplitted images
		for ($m = 1; $m <= $imageNum; $m++) {
			$markerArray['###' . $marker . '_' . $m . '###'] = '';
		}

		$cc = 0;
		$theImgCode = '';
		foreach ($imgs as $val) {
sgalinsk's avatar
sgalinsk committed
224
			if ($cc == $imageNum) {
225
				break;
sgalinsk's avatar
sgalinsk committed
226
			}
227
			if ($val) {
sgalinsk's avatar
sgalinsk committed
228
				if (!empty($sViewSplitLConf[$cc])) {
229 230 231 232 233 234 235 236
					$lConf = $sViewSplitLConf[$cc];
				}

				$lConf['image.']['altText'] = $imgsAltTexts[$cc];
				$lConf['image.']['titleText'] = $imgsTitleTexts[$cc];
				$lConf['image.']['file'] = 'uploads/pics/' . $val;
				$GLOBALS['TSFE']->register['IMAGE_NUM_CURRENT'] = $cc;

sgalinsk's avatar
sgalinsk committed
237 238 239
				$imgHtml = $tt_news->local_cObj->IMAGE($lConf['image.']) . $tt_news->local_cObj->stdWrap(
						$imgsCaptions[$cc], $lConf['caption_stdWrap.']
					);
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261

				if ($osCount) {
					if ($iC > 1) {
						$mName = '###' . $marker . '_' . $lConf['imageMarkerOptionSplit'] . '###';
					} else { // fall back to the first image marker if only one image has been found
						$mName = '###' . $marker . '_1###';
					}
					$tmpMarkers[$mName]['html'] .= $imgHtml;
					$tmpMarkers[$mName]['wrap'] = $lConf['imageWrapIfAny'];
				} else {
					$theImgCode .= $imgHtml;
				}
			}
			$cc++;
		}

		if ($cc) {
			if ($osCount) {
				foreach ($tmpMarkers as $mName => $res) {
					$markerArray[$mName] = $tt_news->local_cObj->wrap($res['html'], $res['wrap']);
				}
			} else {
sgalinsk's avatar
sgalinsk committed
262 263 264
				$markerArray['###' . $marker . '###'] = $tt_news->local_cObj->wrap(
					$theImgCode, $lConf['imageWrapIfAny']
				);
265 266 267 268 269
			}
		} else {
			if ($lConf['imageMarkerOptionSplit']) {
				$m = '_1';
			}
sgalinsk's avatar
sgalinsk committed
270 271 272
			$markerArray['###' . $marker . $m . '###'] = $tt_news->local_cObj->stdWrap(
				$markerArray['###' . $marker . $m . '###'], $lConf['image.']['noImage_stdWrap.']
			);
273 274 275 276 277 278
		}

		return $markerArray;
	}
}

sgalinsk's avatar
sgalinsk committed
279 280 281 282
if (defined(
		'TYPO3_MODE'
	) && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/pmkshadowbox/classes/class.tx_ttnews_imageMarkerHook.php']
) {
283 284 285 286
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/pmkshadowbox/classes/class.tx_ttnews_imageMarkerHook.php']);
}

?>