1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Blog\Model; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Assets\Image; |
6
|
|
|
use SilverStripe\Blog\Forms\GridField\GridFieldConfigBlogPost; |
7
|
|
|
use SilverStripe\Forms\FieldList; |
8
|
|
|
use SilverStripe\Forms\GridField\GridField; |
9
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddNewButton; |
10
|
|
|
use SilverStripe\Forms\Tab; |
11
|
|
|
use SilverStripe\Forms\TextareaField; |
12
|
|
|
use SilverStripe\ORM\DataExtension; |
13
|
|
|
use SilverStripe\ORM\ManyManyList; |
14
|
|
|
use SilverStripe\Security\Member; |
15
|
|
|
use SilverStripe\View\Parsers\URLSegmentFilter; |
16
|
|
|
use SilverStripe\View\Requirements; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* This class is responsible for add Blog specific behaviour to Members. |
20
|
|
|
* |
21
|
|
|
* @property string $URLSegment |
22
|
|
|
* @property string $BlogProfileSummary |
23
|
|
|
* @method Image BlogProfileImage() |
24
|
|
|
* @method ManyManyList|BlogPost[] BlogPosts() |
25
|
|
|
*/ |
26
|
|
|
class BlogMemberExtension extends DataExtension |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private static $db = [ |
32
|
|
|
'URLSegment' => 'Varchar(255)', |
33
|
|
|
'BlogProfileSummary' => 'Text' |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private static $has_one = [ |
40
|
|
|
'BlogProfileImage' => Image::class |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
private static $belongs_many_many = [ |
47
|
|
|
'BlogPosts' => BlogPost::class |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function onBeforeWrite() |
54
|
|
|
{ |
55
|
|
|
$count = 1; |
56
|
|
|
|
57
|
|
|
if ($this->owner->URLSegment && !$this->owner->isChanged('FirstName') && !$this->owner->isChanged('Surname')) { |
58
|
|
|
return; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->owner->URLSegment = $this->generateURLSegment(); |
62
|
|
|
|
63
|
|
|
while (!$this->validURLSegment()) { |
64
|
|
|
$this->owner->URLSegment = preg_replace('/-[0-9]+$/', null, $this->owner->URLSegment) . '-' . $count; |
65
|
|
|
$count++; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// Auto publish profile images |
69
|
|
|
if ($this->owner->BlogProfileImage() && $this->owner->BlogProfileImage()->exists()) { |
70
|
|
|
$this->owner->BlogProfileImage()->publishSingle(); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Generate a unique URL segment based on the Member's name. |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function generateURLSegment() |
80
|
|
|
{ |
81
|
|
|
$filter = URLSegmentFilter::create(); |
82
|
|
|
$name = $this->owner->FirstName . ' ' . $this->owner->Surname; |
83
|
|
|
$urlSegment = $filter->filter($name); |
84
|
|
|
|
85
|
|
|
if (!$urlSegment || $urlSegment == '-' || $urlSegment == '-1') { |
86
|
|
|
$urlSegment = 'profile-' . $this->owner->ID; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $urlSegment; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns TRUE if this object has a URL segment value that does not conflict with any other |
94
|
|
|
* objects. |
95
|
|
|
* |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
|
|
public function validURLSegment() |
99
|
|
|
{ |
100
|
|
|
$conflict = Member::get()->filter('URLSegment', $this->owner->URLSegment); |
101
|
|
|
|
102
|
|
|
if ($this->owner->ID) { |
103
|
|
|
$conflict = $conflict->exclude('ID', $this->owner->ID); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $conflict->count() == 0; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
|
|
public function updateCMSFields(FieldList $fields) |
113
|
|
|
{ |
114
|
|
|
$fields->removeByName('URLSegment'); |
115
|
|
|
|
116
|
|
|
// Remove the automatically-generated posts tab. |
117
|
|
|
$fields->removeFieldFromTab('Root', 'BlogPosts'); |
118
|
|
|
|
119
|
|
|
// Construct a better posts tab. |
120
|
|
|
Requirements::css('silverstripe/blog:client/dist/styles/main.css'); |
121
|
|
|
Requirements::javascript('silverstripe/blog:client/dist/js/main.bundle.js'); |
122
|
|
|
|
123
|
|
|
$tab = Tab::create('BlogPosts', _t(__CLASS__ . '.TABBLOGPOSTS', 'Blog Posts')); |
124
|
|
|
|
125
|
|
|
$gridField = GridField::create( |
126
|
|
|
'BlogPosts', |
127
|
|
|
_t(__CLASS__ . '.BLOGPOSTS', 'Blog Posts'), |
128
|
|
|
$this->owner->BlogPosts(), |
129
|
|
|
$gridFieldConfig = GridFieldConfigBlogPost::create() |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
// Remove the "add new blog post" action from a member's profile |
133
|
|
|
$gridFieldConfig->removeComponentsByType(GridFieldAddNewButton::class); |
134
|
|
|
|
135
|
|
|
$tab->Fields()->add($gridField); |
136
|
|
|
|
137
|
|
|
$fields->addFieldToTab('Root', $tab); |
138
|
|
|
|
139
|
|
|
// Ensure blog fields are added after defaults |
140
|
|
|
$fields->addFieldToTab( |
141
|
|
|
'Root.Main', |
142
|
|
|
TextareaField::create('BlogProfileSummary'), |
143
|
|
|
'BlogProfileImage' |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
return $fields; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|