[+BUGFIX] Fluid (ViewHelpers): added auto-detection of string-format and propper...
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Format / CropViewHelper.php
1 <?php
2
3 /* *
4 * This script is part of the TYPO3 project - inspiring people to share! *
5 * *
6 * TYPO3 is free software; you can redistribute it and/or modify it under *
7 * the terms of the GNU General Public License version 2 as published by *
8 * the Free Software Foundation. *
9 * *
10 * This script is distributed in the hope that it will be useful, but *
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
12 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
13 * Public License for more details. *
14 * */
15
16 /**
17 * @package Fluid
18 * @subpackage ViewHelpers
19 * @version $Id$
20 */
21
22 /**
23 * Use this view helper to crop the text between its opening and closing tags.
24 *
25 * = Examples =
26 *
27 * <code title="Defaults">
28 * <f:format.crop maxCharacters="10">This is some very long text</f:format.crop>
29 * </code>
30 *
31 * Output:
32 * This is...
33 *
34 * <code title="Custom suffix">
35 * <f:format.crop maxCharacters="17" append="&nbsp;[more]">This is some very long text</f:format.crop>
36 * </code>
37 *
38 * Output:
39 * This is some&nbsp;[more]
40 *
41 * <code title="Don't respect word boundaries">
42 * <f:format.crop maxCharacters="10" respectWordBoundaries="false">This is some very long text</f:format.crop>
43 * </code>
44 *
45 * Output:
46 * This is so...
47 *
48 * WARNING: This tag doesn't care about multibyte charsets currently.
49 *
50 * @package Fluid
51 * @subpackage ViewHelpers
52 * @version $Id$
53 * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
54 * @scope prototype
55 */
56 class Tx_Fluid_ViewHelpers_Format_CropViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
57
58 /**
59 * @var tslib_cObj
60 */
61 protected $contentObject;
62
63 /**
64 * Constructor. Used to create an instance of tslib_cObj used by the render() method.
65 *
66 * @param tslib_cObj $contentObject injector for tslib_cObj (optional)
67 * @return void
68 */
69 public function __construct($contentObject = NULL) {
70 $this->contentObject = $contentObject !== NULL ? $contentObject : t3lib_div::makeInstance('tslib_cObj');
71 }
72
73 /**
74 * Render the cropped text
75 *
76 * @param integer $maxCharacters Place where to truncate the string
77 * @param string $append What to append, if truncation happened
78 * @param boolean $respectBoundaries If TRUE and division is in the middle of a word, the remains of that word is removed. This is currently ignored in backend mode!
79 * @return string cropped text
80 * @author Andreas Pattynama <andreas.pattynama@innocube.ch>
81 * @author Sebastian Kurf├╝rst <sebastian@typo3.org>
82 * @author Bastian Waidelich <bastian@typo3.org>
83 * @author Felix Oertel <oertel@networkteam.com>
84 */
85 public function render($maxCharacters, $append = '...', $respectWordBoundaries = TRUE) {
86 $stringToTruncate = $this->renderChildren();
87 if (TYPO3_MODE === 'BE') {
88 if (strlen($stringToTruncate) > $maxCharacters) {
89 $stringToTruncate = substr($stringToTruncate, 0, ($maxCharacters - strlen($append))) . $append;
90 }
91 return $stringToTruncate;
92 } else {
93 if (strip_tags($stringToTruncate) != $stringToTruncate) {
94 return $this->contentObject->cropHTML($stringToTruncate, $maxCharacters . '|' . $append . '|' . $respectWordBoundaries);
95 } else {
96 return $this->contentObject->crop($stringToTruncate, $maxCharacters . '|' . $append . '|' . $respectWordBoundaries);
97 }
98 }
99 }
100 }
101
102
103 ?>