Completed
Push — master ( 17429c...067f41 )
by Damian
02:18
created

BlogPost::canView()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 22
rs 8.9197
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
        if($this->canEdit($member)) {
464
            return true;
465
        }
466
467
        /**
468
         * @var SS_Datetime $publishDate
469
         */
470
        $publishDate = $this->dbObject('PublishDate');
471
        if(!$publishDate->exists()) {
472
            return false;
473
        }
474
475
        return !$publishDate->InFuture();
476
    }
477
478
    /**
479
     * {@inheritdoc}
480
     */
481
    public function canPublish($member = null)
482
    {
483
        $member = $this->getMember($member);
484
485
        if (Permission::checkMember($member, 'ADMIN')) {
486
            return true;
487
        }
488
489
        $extended = $this->extendedCan('canPublish', $member);
490
491
        if ($extended !== null) {
492
            return $extended;
493
        }
494
495
        $parent = $this->Parent();
496
497
        if ($parent instanceof Blog && $parent->exists()) {
498
            if ($parent->isEditor($member)) {
499
                return true;
500
            }
501
502
            if ($parent->isWriter($member) && $this->isAuthor($member)) {
503
                return true;
504
            }
505
506
            if ($parent->isContributor($member)) {
507
                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...
508
            }
509
        }
510
511
        return $this->canEdit($member);
512
    }
513
514
    /**
515
     * {@inheritdoc}
516
     */
517
    public function canEdit($member = null)
518
    {
519
        $member = $this->getMember($member);
520
521
        if (parent::canEdit($member)) {
522
            return true;
523
        }
524
525
        $parent = $this->Parent();
526
527
        if (!$parent || !$parent->exists() || !($parent instanceof Blog)) {
528
            return false;
529
        }
530
531
        if ($parent->isEditor($member)) {
532
            return true;
533
        }
534
535
        if (!$parent->isWriter($member) && !$parent->isContributor($member)) {
536
            return false;
537
        }
538
539
        return $this->isAuthor($member);
540
    }
541
542
    /**
543
     * Returns the post excerpt.
544
     *
545
     * @param int $wordsToDisplay
546
     *
547
     * @return string
548
     */
549
    public function Excerpt($wordsToDisplay = 30)
550
    {
551
        /**
552
         * @var Text $content
553
         */
554
        $content = $this->dbObject('Content');
555
556
        return $content->Summary($wordsToDisplay);
557
    }
558
559
    /**
560
     * Returns a monthly archive link for the current blog post.
561
     *
562
     * @param string $type
563
     *
564
     * @return string
565
     */
566
    public function getMonthlyArchiveLink($type = 'day')
567
    {
568
        /**
569
         * @var SS_Datetime $date
570
         */
571
        $date = $this->dbObject('PublishDate');
572
573
        if ($type != 'year') {
574
            if ($type == 'day') {
575
                return Controller::join_links(
576
                    $this->Parent()->Link('archive'),
577
                    $date->format('Y'),
578
                    $date->format('m'),
579
                    $date->format('d')
580
                );
581
            }
582
583
            return Controller::join_links($this->Parent()->Link('archive'), $date->format('Y'), $date->format('m'));
584
        }
585
586
        return Controller::join_links($this->Parent()->Link('archive'), $date->format('Y'));
587
    }
588
589
    /**
590
     * Returns a yearly archive link for the current blog post.
591
     *
592
     * @return string
593
     */
594
    public function getYearlyArchiveLink()
595
    {
596
        /**
597
         * @var SS_Datetime $date
598
         */
599
        $date = $this->dbObject('PublishDate');
600
601
        return Controller::join_links($this->Parent()->Link('archive'), $date->format('Y'));
602
    }
603
604
    /**
605
     * Resolves static and dynamic authors linked to this post.
606
     *
607
     * @return ArrayList
608
     */
609
    public function getCredits()
610
    {
611
        $list = new ArrayList();
612
613
        $list->merge($this->getDynamicCredits());
614
        $list->merge($this->getStaticCredits());
615
616
        return $list->sort('Name');
617
    }
618
619
    /**
620
     * Resolves dynamic authors linked to this post.
621
     *
622
     * @return ArrayList
623
     */
624
    protected function getDynamicCredits()
625
    {
626
        // Find best page to host user profiles
627
        $parent = $this->Parent();
628
        if (! ($parent instanceof Blog)) {
629
            $parent = Blog::get()->first();
630
        }
631
632
        // If there is no parent blog, return list undecorated
633
        if (!$parent) {
634
            $items = $this->Authors()->toArray();
635
            return new ArrayList($items);
636
        }
637
638
        // Update all authors
639
        $items = new ArrayList();
640
        foreach ($this->Authors() as $author) {
641
            // Add link for each author
642
            $author = $author->customise(array(
643
                'URL' => $parent->ProfileLink($author->URLSegment),
644
            ));
645
            $items->push($author);
646
        }
647
648
        return $items;
649
    }
650
651
    /**
652
     * Resolves static authors linked to this post.
653
     *
654
     * @return ArrayList
655
     */
656
    protected function getStaticCredits()
657
    {
658
        $items = new ArrayList();
659
660
        $authors = array_filter(preg_split('/\s*,\s*/', $this->AuthorNames));
661
662
        foreach ($authors as $author) {
663
            $item = new ArrayData(array(
664
                'Name' => $author,
665
            ));
666
667
            $items->push($item);
668
        }
669
670
        return $items;
671
    }
672
673
    /**
674
     * Sets the label for BlogPost.Title to 'Post Title' (Rather than 'Page name').
675
     *
676
     * @param bool $includeRelations
677
     *
678
     * @return array
679
     */
680
    public function fieldLabels($includeRelations = true)
681
    {
682
        $labels = parent::fieldLabels($includeRelations);
683
684
        $labels['Title'] = _t('BlogPost.PageTitleLabel', 'Post Title');
685
686
        return $labels;
687
    }
688
689
    /**
690
     * {@inheritdoc}
691
     */
692
    protected function onBeforeWrite()
693
    {
694
        parent::onBeforeWrite();
695
696
        if (!$this->exists() && ($member = Member::currentUser())) {
697
            $this->Authors()->add($member);
698
        }
699
    }
700
}
701
702
/**
703
 * @package silverstripe
704
 * @subpackage blog
705
 */
706
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...
707
{
708
}
709