root/trunk/lib/helper/MoreTextHelper.php

Revision 2, 3.7 kB (checked in by nperriault, 8 months ago)

First commit of the extracted code from production, I hope no passwd has been forgotten :-)

Line 
1 <?php
2 /**
3  * Advanced text truncating tool
4  *
5  * @param  string $string
6  * @param  int    $limit
7  * @param  string $break
8  * @param  string $pad
9  * @return string
10  */
11 function adv_truncate_text($string, $limit, $break = ".", $pad = "...")
12 {
13   if (strlen($string) <= $limit)
14   {
15     return $string;
16   }
17
18   if (false !== ($breakpoint = strpos($string, $break, $limit)))
19   {
20     if ($breakpoint < strlen($string) - 1)
21     {
22       $string = substr($string, 0, $breakpoint) . $pad;
23     }
24   }
25   return $string;
26 }
27
28 /**
29  * Formats a place with a given country and even a city name
30  *
31  * @param  string  $country_code
32  * @param  string  $city
33  * @return string
34  */
35 function format_place($country_code, $city = null)
36 {
37   if ($city)
38   {
39     return sprintf('%s (%s)', $city, format_country($country_code));
40   }
41   return format_country($country_code);
42 }
43
44 /**
45  * Format a string describing a relation between a subject for a given role in a
46  * duration context
47  *
48  * @param  string $subject
49  * @param  string $role
50  * @param  mixed  $start_date
51  * @param  mixed  $end_date
52  * @return string
53  */
54 function format_relation($subject, $role, $start_date = null, $end_date = null)
55 {
56   sfLoader::loadHelpers('I18N', 'Date');
57   $string = '';
58
59   if (is_null($start_date) && is_null($end_date))
60   {
61     $string = __('%subject% is or has been %role%',
62               array('%subject%'  => $subject,
63                     '%role%'     => '<span>'.$role.'</span>'));
64   }
65   elseif (!is_null($start_date) && !is_null($end_date))
66   {
67     $string = __('%subject% has been %role% for %time% until %end_date%',
68               array('%subject%'  => $subject,
69                     '%role%'     => '<span>'.$role.'</span>',
70                     '%time%'     => distance_of_time_in_words(strtotime($start_date),
71                                                               strtotime($end_date)),
72                     '%end_date%' => strtolower($end_date)));
73   }
74   elseif (!is_null($start_date) && is_null($end_date))
75   {
76     $string = __('%subject% is %role% since %date%',
77                array('%subject%' => $subject,
78                      '%role%'    => '<span>'.$role.'</span>',
79                      '%date%'    => $start_date));
80   }
81   elseif (is_null($start_date))
82   {
83     $string = __('%subject% has been %role% until %date%',
84               array('%subject%'  => $subject,
85                     '%role%'     => '<span>'.$role.'</span>',
86                     '%date%'     => $end_date));
87   }
88
89   if ($string != '')
90   {
91     return content_tag('p', $string, 'class=relation');
92   }
93 }
94
95 /**
96  * Return pagination information text
97  *
98  * @param  sfPager  $pager
99  * @return string
100  */
101 function pagination_info($pager)
102 {
103   sfLoader::loadHelpers('I18N');
104   $start = (($pager->getPage() - 1 )* $pager->getMaxPerPage()) + 1;
105   $end   = $start + count($pager->getResults()) - 1;
106   return __('Showing results %1%-%2% out of %3%, page %4% on %5%',
107             array('%1%' => $start,
108                   '%2%' => $end,
109                   '%3%' => $pager->getNbResults(),
110                   '%4%' => $pager->getPage(),
111                   '%5%' => $pager->getLastPage()));
112 }
113
114 /**
115  * Split a char separated tags string and return a human readable string
116  *
117  * @param  string  $tags_string
118  * @param  string  $separator
119  * @return string
120  */
121 function tags_string($tags_string, $separator = ',')
122 {
123   sfLoader::loadHelpers('I18N');
124   $tags = explode(',', $tags_string);
125   if (count($tags) < 2)
126   {
127     return $tags_string;
128   }
129   $nb_tags = count($tags);
130   $string = '';
131   for ($i = 0; $i < $nb_tags; $i++)
132   {
133     $tag = $tags[$i];
134     switch ($nb_tags - $i)
135     {
136       case 2:
137         $string .= $tag.' '.__('and').' ';
138       break;
139       case 1;
140         $string .= $tag;
141       break;
142       default:
143         $string .= $tag.', ';
144       break;
145     }
146   }
147   return $string;
148 }
Note: See TracBrowser for help on using the browser.