1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Uccello\Core\Http\Controllers\Core; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Cache; |
6
|
|
|
use Uccello\Core\Models\Domain; |
7
|
|
|
|
8
|
|
|
class DomainsTreeController |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Returns all roots domains where the user can access |
12
|
|
|
* |
13
|
|
|
* @return array |
14
|
|
|
*/ |
15
|
|
|
public function root(?Domain $domain) |
16
|
|
|
{ |
17
|
|
|
// If we don't use multi domains, find the first one |
18
|
|
|
if (!uccello()->useMultiDomains()) { |
|
|
|
|
19
|
|
|
$domain = Domain::firstOrFail(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
return Cache::rememberForever('domains_root_user_'.auth()->id(), function () use($domain) { |
23
|
|
|
$rootDomains = app('uccello')->getRootDomains(); |
|
|
|
|
24
|
|
|
|
25
|
|
|
$domains = []; |
26
|
|
|
foreach ($rootDomains as $_domain) { |
27
|
|
|
$formattedDomain = $this->getFormattedDomainToAdd($domain, $_domain); |
28
|
|
|
if ($formattedDomain) { |
29
|
|
|
$domains[] = $formattedDomain; |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return $domains; |
34
|
|
|
}); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Returns all domain's children where the user can access |
39
|
|
|
* |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
public function children(?Domain $domain) |
43
|
|
|
{ |
44
|
|
|
// If we don't use multi domains, find the first one |
45
|
|
|
if (!uccello()->useMultiDomains()) { |
|
|
|
|
46
|
|
|
$domain = Domain::firstOrFail(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$parentDomain = Domain::find(request('id')); |
50
|
|
|
|
51
|
|
|
return Cache::rememberForever('domains_'.$parentDomain->id.'_children_user_'.auth()->id(), function () use($domain, $parentDomain) { |
52
|
|
|
$domains = []; |
53
|
|
|
if ($parentDomain) { |
54
|
|
|
foreach ($parentDomain->children()->orderBy('name')->get() as $_domain) { |
55
|
|
|
$formattedDomain = $this->getFormattedDomainToAdd($domain, $_domain); |
56
|
|
|
if ($formattedDomain) { |
57
|
|
|
$domains[] = $formattedDomain; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $domains; |
63
|
|
|
}); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get formatted domain to add to the tree |
68
|
|
|
* |
69
|
|
|
* @param \Uccello\Core\Models\Domain $currentDomain |
70
|
|
|
* @param \Uccello\Core\Models\Domain $domain |
71
|
|
|
* @return array|null |
72
|
|
|
*/ |
73
|
|
|
protected function getFormattedDomainToAdd(Domain $currentDomain, Domain $domain) |
74
|
|
|
{ |
75
|
|
|
$formattedDomain = null; |
76
|
|
|
|
77
|
|
|
$hasRoleOnDomain = auth()->user()->hasRoleOnDomain($domain); |
78
|
|
|
$hasRoleOnDescendantDomain = $domain->children->count() > 0 ? auth()->user()->hasRoleOnDescendantDomain($domain) : false; |
79
|
|
|
|
80
|
|
|
if ($hasRoleOnDomain || $hasRoleOnDescendantDomain) { |
81
|
|
|
$formattedDomain = [ |
82
|
|
|
"id" => $domain->id, |
83
|
|
|
"text" => $domain->name, |
84
|
|
|
"children" => $domain->children->count() > 0, |
85
|
|
|
"a_attr" => [ |
86
|
|
|
"href" => $hasRoleOnDomain && $domain->id !== $currentDomain->id ? ucroute('uccello.home', $domain) : '#', |
87
|
|
|
"class" => !$hasRoleOnDomain ? 'disabled' : $domain->id === $currentDomain->id ? 'current' : '' |
88
|
|
|
] |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $formattedDomain; |
93
|
|
|
} |
94
|
|
|
} |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.