/
controller.php
245 lines (222 loc) · 8.25 KB
/
controller.php
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
<?php
namespace Concrete\Block\TopNavigationBar;
use ClassesWithParents\E;
use Concrete\Core\Application\Service\FileManager;
use Concrete\Core\Block\BlockController;
use Concrete\Core\Editor\LinkAbstractor;
use Concrete\Core\Feature\Features;
use Concrete\Core\Feature\UsesFeatureInterface;
use Concrete\Core\File\File;
use Concrete\Core\Navigation\Breadcrumb\PageBreadcrumbFactory;
use Concrete\Core\Navigation\Item\PageItem;
use Concrete\Core\Navigation\Navigation;
use Concrete\Core\Page\Page;
use Concrete\Core\Permission\Checker;
use HtmlObject\Image;
defined('C5_EXECUTE') or die('Access Denied.');
class Controller extends BlockController implements UsesFeatureInterface
{
public $helpers = ['form'];
public $brandingLogo = 0;
public $brandingTransparentLogo = 0;
public $includeBrandLogo = false;
public $includeBrandText = false;
public $includeStickyNav = false;
public $includeNavigationDropdowns = false;
public $includeSearchInput;
protected $btInterfaceWidth = 640;
protected $btInterfaceHeight = 500;
protected $btTable = 'btTopNavigationBar';
protected $btCacheBlockRecord = true;
protected $btCacheBlockOutput = true;
protected $btCacheBlockOutputOnPost = true;
protected $btCacheBlockOutputForRegisteredUsers = true;
protected $btCacheBlockOutputLifetime = 300;
protected $btExportFileColumns = ['brandingLogo', 'brandingTransparentLogo'];
/**
* {@inheritdoc}
*/
public function getBlockTypeName()
{
return t('Top Navigation Bar');
}
/**
* {@inheritdoc}
*/
public function getBlockTypeDescription()
{
return t('Adds a responsive navigation bar with a logo, menu and search..');
}
/**
* {@inheritdoc}
*/
public function getRequiredFeatures(): array
{
return [
Features::NAVIGATION,
];
}
public function add()
{
$site = $this->app->make('site')->getSite();
$brandingText = $site->getSiteName();
$this->set('includeTransparency', false);
$this->set('includeStickyNav', false);
$this->set('includeNavigation', true);
$this->set('includeNavigationDropdowns', false);
$this->set('includeSearchInput', false);
$this->set('includeBrandText', true);
$this->set('includeBrandLogo', false);
$this->set('brandingLogo', null);
$this->set('brandingTransparentLogo', null);
$this->set('searchInputFormActionPageID', null);
$this->set('brandingText', $brandingText);
$this->edit();
}
public function edit()
{
$this->set('fileManager', new FileManager());
$this->set('editor', $this->app->make('editor'));
}
protected function includePageInNavigation(Page $page)
{
$checker = new Checker($page);
if ($checker->canViewPage() && !$page->getAttribute('exclude_nav')) {
return true;
}
return false;
}
protected function includeSubPagesInNavigation(Page $page)
{
if ($this->includeNavigationDropdowns) {
$excludeSubPages = $page->getAttribute('exclude_subpages_from_nav');
if ($excludeSubPages) {
return false;
} else {
return true;
}
}
return false;
}
protected function getNavigation(): Navigation
{
$site = $this->app->make('site')->getSite();
$home = $site->getSiteHomePageObject();
$children = $home->getCollectionChildren();
$navigation = new Navigation();
foreach ($children as $child) {
if ($this->includePageInNavigation($child)) {
$item = new PageItem($child);
if ($this->includeSubPagesInNavigation($child)) {
$dropdownChildren = $child->getCollectionChildren();
foreach ($dropdownChildren as $dropdownChild) {
if ($this->includePageInNavigation($dropdownChild)) {
$dropdownChildItem = new PageItem($dropdownChild);
$item->addChild($dropdownChildItem);
}
}
}
$navigation->add($item);
}
}
return $navigation;
}
public function view()
{
$site = $this->app->make('site')->getSite();
$home = $site->getSiteHomePageObject();
if ($this->brandingLogo) {
$logo = File::getByID($this->brandingLogo);
if ($logo) {
$this->set('logo', $logo);
}
}
if ($this->brandingTransparentLogo) {
$transparentLogo = File::getByID($this->brandingTransparentLogo);
if ($transparentLogo) {
$this->set('transparentLogo', $transparentLogo);
}
}
$this->set('navigation', $this->getNavigation());
$this->set('home', $home);
if ($this->includeSearchInput) {
$searchPage = Page::getByID($this->searchInputFormActionPageID);
if ($searchPage && !$searchPage->isError()) {
$searchAction = (string) $searchPage->getCollectionLink();
} else if ($home) {
$searchAction = (string) $home->getCollectionLink();
}
$this->set('searchAction', $searchAction);
}
}
public function save($args)
{
$data = [];
$data['includeNavigation'] = $args['includeNavigation'] ? 1 : 0;
$data['includeNavigationDropdowns'] = $args['includeNavigationDropdowns'] ? 1 : 0;
$data['includeTransparency'] = $args['includeTransparency'] ? 1 : 0;
$data['includeSearchInput'] = $args['includeSearchInput'] ? 1 : 0;
$data['includeStickyNav'] = $args['includeStickyNav'] ? 1 : 0;
$data['includeBrandLogo'] = 0;
$data['includeBrandText'] = 0;
// This line is for import purposes – in import this is already set this way
if (isset($args['includeBrandText']) || isset($args['includeBrandLogo'])) {
$data['includeBrandLogo'] = $args['includeBrandLogo'];
$data['includeBrandText'] = $args['includeBrandText'];
} else {
switch ($args['brandingMode']) {
case 'logoText':
$data['includeBrandLogo'] = 1;
$data['includeBrandText'] = 1;
break;
case 'logo':
$data['includeBrandLogo'] = 1;
break;
case 'text':
$data['includeBrandText'] = 1;
break;
}
}
$brandingLogo = 0;
if (!empty($args['brandingLogo'])) {
$file = File::getByID($args['brandingLogo']);
if ($file) {
$checker = new Checker($file);
if ($checker->canViewFileInFileManager()) {
$brandingLogo = $file->getFileID();
}
}
}
$brandingTransparentLogo = 0;
if (!empty($args['brandingTransparentLogo'])) {
$file = File::getByID($args['brandingTransparentLogo']);
if ($file) {
$checker = new Checker($file);
if ($checker->canViewFileInFileManager()) {
$brandingTransparentLogo = $file->getFileID();
}
}
}
$searchInputFormActionPageID = 0;
if (!empty($args['searchInputFormActionPageID'])) {
$searchPage = Page::getByID($args['searchInputFormActionPageID']);
if ($searchPage && !$searchPage->isError()) {
$checker = new Checker($searchPage);
if ($checker->canViewPage()) {
$searchInputFormActionPageID = $searchPage->getCollectionID();
}
}
}
if ($data['includeBrandText']) {
$data['brandingText'] = $args['brandingText'];
}
if ($data['includeBrandLogo']) {
$data['brandingLogo'] = $brandingLogo;
$data['brandingTransparentLogo'] = $brandingTransparentLogo;
}
if ($data['includeSearchInput']) {
$data['searchInputFormActionPageID'] = $searchInputFormActionPageID;
}
parent::save($data);
}
}