-
Notifications
You must be signed in to change notification settings - Fork 65
/
helpers.php
194 lines (174 loc) · 4.5 KB
/
helpers.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
if (! function_exists('markdown')) {
/**
* Compile the given text to markdown document.
*
* @param string $text
* @return string
*/
function markdown($text)
{
return app(App\Services\Markdown::class)->text($text);
}
}
if (! function_exists('icon')) {
/**
* Generate FontAwesome icon tag
*
* @param string $class font-awesome class name
* @param string $addition additional class
* @param string $inline inline style
* @return string
*/
function icon($class, $addition = 'icon', $inline = null)
{
$icon = config('icons.' . $class);
$inline = $inline ? 'style="' . $inline . '"' : null;
return sprintf('<i class="%s %s" %s></i>', $icon, $addition, $inline);
}
}
if (! function_exists('attachment_path')) {
/**
* @param string $path
*
* @return string
*/
function attachment_path($path = '')
{
return public_path($path ? 'attachments' . DIRECTORY_SEPARATOR . $path : 'attachments');
}
}
if (! function_exists('gravatar_profile_url')) {
/**
* Get gravatar profile page url
*
* @param string $email
* @return string
*/
function gravatar_profile_url($email)
{
return sprintf("//www.gravatar.com/%s", md5($email));
}
}
if (! function_exists('gravatar_url')) {
/**
* Get gravatar image url
*
* @param string $email
* @param integer $size
* @return string
*/
function gravatar_url($email, $size = 48)
{
return sprintf("//www.gravatar.com/avatar/%s?s=%s", md5($email), $size);
}
}
if (! function_exists('taggable')) {
/**
* Determine if the current cache driver has cacheTags() method
*
* @return bool
*/
function taggable()
{
return ! in_array(config('cache.default'), ['file', 'database']);
}
}
if (! function_exists('link_for_sort')) {
/**
* Build HTML anchor tag for sorting
*
* @param string $column
* @param string $text
* @param array $params
* @return string
*/
function link_for_sort($column, $text, $params = [])
{
$config = config('project.params');
$direction = request()->input($config['order']);
$reverse = ($direction == 'asc') ? 'desc' : 'asc';
if (request()->input($config['sort']) == $column) {
// Update passed $text var, only if it is active sort
$text = sprintf(
"%s %s",
$direction == 'asc' ? icon('asc') : icon('desc'),
$text
);
}
$queryString = http_build_query(array_merge(
request()->except([$config['page'], $config['sort'], $config['order']]),
[$config['sort'] => $column, $config['order'] => $reverse],
$params
));
return sprintf(
'<a href="%s?%s">%s</a>',
urldecode(request()->url()),
$queryString,
$text
);
}
}
if (! function_exists('current_url')) {
/**
* Build current url string, without return param.
*
* @return string
*/
function current_url()
{
if (! request()->has('return')) {
return request()->fullUrl();
}
return sprintf(
'%s?%s',
request()->url(),
http_build_query(request()->except('return'))
);
}
}
if (! function_exists('is_api_request')) {
/**
* Determine if the current request is for HTTP api.
*
* @return bool
*/
function is_api_request()
{
return starts_with(request()->getHttpHost(), config('project.api_domain'));
}
}
if (! function_exists('optimus')) {
/**
* Create Optimus instance.
*
* @param int|null $id
* @return int|\Jenssegers\Optimus\Optimus
*/
function optimus($id = null)
{
$factory = app(\Jenssegers\Optimus\Optimus::class);
if (func_num_args() === 0) {
return $factory;
}
return $factory->encode($id);
}
}
if (! function_exists('cache_key')) {
/**
* Generate key for caching.
*
* Note. Even though the request endpoints are the same,
* the response body should be different because of the query string.
*
* @param $base
* @return string
*/
function cache_key($base)
{
$key = ($uri = request()->fullUrl())
? $base . '.' . urlencode($uri)
: $base;
return md5($key);
}
}