Completed
Pull Request — master (#373)
by Michael
02:31
created

BlogPost::canView()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 23
rs 8.7972
cc 4
eloc 10
nc 4
nop 1
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' => 'Text',
87
    );
88
89
    /**
90
     * @var array
91
     */
92
    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...
93
94
    /**
95
     * The default sorting lists BlogPosts with an empty PublishDate at the top.
96
     *
97
     * @var string
98
     */
99
    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...
100
101
    /**
102
     * @var bool
103
     */
104
    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...
105
106
    /**
107
     * This will display or hide the current class from the SiteTree. This variable can be
108
     * configured using YAML.
109
     *
110
     * @var bool
111
     */
112
    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...
113
114
    /**
115
     * Determine the role of the given member.
116
     *
117
     * Call be called via template to determine the current user.
118
     *
119
     * @example "Hello $RoleOf($CurrentMember.ID)"
120
     *
121
     * @param null|int|Member $member
122
     *
123
     * @return null|string
124
     */
125
    public function RoleOf($member = null)
126
    {
127
        $member = $this->getMember($member);
128
129
        if (!$member) {
130
            return null;
131
        }
132
133
        if ($this->isAuthor($member)) {
134
            return _t('BlogPost.AUTHOR', 'Author');
135
        }
136
137
        $parent = $this->Parent();
138
139
        if ($parent instanceof Blog) {
140
            return $parent->RoleOf($member);
141
        }
142
143
        return null;
144
    }
145
146
    /**
147
     * Determine if the given member is an author of this post.
148
     *
149
     * @param null|Member $member
150
     *
151
     * @return bool
152
     */
153 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...
154
    {
155
        if (!$member || !$member->exists()) {
156
            return false;
157
        }
158
159
        $list = $this->Authors();
160
161
        if ($list instanceof UnsavedRelationList) {
162
            return in_array($member->ID, $list->getIDList());
163
        }
164
165
        return $list->byID($member->ID) !== null;
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171
    public function getCMSFields()
172
    {
173
        Requirements::css(BLOGGER_DIR . '/css/cms.css');
174
        Requirements::javascript(BLOGGER_DIR . '/js/cms.js');
175
176
        $self =& $this;
177
178
        $this->beforeUpdateCMSFields(function ($fields) use ($self) {
179
            $uploadField = UploadField::create('FeaturedImage', _t('BlogPost.FeaturedImage', 'Featured Image'));
180
            $uploadField->getValidator()->setAllowedExtensions(array('jpg', 'jpeg', 'png', 'gif'));
181
182
            /**
183
             * @var FieldList $fields
184
             */
185
            $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...
186
187
            $summary = HtmlEditorField::create('Summary', false);
188
            $summary->setRows(5);
189
            $summary->setDescription(_t(
190
                'BlogPost.SUMMARY_DESCRIPTION',
191
                'If no summary is specified the first 30 words will be used.'
192
            ));
193
194
            $summaryHolder = ToggleCompositeField::create(
195
                'CustomSummary',
196
                _t('BlogPost.CUSTOMSUMMARY', 'Add A Custom Summary'),
197
                array(
198
                    $summary,
199
                )
200
            );
201
            $summaryHolder->setHeadingLevel(4);
202
            $summaryHolder->addExtraClass('custom-summary');
203
204
            $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...
205
206
            $fields->push(HiddenField::create('MenuTitle'));
207
208
            $urlSegment = $fields->dataFieldByName('URLSegment');
209
            $urlSegment->setURLPrefix($self->Parent()->RelativeLink());
210
211
            $fields->removeFieldsFromTab('Root.Main', array(
212
                'MenuTitle',
213
                'URLSegment',
214
            ));
215
216
            $authorField = ListboxField::create(
217
                'Authors',
218
                _t('BlogPost.Authors', 'Authors'),
219
                $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...
220
            )->setMultiple(true);
221
222
            $authorNames = TextField::create(
223
                'AuthorNames',
224
                _t('BlogPost.AdditionalCredits', 'Additional Credits'),
225
                null,
226
                1024
227
            )->setDescription(_t(
228
                    'BlogPost.AdditionalCredits_Description',
229
                    'If some authors of this post don\'t have CMS access, enter their name(s) here. You can separate multiple names with a comma.')
230
            );
231
232
            if (!$self->canEditAuthors()) {
233
                $authorField = $authorField->performDisabledTransformation();
234
                $authorNames = $authorNames->performDisabledTransformation();
235
            }
236
237
            $publishDate = DatetimeField::create('PublishDate', _t('BlogPost.PublishDate', 'Publish Date'));
238
            $publishDate->getDateField()->setConfig('showcalendar', true);
239
            if (!$self->PublishDate) {
240
                $publishDate->setDescription(_t(
241
                        'BlogPost.PublishDate_Description',
242
                        'Will be set to "now" if published without a value.')
243
                );
244
            }
245
246
            // Get categories and tags
247
            $parent = $self->Parent();
248
            $categories = $parent instanceof Blog
249
                ? $parent->Categories()
250
                : BlogCategory::get();
251
            $tags = $parent instanceof Blog
252
                ? $parent->Tags()
253
                : BlogTag::get();
254
255
            $options = BlogAdminSidebar::create(
256
                $publishDate,
257
                $urlSegment,
258
                TagField::create(
259
                    'Categories',
260
                    _t('BlogPost.Categories', 'Categories'),
261
                    $categories,
262
                    $self->Categories()
263
                )
264
                    ->setCanCreate($self->canCreateCategories())
265
                    ->setShouldLazyLoad(true),
266
                TagField::create(
267
                    'Tags',
268
                    _t('BlogPost.Tags', 'Tags'),
269
                    $tags,
270
                    $self->Tags()
271
                )
272
                    ->setCanCreate($self->canCreateTags())
273
                    ->setShouldLazyLoad(true),
274
                $authorField,
275
                $authorNames
276
            )->setTitle('Post Options');
277
278
            $options->setName('blog-admin-sidebar');
279
280
            $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...
281
        });
282
283
        $fields = parent::getCMSFields();
284
285
        $fields->fieldByName('Root')->setTemplate('TabSet_holder');
286
287
        return $fields;
288
    }
289
290
    /**
291
     * Gets the list of author candidates to be assigned as authors of this blog post.
292
     *
293
     * @return SS_List
294
     */
295
    public function getCandidateAuthors()
296
    {
297
        if ($this->config()->restrict_authors_to_group) {
298
            return Group::get()->filter('Code', $this->config()->restrict_authors_to_group)->first()->Members();
299
        } else {
300
            $list = Member::get();
301
            $this->extend('updateCandidateAuthors', $list);
302
            return $list;
303
        }
304
    }
305
306
    /**
307
     * Determine if this user can edit the authors list.
308
     *
309
     * @param null|int|Member $member
310
     *
311
     * @return bool
312
     */
313
    public function canEditAuthors($member = null)
314
    {
315
        $member = $this->getMember($member);
316
317
        $extended = $this->extendedCan('canEditAuthors', $member);
318
319
        if ($extended !== null) {
320
            return $extended;
321
        }
322
323
        $parent = $this->Parent();
324
325
        if ($parent instanceof Blog && $parent->exists()) {
326
            if ($parent->isEditor($member)) {
327
                return true;
328
            }
329
330
            if ($parent->isWriter($member) && $this->isAuthor($member)) {
331
                return true;
332
            }
333
        }
334
335
        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 335 which is incompatible with the return type documented by BlogPost::canEditAuthors of type boolean.
Loading history...
336
    }
337
338
    /**
339
     * @param null|int|Member $member
340
     *
341
     * @return null|Member
342
     */
343 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...
344
    {
345
        if (!$member) {
346
            $member = Member::currentUser();
347
        }
348
349
        if (is_numeric($member)) {
350
            $member = Member::get()->byID($member);
351
        }
352
353
        return $member;
354
    }
355
356
    /**
357
     * Determine whether user can create new categories.
358
     *
359
     * @param null|int|Member $member
360
     *
361
     * @return bool
362
     */
363 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...
364
    {
365
        $member = $this->getMember($member);
366
367
        $parent = $this->Parent();
368
369
        if (!$parent || !$parent->exists() || !($parent instanceof Blog)) {
370
            return false;
371
        }
372
373
        if ($parent->isEditor($member)) {
374
            return true;
375
        }
376
377
        return Permission::checkMember($member, 'ADMIN');
378
    }
379
380
    /**
381
     * Determine whether user can create new tags.
382
     *
383
     * @param null|int|Member $member
384
     *
385
     * @return bool
386
     */
387 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...
388
    {
389
        $member = $this->getMember($member);
390
391
        $parent = $this->Parent();
392
393
        if (!$parent || !$parent->exists() || !($parent instanceof Blog)) {
394
            return false;
395
        }
396
397
        if ($parent->isEditor($member)) {
398
            return true;
399
        }
400
401
        if ($parent->isWriter($member)) {
402
            return true;
403
        }
404
405
        return Permission::checkMember($member, 'ADMIN');
406
    }
407
408
    /**
409
     * {@inheritdoc}
410
     *
411
     * Update the PublishDate to now if the BlogPost would otherwise be published without a date.
412
     */
413
    public function onBeforePublish()
414
    {
415
        /**
416
         * @var SS_Datetime $publishDate
417
         */
418
        $publishDate = $this->dbObject('PublishDate');
419
420
        if (!$publishDate->getValue()) {
421
            $this->PublishDate = SS_Datetime::now()->getValue();
422
            $this->write();
423
        }
424
    }
425
426
    /**
427
     * {@inheritdoc}
428
     *
429
     * Sets blog relationship on all categories and tags assigned to this post.
430
     */
431
    public function onAfterWrite()
432
    {
433
        parent::onAfterWrite();
434
435
        foreach ($this->Categories() as $category) {
436
            /**
437
             * @var BlogCategory $category
438
             */
439
            $category->BlogID = $this->ParentID;
440
            $category->write();
441
        }
442
443
        foreach ($this->Tags() as $tag) {
444
            /**
445
             * @var BlogTag $tag
446
             */
447
            $tag->BlogID = $this->ParentID;
448
            $tag->write();
449
        }
450
    }
451
452
    /**
453
     * {@inheritdoc}
454
     */
455
    public function canView($member = null)
456
    {
457
        $member = $this->getMember($member);
458
459
        if (!parent::canView($member)) {
460
            return false;
461
        }
462
463
        /**
464
         * @var SS_Datetime $publishDate
465
         */
466
        $publishDate = $this->dbObject('PublishDate');
467
        if(!$publishDate->exists()) {
468
            return false;
469
        }
470
471
        if(!$publishDate->InFuture()) {
472
            return true;
473
        }
474
475
        // Anyone that can edit this page can view it
476
        return $this->canEdit($member);
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
        /**
553
         * @var Text $content
554
         */
555
        $content = $this->dbObject('Content');
556
557
        return $content->Summary($wordsToDisplay);
558
    }
559
560
    /**
561
     * Returns a monthly archive link for the current blog post.
562
     *
563
     * @param string $type
564
     *
565
     * @return string
566
     */
567
    public function getMonthlyArchiveLink($type = 'day')
568
    {
569
        /**
570
         * @var SS_Datetime $date
571
         */
572
        $date = $this->dbObject('PublishDate');
573
574
        if ($type != 'year') {
575
            if ($type == 'day') {
576
                return Controller::join_links(
577
                    $this->Parent()->Link('archive'),
578
                    $date->format('Y'),
579
                    $date->format('m'),
580
                    $date->format('d')
581
                );
582
            }
583
584
            return Controller::join_links($this->Parent()->Link('archive'), $date->format('Y'), $date->format('m'));
585
        }
586
587
        return Controller::join_links($this->Parent()->Link('archive'), $date->format('Y'));
588
    }
589
590
    /**
591
     * Returns a yearly archive link for the current blog post.
592
     *
593
     * @return string
594
     */
595
    public function getYearlyArchiveLink()
596
    {
597
        /**
598
         * @var SS_Datetime $date
599
         */
600
        $date = $this->dbObject('PublishDate');
601
602
        return Controller::join_links($this->Parent()->Link('archive'), $date->format('Y'));
603
    }
604
605
    /**
606
     * Resolves static and dynamic authors linked to this post.
607
     *
608
     * @return ArrayList
609
     */
610
    public function getCredits()
611
    {
612
        $list = new ArrayList();
613
614
        $list->merge($this->getDynamicCredits());
615
        $list->merge($this->getStaticCredits());
616
617
        return $list->sort('Name');
618
    }
619
620
    /**
621
     * Resolves dynamic authors linked to this post.
622
     *
623
     * @return ArrayList
624
     */
625
    protected function getDynamicCredits()
626
    {
627
        // Find best page to host user profiles
628
        $parent = $this->Parent();
629
        if (! ($parent instanceof Blog)) {
630
            $parent = Blog::get()->first();
631
        }
632
633
        // If there is no parent blog, return list undecorated
634
        if (!$parent) {
635
            $items = $this->Authors()->toArray();
636
            return new ArrayList($items);
637
        }
638
639
        // Update all authors
640
        $items = new ArrayList();
641
        foreach ($this->Authors() as $author) {
642
            // Add link for each author
643
            $author = $author->customise(array(
644
                'URL' => $parent->ProfileLink($author->URLSegment),
645
            ));
646
            $items->push($author);
647
        }
648
649
        return $items;
650
    }
651
652
    /**
653
     * Resolves static authors linked to this post.
654
     *
655
     * @return ArrayList
656
     */
657
    protected function getStaticCredits()
658
    {
659
        $items = new ArrayList();
660
661
        $authors = array_filter(preg_split('/\s*,\s*/', $this->AuthorNames));
662
663
        foreach ($authors as $author) {
664
            $item = new ArrayData(array(
665
                'Name' => $author,
666
            ));
667
668
            $items->push($item);
669
        }
670
671
        return $items;
672
    }
673
674
    /**
675
     * Sets the label for BlogPost.Title to 'Post Title' (Rather than 'Page name').
676
     *
677
     * @param bool $includeRelations
678
     *
679
     * @return array
680
     */
681
    public function fieldLabels($includeRelations = true)
682
    {
683
        $labels = parent::fieldLabels($includeRelations);
684
685
        $labels['Title'] = _t('BlogPost.PageTitleLabel', 'Post Title');
686
687
        return $labels;
688
    }
689
690
    /**
691
     * {@inheritdoc}
692
     */
693
    protected function onBeforeWrite()
694
    {
695
        parent::onBeforeWrite();
696
697
        if (!$this->exists() && ($member = Member::currentUser())) {
698
            $this->Authors()->add($member);
699
        }
700
    }
701
}
702
703
/**
704
 * @package silverstripe
705
 * @subpackage blog
706
 */
707
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...
708
{
709
}
710