1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverCommerce\CatalogueFrontend\Extensions; |
4
|
|
|
|
5
|
|
|
use SilverStripe\View\HTML; |
6
|
|
|
use SilverStripe\Core\ClassInfo; |
7
|
|
|
use SilverStripe\Dev\Deprecation; |
8
|
|
|
use SilverStripe\Forms\FieldList; |
9
|
|
|
use SilverStripe\Control\Director; |
10
|
|
|
use SilverStripe\ORM\DataExtension; |
11
|
|
|
use SilverStripe\View\Requirements; |
12
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
13
|
|
|
use SilverStripe\Control\Controller; |
14
|
|
|
use SilverStripe\Core\Config\Config; |
15
|
|
|
use SilverStripe\Forms\TextareaField; |
16
|
|
|
use SilverStripe\Security\Permission; |
17
|
|
|
use SilverStripe\Core\Injector\Injector; |
18
|
|
|
use SilverStripe\Control\ContentNegotiator; |
19
|
|
|
use SilverStripe\Forms\ToggleCompositeField; |
20
|
|
|
use SilverStripe\View\Parsers\URLSegmentFilter; |
21
|
|
|
use SilverStripe\CMS\Forms\SiteTreeURLSegmentField; |
22
|
|
|
use SilverCommerce\CatalogueAdmin\Model\CatalogueProduct; |
23
|
|
|
use SilverCommerce\CatalogueAdmin\Model\CatalogueCategory; |
24
|
|
|
use SilverCommerce\CatalogueFrontend\Control\CatalogueController; |
25
|
|
|
|
26
|
|
|
class CatalogueExtension extends DataExtension |
27
|
|
|
{ |
28
|
|
|
private static $db = [ |
|
|
|
|
29
|
|
|
"URLSegment" => "Varchar", |
30
|
|
|
"MetaDescription" => "Text", |
31
|
|
|
"ExtraMeta" => "HTMLFragment(['whitelist' => ['meta', 'link']])" |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
private static $casting = [ |
|
|
|
|
35
|
|
|
'MetaTags' => 'HTMLFragment' |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
public function updateRelativeLink(&$link, $action) |
39
|
|
|
{ |
40
|
|
|
$parent = $this->owner->Parent(); |
41
|
|
|
|
42
|
|
|
if ($parent && $parent->exists()) { |
43
|
|
|
$link = Controller::join_links( |
44
|
|
|
$parent->RelativeLink(), |
45
|
|
|
$this->owner->URLSegment, |
46
|
|
|
$action |
47
|
|
|
); |
48
|
|
|
} else { |
49
|
|
|
$link = Controller::join_links( |
50
|
|
|
$this->owner->URLSegment, |
51
|
|
|
$action |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns true if this is the currently active page being used to handle this request. |
58
|
|
|
* |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
public function isCurrent() |
62
|
|
|
{ |
63
|
|
|
$currentPage = Director::get_current_page(); |
64
|
|
|
|
65
|
|
|
if ($currentPage instanceof ContentController) { |
|
|
|
|
66
|
|
|
$currentPage = $currentPage->data(); |
67
|
|
|
} |
68
|
|
|
if ($currentPage instanceof CatalogueCategory || $currentPage instanceof CatalogueProduct) { |
69
|
|
|
return $currentPage === $this->owner || $currentPage->ID === $this->owner->ID; |
70
|
|
|
} |
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Check if this page is in the currently active section (e.g. it is either current or one of its children is |
76
|
|
|
* currently being viewed). |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
public function isSection() |
81
|
|
|
{ |
82
|
|
|
$is_curr = $this->isCurrent(); |
83
|
|
|
$curr = Director::get_current_page(); |
84
|
|
|
|
85
|
|
|
return $is_curr || ( |
86
|
|
|
($curr instanceof CatalogueCategory || $curr instanceof CatalogueProduct) && in_array($this->owner->ID, $curr->getAncestors()->column()) |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Return "link", "current" or section depending on if this page is the current page, or not on the current page but |
93
|
|
|
* in the current section. |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function LinkingMode() |
98
|
|
|
{ |
99
|
|
|
if ($this->isCurrent()) { |
100
|
|
|
return 'current'; |
101
|
|
|
} elseif ($this->isSection()) { |
102
|
|
|
return 'section'; |
103
|
|
|
} else { |
104
|
|
|
return 'link'; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Return "link" or "section" depending on if this is the current section. |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function LinkOrSection() |
114
|
|
|
{ |
115
|
|
|
return $this->isSection() ? 'section' : 'link'; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function updateCMSFields(FieldList $fields) |
119
|
|
|
{ |
120
|
|
|
// Add CMS requirements for URL Segment Field |
121
|
|
|
Requirements::javascript('silverstripe/cms: client/dist/js/bundle.js'); |
122
|
|
|
Requirements::css('silverstripe/cms: client/dist/styles/bundle.css'); |
123
|
|
|
Requirements::add_i18n_javascript('silverstripe/cms: client/lang', false, true); |
|
|
|
|
124
|
|
|
|
125
|
|
|
$fields->removeByName("MetaDescription"); |
126
|
|
|
$fields->removeByName("ExtraMeta"); |
127
|
|
|
|
128
|
|
|
$parent = null; |
129
|
|
|
$parent_link = null; |
130
|
|
|
|
131
|
|
|
if ($this->owner instanceof CatalogueCategory) { |
132
|
|
|
$parent = $this |
133
|
|
|
->owner |
134
|
|
|
->Parent(); |
135
|
|
|
} elseif ($this->owner instanceof CatalogueProduct) { |
136
|
|
|
$parent = $this |
137
|
|
|
->owner |
138
|
|
|
->Categories() |
139
|
|
|
->first(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if ($parent) { |
143
|
|
|
$parent_link = $parent->RelativeLink(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$baseLink = Controller::join_links( |
147
|
|
|
Director::absoluteBaseURL(), |
148
|
|
|
$parent_link |
149
|
|
|
); |
150
|
|
|
|
151
|
|
|
if (substr(rtrim($baseLink), -1) != "/") { |
152
|
|
|
$baseLink = $baseLink . "/"; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$fields->addFieldToTab( |
156
|
|
|
"Root.Main", |
157
|
|
|
SiteTreeURLSegmentField::create( |
158
|
|
|
"URLSegment", |
159
|
|
|
$this->owner->fieldLabel('URLSegment') |
160
|
|
|
)->setURLPrefix($baseLink), |
161
|
|
|
'Content' |
162
|
|
|
); |
163
|
|
|
|
164
|
|
|
// Add meta info fields |
165
|
|
|
$fields->addFieldToTab( |
166
|
|
|
"Root.Main", |
167
|
|
|
ToggleCompositeField::create( |
168
|
|
|
'Metadata', |
169
|
|
|
_t(__CLASS__.'.MetadataToggle', 'Metadata'), |
170
|
|
|
[ |
171
|
|
|
$metaFieldDesc = TextareaField::create( |
172
|
|
|
"MetaDescription", |
173
|
|
|
$this->owner->fieldLabel('MetaDescription') |
174
|
|
|
), |
175
|
|
|
$metaFieldExtra = TextareaField::create( |
176
|
|
|
"ExtraMeta", |
177
|
|
|
$this->owner->fieldLabel('ExtraMeta') |
178
|
|
|
) |
179
|
|
|
] |
180
|
|
|
)->setHeadingLevel(4) |
181
|
|
|
); |
182
|
|
|
|
183
|
|
|
// Help text for MetaData on page content editor |
184
|
|
|
$metaFieldDesc |
185
|
|
|
->setRightTitle( |
186
|
|
|
_t( |
187
|
|
|
'SilverStripe\\CMS\\Model\\SiteTree.METADESCHELP', |
188
|
|
|
"Search engines use this content for displaying search results (although it will not influence their ranking)." |
189
|
|
|
) |
190
|
|
|
)->addExtraClass('help'); |
191
|
|
|
|
192
|
|
|
$metaFieldExtra |
193
|
|
|
->setRightTitle( |
194
|
|
|
_t( |
195
|
|
|
'SilverStripe\\CMS\\Model\\SiteTree.METAEXTRAHELP', |
196
|
|
|
"HTML tags for additional meta information. For example <meta name=\"customName\" content=\"your custom content here\" />" |
197
|
|
|
) |
198
|
|
|
) |
199
|
|
|
->addExtraClass('help'); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Find the controller name by our convention of {$ModelClass}Controller |
204
|
|
|
* |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
|
|
public function getControllerName() |
208
|
|
|
{ |
209
|
|
|
//default controller for SiteTree objects |
210
|
|
|
$controller = CatalogueController::class; |
211
|
|
|
|
212
|
|
|
//go through the ancestry for this class looking for |
213
|
|
|
$ancestry = ClassInfo::ancestry($this->owner->ClassName); |
214
|
|
|
|
215
|
|
|
// loop over the array going from the deepest descendant (ie: the current class) to SiteTree |
216
|
|
|
while ($class = array_pop($ancestry)) { |
217
|
|
|
//we don't need to go any deeper than the SiteTree class |
218
|
|
|
if ($class == CatalogueProduct::class || $class == CatalogueCategory::class) { |
219
|
|
|
break; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
// If we have a class of "{$ClassName}Controller" then we found our controller |
223
|
|
|
if (class_exists($candidate = sprintf('%sController', $class))) { |
224
|
|
|
$controller = $candidate; |
225
|
|
|
break; |
226
|
|
|
} elseif (class_exists($candidate = sprintf('%s_Controller', $class))) { |
227
|
|
|
// Support the legacy underscored filename, but raise a deprecation notice |
228
|
|
|
Deprecation::notice( |
229
|
|
|
'5.0', |
230
|
|
|
'Underscored controller class names are deprecated. Use "MyController" instead of "My_Controller".', |
231
|
|
|
Deprecation::SCOPE_GLOBAL |
232
|
|
|
); |
233
|
|
|
$controller = $candidate; |
234
|
|
|
break; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
return $controller; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Return the title, description, keywords and language metatags. |
243
|
|
|
* NOTE: Shamelessley taken from SiteTree |
244
|
|
|
* |
245
|
|
|
* @param bool $includeTitle Show default <title>-tag, set to false for custom templating |
246
|
|
|
* |
247
|
|
|
* @return string The XHTML metatags |
248
|
|
|
*/ |
249
|
|
|
public function MetaTags($includeTitle = true) |
250
|
|
|
{ |
251
|
|
|
$tags = []; |
252
|
|
|
$owner = $this->getOwner(); |
253
|
|
|
|
254
|
|
|
if ($includeTitle && strtolower($includeTitle) != 'false') { |
|
|
|
|
255
|
|
|
$tags[] = HTML::createTag( |
256
|
|
|
'title', |
257
|
|
|
[], |
258
|
|
|
$owner->obj('Title')->forTemplate() |
259
|
|
|
); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
$generator = trim(Config::inst()->get(self::class, 'meta_generator')); |
263
|
|
|
if (!empty($generator)) { |
264
|
|
|
$tags[] = HTML::createTag( |
265
|
|
|
'meta', array( |
266
|
|
|
'name' => 'generator', |
267
|
|
|
'content' => $generator, |
268
|
|
|
) |
269
|
|
|
); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
$charset = ContentNegotiator::config()->uninherited('encoding'); |
273
|
|
|
$tags[] = HTML::createTag( |
274
|
|
|
'meta', array( |
275
|
|
|
'http-equiv' => 'Content-Type', |
276
|
|
|
'content' => 'text/html; charset=' . $charset, |
277
|
|
|
) |
278
|
|
|
); |
279
|
|
|
if ($owner->MetaDescription) { |
280
|
|
|
$tags[] = HTML::createTag( |
281
|
|
|
'meta', array( |
282
|
|
|
'name' => 'description', |
283
|
|
|
'content' => $owner->MetaDescription, |
284
|
|
|
) |
285
|
|
|
); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
if (Permission::check('CMS_ACCESS_CMSMain') && $owner->exists()) { |
289
|
|
|
$tags[] = HTML::createTag( |
290
|
|
|
'meta', |
291
|
|
|
[ |
292
|
|
|
'name' => 'x-page-id', |
293
|
|
|
'content' => $owner->obj('ID')->forTemplate() |
294
|
|
|
] |
295
|
|
|
); |
296
|
|
|
$tags[] = HTML::createTag( |
297
|
|
|
'meta', |
298
|
|
|
[ |
299
|
|
|
'name' => 'x-cms-edit-link', |
300
|
|
|
'content' => $owner->obj('CMSEditLink')->forTemplate() |
301
|
|
|
] |
302
|
|
|
); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
$tags = implode("\n", $tags); |
306
|
|
|
if ($owner->ExtraMeta) { |
307
|
|
|
$tags .= $owner->obj('ExtraMeta')->forTemplate(); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
$owner->extend('updateMetaTags', $tags); |
311
|
|
|
|
312
|
|
|
return $tags; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
public function onBeforeWrite() |
316
|
|
|
{ |
317
|
|
|
// Only call on first creation, ir if title is changed |
318
|
|
|
if ($this->owner->isChanged('Title') || !$this->owner->URLSegment) { |
319
|
|
|
// Set the URL Segment, so it can be accessed via the controller |
320
|
|
|
$filter = URLSegmentFilter::create(); |
321
|
|
|
$t = $filter->filter($this->owner->Title); |
322
|
|
|
|
323
|
|
|
// Fallback to generic name if path is empty (= no valid, convertable characters) |
324
|
|
|
if (!$t || $t == '-' || $t == '-1') { |
325
|
|
|
$t = "{$this->owner->ID}"; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
// Ensure that this object has a non-conflicting URLSegment value. |
329
|
|
|
$existing_cats = CatalogueCategory::get()->filter('URLSegment', $t)->count(); |
330
|
|
|
$existing_products = CatalogueProduct::get()->filter('URLSegment', $t)->count(); |
331
|
|
|
$existing_pages = SiteTree::get()->filter('URLSegment', $t)->count(); |
332
|
|
|
$count = (int)$existing_cats + (int)$existing_products + (int)$existing_pages; |
333
|
|
|
$this->owner->URLSegment = ($count) ? $t . '-' . ($count + 1) : $t; |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Hides disabled products from googlesitemaps |
339
|
|
|
* Only called if googlesitemaps module is installed |
340
|
|
|
* |
341
|
|
|
* @param [type] $can |
|
|
|
|
342
|
|
|
* @return bool |
343
|
|
|
*/ |
344
|
|
|
public function alterCanIncludeInGoogleSitemap(&$can) |
|
|
|
|
345
|
|
|
{ |
346
|
|
|
return !$this->Disabled; |
|
|
|
|
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
|