Completed
Pull Request — master (#460)
by Daniel
09:57
created

BlogPost::getDate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 3
nop 0
1
<?php
2
3
/**
4
 * An individual blog post.
5
 *
6
 * @package silverstripe
7
 * @subpackage blog
8
 *
9
 * @method ManyManyList Categories()
10
 * @method ManyManyList Tags()
11
 * @method ManyManyList Authors()
12
 * @method Blog Parent()
13
 *
14
 * @property string $PublishDate
15
 * @property string $AuthorNames
16
 * @property int $ParentID
17
 */
18
class BlogPost extends Page
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
19
{
20
    /**
21
     * Same as above, but for list of users that can be
22
     * given credit in the author field for blog posts
23
     * @var string|bool false or group code
24
     */
25
    private static $restrict_authors_to_group = false;
0 ignored issues
show
Unused Code introduced by
The property $restrict_authors_to_group is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
26
27
    /**
28
     * @var array
29
     */
30
    private static $db = array(
0 ignored issues
show
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
31
        'PublishDate' => 'SS_Datetime',
32
        'AuthorNames' => 'Varchar(1024)',
33
        'Summary' => 'HTMLText',
34
    );
35
36
    /**
37
     * @var array
38
     */
39
    private static $has_one = array(
0 ignored issues
show
Unused Code introduced by
The property $has_one is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
40
        'FeaturedImage' => 'Image',
41
    );
42
43
    /**
44
     * @var array
45
     */
46
    private static $many_many = array(
0 ignored issues
show
Unused Code introduced by
The property $many_many is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
47
        'Categories' => 'BlogCategory',
48
        'Tags' => 'BlogTag',
49
        'Authors' => 'Member',
50
    );
51
52
    /**
53
     * @var array
54
     */
55
    private static $defaults = array(
0 ignored issues
show
Unused Code introduced by
The property $defaults is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
56
        'ShowInMenus' => false,
57
        'InheritSideBar' => true,
58
        'ProvideComments' => true,
59
    );
60
61
    /**
62
     * @var array
63
     */
64
    private static $extensions = array(
0 ignored issues
show
Unused Code introduced by
The property $extensions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
65
        'BlogPostFilter',
66
    );
67
68
    /**
69
     * @var array
70
     */
71
    private static $searchable_fields = array(
0 ignored issues
show
Unused Code introduced by
The property $searchable_fields is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
72
        'Title',
73
    );
74
75
    /**
76
     * @var array
77
     */
78
    private static $summary_fields = array(
0 ignored issues
show
Unused Code introduced by
The property $summary_fields is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
79
        'Title',
80
    );
81
82
    /**
83
     * @var array
84
     */
85
    private static $casting = array(
0 ignored issues
show
Unused Code introduced by
The property $casting is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
86
        'Excerpt' => 'HTMLText',
87
        'Date' => 'SS_Datetime',
88
    );
89
90
    /**
91
     * @var array
92
     */
93
    private static $allowed_children = array();
0 ignored issues
show
Unused Code introduced by
The property $allowed_children is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
94
95
    /**
96
     * The default sorting lists BlogPosts with an empty PublishDate at the top.
97
     *
98
     * @var string
99
     */
100
    private static $default_sort = '"PublishDate" IS NULL DESC, "PublishDate" DESC';
0 ignored issues
show
Unused Code introduced by
The property $default_sort is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
101
102
    /**
103
     * @var bool
104
     */
105
    private static $can_be_root = false;
0 ignored issues
show
Unused Code introduced by
The property $can_be_root is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
106
107
    /**
108
     * This will display or hide the current class from the SiteTree. This variable can be
109
     * configured using YAML.
110
     *
111
     * @var bool
112
     */
113
    private static $show_in_sitetree = false;
0 ignored issues
show
Unused Code introduced by
The property $show_in_sitetree is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
114
115
    /**
116
     * Determine the role of the given member.
117
     *
118
     * Call be called via template to determine the current user.
119
     *
120
     * @example "Hello $RoleOf($CurrentMember.ID)"
121
     *
122
     * @param null|int|Member $member
123
     *
124
     * @return null|string
125
     */
126
    public function RoleOf($member = null)
127
    {
128
        $member = $this->getMember($member);
129
130
        if (!$member) {
131
            return null;
132
        }
133
134
        if ($this->isAuthor($member)) {
135
            return _t('BlogPost.AUTHOR', 'Author');
136
        }
137
138
        $parent = $this->Parent();
139
140
        if ($parent instanceof Blog) {
141
            return $parent->RoleOf($member);
142
        }
143
144
        return null;
145
    }
146
147
    /**
148
     * Determine if the given member is an author of this post.
149
     *
150
     * @param null|Member $member
151
     *
152
     * @return bool
153
     */
154 View Code Duplication
    public function isAuthor($member = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
    {
156
        if (!$member || !$member->exists()) {
157
            return false;
158
        }
159
160
        $list = $this->Authors();
161
162
        if ($list instanceof UnsavedRelationList) {
163
            return in_array($member->ID, $list->getIDList());
164
        }
165
166
        return $list->byID($member->ID) !== null;
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172
    public function getCMSFields()
173
    {
174
        Requirements::css(BLOGGER_DIR . '/css/cms.css');
175
        Requirements::javascript(BLOGGER_DIR . '/js/cms.js');
176
177
        $self =& $this;
178
179
        $this->beforeUpdateCMSFields(function ($fields) use ($self) {
180
            $uploadField = UploadField::create('FeaturedImage', _t('BlogPost.FeaturedImage', 'Featured Image'));
181
            $uploadField->getValidator()->setAllowedExtensions(array('jpg', 'jpeg', 'png', 'gif'));
182
183
            /**
184
             * @var FieldList $fields
185
             */
186
            $fields->insertAfter($uploadField, 'Content');
0 ignored issues
show
Documentation introduced by
$uploadField is of type this<BlogPost>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'Content' is of type string, but the function expects a object<FormField>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
187
188
            $summary = HtmlEditorField::create('Summary', false);
189
            $summary->setRows(5);
190
            $summary->setDescription(_t(
191
                'BlogPost.SUMMARY_DESCRIPTION',
192
                'If no summary is specified the first 30 words will be used.'
193
            ));
194
195
            $summaryHolder = ToggleCompositeField::create(
196
                'CustomSummary',
197
                _t('BlogPost.CUSTOMSUMMARY', 'Add A Custom Summary'),
198
                array(
199
                    $summary,
200
                )
201
            );
202
            $summaryHolder->setHeadingLevel(4);
203
            $summaryHolder->addExtraClass('custom-summary');
204
205
            $fields->insertAfter($summaryHolder, 'FeaturedImage');
0 ignored issues
show
Documentation introduced by
$summaryHolder is of type this<BlogPost>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'FeaturedImage' is of type string, but the function expects a object<FormField>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
206
207
            $fields->push(HiddenField::create('MenuTitle'));
208
209
            $urlSegment = $fields->dataFieldByName('URLSegment');
210
            $urlSegment->setURLPrefix($self->Parent()->RelativeLink());
211
212
            $fields->removeFieldsFromTab('Root.Main', array(
213
                'MenuTitle',
214
                'URLSegment',
215
            ));
216
217
            $authorField = ListboxField::create(
218
                'Authors',
219
                _t('BlogPost.Authors', 'Authors'),
220
                $self->getCandidateAuthors()->map()->toArray()
0 ignored issues
show
Bug introduced by
The method toArray cannot be called on $self->getCandidateAuthors()->map() (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
221
            )->setMultiple(true);
222
223
            $authorNames = TextField::create(
224
                'AuthorNames',
225
                _t('BlogPost.AdditionalCredits', 'Additional Credits'),
226
                null,
227
                1024
228
            )->setDescription(_t(
229
                    'BlogPost.AdditionalCredits_Description',
230
                    'If some authors of this post don\'t have CMS access, enter their name(s) here. You can separate multiple names with a comma.')
231
            );
232
233
            if (!$self->canEditAuthors()) {
234
                $authorField = $authorField->performDisabledTransformation();
235
                $authorNames = $authorNames->performDisabledTransformation();
236
            }
237
238
            $publishDate = DatetimeField::create('PublishDate', _t('BlogPost.PublishDate', 'Publish Date'));
239
            $publishDate->getDateField()->setConfig('showcalendar', true);
240
            if (!$self->PublishDate) {
241
                $publishDate->setDescription(_t(
242
                        'BlogPost.PublishDate_Description',
243
                        'Will be set to "now" if published without a value.')
244
                );
245
            }
246
247
            // Get categories and tags
248
            $parent = $self->Parent();
249
            $categories = $parent instanceof Blog
250
                ? $parent->Categories()
251
                : BlogCategory::get();
252
            $tags = $parent instanceof Blog
253
                ? $parent->Tags()
254
                : BlogTag::get();
255
256
            $options = BlogAdminSidebar::create(
257
                $publishDate,
258
                $urlSegment,
259
                TagField::create(
260
                    'Categories',
261
                    _t('BlogPost.Categories', 'Categories'),
262
                    $categories,
263
                    $self->Categories()
264
                )
265
                    ->setCanCreate($self->canCreateCategories())
266
                    ->setShouldLazyLoad(true),
267
                TagField::create(
268
                    'Tags',
269
                    _t('BlogPost.Tags', 'Tags'),
270
                    $tags,
271
                    $self->Tags()
272
                )
273
                    ->setCanCreate($self->canCreateTags())
274
                    ->setShouldLazyLoad(true),
275
                $authorField,
276
                $authorNames
277
            )->setTitle('Post Options');
278
279
            $options->setName('blog-admin-sidebar');
280
281
            $fields->insertBefore($options, 'Root');
0 ignored issues
show
Documentation introduced by
'Root' is of type string, but the function expects a object<FormField>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
282
        });
283
284
        $fields = parent::getCMSFields();
285
286
        $fields->fieldByName('Root')->setTemplate('TabSet_holder');
287
288
        return $fields;
289
    }
290
291
    /**
292
     * Gets the list of author candidates to be assigned as authors of this blog post.
293
     *
294
     * @return SS_List
295
     */
296
    public function getCandidateAuthors()
297
    {
298
        if ($this->config()->restrict_authors_to_group) {
299
            return Group::get()->filter('Code', $this->config()->restrict_authors_to_group)->first()->Members();
300
        } else {
301
            $list = Member::get();
302
            $this->extend('updateCandidateAuthors', $list);
303
            return $list;
304
        }
305
    }
306
307
    /**
308
     * Determine if this user can edit the authors list.
309
     *
310
     * @param null|int|Member $member
311
     *
312
     * @return bool
313
     */
314
    public function canEditAuthors($member = null)
315
    {
316
        $member = $this->getMember($member);
317
318
        $extended = $this->extendedCan('canEditAuthors', $member);
319
320
        if ($extended !== null) {
321
            return $extended;
322
        }
323
324
        $parent = $this->Parent();
325
326
        if ($parent instanceof Blog && $parent->exists()) {
327
            if ($parent->isEditor($member)) {
328
                return true;
329
            }
330
331
            if ($parent->isWriter($member) && $this->isAuthor($member)) {
332
                return true;
333
            }
334
        }
335
336
        return Permission::checkMember($member, Blog::MANAGE_USERS);
0 ignored issues
show
Bug Compatibility introduced by
The expression \Permission::checkMember..., \Blog::MANAGE_USERS); of type boolean|string|null adds the type string to the return on line 336 which is incompatible with the return type documented by BlogPost::canEditAuthors of type boolean.
Loading history...
337
    }
338
339
    /**
340
     * @param null|int|Member $member
341
     *
342
     * @return null|Member
343
     */
344 View Code Duplication
    protected function getMember($member = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
345
    {
346
        if (!$member) {
347
            $member = Member::currentUser();
348
        }
349
350
        if (is_numeric($member)) {
351
            $member = Member::get()->byID($member);
352
        }
353
354
        return $member;
355
    }
356
357
    /**
358
     * Determine whether user can create new categories.
359
     *
360
     * @param null|int|Member $member
361
     *
362
     * @return bool
363
     */
364 View Code Duplication
    public function canCreateCategories($member = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
365
    {
366
        $member = $this->getMember($member);
367
368
        $parent = $this->Parent();
369
370
        if (!$parent || !$parent->exists() || !($parent instanceof Blog)) {
371
            return false;
372
        }
373
374
        if ($parent->isEditor($member)) {
375
            return true;
376
        }
377
378
        return Permission::checkMember($member, 'ADMIN');
379
    }
380
381
    /**
382
     * Determine whether user can create new tags.
383
     *
384
     * @param null|int|Member $member
385
     *
386
     * @return bool
387
     */
388 View Code Duplication
    public function canCreateTags($member = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
389
    {
390
        $member = $this->getMember($member);
391
392
        $parent = $this->Parent();
393
394
        if (!$parent || !$parent->exists() || !($parent instanceof Blog)) {
395
            return false;
396
        }
397
398
        if ($parent->isEditor($member)) {
399
            return true;
400
        }
401
402
        if ($parent->isWriter($member)) {
403
            return true;
404
        }
405
406
        return Permission::checkMember($member, 'ADMIN');
407
    }
408
409
    /**
410
     * {@inheritdoc}
411
     *
412
     * Update the PublishDate to now if the BlogPost would otherwise be published without a date.
413
     */
414
    public function onBeforePublish()
415
    {
416
        /**
417
         * @var SS_Datetime $publishDate
418
         */
419
        $publishDate = $this->dbObject('PublishDate');
420
421
        if (!$publishDate->getValue()) {
422
            $this->PublishDate = SS_Datetime::now()->getValue();
423
            $this->write();
424
        }
425
    }
426
427
    /**
428
     * {@inheritdoc}
429
     *
430
     * Sets blog relationship on all categories and tags assigned to this post.
431
     */
432
    public function onAfterWrite()
433
    {
434
        parent::onAfterWrite();
435
436
        foreach ($this->Categories() as $category) {
437
            /**
438
             * @var BlogCategory $category
439
             */
440
            $category->BlogID = $this->ParentID;
441
            $category->write();
442
        }
443
444
        foreach ($this->Tags() as $tag) {
445
            /**
446
             * @var BlogTag $tag
447
             */
448
            $tag->BlogID = $this->ParentID;
449
            $tag->write();
450
        }
451
    }
452
453
    /**
454
     * {@inheritdoc}
455
     */
456
    public function canView($member = null)
457
    {
458
        $member = $this->getMember($member);
459
460
        if (!parent::canView($member)) {
461
            return false;
462
        }
463
464
        if($this->canEdit($member)) {
465
            return true;
466
        }
467
468
        /**
469
         * @var SS_Datetime $publishDate
470
         */
471
        $publishDate = $this->dbObject('PublishDate');
472
        if(!$publishDate->exists()) {
473
            return false;
474
        }
475
476
        return !$publishDate->InFuture();
477
    }
478
479
    /**
480
     * {@inheritdoc}
481
     */
482
    public function canPublish($member = null)
483
    {
484
        $member = $this->getMember($member);
485
486
        if (Permission::checkMember($member, 'ADMIN')) {
487
            return true;
488
        }
489
490
        $extended = $this->extendedCan('canPublish', $member);
491
492
        if ($extended !== null) {
493
            return $extended;
494
        }
495
496
        $parent = $this->Parent();
497
498
        if ($parent instanceof Blog && $parent->exists()) {
499
            if ($parent->isEditor($member)) {
500
                return true;
501
            }
502
503
            if ($parent->isWriter($member) && $this->isAuthor($member)) {
504
                return true;
505
            }
506
507
            if ($parent->isContributor($member)) {
508
                return parent::canEdit($member);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (canEdit() instead of canPublish()). Are you sure this is correct? If so, you might want to change this to $this->canEdit().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
509
            }
510
        }
511
512
        return $this->canEdit($member);
513
    }
514
515
    /**
516
     * {@inheritdoc}
517
     */
518
    public function canEdit($member = null)
519
    {
520
        $member = $this->getMember($member);
521
522
        if (parent::canEdit($member)) {
523
            return true;
524
        }
525
526
        $parent = $this->Parent();
527
528
        if (!$parent || !$parent->exists() || !($parent instanceof Blog)) {
529
            return false;
530
        }
531
532
        if ($parent->isEditor($member)) {
533
            return true;
534
        }
535
536
        if (!$parent->isWriter($member) && !$parent->isContributor($member)) {
537
            return false;
538
        }
539
540
        return $this->isAuthor($member);
541
    }
542
543
    /**
544
     * Returns the post excerpt.
545
     *
546
     * @param int $wordsToDisplay
547
     *
548
     * @return string
549
     */
550
    public function Excerpt($wordsToDisplay = 30)
551
    {
552
        /** @var HTMLText $content */
553
        $content = $this->dbObject('Content');
554
555
        return $content->Summary($wordsToDisplay);
556
    }
557
558
    /**
559
     * Returns a monthly archive link for the current blog post.
560
     *
561
     * @param string $type
562
     *
563
     * @return string
564
     */
565
    public function getMonthlyArchiveLink($type = 'day')
566
    {
567
        /**
568
         * @var SS_Datetime $date
569
         */
570
        $date = $this->dbObject('PublishDate');
571
572
        if ($type != 'year') {
573
            if ($type == 'day') {
574
                return Controller::join_links(
575
                    $this->Parent()->Link('archive'),
576
                    $date->format('Y'),
577
                    $date->format('m'),
578
                    $date->format('d')
579
                );
580
            }
581
582
            return Controller::join_links($this->Parent()->Link('archive'), $date->format('Y'), $date->format('m'));
583
        }
584
585
        return Controller::join_links($this->Parent()->Link('archive'), $date->format('Y'));
586
    }
587
588
    /**
589
     * Returns a yearly archive link for the current blog post.
590
     *
591
     * @return string
592
     */
593
    public function getYearlyArchiveLink()
594
    {
595
        /**
596
         * @var SS_Datetime $date
597
         */
598
        $date = $this->dbObject('PublishDate');
599
600
        return Controller::join_links($this->Parent()->Link('archive'), $date->format('Y'));
601
    }
602
603
    /**
604
     * Resolves static and dynamic authors linked to this post.
605
     *
606
     * @return ArrayList
607
     */
608
    public function getCredits()
609
    {
610
        $list = new ArrayList();
611
612
        $list->merge($this->getDynamicCredits());
613
        $list->merge($this->getStaticCredits());
614
615
        return $list->sort('Name');
616
    }
617
618
    /**
619
     * Resolves dynamic authors linked to this post.
620
     *
621
     * @return ArrayList
622
     */
623
    protected function getDynamicCredits()
624
    {
625
        // Find best page to host user profiles
626
        $parent = $this->Parent();
627
        if (! ($parent instanceof Blog)) {
628
            $parent = Blog::get()->first();
629
        }
630
631
        // If there is no parent blog, return list undecorated
632
        if (!$parent) {
633
            $items = $this->Authors()->toArray();
634
            return new ArrayList($items);
635
        }
636
637
        // Update all authors
638
        $items = new ArrayList();
639
        foreach ($this->Authors() as $author) {
640
            // Add link for each author
641
            $author = $author->customise(array(
642
                'URL' => $parent->ProfileLink($author->URLSegment),
643
            ));
644
            $items->push($author);
645
        }
646
647
        return $items;
648
    }
649
650
    /**
651
     * Resolves static authors linked to this post.
652
     *
653
     * @return ArrayList
654
     */
655
    protected function getStaticCredits()
656
    {
657
        $items = new ArrayList();
658
659
        $authors = array_filter(preg_split('/\s*,\s*/', $this->AuthorNames));
660
661
        foreach ($authors as $author) {
662
            $item = new ArrayData(array(
663
                'Name' => $author,
664
            ));
665
666
            $items->push($item);
667
        }
668
669
        return $items;
670
    }
671
672
    /**
673
     * Sets the label for BlogPost.Title to 'Post Title' (Rather than 'Page name').
674
     *
675
     * @param bool $includeRelations
676
     *
677
     * @return array
678
     */
679
    public function fieldLabels($includeRelations = true)
680
    {
681
        $labels = parent::fieldLabels($includeRelations);
682
683
        $labels['Title'] = _t('BlogPost.PageTitleLabel', 'Post Title');
684
685
        return $labels;
686
    }
687
688
    /**
689
     * Proxy method for displaying the publish date in rss feeds.
690
     * @see https://github.com/silverstripe/silverstripe-blog/issues/394
691
     *
692
     * @return string|null
693
     */
694
    public function getDate()
695
    {
696
        if ($this->hasDatabaseField('Date')) {
697
            return $this->getField('Date');
698
        }
699
        return !empty($this->PublishDate) ? $this->PublishDate : null;
700
    }
701
702
    /**
703
     * {@inheritdoc}
704
     */
705
    protected function onBeforeWrite()
706
    {
707
        parent::onBeforeWrite();
708
709
        if (!$this->exists() && ($member = Member::currentUser())) {
710
            $this->Authors()->add($member);
711
        }
712
    }
713
}
714
715
/**
716
 * @package silverstripe
717
 * @subpackage blog
718
 */
719
class BlogPost_Controller extends Page_Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
720
{
721
}
722