1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Helpers to Validate some data with laravel validator. |
5
|
|
|
* |
6
|
|
|
* @param string|array $fields |
7
|
|
|
* @param string|array $rules |
8
|
|
|
* |
9
|
|
|
* @return bool |
10
|
|
|
*/ |
11
|
|
|
function validate($fields, $rules): bool |
12
|
|
|
{ |
13
|
|
|
if (!is_array($fields)) { |
14
|
|
|
$fields = ['default' => $fields]; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
if (!is_array($rules)) { |
18
|
|
|
$rules = ['default' => $rules]; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
return Validator::make($fields, $rules)->passes(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* getLocale |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
|
|
function locale(): string |
29
|
|
|
{ |
30
|
|
|
return app()->getLocale(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Return true if current user is logged, otherwise return false. |
35
|
|
|
* @return bool |
36
|
|
|
*/ |
37
|
|
|
function userIsLogged() : bool |
38
|
|
|
{ |
39
|
|
|
if (Auth::guest()) { |
40
|
|
|
return false; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return true; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (!function_exists('current_user')) { |
47
|
|
|
/** |
48
|
|
|
* Fetch currently logged in useruser() |
49
|
|
|
* if User not logged in return false. |
50
|
|
|
* Otherwise return |
51
|
|
|
* a) User object of currently logged in user if $field is null or empty |
52
|
|
|
* b) return single field user()->{$field} if $field is not empty and not null |
53
|
|
|
* |
54
|
|
|
* Returns false if not logged in |
55
|
|
|
* @param string $field |
56
|
|
|
* @return mixed |
57
|
|
|
*/ |
58
|
|
|
function current_user(string $field = '') |
59
|
|
|
{ |
60
|
|
|
if (!Auth::check()) { |
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($field == 'id') { |
65
|
|
|
return Auth::id(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$user = Auth::user(); |
69
|
|
|
|
70
|
|
|
if ($field === null || $field == '') { |
71
|
|
|
return $user; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $user->{$field}; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Fetch log of database queries |
80
|
|
|
* |
81
|
|
|
* @param bool $last [false] - if true, only return last query |
82
|
|
|
* @return array of queries |
83
|
|
|
*/ |
84
|
|
|
function queries($last = false) |
85
|
|
|
{ |
86
|
|
|
$queries = \DB::getQueryLog(); |
87
|
|
|
|
88
|
|
|
foreach ($queries as &$query) { |
89
|
|
|
$query['look'] = query_interpolate($query['query'], $query['bindings']); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if ($last) { |
93
|
|
|
return end($queries); |
94
|
|
|
} |
95
|
|
|
return $queries; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Echo log of database queries |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
function query_table() : string |
104
|
|
|
{ |
105
|
|
|
$queries = queries(); |
106
|
|
|
$html = '<table style="background-color: #FFFF00;border: 1px solid #000000;color: #000000;padding-left: 10px;padding-right: 10px;width: 100%;">'; |
107
|
|
|
foreach ($queries as $query) { |
108
|
|
|
$html .= '<tr style="border-top: 1px dashed #000000;"><td style="padding:8px;">' . e($query['look']) . '</td><td style="padding:8px;">' . e($query['time']) . '</td></tr>'; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $html . '</table>'; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Replaces any parameter placeholders in a query with the value of that |
116
|
|
|
* parameter. Useful for debugging. Assumes anonymous parameters from |
117
|
|
|
* $params are are in the same order as specified in $query |
118
|
|
|
* @author glendemon |
119
|
|
|
* |
120
|
|
|
* @param string $query The sql query with parameter placeholders |
121
|
|
|
* @param array $params The array of substitution parameters |
122
|
|
|
* @return string The interpolated query |
123
|
|
|
*/ |
124
|
|
|
function query_interpolate($query, $params) |
125
|
|
|
{ |
126
|
|
|
$keys = array(); |
127
|
|
|
$values = $params; |
128
|
|
|
foreach ($params as $key => $value) { |
129
|
|
|
if (is_string($key)) { |
130
|
|
|
$keys[] = '/:' . $key . '/'; |
131
|
|
|
} else { |
132
|
|
|
$keys[] = '/[?]/'; |
133
|
|
|
} |
134
|
|
|
if (is_array($value)) { |
135
|
|
|
$values[$key] = implode(',', $value); |
136
|
|
|
} |
137
|
|
|
if (is_null($value)) { |
138
|
|
|
$values[$key] = 'NULL'; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
// Walk the array to see if we can add single-quotes to strings |
142
|
|
|
array_walk($values, function(&$v) { if (!is_numeric($v) && $v!="NULL") $v = "'".$v."'";}); |
143
|
|
|
$query = preg_replace($keys, $values, $query, 1, $count); |
144
|
|
|
return $query; |
145
|
|
|
} |
146
|
|
|
|