1<?php
2/*
3 * Gallery - a web based photo album viewer and editor
4 * Copyright (C) 2000-2008 Bharat Mediratta
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
19 *
20 * NOTE: Portions of this file are copied from the Smarty modifier.truncate.php
21 * file and are bound by their license, found in lib/smarty/COPYING.lib
22 */
23
24/**
25 * Smarty truncate modifier plugin.  This differs from the standard Smarty plugin
26 * in that it respects HTML entities and doesn't split them.
27 *
28 * Type:     modifier<br>
29 * Name:     entitytruncate<br>
30 * Purpose:  Truncate a string to a certain length if necessary,
31 *           optionally splitting in the middle of a word, and
32 *           appending the $etc string.  Won't split an HTML entity.
33 *
34 * @param string $string the input string
35 * @param int $length what to truncate it to (max length upon return)
36 * @param string $etc what to use to indicate that there was more (default: "...")
37 * @param boolean $breakWords break words or not?
38 * @return string
39 */
40function smarty_modifier_entitytruncate($string, $length, $etc='...', $breakWords=false) {
41    if (empty($string)) {
42	return '';
43    }
44
45    /*
46     * Convert multibyte characters to html entities and then get an entity-safe substring.
47     * Split the string exactly on the boundary.  If there's no change, then we're done.
48     */
49    $string = GalleryUtilities::utf8ToUnicodeEntities($string);
50    list ($tmp, $piece) = GalleryUtilities::entitySubstr($string, 0, $length);
51    if ($piece == $string) {
52	return GalleryUtilities::unicodeEntitiesToUtf8($piece);
53    }
54
55    $etcLength = strlen($etc);
56    if ($etcLength < $length) {
57	/* Make room for the $etc string */
58	list ($tmp, $piece) = GalleryUtilities::entitySubstr($piece, 0, $length - $etcLength);
59
60	$pieceLength = strlen($piece);
61	if (!$breakWords && $string{$pieceLength-1} != ' ' && $string{$pieceLength} != ' ') {
62	    /* We split a word, and we're not allowed to.  Try to back up to the last space */
63	    $splitIndex = strrpos($piece, ' ');
64	    if ($splitIndex > 0) {
65		/* Found a space, truncate there. */
66		$piece = substr($piece, 0, $splitIndex);
67	    }
68	}
69	$piece .= $etc;
70    }
71
72    /* Unicode entities back to UTF-8; may convert entities in original string, but that's ok */
73    return GalleryUtilities::unicodeEntitiesToUtf8($piece);
74}
75?>
76