1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Epesi\Core\System\Modules\Concerns; |
4
|
|
|
|
5
|
|
|
use Epesi\Core\System\Modules\ModuleView; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use atk4\core\SessionTrait; |
8
|
|
|
|
9
|
|
|
trait HasLinks |
10
|
|
|
{ |
11
|
|
|
use HasModule; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Create self module link |
15
|
|
|
* |
16
|
|
|
* @param string $method |
17
|
|
|
* @param array $args |
18
|
|
|
* @return string |
19
|
|
|
*/ |
20
|
|
|
final public static function selfLink($method = 'body', $args = []) |
21
|
|
|
{ |
22
|
|
|
$defaultView = self::module()::view(); |
23
|
|
|
|
24
|
|
|
$viewClass = is_a(static::class, ModuleView::class, true)? static::class: $defaultView; |
25
|
|
|
|
26
|
|
|
$viewAlias = null; |
27
|
|
|
if ($viewClass !== $defaultView) { |
28
|
|
|
$names = array_slice(explode('\\', $viewClass), -1); |
29
|
|
|
|
30
|
|
|
$viewAlias = str_ireplace('_', '-', Str::snake(reset($names))); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return url(implode('/', ['view', implode(':', array_filter([self::alias(), $viewAlias])), $method, self::encodeArgs($args)])); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Create module link |
38
|
|
|
* Associative $args are used to set module properties |
39
|
|
|
* Numeric key values are used as method arguments |
40
|
|
|
* |
41
|
|
|
* @param string $module |
42
|
|
|
* @param string $method |
43
|
|
|
* @param array $args |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
final public static function moduleLink($module, $method = 'body', $args = []) |
47
|
|
|
{ |
48
|
|
|
$alias = class_exists($module)? $module::alias(): $module; |
49
|
|
|
|
50
|
|
|
if (is_array($method) && !$args) { |
|
|
|
|
51
|
|
|
$args = $method; |
52
|
|
|
$method = 'body'; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return url(implode('/', ['view', $alias, $method, self::encodeArgs($args)])); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Decode the arguments hash and return stored arguments |
60
|
|
|
* |
61
|
|
|
* @param string $hash |
62
|
|
|
* @return array |
63
|
|
|
* |
64
|
|
|
* @throws \Illuminate\Http\Exceptions\HttpResponseException |
65
|
|
|
*/ |
66
|
|
|
final public static function decodeArgs($hash) { |
67
|
|
|
if (! $hash) return []; |
68
|
|
|
|
69
|
|
|
$args = session($hash); |
70
|
|
|
|
71
|
|
|
if (is_null($args)) abort(419); |
72
|
|
|
|
73
|
|
|
return (array) $args; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Encode arguments for the module method |
78
|
|
|
* |
79
|
|
|
* @param array|mixed $args |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
final public static function encodeArgs($args) { |
83
|
|
|
$args = (array) $args; |
84
|
|
|
|
85
|
|
|
if (! $args) return; |
|
|
|
|
86
|
|
|
|
87
|
|
|
$hash = md5(serialize($args)); |
88
|
|
|
|
89
|
|
|
session([$hash => $args]); |
90
|
|
|
|
91
|
|
|
return $hash; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.