-
Notifications
You must be signed in to change notification settings - Fork 1
/
Util.php
178 lines (150 loc) · 5.21 KB
/
Util.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/**
*
* @author Wilson Ramiro Champi Tacuri
*/
class ZendR_Util
{
public static function prepareStringByUrl($string)
{
$filter = new Zend_Filter_Alnum();
$filter->setAllowWhiteSpace(true);
$stringFilter = $filter->filter(strip_tags($string));
$stringFilter = ZendR_String::parseString($stringFilter)->toLower()->toUTF8();
$stringFilter = preg_replace('[aáàãâä]','a',$stringFilter);
$stringFilter = preg_replace('[eéèêë]','e',$stringFilter);
$stringFilter = preg_replace('[iíìîï]','i',$stringFilter);
$stringFilter = preg_replace('[oóòõôö]','o',$stringFilter);
$stringFilter = preg_replace('[uúùûü]','u',$stringFilter);
$stringFilter = preg_replace('[ç]','c',$stringFilter);
$stringFilter = preg_replace('[ñ]','n',$stringFilter);
$stringFilter = str_replace(' ', '+', $stringFilter);
$stringFilter = str_replace("\\", '', $stringFilter);
return $stringFilter;
}
public static function prepareEditOptionsSelectJqFrid($options)
{
$editOptions = array();
foreach ($options as $key => $value) {
$editOptions[] = $key . ':' . $value;
}
return implode(';', $editOptions);
}
public static function prepareResponseJqGrid($page, $rowsByPage, $rowsTotal, $rows, $indexId = 'id')
{
if ($rowsTotal > 0) {
$totalPages = ceil($rowsTotal / $rowsByPage);
} else {
$totalPages = 0;
}
$response = array(
'page' => $page,
'total' => $totalPages,
'records' => $rowsTotal,
'rows' => array()
);
foreach ($rows as $row) {
$id = $row[$indexId];
$response['rows'][] = array(
'id' => $id,
'cell' => array_values($row),
);
}
return $response;
}
public static function fetchOffset($limit, $page)
{
return $limit * ($page - 1);
}
public static function generateArbitraryCode($numeroLetras = 12)
{
$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
$cad = "";
for ($i = 1; $i <= $numeroLetras; $i++) {
$cad .= substr($str, rand(0, 62), 1);
}
return $cad;
}
public static function obtenerComision($porcentaje)
{
return 100 / (100 - $porcentaje);
}
public static function redondeo($valor)
{
if (($valor - floor($valor)) < 0.1) {
$valor = floor($valor);
} else {
$valor = ceil($valor);
}
return $valor;
}
public static function obtenerPathIniString($path)
{
$content = 'path = ' . $path;
$fileIniTmp = tempnam(sys_get_temp_dir(), 'ini');
file_put_contents($fileIniTmp, $content);
$iniArray = parse_ini_file($fileIniTmp);
unlink($fileIniTmp);
return $iniArray['path'];
}
public static function collectionRand($collection, $numReq)
{
if (count($collection) > 0) {
if (is_array($collection)) {
if (count($collection) < $numReq) {
$numReq = count($collection);
}
return array_rand($collection, $numReq);
} else {
$arrayKeys = array();
foreach ($collection as $key => $value) {
$arrayKeys[] = $key;
}
if (count($arrayKeys) < $numReq) {
$numReq = count($arrayKeys);
}
$arrayKeys = array_rand($arrayKeys, $numReq);
$arrayValues = array();
if (is_array($arrayKeys)) {
foreach ($arrayKeys as $key) {
$arrayValues[] = $collection[$key];
}
} else {
$arrayValues[] = $collection[$arrayKeys];
}
return $arrayValues;
}
}
return array();
}
public static function arraySort($array, $on, $order = SORT_ASC)
{
$new_array = array();
$sortable_array = array();
if (count($array) > 0) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $on) {
$sortable_array[$k] = $v2;
}
}
} else {
$sortable_array[$k] = $v;
}
}
switch ($order) {
case SORT_ASC:
asort($sortable_array);
break;
case SORT_DESC:
arsort($sortable_array);
break;
}
foreach ($sortable_array as $k => $v) {
$new_array[$k] = $array[$k];
}
}
return $new_array;
}
}