2015-03-22 23:18:02 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class is responsible for add Blog specific behaviour to Members.
|
|
|
|
*
|
|
|
|
* @package silverstripe
|
|
|
|
* @subpackage blog
|
2015-05-09 16:33:12 +02:00
|
|
|
*/
|
2015-11-21 07:17:29 +01:00
|
|
|
class BlogMemberExtension extends DataExtension
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $db = array(
|
2017-09-01 02:06:29 +02:00
|
|
|
'URLSegment' => 'Varchar(255)',
|
2015-11-21 07:17:29 +01:00
|
|
|
'BlogProfileSummary' => 'Text',
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $has_one = array(
|
|
|
|
'BlogProfileImage' => 'Image',
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $belongs_many_many = array(
|
|
|
|
'BlogPosts' => 'BlogPost',
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function onBeforeWrite()
|
|
|
|
{
|
|
|
|
$count = 1;
|
|
|
|
|
2017-07-20 11:31:55 +02:00
|
|
|
if ($this->owner->URLSegment && !$this->owner->isChanged('FirstName') && !$this->owner->isChanged('Surname')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
$this->owner->URLSegment = $this->generateURLSegment();
|
|
|
|
|
|
|
|
while (!$this->validURLSegment()) {
|
|
|
|
$this->owner->URLSegment = preg_replace('/-[0-9]+$/', null, $this->owner->URLSegment) . '-' . $count;
|
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a unique URL segment based on the Member's name.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function generateURLSegment()
|
|
|
|
{
|
|
|
|
$filter = URLSegmentFilter::create();
|
|
|
|
$name = $this->owner->FirstName . ' ' . $this->owner->Surname;
|
|
|
|
$urlSegment = $filter->filter($name);
|
|
|
|
|
|
|
|
if (!$urlSegment || $urlSegment == '-' || $urlSegment == '-1') {
|
|
|
|
$urlSegment = 'profile-' . $this->owner->ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $urlSegment;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns TRUE if this object has a URL segment value that does not conflict with any other
|
|
|
|
* objects.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function validURLSegment()
|
|
|
|
{
|
|
|
|
$conflict = Member::get()->filter('URLSegment', $this->owner->URLSegment);
|
|
|
|
|
|
|
|
if ($this->owner->ID) {
|
|
|
|
$conflict = $conflict->exclude('ID', $this->owner->ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $conflict->count() == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function updateCMSFields(FieldList $fields)
|
|
|
|
{
|
|
|
|
$fields->removeByName('URLSegment');
|
|
|
|
|
|
|
|
// Remove the automatically-generated posts tab.
|
|
|
|
|
|
|
|
$fields->removeFieldFromTab('Root', 'BlogPosts');
|
|
|
|
|
|
|
|
// Construct a better posts tab.
|
|
|
|
|
|
|
|
Requirements::css(BLOGGER_DIR . '/css/cms.css');
|
|
|
|
Requirements::javascript(BLOGGER_DIR . '/js/cms.js');
|
|
|
|
|
2017-04-12 10:33:29 +02:00
|
|
|
$tab = new Tab('BlogPosts', _t('BlogMemberExtension.TABBLOGPOSTS', 'Blog Posts'));
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
$gridField = new GridField(
|
|
|
|
'BlogPosts',
|
2017-04-12 10:33:29 +02:00
|
|
|
_t('BlogMemberExtension.BLOGPOSTS', 'Blog Posts'),
|
2015-11-21 07:17:29 +01:00
|
|
|
$this->owner->BlogPosts(),
|
|
|
|
new GridFieldConfig_BlogPost()
|
|
|
|
);
|
|
|
|
|
|
|
|
$tab->Fields()->add($gridField);
|
|
|
|
|
|
|
|
$fields->addFieldToTab('Root', $tab);
|
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
2015-03-22 23:18:02 +01:00
|
|
|
}
|