-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathhelpers.php
More file actions
379 lines (344 loc) · 9.3 KB
/
helpers.php
File metadata and controls
379 lines (344 loc) · 9.3 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
use Illuminate\Support\Facades\Auth;
use Laravel\Horizon\Repositories\RedisJobRepository;
use ProcessMaker\Events\MarkArtisanCachesAsInvalid;
use ProcessMaker\Models\FormalExpression;
use ProcessMaker\Models\Setting;
use ProcessMaker\SanitizeHelper;
use ProcessMaker\Support\JsonOptimizer;
if (!function_exists('job_pending')) {
/**
* Check if a given job (by instance or class) is already pending in the queue.
*
* @param $job
*
* @return bool
*/
function job_pending($job): bool
{
if (is_object($job)) {
$job = class_basename($job);
}
$queue = app(RedisJobRepository::class)->getRecent();
return $queue->filter(
function ($queued) use ($job) {
return $queued->name === $job
&& ('delayed' === $queued->status
|| 'pending' === $queued->status);
}
)->isNotEmpty();
}
}
if (!function_exists('settings')) {
/**
* Forwards call to the config() helper.
*
* TODO Remove this helper function as it exists now only for backwards compatability
*
* @param $key
*
* @throws Psr\Container\ContainerExceptionInterface
* @throws Psr\Container\NotFoundExceptionInterface
*
* @return array|mixed
*/
function settings($key = null)
{
return $key ? config()->get($key) : config()->all();
}
}
if (!function_exists('default_color')) {
/**
* Returns the default color value.
*
* @param $key
*
* @return string
*/
function default_color($key)
{
return config('app.default_colors.' . $key);
}
}
if (!function_exists('color')) {
/**
* Returns the color value from the settings.
*
* @param $key
*
* @return string
*/
function color($key)
{
if ($setting = Setting::byKey('css-override')) {
$colors = json_decode($setting->config['variables']);
foreach ($colors as $color) {
if ($color->id === '$' . $key) {
return $color->value;
}
}
}
return default_color($key);
}
}
if (!function_exists('sidebar_class')) {
/**
* Returns the class for the sidebar based on admin settings.
*
* @return bool
*/
function sidebar_class()
{
if (Setting::byKey('css-override')) {
$defaults = ['#0872C2', '#2773F3'];
if (!in_array(color('primary'), $defaults)) {
return 'sidebar-custom';
}
}
return 'sidebar-default';
}
}
if (!function_exists('refresh_artisan_caches')) {
/**
* Re-caches artisan config, routes, and/or events when they are already cached.
*/
function refresh_artisan_caches(): void
{
MarkArtisanCachesAsInvalid::dispatch();
}
}
if (!function_exists('lavaryMenuArray')) {
/**
* Convert the Laravy menu into associative array.
*
* @param Lavary\Menu\Item $menu
* @param mixed $includeSubMenus
*
* @return array
*/
function lavaryMenuArray($menu, $includeSubMenus = false)
{
$children = [];
$subMenus = $includeSubMenus ? $menu->children() : null;
if ($subMenus) {
foreach ($subMenus as $child) {
$children[] = lavaryMenuArray($child);
}
}
return [
'title' => __($menu->title),
'id' => $menu->id,
'attributes' => $menu->attributes,
'url' => $menu->url(),
'children' => $children,
];
}
}
if (!function_exists('lavaryMenuJson')) {
/**
* Convert the Laravy menu into json string.
*
* @param Lavary\Menu\Item $menu
*
* @return false|string
*/
function lavaryMenuJson($menu)
{
return json_encode(lavaryMenuArray($menu, true));
}
}
if (!function_exists('hasPackage')) {
/**
* Check if a package exists based on its provider name.
*
* @param $name
*
* @throws Illuminate\Contracts\Container\BindingResolutionException
*
* @return bool
*/
function hasPackage($name)
{
$list = app()->make(ProcessMaker\Managers\PackageManager::class)->listPackages();
return in_array($name, $list);
}
}
if (!function_exists('pmUser')) {
/**
* Check both the web and api middleware for an existing user.
*
* @return null|ProcessMaker\Models\User
*/
function pmUser()
{
if (Auth::user()) {
return Auth::user();
}
if (Auth::guard('api')->user()) {
return Auth::guard('api')->user();
}
return null;
}
}
if (!function_exists('sanitizeVueExp')) {
/**
* @param $expression
*
* @return array|string|string[]
*/
function sanitizeVueExp($expression)
{
return SanitizeHelper::sanitizeVueExp($expression);
}
}
if (!function_exists('packTemporalData')) {
/**
* Store data into store/app/private.
*
* @param $data
*
* @return string
*/
function packTemporalData($data)
{
$uid = uniqid('data_', true);
$path = storage_path('app/private/' . $uid);
file_put_contents($path, json_encode($data));
return $uid;
}
}
if (!function_exists('unpackTemporalData')) {
/**
* @param $uid
*
* @return array|mixed
*/
function unpackTemporalData($uid)
{
$path = storage_path('app/private/' . $uid);
if (file_exists($path)) {
return json_decode(file_get_contents($path), true);
}
return [];
}
}
if (!function_exists('removeTemporalData')) {
/**
* @param $uid
*/
function removeTemporalData($uid)
{
$path = storage_path('app/private/' . $uid);
if (file_exists($path)) {
@unlink($path);
}
}
}
if (!function_exists('shouldShow')) {
/**
* @param string $element
*
* @return bool
*/
function shouldShow($element)
{
if (Session::has('visibilitySettings')) {
return Session::get('visibilitySettings')->{$element} ?? true;
} else {
return true;
}
}
}
if (!function_exists('calculateProgressById')) {
/**
* Calculates the progress percentage of a stage based on its order
* within the total number of stages.
*
* This function searches for the stage with the given ID in the provided list
* of stages. Each stage must contain at least 'id' and 'order' keys.
* If the stage is found, it calculates the progress as:
* (stage_order / total_stages) * 100
*
* The result is rounded to two decimal places.
*
* If the list of stages is `null` or empty, or if the stage with the given ID
* is not found, the function returns 0.0.
*
* @param int|null $id The ID of the stage to calculate progress for.
* @param array|null $stages An array of stages, where each stage is an associative array
* containing at least the keys 'id' and 'order'.
* This parameter can be null.
*
* @return float The progress percentage (0.0 to 100.0), rounded to two decimal places.
*/
function calculateProgressById(int|null $id, array|null $stages = [])
{
if (empty($stages)) {
return 0.0;
}
$totalStages = count($stages);
// Consider the completed stage as the last one
$totalStages++;
$currentStageOrder = 0;
foreach ($stages as $stage) {
if ($stage['id'] === $id) {
$currentStageOrder = $stage['order'];
break;
}
}
if ($currentStageOrder === 0) {
return 0.0;
}
$progress = ($currentStageOrder / $totalStages) * 100;
return round($progress, 2);
}
}
if (!function_exists('validateURL')) {
/**
* Validate the URL
*
* @return Closure
*/
function validateURL(): Closure
{
return function ($attribute, $value, $fail) {
if (!empty($value)) {
$pattern = '/^(https:\/\/|http:\/\/)(\*\.)?([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}(:\d+)?$/';
if (!preg_match($pattern, $value)) {
$fail('The URL format is invalid. It must start with http:// or https:// and be a valid domain.');
}
}
};
}
}
if (!function_exists('json_optimize_decode')) {
/**
* Decodes a JSON using simdjson if available.
*
* @param string $json
* @param bool $assoc
* @param int $depth
* @param int $options
* @return mixed
*/
function json_optimize_decode(string $json, bool $assoc = false, int $depth = 512, int $options = 0)
{
return JsonOptimizer::decode($json, $assoc, $depth, $options);
}
}
if (!function_exists('feelExpression')) {
/**
* Evaluate a BPMN FEEL expression
*
* @param string $expression
* @param array $data
* @return mixed
*/
function feelExpression(string $expression, array $data)
{
$formalExp = new FormalExpression();
$formalExp->setLanguage('FEEL');
$formalExp->setBody($expression);
return $formalExp($data);
}
}