-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.php
More file actions
executable file
·359 lines (301 loc) · 9.66 KB
/
helpers.php
File metadata and controls
executable file
·359 lines (301 loc) · 9.66 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
if (!function_exists('artisan_call')) {
function artisan_call($command, array $parameters = array())
{
$buffer = new \Symfony\Component\Console\Output\BufferedOutput();
\Artisan::call($command, $parameters, $buffer);
return $buffer->fetch();
}
}
if (!function_exists('is_number')) {
function is_number($number)
{
return ctype_digit((string) $number);
}
}
if (!function_exists('truncate')) {
/* http://stackoverflow.com/a/9219884 */
function truncate($text, $chars = 25)
{
$text = $text.' ';
$text = substr($text, 0, $chars);
$text = substr($text, 0, strrpos($text, ' '));
$text = $text.'...';
return $text;
}
}
if (!function_exists('partial')) {
function partial($view)
{
$theme = Request::is(config('cms.core.app.paths.backend').'*')
? config('cms.core.app.themes.backend')
: config('cms.core.app.themes.frontend');
$viewStr = 'theme.'.$theme.'::views.modules.'.str_replace('::', '.', $view);
if (view()->exists($viewStr)) {
$view = $viewStr;
}
return $view;
}
}
if (!function_exists('date_array')) {
/**
* Compiles a list of dates to return.
*
* @return array
*/
function date_array($date)
{
$return = [
'default' => $date,
'atom' => date_carbon($date, DateTime::ATOM),
'ago' => date_ago($date),
'fuzzy' => date_fuzzy($date),
];
$return['element'] = sprintf(
'<time datetime="%s" title="%s">%s</time>',
array_get($return, 'atom'),
array_get($return, 'fuzzy'),
in_array($date, [null, '0000-00-00 00:00:00']) ? 'Never' : array_get($return, 'ago')
);
return $return;
}
}
if (!function_exists('date_carbon')) {
function date_carbon($value, $format = null)
{
if (!Config::has('cms.core.config.date-format')) {
return $value;
} else {
if ($format === null) {
$format = config('cms.core.config.date-format');
}
}
if (is_number($value)) {
$carbon = \Carbon\Carbon::createFromTimeStamp($value)
->timezone(config('app.timezone'));
} else {
$carbon = \Carbon\Carbon::parse($value)
->timezone(config('app.timezone'));
}
if ($format !== null) {
$carbon = $carbon->format($format);
}
return $carbon;
}
}
if (!function_exists('date_now')) {
function date_now()
{
// returns current time/date
return date('Y-m-d H:i:s');
}
}
if (!function_exists('date_ago')) {
function date_ago($string)
{
return \Carbon\Carbon::createFromTimeStamp(strtotime($string))
->timezone(config('app.timezone'))
->diffForHumans();
}
}
if (!function_exists('date_difference')) {
function date_difference($string)
{
return \Carbon\Carbon::now()
->subSeconds($string)
->timezone(config('app.timezone'))
->diffForHumans();
}
}
if (!function_exists('date_fuzzy')) {
/* http://daveyshafik.com/archives/28101-datetime-timestamp-parsing.html */
function date_fuzzy($date, $inputFormat = null, $outputDateFormat = null, $outputTimeFormat = null)
{
$inputFormat = $inputFormat ?: DateTime::ATOM;
$outputDateFormat = $outputDateFormat ?: 'F dS Y';
$outputTimeFormat = $outputTimeFormat ?: 'H:ia';
$dateTime = (is_int($date) ? with(new Carbon\Carbon())->createFromTimestamp($date) : new DateTime($date));
// Failed to parse, probably invalid date
if (!$dateTime) {
return false;
}
// Get Timezone so we can use it for the other dates
$timezone = $dateTime->getTimeZone();
// Fuzzy Date ranges
$lastWeekStart = new DateTime('2 weeks ago sunday 11:59:59', $timezone);
$yesterdayStart = new DateTime('yesterday midnight', $timezone);
$todayStart = new DateTime('today midnight', $timezone);
$todayEnd = new DateTime('today 23:59:59', $timezone);
// $tomorrowStart = new DateTime('tomorrow midnight', $timezone);
$tomorrowEnd = new DateTime('tomorrow 23:59:59', $timezone);
$thisWeekStart = new DateTime('1 week ago sunday 11:59:59', $timezone);
$thisWeekEnd = new DateTime('sunday 11:59:59', $timezone);
$nextWeekEnd = new DateTime('1 week sunday midnight', $timezone);
$prefix = '';
// We have to start with the oldest ones first
if ($dateTime < $lastWeekStart) {
// Older than 1 week
$prefix = 'on';
$fuzzyDate = ucwords($dateTime->format($outputDateFormat));
} elseif ($dateTime > $lastWeekStart && $dateTime < $thisWeekStart) {
// Some time in the previous week
$prefix = 'Last';
$fuzzyDate = ucwords($dateTime->format('l'));
} elseif ($dateTime > $thisWeekStart && $dateTime < $yesterdayStart) {
// Some time in the this week
$fuzzyDate = ucwords($dateTime->format('l'));
} elseif ($dateTime > $yesterdayStart && $dateTime < $todayStart) {
// Yesterday
$fuzzyDate = 'Yesterday';
} elseif ($dateTime < $todayEnd) {
// Today
$fuzzyDate = 'Today';
} elseif ($dateTime < $tomorrowEnd) {
// Tomorrow
$fuzzyDate = 'Tomorrow';
} elseif ($dateTime < $thisWeekEnd) {
// Sometime in the current week
$prefix = 'This';
$fuzzyDate = ucwords($dateTime->format('l'));
} elseif ($dateTime < $nextWeekEnd) {
// Some time in the following week
$prefix = 'Next';
$fuzzyDate = ucwords($dateTime->format('l'));
} else {
// More than 2 weeks out.
$prefix = 'on';
$fuzzyDate = ucwords($dateTime->format($outputDateFormat));
}
// Midnight or an actual time
if ($dateTime->format('Hi') != '0000') {
$fuzzyTime = $dateTime->format($outputTimeFormat);
} else {
$fuzzyTime = 'midnight';
}
$format = '%s %s at %s';
return trim(sprintf($format, $prefix, $fuzzyDate, $fuzzyTime));
}
}
if (!function_exists('get_array_column')) {
function get_array_column($array, $key)
{
return array_filter(array_map(function ($row) use ($key) {
return array_get($row, $key, null);
}, $array));
}
}
if (!function_exists('getCurrentTheme')) {
function getCurrentTheme()
{
if (Request::is('admin/*')) {
return config('cms.core.app.themes.backend', 'default_admin');
}
return config('cms.core.app.themes.frontend', 'default');
}
}
if (!function_exists('escape')) {
function escape($value)
{
return Purifier::clean($value);
}
}
if (!function_exists('cache_remember')) {
function cache_remember($tag, $key, $length, $callback)
{
if (Cache::getFacadeRoot() instanceof TaggableStore) {
return Cache::tags($tag)->remember($key, $length, $callback);
}
if (is_array($tag)) {
$tag = implode('.', $tag).'.';
}
return Cache::remember($tag.$key, $length, $callback);
}
}
if (!function_exists('cache_forever')) {
function cache_forever($tag, $key, $callback)
{
if (Cache::getFacadeRoot() instanceof TaggableStore) {
return Cache::tags($tag)->rememberForever($key, $callback);
}
if (is_array($tag)) {
$tag = implode('.', $tag).'.';
}
return Cache::rememberForever($tag.$key, $callback);
}
}
if (!function_exists('cache_flush')) {
function cache_flush($tag)
{
if (Cache::getFacadeRoot() instanceof TaggableStore) {
Cache::tags($tag)->flush();
return;
}
artisan_call('cache:clear');
}
}
if (!function_exists('cache_has')) {
function cache_has($tag, $key)
{
if (Cache::getFacadeRoot() instanceof TaggableStore) {
return Cache::tags($tag)->has($key);
}
if (is_array($tag)) {
$tag = implode('.', $tag).'.';
}
return Cache::has($tag.$key);
}
}
if (!function_exists('cache_get')) {
function cache_get($tag, $key)
{
if (Cache::getFacadeRoot() instanceof TaggableStore) {
return Cache::tags($tag)->get($key);
}
if (is_array($tag)) {
$tag = implode('.', $tag).'.';
}
return Cache::get($tag.$key);
}
}
if (!function_exists('transform_button_args')) {
/**
* Run a transformer for 'segment:x' calls for the button helper.
*
* @param array $args
*
* @return array
*/
function transform_button_args($args)
{
if (!count($args)) {
return $args;
}
foreach ($args as $key => $value) {
if (substr($value, 0, 7) == 'segment') {
list(, $value) = explode(':', $value);
$args[$key] = Request::segment($value);
}
}
return $args;
}
}
if (!function_exists('validate')) {
/**
* Validate some data.
*
* @param string|array $fields
* @param string|array $rules
*
* @return bool
*/
function validate($fields, $rules)
{
if (!is_array($fields)) {
$fields = ['default' => $fields];
}
if (!is_array($rules)) {
$rules = ['default' => $rules];
}
return \Validator::make($fields, $rules)->passes();
}
}