Revert "[CLEANUP] Rework/simplify copyright header and remove @package"
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Format / CropViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Format;
3
4 /* *
5 * This script is part of the TYPO3 project - inspiring people to share! *
6 * *
7 * TYPO3 is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU General Public License version 2 as published by *
9 * the Free Software Foundation. *
10 * *
11 * This script is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
13 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
14 * Public License for more details. *
15 * */
16 /**
17 * Use this view helper to crop the text between its opening and closing tags.
18 *
19 * = Examples =
20 *
21 * <code title="Defaults">
22 * <f:format.crop maxCharacters="10">This is some very long text</f:format.crop>
23 * </code>
24 * <output>
25 * This is...
26 * </output>
27 *
28 * <code title="Custom suffix">
29 * <f:format.crop maxCharacters="17" append="&nbsp;[more]">This is some very long text</f:format.crop>
30 * </code>
31 * <output>
32 * This is some&nbsp;[more]
33 * </output>
34 *
35 * <code title="Don't respect word boundaries">
36 * <f:format.crop maxCharacters="10" respectWordBoundaries="false">This is some very long text</f:format.crop>
37 * </code>
38 * <output>
39 * This is so...
40 * </output>
41 *
42 * <code title="Don't respect HTML tags">
43 * <f:format.crop maxCharacters="28" respectWordBoundaries="false" respectHtml="false">This is some text with <strong>HTML</strong> tags</f:format.crop>
44 * </code>
45 * <output>
46 * This is some text with <stro
47 * </output>
48 *
49 * <code title="Inline notation">
50 * {someLongText -> f:format.crop(maxCharacters: 10)}
51 * </code>
52 * <output>
53 * someLongText cropped after 10 characters...
54 * (depending on the value of {someLongText})
55 * </output>
56 */
57 class CropViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
58
59 /**
60 * @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
61 */
62 protected $contentObject;
63
64 /**
65 * @var \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController contains a backup of the current $GLOBALS['TSFE'] if used in BE mode
66 */
67 protected $tsfeBackup;
68
69 /**
70 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
71 */
72 protected $configurationManager;
73
74 /**
75 * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
76 * @return void
77 */
78 public function injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager) {
79 $this->configurationManager = $configurationManager;
80 $this->contentObject = $this->configurationManager->getContentObject();
81 }
82
83 /**
84 * Render the cropped text
85 *
86 * @param integer $maxCharacters Place where to truncate the string
87 * @param string $append What to append, if truncation happened
88 * @param boolean $respectWordBoundaries If TRUE and division is in the middle of a word, the remains of that word is removed.
89 * @param boolean $respectHtml If TRUE the cropped string will respect HTML tags and entities. Technically that means, that cropHTML() is called rather than crop()
90 * @return string cropped text
91 */
92 public function render($maxCharacters, $append = '...', $respectWordBoundaries = TRUE, $respectHtml = TRUE) {
93 $stringToTruncate = $this->renderChildren();
94 if (TYPO3_MODE === 'BE') {
95 $this->simulateFrontendEnvironment();
96 }
97 if ($respectHtml) {
98 $content = $this->contentObject->cropHTML($stringToTruncate, $maxCharacters . '|' . $append . '|' . $respectWordBoundaries);
99 } else {
100 $content = $this->contentObject->crop($stringToTruncate, $maxCharacters . '|' . $append . '|' . $respectWordBoundaries);
101 }
102 if (TYPO3_MODE === 'BE') {
103 $this->resetFrontendEnvironment();
104 }
105 return $content;
106 }
107
108 /**
109 * Sets the global variables $GLOBALS['TSFE']->csConvObj and $GLOBALS['TSFE']->renderCharset in Backend mode
110 * This somewhat hacky work around is currently needed because the crop() and cropHTML() functions of \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer rely on those variables to be set
111 *
112 * @return void
113 */
114 protected function simulateFrontendEnvironment() {
115 $this->tsfeBackup = isset($GLOBALS['TSFE']) ? $GLOBALS['TSFE'] : NULL;
116 $GLOBALS['TSFE'] = new \stdClass();
117 // preparing csConvObj
118 if (!is_object($GLOBALS['TSFE']->csConvObj)) {
119 if (is_object($GLOBALS['LANG'])) {
120 $GLOBALS['TSFE']->csConvObj = $GLOBALS['LANG']->csConvObj;
121 } else {
122 $GLOBALS['TSFE']->csConvObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Charset\\CharsetConverter');
123 }
124 }
125 // preparing renderCharset
126 if (!is_object($GLOBALS['TSFE']->renderCharset)) {
127 if (is_object($GLOBALS['LANG'])) {
128 $GLOBALS['TSFE']->renderCharset = $GLOBALS['LANG']->charSet;
129 } else {
130 $GLOBALS['TSFE']->renderCharset = 'utf-8';
131 }
132 }
133 }
134
135 /**
136 * Resets $GLOBALS['TSFE'] if it was previously changed by simulateFrontendEnvironment()
137 *
138 * @return void
139 * @see simulateFrontendEnvironment()
140 */
141 protected function resetFrontendEnvironment() {
142 $GLOBALS['TSFE'] = $this->tsfeBackup;
143 }
144 }