TYPO3 API  SVNRelease
CropViewHelper.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*                                                                        *
00004  * This script is part of the TYPO3 project - inspiring people to share!  *
00005  *                                                                        *
00006  * TYPO3 is free software; you can redistribute it and/or modify it under *
00007  * the terms of the GNU General Public License version 2 as published by  *
00008  * the Free Software Foundation.                                          *
00009  *                                                                        *
00010  * This script is distributed in the hope that it will be useful, but     *
00011  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
00012  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
00013  * Public License for more details.                                       *
00014  *                                                                        */
00015 
00016 /**
00017  */
00018 
00019 /**
00020  * Use this view helper to crop the text between its opening and closing tags.
00021  *
00022  * = Examples =
00023  *
00024  * <code title="Defaults">
00025  * <f:format.crop maxCharacters="10">This is some very long text</f:format.crop>
00026  * </code>
00027  * <output>
00028  * This is...
00029  * </output>
00030  *
00031  * <code title="Custom suffix">
00032  * <f:format.crop maxCharacters="17" append="&nbsp;[more]">This is some very long text</f:format.crop>
00033  * </code>
00034  * <output>
00035  * This is some&nbsp;[more]
00036  * </output>
00037  *
00038  * <code title="Don't respect word boundaries">
00039  * <f:format.crop maxCharacters="10" respectWordBoundaries="false">This is some very long text</f:format.crop>
00040  * </code>
00041  * <output>
00042  * This is so...
00043  * </output>
00044  *
00045  * <code title="Don't respect HTML tags">
00046  * <f:format.crop maxCharacters="28" respectWordBoundaries="false" respectHtml="false">This is some text with <strong>HTML</strong> tags</f:format.crop>
00047  * </code>
00048  * <output>
00049  * This is some text with <stro
00050  * </output>
00051  *
00052  * <code title="Inline notation">
00053  * {someLongText -> f:format.crop(maxCharacters: 10)}
00054  * </code>
00055  * <output>
00056  * someLongText cropped after 10 characters...
00057  * (depending on the value of {someLongText})
00058  * </output>
00059  *
00060  * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
00061  */
00062 class Tx_Fluid_ViewHelpers_Format_CropViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
00063 
00064     /**
00065      * @var tslib_cObj
00066      */
00067     protected $contentObject;
00068 
00069     /**
00070      * @var t3lib_fe contains a backup of the current $GLOBALS['TSFE'] if used in BE mode
00071      */
00072     protected $tsfeBackup;
00073 
00074     /**
00075      * @var Tx_Extbase_Configuration_ConfigurationManagerInterface
00076      */
00077     protected $configurationManager;
00078 
00079     /**
00080      * @param Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager
00081      * @return void
00082      */
00083     public function injectConfigurationManager(Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager) {
00084         $this->configurationManager = $configurationManager;
00085         $this->contentObject = $this->configurationManager->getContentObject();
00086     }
00087 
00088     /**
00089      * Render the cropped text
00090      *
00091      * @param integer $maxCharacters Place where to truncate the string
00092      * @param string $append What to append, if truncation happened
00093      * @param boolean $respectBoundaries If TRUE and division is in the middle of a word, the remains of that word is removed.
00094      * @param boolean $respectHtml If TRUE the cropped string will respect HTML tags and entities. Technically that means, that cropHTML() is called rather than crop()
00095      * @return string cropped text
00096      * @author Andreas Pattynama <andreas.pattynama@innocube.ch>
00097      * @author Sebastian Kurfürst <sebastian@typo3.org>
00098      * @author Bastian Waidelich <bastian@typo3.org>
00099      * @author Felix Oertel <oertel@networkteam.com>
00100      */
00101     public function render($maxCharacters, $append = '...', $respectWordBoundaries = TRUE, $respectHtml = TRUE) {
00102         $stringToTruncate = $this->renderChildren();
00103         if (TYPO3_MODE === 'BE') {
00104             $this->simulateFrontendEnvironment();
00105         }
00106 
00107         if ($respectHtml) {
00108             $content = $this->contentObject->cropHTML($stringToTruncate, $maxCharacters . '|' . $append . '|' . $respectWordBoundaries);
00109         } else {
00110             $content = $this->contentObject->crop($stringToTruncate, $maxCharacters . '|' . $append . '|' . $respectWordBoundaries);
00111         }
00112 
00113         if (TYPO3_MODE === 'BE') {
00114             $this->resetFrontendEnvironment();
00115         }
00116         return $content;
00117     }
00118 
00119     /**
00120      * Sets the global variables $GLOBALS['TSFE']->csConvObj and $GLOBALS['TSFE']->renderCharset in Backend mode
00121      * This somewhat hacky work around is currently needed because the crop() and cropHTML() functions of tslib_cObj rely on those variables to be set
00122      *
00123      * @return void
00124      * @author Bastian Waidelich <bastian@typo3.org>
00125      */
00126     protected function simulateFrontendEnvironment() {
00127         $this->tsfeBackup = isset($GLOBALS['TSFE']) ? $GLOBALS['TSFE'] : NULL;
00128         $GLOBALS['TSFE'] = new stdClass();
00129 
00130             // preparing csConvObj
00131         if (!is_object($GLOBALS['TSFE']->csConvObj)) {
00132             if (is_object($GLOBALS['LANG'])) {
00133                 $GLOBALS['TSFE']->csConvObj = $GLOBALS['LANG']->csConvObj;
00134             } else {
00135                 $GLOBALS['TSFE']->csConvObj = t3lib_div::makeInstance('t3lib_cs');
00136             }
00137         }
00138 
00139             // preparing renderCharset
00140         if (!is_object($GLOBALS['TSFE']->renderCharset)) {
00141             if (is_object($GLOBALS['LANG'])) {
00142                 $GLOBALS['TSFE']->renderCharset = $GLOBALS['LANG']->charSet;
00143             } else {
00144                 $GLOBALS['TSFE']->renderCharset = $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'];
00145             }
00146         }
00147     }
00148 
00149     /**
00150      * Resets $GLOBALS['TSFE'] if it was previously changed by simulateFrontendEnvironment()
00151      *
00152      * @return void
00153      * @author Bastian Waidelich <bastian@typo3.org>
00154      * @see simulateFrontendEnvironment()
00155      */
00156     protected function resetFrontendEnvironment() {
00157         $GLOBALS['TSFE'] = $this->tsfeBackup;
00158     }
00159 }
00160 
00161 
00162 ?>