|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace JumpGate\ViewResolution\Resolvers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Config\Repository; |
|
6
|
|
|
use Illuminate\View\Factory; |
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
|
|
9
|
|
|
class Layout |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var \Illuminate\View\View |
|
13
|
|
|
*/ |
|
14
|
|
|
public $layout; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var \Illuminate\View\View |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $view; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var \Illuminate\Http\Request |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $request; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var \Illuminate\Config\Repository |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $config; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $layoutOptions; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param \Illuminate\View\Factory $view |
|
38
|
|
|
* @param \Illuminate\Http\Request $request |
|
39
|
|
|
* @param \Illuminate\Config\Repository $config |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(Factory $view, Request $request, Repository $config) |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
|
|
$this->view = $view; |
|
|
|
|
|
|
44
|
|
|
$this->request = $request; |
|
45
|
|
|
$this->config = $config; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Set up the initial layout to be used. |
|
50
|
|
|
* |
|
51
|
|
|
* @param $layoutOptions |
|
52
|
|
|
* |
|
53
|
|
|
* @return $this |
|
54
|
|
|
*/ |
|
55
|
|
View Code Duplication |
public function setUp($layoutOptions) |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
|
|
$this->layoutOptions = $this->verifyLayoutOptions($layoutOptions); |
|
|
|
|
|
|
58
|
|
|
$this->layout = $this->determineLayout(null); |
|
59
|
|
|
$this->setPageTitle(); |
|
60
|
|
|
$this->layout->content = null; |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Change the selected layout to something else and ready it for content. |
|
67
|
|
|
* |
|
68
|
|
|
* @param $view |
|
69
|
|
|
* |
|
70
|
|
|
* @return $this |
|
71
|
|
|
*/ |
|
72
|
|
View Code Duplication |
public function change($view) |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
$this->layout = $this->determineLayout($view); |
|
75
|
|
|
$this->setPageTitle(); |
|
76
|
|
|
$this->layout->content = null; |
|
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Auto determine a logic page title. |
|
83
|
|
|
*/ |
|
84
|
|
|
public function setPageTitle() |
|
85
|
|
|
{ |
|
86
|
|
|
$area = $this->request->segment(1); |
|
87
|
|
|
$location = ($this->request->segment(2) != null ? ': ' . ucwords($this->request->segment(2)) : ''); |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
if ($area != null) { |
|
|
|
|
|
|
90
|
|
|
$pageTitle = ucwords($area) . $location; |
|
91
|
|
|
} else { |
|
92
|
|
|
$pageTitle = $this->config->get('core::siteName') . ($this->request->segment(1) != null ? ': ' . ucwords($this->request->segment(1)) : ''); |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$this->view->share('pageTitle', $pageTitle); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Determine which layout to use based on the request. |
|
100
|
|
|
* |
|
101
|
|
|
* @param $layout |
|
102
|
|
|
* |
|
103
|
|
|
* @return \Illuminate\Contracts\View\View |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function determineLayout($layout) |
|
106
|
|
|
{ |
|
107
|
|
|
if (! is_null($layout)) { |
|
108
|
|
|
return $this->view->make($layout); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
if (is_string($this->layout)) { |
|
112
|
|
|
return $this->view->make($this->layout); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if (is_null($this->layout) && $this->request->ajax()) { |
|
116
|
|
|
return $this->view->make($this->layoutOptions['ajax']); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
if (is_null($this->layout)) { |
|
120
|
|
|
return $this->view->make($this->layoutOptions['default']); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $this->layout; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Make sure that we have a valid set of layout options. |
|
128
|
|
|
* |
|
129
|
|
|
* @param $layoutOptions |
|
130
|
|
|
* |
|
131
|
|
|
* @return mixed |
|
132
|
|
|
*/ |
|
133
|
|
|
private function verifyLayoutOptions($layoutOptions) |
|
134
|
|
|
{ |
|
135
|
|
|
// If using the config details, go with that instead of expecting passed options. |
|
136
|
|
|
if (config('jumpgate.view-resolution.load_layout')) { |
|
137
|
|
|
return config('jumpgate.view-resolution.layout_options'); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
// If using the passed options, make sure they are valid options. |
|
141
|
|
|
if (! is_array($layoutOptions)) { |
|
142
|
|
|
throw new \InvalidArgumentException('The layoutOptions must be an array.'); |
|
143
|
|
|
} |
|
144
|
|
|
if (! isset($layoutOptions['default'])) { |
|
145
|
|
|
throw new \InvalidArgumentException('The layoutOptions must have a default layout view.'); |
|
146
|
|
|
} |
|
147
|
|
|
if (! isset($layoutOptions['ajax'])) { |
|
148
|
|
|
throw new \InvalidArgumentException('The layoutOptions must have a ajax layout view.'); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $layoutOptions; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|