Ajout du support des écritures ventilées.
[garradin.git] / include / lib.squelette_filtres.php
1 <?php
2
3 namespace Garradin;
4
5 class Squelette_Filtres
6 {
7 static private $g2x = null;
8 static private $alt = [];
9
10 static public $filtres_php = [
11 'strtolower',
12 'strtoupper',
13 'ucfirst',
14 'ucwords',
15 'str_rot13',
16 'str_shuffle',
17 'htmlentities',
18 'htmlspecialchars',
19 'trim',
20 'ltrim',
21 'rtrim',
22 'lcfirst',
23 'md5',
24 'sha1',
25 'metaphone',
26 'nl2br',
27 'soundex',
28 'str_split',
29 'str_word_count',
30 'strrev',
31 'strlen',
32 'wordwrap',
33 'strip_tags' => 'supprimer_tags',
34 'var_dump',
35 ];
36
37 static public $filtres_alias = [
38 '!=' => 'different_de',
39 '==' => 'egal_a',
40 '?' => 'choixsivide',
41 '>' => 'superieur_a',
42 '>=' => 'superieur_ou_egal_a',
43 '<' => 'inferieur_a',
44 '<=' => 'inferieur_ou_egal_a',
45 'yes' => 'oui',
46 'no' => 'non',
47 'and' => 'et',
48 'or' => 'ou',
49 'xor' => 'xou',
50 ];
51
52 static public $desactiver_defaut = [
53 'formatter_texte',
54 'entites_html',
55 'proteger_contact',
56 'echapper_xml',
57 ];
58
59 static public function date_en_francais($date)
60 {
61 return ucfirst(strtolower(utils::strftime_fr('%A %e %B %Y', $date)));
62 }
63
64 static public function heure_en_francais($date)
65 {
66 return utils::strftime_fr('%Hh%I', $date);
67 }
68
69 static public function mois_en_francais($date)
70 {
71 return utils::strftime_fr('%B %Y', $date);
72 }
73
74 static public function date_perso($date, $format)
75 {
76 return utils::strftime_fr($format, $date);
77 }
78
79 static public function date_intelligente($date)
80 {
81 if (date('Ymd', $date) == date('Ymd'))
82 return 'Aujourd\'hui, '.date('H\hi', $date);
83 elseif (date('Ymd', $date) == date('Ymd', strtotime('yesterday')))
84 return 'Hier, '.date('H\hi', $date);
85 elseif (date('Y', $date) == date('Y'))
86 return strtolower(utils::strftime_fr('%e %B, %Hh%M', $date));
87 else
88 return strtolower(utils::strftime_fr('%e %B %Y', $date));
89 }
90
91 static public function date_atom($date)
92 {
93 return date(DATE_ATOM, $date);
94 }
95
96 static public function alterner($v, $name, $valeur1, $valeur2)
97 {
98 if (!array_key_exists($name, self::$alt))
99 {
100 self::$alt[$name] = 0;
101 }
102
103 if (self::$alt[$name]++ % 2 == 0)
104 return $valeur1;
105 else
106 return $valeur2;
107 }
108
109 static public function proteger_contact($contact)
110 {
111 if (!trim($contact))
112 return '';
113
114 if (strpos($contact, '@'))
115 return '<span style="unicode-bidi:bidi-override;direction: rtl;">'.htmlspecialchars(strrev($contact), ENT_QUOTES, 'UTF-8').'</span>';
116 else
117 return '<a href="'.htmlspecialchars($contact, ENT_QUOTES, 'UTF-8').'">'.htmlspecialchars($contact, ENT_QUOTES, 'UTF-8').'</a>';
118 }
119
120 static public function entites_html($texte)
121 {
122 return htmlspecialchars($texte, ENT_QUOTES, 'UTF-8');
123 }
124
125 static public function echapper_xml($texte)
126 {
127 return str_replace('&#039;', '&apos;', htmlspecialchars($texte, ENT_QUOTES, 'UTF-8'));
128 }
129
130 static public function formatter_texte($texte)
131 {
132 $texte = utils::htmlLinksOnUrls($texte);
133 $texte = utils::htmlSpip($texte);
134 $texte = utils::htmlGarbage2xhtml($texte);
135
136 $texte = self::typo_fr($texte);
137
138 return $texte;
139 }
140
141 static public function typo_fr($str, $html = true)
142 {
143 $space = $html ? '&nbsp;' : ' ';
144 $str = preg_replace('/(?:[\h]|&nbsp;)*([?!:»])(\s+|$)/u', $space.'\\1\\2', $str);
145 $str = preg_replace('/(^|\s+)([«])(?:[\h]|&nbsp;)*/u', '\\1\\2'.$space, $str);
146 return $str;
147 }
148
149 static public function pagination($total, $debut, $par_page)
150 {
151 $max_page = ceil($total / $par_page);
152 $current = ($debut > 0) ? ceil($debut / $par_page) + 1 : 1;
153 $out = '';
154
155 if ($current > 1)
156 {
157 $out .= '<a href="./'.($current > 2 ? '+' . ($debut - $par_page) : '').'">&laquo; Page pr&eacute;c&eacute;dente</a> - ';
158 }
159
160 for ($i = 1; $i <= $max_page; $i++)
161 {
162 $link = ($i == 1) ? './' : './+' . (($i - 1) * $par_page);
163
164 if ($i == $current)
165 $out .= '<strong>'.$i.'</strong> - ';
166 else
167 $out .= '<a href="'.$link.'">'.$i.'</a> - ';
168 }
169
170 if ($current < $max_page)
171 {
172 $out .= '<a href="./+'.($debut + $par_page).'">Page suivante &raquo;</a>';
173 }
174 else
175 {
176 $out = substr($out, 0, -3);
177 }
178
179 return $out;
180 }
181
182 // Compatibilité SPIP
183
184 static public function egal_a($value, $test)
185 {
186 if ($value == $test)
187 return true;
188 else
189 return false;
190 }
191
192 static public function different_de($value, $test)
193 {
194 if ($value != $test)
195 return true;
196 else
197 return false;
198 }
199
200 // disponible aussi avec : | ?{sioui, sinon}
201 static public function choixsivide($value, $un, $deux = '')
202 {
203 if (empty($value) || !trim($value))
204 return $deux;
205 else
206 return $un;
207 }
208
209 static public function sinon($value, $sinon = '')
210 {
211 if ($value)
212 return $value;
213 else
214 return $sinon;
215 }
216
217 static public function choixsiegal($value, $test, $un, $deux)
218 {
219 return ($value == $test) ? $un : $deux;
220 }
221
222 static public function supprimer_tags($value, $replace = '')
223 {
224 return preg_replace('!<[^>]*>!', $replace, $value);
225 }
226
227 static public function supprimer_spip($value)
228 {
229 $value = preg_replace('!\[([^\]]+)(?:->[^\]]*)?\]!U', '$1', $value);
230 $value = preg_replace('!\{+([^\}]*)\}+!', '$1', $value);
231 return $value;
232 }
233
234 static public function couper($texte, $taille, $etc = ' (...)')
235 {
236 if (strlen($texte) > $taille)
237 {
238 $texte = substr($texte, 0, $taille);
239 $taille -= ($taille * 0.1);
240
241 $texte = preg_replace('!([\s.,;:\!?])[^\s.,;:\!?]*?$!', '\\1', $texte);
242 $texte.= $etc;
243 }
244
245 return $texte;
246 }
247
248 static public function replace($texte, $expression, $replace, $modif='UsimsS')
249 {
250 return preg_replace('/'.$expression.'/'.$modif, $replace, $texte);
251 }
252
253 static public function plus($a, $b)
254 {
255 return $a + $b;
256 }
257
258 static public function moins($a, $b)
259 {
260 return $a - $b;
261 }
262
263 static public function mult($a, $b)
264 {
265 return $a * $b;
266 }
267
268 static public function div($a, $b)
269 {
270 return $b ? $a / $b : 0;
271 }
272
273 static public function modulo($a, $mod, $add)
274 {
275 return ($mod ? $nb % $mod : 0) + $add;
276 }
277
278 static public function vide($value)
279 {
280 return '';
281 }
282
283 static public function concat()
284 {
285 return implode('', func_get_args());
286 }
287
288 static public function singulier_ou_pluriel($nb, $singulier, $pluriel, $var = null)
289 {
290 if (!$nb)
291 return '';
292
293 if ($nb == 1)
294 return str_replace('@'.$var.'@', $nb, $singulier);
295 else
296 return str_replace('@'.$var.'@', $nb, $pluriel);
297 }
298
299 static public function date_w3c($date)
300 {
301 return date(DATE_W3C, $date);
302 }
303
304 static public function et($value, $test)
305 {
306 return ($value && $test);
307 }
308
309 static public function ou($value, $test)
310 {
311 return ($value || $test);
312 }
313
314 static public function xou($value, $test)
315 {
316 return ($value XOR $test);
317 }
318
319 static public function oui($value)
320 {
321 return $value ? true : false;
322 }
323
324 static public function non($value)
325 {
326 return !$value ? true : false;
327 }
328
329 static public function superieur_a($value, $test)
330 {
331 return ($value > $test) ? true : false;
332 }
333
334 static public function superieur_ou_egal_a($value, $test)
335 {
336 return ($value >= $test) ? true : false;
337 }
338
339 static public function inferieur_a($value, $test)
340 {
341 return ($value < $test) ? true : false;
342 }
343
344 static public function inferieur_ou_egal_a($value, $test)
345 {
346 return ($value <= $test) ? true : false;
347 }
348 }
349
350 ?>