Completed
Push — 2.2 ( 8a877d )
by Damian
09:45 queued 06:58
created

BlogPost::getDynamicCredits()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 25
Code Lines 13

Duplication

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