1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ocrend Framewok 2 package. |
5
|
|
|
* |
6
|
|
|
* (c) Ocrend Software <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ocrend\Kernel\Helpers; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Funciones reutilizables dentro del sistema. |
16
|
|
|
* |
17
|
|
|
* @author Brayan Narváez <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
final class Functions extends \Twig_Extension { |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Verifica parte de una fecha, método privado usado en str_to_time |
24
|
|
|
* |
25
|
|
|
* @param int $index: Índice del arreglo |
26
|
|
|
* @param array $detail: Arreglo |
27
|
|
|
* @param int $max: Valor a comparar |
28
|
|
|
* |
29
|
|
|
* @return bool con el resultado de la comparación |
30
|
|
|
*/ |
31
|
|
|
final private function check_str_to_time(int $index, array $detail, int $max) : bool { |
32
|
|
|
return !array_key_exists($index0,$detail) || !is_numeric($detail[$index]) || intval($detail[$index]) < $max; |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
//------------------------------------------------ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Redirecciona a una URL |
39
|
|
|
* |
40
|
|
|
* @param string $url: Sitio a donde redireccionará, si no se pasa, por defecto |
41
|
|
|
* se redirecciona a la URL principal del sitio |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
final public function redir($url = null) { |
46
|
|
|
global $config; |
47
|
|
|
|
48
|
|
|
if(null == $url) { |
49
|
|
|
$url = $config['site']['url']; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
\Symfony\Component\HttpFoundation\RedirectResponse::create($url)->send(); |
53
|
|
|
exit(1); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
//------------------------------------------------ |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Calcula el porcentaje de una cantidad |
60
|
|
|
* |
61
|
|
|
* @param float $por: El porcentaje a evaluar, por ejemplo 1, 20, 30 % sin el "%", sólamente el número |
62
|
|
|
* @param float $n: El número al cual se le quiere sacar el porcentaje |
63
|
|
|
* |
64
|
|
|
* @return float con el porcentaje correspondiente |
65
|
|
|
*/ |
66
|
|
|
final public function percent(float $por, float $n) : float { |
67
|
|
|
return $n * ($por / 100); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
//------------------------------------------------ |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Da unidades de peso a un integer según sea su tamaño asumida en bytes |
74
|
|
|
* |
75
|
|
|
* @param int $size: Un entero que representa el tamaño a convertir |
76
|
|
|
* |
77
|
|
|
* @return string del tamaño $size convertido a la unidad más adecuada |
78
|
|
|
*/ |
79
|
|
|
final public function convert(int $size) : string { |
80
|
|
|
$unit = array('bytes','kb','mb','gb','tb','pb'); |
81
|
|
|
return round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
//------------------------------------------------ |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Retorna la URL de un gravatar, según el email |
88
|
|
|
* |
89
|
|
|
* @param string $email: El email del usuario a extraer el gravatar |
90
|
|
|
* @param int $size: El tamaño del gravatar |
91
|
|
|
* @return string con la URl |
92
|
|
|
*/ |
93
|
|
|
final public function get_gravatar(string $email, int $size = 32) : string { |
94
|
|
|
return 'http://www.gravatar.com/avatar/' . md5($email) . '?s=' . (int) abs($size); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
//------------------------------------------------ |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Alias de Empty, más completo |
101
|
|
|
* |
102
|
|
|
* @param mixed $var: Variable a analizar |
103
|
|
|
* |
104
|
|
|
* @return bool con true si está vacío, false si no, un espacio en blanco cuenta como vacío |
105
|
|
|
*/ |
106
|
|
|
final public function emp($var) : bool { |
107
|
|
|
return (isset($var) && empty(trim(str_replace(' ','',$var)))); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
//------------------------------------------------ |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Aanaliza que TODOS los elementos de un arreglo estén llenos, útil para analizar por ejemplo que todos los elementos de un formulario esté llenos |
114
|
|
|
* pasando como parámetro $_POST |
115
|
|
|
* |
116
|
|
|
* @param array $array, arreglo a analizar |
117
|
|
|
* |
118
|
|
|
* @return bool con true si están todos llenos, false si al menos uno está vacío |
119
|
|
|
*/ |
120
|
|
|
final public function all_full(array $array) : bool { |
121
|
|
|
foreach($array as $e) { |
122
|
|
|
if($this->emp($e) and $e != '0') { |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
return true; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
//------------------------------------------------ |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Alias de Empty() pero soporta más de un parámetro (infinitos) |
133
|
|
|
* |
134
|
|
|
* @return bool con true si al menos uno está vacío, false si todos están llenos |
135
|
|
|
*/ |
136
|
|
|
final public function e() : bool { |
137
|
|
|
for ($i = 0, $nargs = func_num_args(); $i < $nargs; $i++) { |
138
|
|
|
if(null === func_get_arg($i) || ($this->emp(func_get_arg($i)) && func_get_arg($i) != '0')) { |
139
|
|
|
return true; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
return false; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
//------------------------------------------------ |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Alias de date() pero devuele días y meses en español |
149
|
|
|
* |
150
|
|
|
* @param string $format: Formato de salida (igual que en date()) |
151
|
|
|
* @param int $time: Tiempo, por defecto es time() (igual que en date()) |
152
|
|
|
* |
153
|
|
|
* @return string con la fecha en formato humano (y en español) |
154
|
|
|
*/ |
155
|
|
|
final public function fecha(string $format, int $time = 0) : string { |
156
|
|
|
$date = date($format,$time == 0 ? time() : $time); |
157
|
|
|
$cambios = array( |
158
|
|
|
'Monday'=> 'Lunes', |
159
|
|
|
'Tuesday'=> 'Martes', |
160
|
|
|
'Wednesday'=> 'Miércoles', |
161
|
|
|
'Thursday'=> 'Jueves', |
162
|
|
|
'Friday'=> 'Viernes', |
163
|
|
|
'Saturday'=> 'Sábado', |
164
|
|
|
'Sunday'=> 'Domingo', |
165
|
|
|
'January'=> 'Enero', |
166
|
|
|
'February'=> 'Febrero', |
167
|
|
|
'March'=> 'Marzo', |
168
|
|
|
'April'=> 'Abril', |
169
|
|
|
'May'=> 'Mayo', |
170
|
|
|
'June'=> 'Junio', |
171
|
|
|
'July'=> 'Julio', |
172
|
|
|
'August'=> 'Agosto', |
173
|
|
|
'September'=> 'Septiembre', |
174
|
|
|
'October'=> 'Octubre', |
175
|
|
|
'November'=> 'Noviembre', |
176
|
|
|
'December'=> 'Diciembre', |
177
|
|
|
'Mon'=> 'Lun', |
178
|
|
|
'Tue'=> 'Mar', |
179
|
|
|
'Wed'=> 'Mie', |
180
|
|
|
'Thu'=> 'Jue', |
181
|
|
|
'Fri'=> 'Vie', |
182
|
|
|
'Sat'=> 'Sab', |
183
|
|
|
'Sun'=> 'Dom', |
184
|
|
|
'Jan'=> 'Ene', |
185
|
|
|
'Aug'=> 'Ago', |
186
|
|
|
'Apr'=> 'Abr', |
187
|
|
|
'Dec'=> 'Dic' |
188
|
|
|
); |
189
|
|
|
return str_replace(array_keys($cambios),array_values($cambios),$date); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
//------------------------------------------------ |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Devuelve la etiqueta <base> html adecuada para que los assets carguen desde allí. |
196
|
|
|
* Se adapta a la configuración del dominio en general. |
197
|
|
|
* |
198
|
|
|
* @return string <base href="ruta" /> |
199
|
|
|
*/ |
200
|
|
|
final public function base_assets() : string { |
201
|
|
|
global $config, $http; |
202
|
|
|
|
203
|
|
|
# Revisar subdominio |
204
|
|
|
$server = $http->server->get('SERVER_NAME'); |
205
|
|
|
$www = $server[0] . $server[1] . $server[2]; |
206
|
|
|
# Revisar protocolo |
207
|
|
|
$base = $config['site']['router']['protocol'] . '://'; |
208
|
|
|
|
209
|
|
|
if(strtolower($www) == 'www') { |
210
|
|
|
$base .= 'www.' . $config['site']['router']['path']; |
211
|
|
|
} else { |
212
|
|
|
$base .= $config['site']['router']['path']; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return '<base href="'.$base.'" />'; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
//------------------------------------------------ |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Obtiene el último día de un mes específico |
222
|
|
|
* |
223
|
|
|
* @param int $mes: Mes (1 a 12) |
224
|
|
|
* @param int $anio: Año (1975 a 2xxx) |
225
|
|
|
* |
226
|
|
|
* @return string con el número del día |
227
|
|
|
*/ |
228
|
|
|
final public function last_day_month(int $mes, int $anio) : string { |
229
|
|
|
return date('d', (mktime(0,0,0,$mes + 1, 1, $anio) - 1)); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
//------------------------------------------------ |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Pone un cero a la izquierda si la cifra es menor a diez |
236
|
|
|
* |
237
|
|
|
* @param int $num: cifra |
238
|
|
|
* |
239
|
|
|
* @return string cifra con cero a la izquirda |
240
|
|
|
*/ |
241
|
|
|
final public function cero_izq(int $num) : string { |
242
|
|
|
if($num < 10) { |
243
|
|
|
return '0' . $num; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
return $num; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
//------------------------------------------------ |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Devuelve el timestamp de una fecha, y null si su formato es incorrecto. |
253
|
|
|
* |
254
|
|
|
* @param string|null $fecha: Fecha con formato dd/mm/yy |
255
|
|
|
* @param string $hora: Hora de inicio de la $fecha |
256
|
|
|
* |
257
|
|
|
* @return int|null con el timestamp |
258
|
|
|
*/ |
259
|
|
|
final public function str_to_time($fecha, string $hora = '00:00:00') { |
260
|
|
|
if(null == $fecha) { |
261
|
|
|
return null; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
$detail = explode('/',$fecha); |
265
|
|
|
|
266
|
|
|
// Formato de día incorrecto |
267
|
|
|
if($this->check_str_to_time(0,$detail,1)) { |
268
|
|
|
return null; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
// Formato de mes incorrecto |
272
|
|
|
if($this->check_str_to_time(1,$detail,1) || intval($detail[1]) > 12) { |
273
|
|
|
return null; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
// Formato del año |
277
|
|
|
if($this->check_str_to_time(2,$detail,1970)) { |
278
|
|
|
return null; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
// Verificar días según año y mes |
282
|
|
|
$day = intval($detail[0]); |
283
|
|
|
$month = intval($detail[1]); |
284
|
|
|
$year = intval($detail[2]); |
285
|
|
|
|
286
|
|
|
// Veriricar dia según mes |
287
|
|
|
if($day > $this->last_day_month($month,$year)) { |
288
|
|
|
return null; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
return strtotime($detail[0] . '-' . $detail[1] . '-' . $detail[2] . ' ' . $hora); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
//------------------------------------------------ |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Devuelve la fecha en format dd/mm/yyy desde el principio de la semana, mes o año actual. |
298
|
|
|
* |
299
|
|
|
* @param int $desde: Desde donde |
300
|
|
|
* |
301
|
|
|
* @return mixed |
302
|
|
|
*/ |
303
|
|
|
final public function desde_date(int $desde) { |
304
|
|
|
# Obtener esta fecha |
305
|
|
|
$hoy = date('d/m/Y/D',time()); |
306
|
|
|
$hoy = explode('/',$hoy); |
307
|
|
|
|
308
|
|
|
|
309
|
|
|
switch($desde) { |
310
|
|
|
# Hoy |
311
|
|
|
case 1: |
312
|
|
|
return date('d/m/Y', time()); |
313
|
|
|
|
314
|
|
|
# Ayer |
315
|
|
|
case 2: |
316
|
|
|
return date('d/m/Y', time() - (60*60*24)); |
317
|
|
|
|
318
|
|
|
# Semana |
319
|
|
|
case 3: |
320
|
|
|
# Día de la semana actual |
321
|
|
|
switch($hoy[3]) { |
322
|
|
|
case 'Mon': |
323
|
|
|
$dia = $hoy[0]; |
324
|
|
|
break; |
325
|
|
|
case 'Tue': |
326
|
|
|
$dia = intval($hoy[0]) - 1; |
327
|
|
|
break; |
328
|
|
|
case 'Wed': |
329
|
|
|
$dia = intval($hoy[0]) - 2; |
330
|
|
|
break; |
331
|
|
|
case 'Thu': |
332
|
|
|
$dia = intval($hoy[0]) - 3; |
333
|
|
|
break; |
334
|
|
|
case 'Fri': |
335
|
|
|
$dia = intval($hoy[0]) - 4; |
336
|
|
|
break; |
337
|
|
|
case 'Sat': |
338
|
|
|
$dia = intval($hoy[0]) - 5; |
339
|
|
|
break; |
340
|
|
|
default: # 'Sun' |
341
|
|
|
$dia = intval($hoy[0]) - 6; |
342
|
|
|
break; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
# Mes anterior y posiblemente, año también. |
346
|
|
|
if($dia == 0) { |
347
|
|
|
# Verificamos si estamos en enero |
348
|
|
|
if($hoy[1] == 1) { |
349
|
|
|
return $this->last_day_month($hoy[1],$hoy[2]) .'/'. $this->cero_izq($hoy[1] - 1) .'/' . ($hoy[2] - 1); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
# Si no es enero, es el año actual |
353
|
|
|
return $this->last_day_month($hoy[1],$hoy[2]) .'/'. $this->cero_izq($hoy[1] - 1) .'/' . $hoy[2]; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
return $this->cero_izq($dia) .'/'. $this->cero_izq($hoy[1]) .'/' . $hoy[2]; |
357
|
|
|
|
358
|
|
|
# Mes |
359
|
|
|
case 4: |
360
|
|
|
return '01/'. $this->cero_izq($hoy[1]) .'/' . $hoy[2]; |
361
|
|
|
|
362
|
|
|
# Año |
363
|
|
|
case 5: |
364
|
|
|
return '01/01/' . $hoy[2]; |
365
|
|
|
|
366
|
|
|
default: |
367
|
|
|
throw new \RuntimeException('Problema con el valor $desde en desde_date()'); |
368
|
|
|
break; |
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
//------------------------------------------------ |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Obtiene el tiempo actual |
376
|
|
|
* |
377
|
|
|
* @return int devuelve time() |
378
|
|
|
*/ |
379
|
|
|
final public function timestamp() : int { |
380
|
|
|
return time(); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
//------------------------------------------------ |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* Se obtiene de Twig_Extension y sirve para que cada función esté disponible como etiqueta en twig |
387
|
|
|
* |
388
|
|
|
* @return array con todas las funciones con sus respectivos nombres de acceso en plantillas twig |
389
|
|
|
*/ |
390
|
|
|
public function getFunctions() : array { |
391
|
|
|
return array( |
392
|
|
|
new \Twig_Function('percent', array($this, 'percent')), |
393
|
|
|
new \Twig_Function('convert', array($this, 'convert')), |
394
|
|
|
new \Twig_Function('get_gravatar', array($this, 'get_gravatar')), |
395
|
|
|
new \Twig_Function('emp', array($this, 'emp')), |
396
|
|
|
new \Twig_Function('e_dynamic', array($this, 'e')), |
397
|
|
|
new \Twig_Function('all_full', array($this, 'all_full')), |
398
|
|
|
new \Twig_Function('fecha', array($this, 'fecha')), |
399
|
|
|
new \Twig_Function('base_assets',array($this, 'base_assets')), |
400
|
|
|
new \Twig_Function('timestamp',array($this, 'timestamp')), |
401
|
|
|
new \Twig_Function('desde_date',array($this, 'desde_date')), |
402
|
|
|
new \Twig_Function('cero_izq',array($this, 'cero_izq')), |
403
|
|
|
new \Twig_Function('last_day_month',array($this, 'last_day_month')), |
404
|
|
|
new \Twig_Function('str_to_time',array($this, 'str_to_time')), |
405
|
|
|
new \Twig_Function('desde_date',array($this, 'desde_date')) |
406
|
|
|
); |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
//------------------------------------------------ |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* Identificador único para la extensión de twig |
413
|
|
|
* |
414
|
|
|
* @return string con el nombre de la extensión |
415
|
|
|
*/ |
416
|
|
|
public function getName() : string { |
417
|
|
|
return 'ocrend_framework_func_class'; |
418
|
|
|
} |
419
|
|
|
} |
420
|
|
|
|