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