/
DataObjectAsPage.php
672 lines (561 loc) · 18.1 KB
/
DataObjectAsPage.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
<?php
/*
* Base class for DataObjects that behave like pages
*
*/
class DataObjectAsPage extends DataObject {
/**
* @var defind the listing page class name
*/
private static $listing_page_class = 'DataObjectAsPageHolder';
private static $db = array (
'URLSegment' => 'Varchar(100)',
'Title' => 'Varchar(255)',
'MetaTitle' => 'Varchar(255)',
'MetaDescription' => 'Varchar(255)',
'Content' => 'HTMLText'
);
private static $defaults = array(
'Title'=>'New Item',
'URLSegment' => 'new-item'
);
private static $summary_fields = array(
'Title' => 'Title',
'URLSegment' => 'URLSegment'
);
private static $indexes = array(
"URLSegment" => array(
'type' => 'unique',
'value' => 'URLSegment'
)
);
private static $default_sort = 'Created DESC';
/**
* Provide compatability with Menu loops in templates
*/
public function MenuTitle()
{
return $this->Title;
}
/**
* Override getMetaTitle to keep DB cleaner
*
* @return string The Meta Title
*/
public function getMetaTitle()
{
if ($value = $this->getField('MetaTitle'))
{
return $value;
}
return $this->getField('Title');
}
/**
* Override getMetaTitle to keep DB cleaner
*
* @param string $value The value for the MetaTitle field
*/
public function setMetaTitle($value) {
if ($value == $this->getField('Title'))
{
$this->setField('MetaTitle', null);
}
else
{
$this->setField('MetaTitle', $value);
}
}
/**
* Check if the member can view
*
* @param Member $member The member to check against
* @return boolean Whether the member or current member can view
*/
public function canView($member = null)
{
//if no member was supplied assume current member
if(!$member || !(is_a($member, 'Member')) || is_numeric($member)) $member = Member::currentUser();
// Standard mechanism for accepting permission changes from extensions
$extended = $this->extendedCan('canView', $member);
if($extended !==null) return $extended;
//If this is draft check for permissions to view draft content
//getSearchResultItem is needed to ensure unpublished items don't show up in search results
if($this->isVersioned && Versioned::current_stage() == 'Stage' && $this->Status == 'Draft')
{
return Permission::checkMember($member,'VIEW_DRAFT_CONTENT');
}
elseif(Controller::curr()->hasMethod("canView"))
{
//Otherwise return the parent listing pages view permission
return Controller::curr()->canView($member);
}
return true;
}
/**
* Check if the member can publish
*
* @param Member $member The member to check against
* @return boolean Whether the member or current member can publish
*/
public function canPublish($member = null)
{
if(!$member || !(is_a($member, 'Member')) || is_numeric($member)) $member = Member::currentUser();
if($member && Permission::checkMember($member, "ADMIN")) return true;
// Standard mechanism for accepting permission changes from extensions
$extended = $this->extendedCan('canPublish', $member);
if($extended !== null) return $extended;
// Normal case - fail over to canEdit()
return $this->canEdit($member);
}
/**
* Check if the member can delete live content
*
* @param Member $member The member to check against
* @return boolean Whether the member or current member can delete live content
*/
public function canDeleteFromLive($member = null)
{
//if no member was supplied assume current member
if(!$member || !(is_a($member, 'Member')) || is_numeric($member)) $member = Member::currentUser();
// Standard mechanism for accepting permission changes from extensions
$extended = $this->extendedCan('canDeleteFromLive', $member);
if($extended !==null) return $extended;
return $this->canPublish($member);
}
/**
* Override the CMS acctions to create a "duplicate" button
*
* @return FieldList The updated list of actions
*/
public function getCMSActions()
{
$minorActions = CompositeField::create()->setTag('fieldset')->addExtraClass('ss-ui-buttonset');
$actions = new FieldList($minorActions);
if($this->ID)
{
if($this->isPublished() && $this->canPublish() && $this->canDeleteFromLive()) {
// "unpublish"
$minorActions->push(
FormAction::create('unpublish', _t('SiteTree.BUTTONUNPUBLISH', 'Unpublish'), 'delete')
->setDescription(_t('SiteTree.BUTTONUNPUBLISHDESC', 'Remove this page from the published site'))
->addExtraClass('ss-ui-action-destructive')->setAttribute('data-icon', 'unpublish')
);
}
if($this->canEdit()) {
if($this->canDelete()) {
// "delete"
$minorActions->push(
FormAction::create('delete','Delete')->addExtraClass('delete ss-ui-action-destructive')
->setAttribute('data-icon', 'decline')
);
}
if($this->hasChangesOnStage()) {
if($this->isPublished() && $this->canEdit()) {
// "rollback"
$minorActions->push(
FormAction::create('rollback', _t('SiteTree.BUTTONCANCELDRAFT', 'Cancel draft changes'), 'delete')
->setDescription(_t('SiteTree.BUTTONCANCELDRAFTDESC', 'Delete your draft and revert to the currently published page'))
);
}
}
if ($this->canCreate())
{
//Create the Duplicate action
$minorActions->push( FormAction::create('duplicate', 'Duplicate')
->setDescription("Duplicate this item")
);
}
// "save"
$minorActions->push(
FormAction::create('doSave',_t('CMSMain.SAVEDRAFT','Save Draft'))->setAttribute('data-icon', 'addpage')
);
}
if($this->canPublish()) {
// "publish"
$actions->push(
FormAction::create('publish', _t('SiteTree.BUTTONSAVEPUBLISH', 'Save & Publish'))
->addExtraClass('ss-ui-action-constructive')->setAttribute('data-icon', 'accept')
);
}
}
else
{
//Change the Save label to 'Create'
$actions->push(FormAction::create('doSave', _t('GridFieldDetailForm.Create', 'Create'))
->setUseButtonTag(true)
->addExtraClass('ss-ui-action-constructive')
->setAttribute('data-icon', 'add'));
}
return $actions;
}
/**
* Overload getCMSFields for our custom fields
*
* @return FieldList The list of CMS Fields
*/
public function getCMSFields()
{
$fields = parent::getCMSFields();
//Add the status/view link
if($this->ID)
{
if($this->isVersioned)
{
$status = $this->Status;
$color = '#E88F31';
$links = sprintf(
"<a target=\"_blank\" class=\"ss-ui-button\" data-icon=\"preview\" href=\"%s\">%s</a>", $this->Link() . '?Stage=stage', 'Draft'
);
if($this->Status == 'Published')
{
$color = '#000';
$links .= sprintf(
"<a target=\"_blank\" class=\"ss-ui-button\" data-icon=\"preview\" href=\"%s\">%s</a>", $this->Link() . '?Stage=live', 'Published'
);
if($this->hasChangesOnStage())
{
$status .= ' (changed)';
$color = '#428620';
}
}
$statusPill = '<h3 class="doapTitle" style="background: '.$color.';">'. $status . '</h3>';
}
else
{
$links = sprintf(
"<a target=\"_blank\" class=\"ss-ui-button\" data-icon=\"preview\" href=\"%s\">%s</a>", $this->Link() . '?Stage=stage', 'View'
);
$statusPill = "";
}
$fields->addFieldToTab('Root.Main', new LiteralField('',
'<div class="doapToolbar">
' . $statusPill . '
<p class="doapViewLinks">
' . $links . '
</p>
</div>'
));
}
//Remove Scafolded fields
$fields->removeFieldFromTab('Root.Main', 'URLSegment');
$fields->removeFieldFromTab('Root.Main', 'Status');
$fields->removeFieldFromTab('Root.Main', 'Version');
$fields->removeFieldFromTab('Root.Main', 'MetaTitle');
$fields->removeFieldFromTab('Root.Main', 'MetaDescription');
$fields->removeByName('Versions');
$fields->addFieldToTab('Root.Main', new TextField('Title'));
if($this->ID)
{
$urlsegment = new SiteTreeURLSegmentField("URLSegment", $this->fieldLabel('URLSegment'));
$urlsegment->setURLPrefix(Director::absoluteBaseURL() . 'listing-page/show/');
$helpText = _t('SiteTreeURLSegmentField.HelpChars', ' Special characters are automatically converted or removed.');
$urlsegment->setHelpText($helpText);
$fields->addFieldToTab('Root.Main', $urlsegment);
}
$fields->addFieldToTab('Root.Main', new HTMLEditorField('Content'));
$fields->addFieldToTab('Root.Main',new ToggleCompositeField('Metadata', 'Metadata',
array(
new TextField("MetaTitle", $this->fieldLabel('MetaTitle')),
new TextareaField("MetaDescription", $this->fieldLabel('MetaDescription'))
)
));
//$fields->push(new HiddenField('PreviewURL', 'Preview URL', $this->StageLink()));
//$fields->push(new TextField('CMSEditURL', 'Preview URL', $this->CMSEditLink()));
return $fields;
}
/**
* Utility function to enable versioning in a simple call
*/
public static function enable_versioning()
{
DataObject::add_extension('DataObjectAsPage','VersionedDataObjectAsPage');
DataObject::add_extension('DataObjectAsPage',"Versioned('Stage', 'Live')");
}
/**
* Check if the DOAP is versioned
*
* @return boolean
*/
public function getisVersioned()
{
return $this->hasExtension('Versioned');
}
/**
* Produce the correct breadcrumb trail for use on the DataObject Item Page
*/
public function Breadcrumbs($maxDepth = 20, $unlinked = false, $stopAtPageType = false, $showHidden = false)
{
$page = Controller::curr();
$pages = array();
$pages[] = $this;
while(
$page
&& (!$maxDepth || count($pages) < $maxDepth)
&& (!$stopAtPageType || $page->ClassName != $stopAtPageType)
) {
if($showHidden || $page->ShowInMenus || ($page->ID == $this->ID)) {
$pages[] = $page;
}
$page = $page->Parent;
}
$template = new SSViewer('BreadcrumbsTemplate');
return $template->process($this->customise(new ArrayData(array(
'Pages' => new ArrayList(array_reverse($pages))
))));
}
/**
* Generate custom metatags to display on the DataObject Item page
*/
public function MetaTags($includeTitle = true)
{
$tags = "";
if($includeTitle === true || $includeTitle == 'true') {
$tags .= "<title>" . Convert::raw2xml(($this->MetaTitle)
? $this->MetaTitle
: $this->Title) . "</title>\n";
}
$tags .= "<meta name=\"generator\" content=\"SilverStripe - http://silverstripe.org\" />\n";
$charset = ContentNegotiator::get_encoding();
$tags .= "<meta http-equiv=\"Content-type\" content=\"text/html; charset=$charset\" />\n";
if($this->MetaDescription) {
$tags .= "<meta name=\"description\" content=\"" . Convert::raw2att($this->MetaDescription) . "\" />\n";
}
$this->extend('MetaTags', $tags);
return $tags;
}
/**
* Check if this page has been published.
*
* @return boolean True if this page has been published.
*/
public function isPublished()
{
return (DB::query("SELECT \"ID\" FROM \"DataObjectAsPage_Live\" WHERE \"ID\" = $this->ID")->value())
? true
: false;
}
/**
* Create a duplicate of this node. Doesn't affect joined data - create a
* custom overloading of this if you need such behaviour.
*
* @return SiteTree The duplicated object.
*/
public function doDuplicate($doWrite = true)
{
$item = parent::duplicate(false);
$this->extend('onBeforeDuplicate', $item);
//Change the title so we know we are looking at the copy
$item->Title = 'Copy of ' . $this->Title;
$item->Status = 'Draft';
if($doWrite) {
$item->write();
}
$this->extend('onAfterDuplicate', $page);
return $item;
}
/**
* Publish this page.
*
* @uses SiteTreeDecorator->onBeforePublish()
* @uses SiteTreeDecorator->onAfterPublish()
*/
public function doPublish()
{
if (!$this->canPublish()) return false;
$original = Versioned::get_one_by_stage("DataObjectAsPage", "Live", "\"DataObjectAsPage\".\"ID\" = $this->ID");
if(!$original) $original = new DataObjectAsPage();
// Handle activities undertaken by decorators
$this->invokeWithExtensions('onBeforePublish', $original);
$this->Status = "Published";
//$this->PublishedByID = Member::currentUser()->ID;
$this->write();
$this->publish("Stage", "Live");
// Handle activities undertaken by decorators
$this->invokeWithExtensions('onAfterPublish', $original);
return true;
}
/**
* Unpublish this DataObject - remove it from the live site
*
*/
public function doUnpublish()
{
if(!$this->ID) return false;
if (!$this->canDeleteFromLive()) return false;
$this->extend('onBeforeUnpublish');
$origStage = Versioned::current_stage();
Versioned::reading_stage('Live');
// This way our ID won't be unset
$clone = clone $this;
$clone->delete();
Versioned::reading_stage($origStage);
// If we're on the draft site, then we can update the status.
// Otherwise, these lines will resurrect an inappropriate record
if(DB::query("SELECT \"ID\" FROM \"DataObjectAsPage\" WHERE \"ID\" = $this->ID")->value()
&& Versioned::current_stage() != 'Live') {
$this->Status = "Draft";
$this->write();
}
$this->extend('onAfterUnpublish');
return true;
}
/**
* Function to delete the DOAP including from versioned tables
*/
public function doDelete()
{
$this->doUnpublish();
$oldMode = Versioned::get_reading_mode();
Versioned::reading_stage('Draft');
//delete all versioned objects with this ID
$result = DB::query("DELETE FROM DataObjectAsPage_versions WHERE RecordID = '$this->ID'");
$result = $this->delete();
Versioned::set_reading_mode($oldMode);
return $result;
}
/**
* Revert the draft changes: replace the draft content with the content on live
*/
public function doRevertToLive()
{
$this->publish("Live", "Stage", false);
// Use a clone to get the updates made by $this->publish
$clone = DataObject::get_by_id("DataObjectAsPage", $this->ID);
$clone->writeWithoutVersion();
$this->extend('onAfterRevertToLive');
return $clone;
}
/**
* Check whether this DO has changes which are not published
*/
public function hasChangesOnStage()
{
$latestPublishedVersion = $this->get_versionnumber_by_stage('DataObjectAsPage', 'Live', $this->ID);
$latestVersion = $this->get_versionnumber_by_stage('DataObjectAsPage', 'Stage', $this->ID);
return ($latestPublishedVersion < $latestVersion);
}
/**
* Get the listing page to view this Event on (used in Link functions below)
*/
public function getListingPage(){
$listingClass = $this->stat('listing_page_class');
if(Controller::curr() instanceof $listingClass)
{
$listingPage = Controller::curr();
}
else
{
$listingPage = $listingClass::get()->First();
}
return $listingPage;
}
/**
* Generate the link to this DataObject Item page
*/
public function Link($action = null)
{
//Hack for search results
if($item = DataObjectAsPage::get()->byID($this->ID))
{
//Build link
if($listingPage = $item->getListingPage())
{
return Controller::join_links($listingPage->Link(), 'show', $item->URLSegment, $action);
}
}
}
/**
* Create an absolute link to the DOAP
*
* @param string $action Optional URL action to append
* @return string The absolute link
*/
public function AbsoluteLink($action = null)
{
if($listingPage = $this->getListingPage())
{
return Controller::join_links($listingPage->AbsoluteLink(), 'show', $this->URLSegment, $action);
}
}
/**
* Return the correct linking mode, for use in menus
*/
public function LinkingMode()
{
$listingClass = $this->stat('listing_page_class');
//Check that we have a controller to work with and that it is a listing page
if(($controller = Controller::Curr()) && (Controller::curr() instanceof $listingClass))
{
//check that the action is 'show' and that we have an item to work with
if($controller->getAction() == 'show' && $item = $controller->getCurrentItem())
{
return ($item->ID == $this->ID) ? 'current' : 'link';
}
}
}
/**
* Set URLSegment to be unique on write
*/
public function onBeforeWrite()
{
parent::onBeforeWrite();
$defaults = $this->config()->defaults;
// If there is no URLSegment set, generate one from Title
if((!$this->URLSegment || $this->URLSegment == $defaults['URLSegment']) && $this->Title != $defaults['Title'])
{
$this->URLSegment = $this->generateURLSegment($this->Title);
}
else if($this->isChanged('URLSegment'))
{
// Make sure the URLSegment is valid for use in a URL
$segment = preg_replace('/[^A-Za-z0-9]+/','-',$this->URLSegment);
$segment = preg_replace('/-+/','-',$segment);
// If after sanitising there is no URLSegment, give it a reasonable default
if(!$segment) {
$segment = "item-$this->ID";
}
$this->URLSegment = $segment;
}
// Ensure that this object has a non-conflicting URLSegment value.
$count = 2;
$URLSegment = $this->URLSegment;
$ID = $this->ID;
while($this->LookForExistingURLSegment($URLSegment, $ID))
{
$URLSegment = preg_replace('/-[0-9]+$/', null, $URLSegment) . '-' . $count;
$count++;
}
$this->URLSegment = $URLSegment;
}
/**
* Check if there is already a DOAP with this URLSegment
*/
public function LookForExistingURLSegment($URLSegment, $ID)
{
return DataObjectAsPage::get()->filter(array(
'URLSegment' => $URLSegment,
'ID' => $ID
))->exists();
}
/**
* Generate a URL segment based on the title provided.
*
* If {@link Extension}s wish to alter URL segment generation, they can do so by defining
* updateURLSegment(&$url, $title). $url will be passed by reference and should be modified.
* $title will contain the title that was originally used as the source of this generated URL.
* This lets extensions either start from scratch, or incrementally modify the generated URL.
*
* @param string $title Page title.
* @return string Generated url segment
*/
public function generateURLSegment($title)
{
$filter = URLSegmentFilter::create();
$t = $filter->filter($title);
// Fallback to generic page name if path is empty (= no valid, convertable characters)
if(!$t || $t == '-' || $t == '-1') $t = "page-$this->ID";
// Hook for extensions
$this->extend('updateURLSegment', $t, $title);
return $t;
}
}