|
1
|
|
|
<?php namespace Anomaly\Streams\Platform\Ui\ControlPanel\Component\Section\Command; |
|
2
|
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Support\Authorizer; |
|
4
|
|
|
use Anomaly\Streams\Platform\Ui\Breadcrumb\BreadcrumbCollection; |
|
5
|
|
|
use Anomaly\Streams\Platform\Ui\ControlPanel\Component\Section\Contract\SectionInterface; |
|
6
|
|
|
use Anomaly\Streams\Platform\Ui\ControlPanel\ControlPanelBuilder; |
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class SetActiveSection |
|
11
|
|
|
* |
|
12
|
|
|
* @link http://pyrocms.com/ |
|
13
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
|
14
|
|
|
* @author Ryan Thompson <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class SetActiveSection |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The control_panel builder. |
|
21
|
|
|
* |
|
22
|
|
|
* @var ControlPanelBuilder |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $builder; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Create a new SetActiveSection instance. |
|
28
|
|
|
* |
|
29
|
|
|
* @param ControlPanelBuilder $builder |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(ControlPanelBuilder $builder) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->builder = $builder; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Handle the command. |
|
38
|
|
|
* |
|
39
|
|
|
* @param Request $request |
|
40
|
|
|
* @param Authorizer $authorizer |
|
41
|
|
|
* @param BreadcrumbCollection $breadcrumbs |
|
42
|
|
|
*/ |
|
43
|
|
|
public function handle(Request $request, Authorizer $authorizer, BreadcrumbCollection $breadcrumbs) |
|
44
|
|
|
{ |
|
45
|
|
|
$controlPanel = $this->builder->getControlPanel(); |
|
46
|
|
|
$sections = $controlPanel->getSections(); |
|
47
|
|
|
|
|
48
|
|
|
/* |
|
49
|
|
|
* If we already have an active section |
|
50
|
|
|
* then we don't need to do this. |
|
51
|
|
|
*/ |
|
52
|
|
|
if ($active = $sections->active()) { |
|
53
|
|
|
return; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/* @var SectionInterface $section */ |
|
57
|
|
|
foreach ($sections as $section) { |
|
58
|
|
|
|
|
59
|
|
|
if (($matcher = $section->getMatcher()) && str_is($matcher, $request->path())) { |
|
60
|
|
|
$active = $section; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/* |
|
64
|
|
|
* Get the HREF for both the active |
|
65
|
|
|
* and loop iteration section. |
|
66
|
|
|
*/ |
|
67
|
|
|
$href = $section->getPermalink() ?: array_get($section->getAttributes(), 'href'); |
|
68
|
|
|
$activeHref = ''; |
|
69
|
|
|
|
|
70
|
|
|
if ($active && $active instanceof SectionInterface) { |
|
71
|
|
|
$activeHref = $active->getPermalink() ?: array_get($active->getAttributes(), 'href'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/* |
|
75
|
|
|
* If the request URL does not even |
|
76
|
|
|
* contain the HREF then skip it. |
|
77
|
|
|
*/ |
|
78
|
|
|
if (!str_contains($request->url(), $href)) { |
|
79
|
|
|
continue; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/* |
|
83
|
|
|
* Compare the length of the active HREF |
|
84
|
|
|
* and loop iteration HREF. The longer the |
|
85
|
|
|
* HREF the more detailed and exact it is and |
|
86
|
|
|
* the more likely it is the active HREF and |
|
87
|
|
|
* therefore the active section. |
|
88
|
|
|
*/ |
|
89
|
|
|
$hrefLength = strlen($href); |
|
90
|
|
|
$activeHrefLength = strlen($activeHref); |
|
91
|
|
|
|
|
92
|
|
|
if ($hrefLength > $activeHrefLength) { |
|
93
|
|
|
$active = $section; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* If we have an active section determined |
|
99
|
|
|
* then mark it as such. |
|
100
|
|
|
* |
|
101
|
|
|
* @var SectionInterface $active |
|
102
|
|
|
* @var SectionInterface $section |
|
103
|
|
|
*/ |
|
104
|
|
|
if ($active) { |
|
105
|
|
|
if ($active->getParent()) { |
|
|
|
|
|
|
106
|
|
|
$active->setActive(true); |
|
107
|
|
|
|
|
108
|
|
|
$section = $sections->get($active->getParent(), $sections->first()); |
|
109
|
|
|
|
|
110
|
|
|
$section->setHighlighted(true); |
|
111
|
|
|
|
|
112
|
|
|
$breadcrumbs->put($section->getBreadcrumb() ?: $section->getTitle(), $section->getHref()); |
|
113
|
|
|
} else { |
|
114
|
|
|
$active->setActive(true)->setHighlighted(true); |
|
115
|
|
|
} |
|
116
|
|
|
} elseif ($active = $sections->first()) { |
|
117
|
|
|
$active->setActive(true)->setHighlighted(true); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
// No active section! |
|
121
|
|
|
if (!$active) { |
|
122
|
|
|
return; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
// Authorize the active section. |
|
126
|
|
|
if (!$authorizer->authorize($active->getPermission())) { |
|
127
|
|
|
abort(403); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
// Add the bread crumb. |
|
131
|
|
View Code Duplication |
if (($breadcrumb = $active->getBreadcrumb()) !== false) { |
|
|
|
|
|
|
132
|
|
|
$breadcrumbs->put($breadcrumb ?: $active->getTitle(), $active->getHref()); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: