|
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 {
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/**
|
|
28
|
|
|
* @var array
|
|
29
|
|
|
*/
|
|
30
|
|
|
private static $db = array(
|
|
|
|
|
|
|
31
|
|
|
'PublishDate' => 'SS_Datetime',
|
|
32
|
|
|
'AuthorNames' => 'Varchar(1024)',
|
|
33
|
|
|
'Summary' => 'HTMLText',
|
|
34
|
|
|
);
|
|
35
|
|
|
|
|
36
|
|
|
/**
|
|
37
|
|
|
* @var array
|
|
38
|
|
|
*/
|
|
39
|
|
|
private static $has_one = array(
|
|
|
|
|
|
|
40
|
|
|
'FeaturedImage' => 'Image',
|
|
41
|
|
|
);
|
|
42
|
|
|
|
|
43
|
|
|
/**
|
|
44
|
|
|
* @var array
|
|
45
|
|
|
*/
|
|
46
|
|
|
private static $many_many = array(
|
|
|
|
|
|
|
47
|
|
|
'Categories' => 'BlogCategory',
|
|
48
|
|
|
'Tags' => 'BlogTag',
|
|
49
|
|
|
'Authors' => 'Member',
|
|
50
|
|
|
);
|
|
51
|
|
|
|
|
52
|
|
|
/**
|
|
53
|
|
|
* @var array
|
|
54
|
|
|
*/
|
|
55
|
|
|
private static $defaults = array(
|
|
|
|
|
|
|
56
|
|
|
'ShowInMenus' => false,
|
|
57
|
|
|
'InheritSideBar' => true,
|
|
58
|
|
|
'ProvideComments' => true,
|
|
59
|
|
|
);
|
|
60
|
|
|
|
|
61
|
|
|
/**
|
|
62
|
|
|
* @var array
|
|
63
|
|
|
*/
|
|
64
|
|
|
private static $extensions = array(
|
|
|
|
|
|
|
65
|
|
|
'BlogPostFilter',
|
|
66
|
|
|
);
|
|
67
|
|
|
|
|
68
|
|
|
/**
|
|
69
|
|
|
* @var array
|
|
70
|
|
|
*/
|
|
71
|
|
|
private static $searchable_fields = array(
|
|
|
|
|
|
|
72
|
|
|
'Title',
|
|
73
|
|
|
);
|
|
74
|
|
|
|
|
75
|
|
|
/**
|
|
76
|
|
|
* @var array
|
|
77
|
|
|
*/
|
|
78
|
|
|
private static $summary_fields = array(
|
|
|
|
|
|
|
79
|
|
|
'Title',
|
|
80
|
|
|
);
|
|
81
|
|
|
|
|
82
|
|
|
/**
|
|
83
|
|
|
* @var array
|
|
84
|
|
|
*/
|
|
85
|
|
|
private static $casting = array(
|
|
|
|
|
|
|
86
|
|
|
'Excerpt' => 'Text',
|
|
87
|
|
|
);
|
|
88
|
|
|
|
|
89
|
|
|
/**
|
|
90
|
|
|
* @var array
|
|
91
|
|
|
*/
|
|
92
|
|
|
private static $allowed_children = array();
|
|
|
|
|
|
|
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';
|
|
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
/**
|
|
102
|
|
|
* @var bool
|
|
103
|
|
|
*/
|
|
104
|
|
|
private static $can_be_root = false;
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
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) {
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
331
|
|
|
}
|
|
332
|
|
|
|
|
333
|
|
|
/**
|
|
334
|
|
|
* @param null|int|Member $member
|
|
335
|
|
|
*
|
|
336
|
|
|
* @return null|Member
|
|
337
|
|
|
*/
|
|
338
|
|
View Code Duplication |
protected function getMember($member = null) {
|
|
|
|
|
|
|
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) {
|
|
|
|
|
|
|
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) {
|
|
|
|
|
|
|
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) {
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
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 {
|
|
|
|
|
|
|
685
|
|
|
|
|
686
|
|
|
}
|
|
687
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.