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-03-22 23:18:02 +01:00
|
|
|
class BlogMemberExtension extends DataExtension {
|
2015-05-09 16:33:12 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2015-03-22 23:18:02 +01:00
|
|
|
private static $db = array(
|
|
|
|
'URLSegment' => 'Varchar',
|
2015-05-09 16:33:12 +02:00
|
|
|
'BlogProfileSummary' => 'Text',
|
2015-03-22 23:18:02 +01:00
|
|
|
);
|
|
|
|
|
2015-05-09 16:33:12 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2015-03-22 23:18:02 +01:00
|
|
|
private static $has_one = array(
|
2015-05-09 16:33:12 +02:00
|
|
|
'BlogProfileImage' => 'Image',
|
2015-03-22 23:18:02 +01:00
|
|
|
);
|
|
|
|
|
2015-05-09 16:33:12 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2015-03-22 23:18:02 +01:00
|
|
|
private static $belongs_many_many = array(
|
2015-05-18 00:55:18 +02:00
|
|
|
'BlogPosts' => 'BlogPost',
|
2015-03-22 23:18:02 +01:00
|
|
|
);
|
|
|
|
|
2015-05-09 16:33:12 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2015-03-22 23:18:02 +01:00
|
|
|
public function onBeforeWrite() {
|
|
|
|
$count = 1;
|
|
|
|
|
|
|
|
$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.
|
2015-05-09 16:33:12 +02:00
|
|
|
*
|
|
|
|
* @return string
|
2015-03-22 23:18:02 +01:00
|
|
|
*/
|
|
|
|
public function generateURLSegment() {
|
|
|
|
$filter = URLSegmentFilter::create();
|
|
|
|
$name = $this->owner->FirstName . ' ' . $this->owner->Surname;
|
|
|
|
$urlSegment = $filter->filter($name);
|
|
|
|
|
2015-03-31 06:07:29 +02:00
|
|
|
if(!$urlSegment || $urlSegment == '-' || $urlSegment == '-1') {
|
2015-05-09 16:33:12 +02:00
|
|
|
$urlSegment = 'profile-' . $this->owner->ID;
|
2015-03-31 06:07:29 +02:00
|
|
|
}
|
2015-03-22 23:18:02 +01:00
|
|
|
|
|
|
|
return $urlSegment;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-09 16:33:12 +02:00
|
|
|
* Returns TRUE if this object has a URL segment value that does not conflict with any other
|
|
|
|
* objects.
|
|
|
|
*
|
2015-03-22 23:18:02 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2015-05-09 16:33:12 +02:00
|
|
|
public function validURLSegment() {
|
2015-03-22 23:18:02 +01:00
|
|
|
$conflict = Member::get()->filter('URLSegment', $this->owner->URLSegment);
|
|
|
|
|
2015-05-09 16:33:12 +02:00
|
|
|
if($this->owner->ID) {
|
2015-03-22 23:18:02 +01:00
|
|
|
$conflict = $conflict->exclude('ID', $this->owner->ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $conflict->count() == 0;
|
|
|
|
}
|
|
|
|
|
2015-05-18 00:55:18 +02:00
|
|
|
|
2015-05-09 16:33:12 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function updateCMSFields(FieldList $fields) {
|
|
|
|
$fields->removeByName('URLSegment');
|
|
|
|
|
2015-05-18 00:55:18 +02:00
|
|
|
// 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');
|
|
|
|
|
|
|
|
$tab = new Tab('BlogPosts', 'Blog Posts');
|
|
|
|
|
|
|
|
$gridField = new GridField(
|
|
|
|
'BlogPosts',
|
|
|
|
'Blog Posts',
|
|
|
|
$this->owner->BlogPosts(),
|
|
|
|
new GridFieldConfig_BlogPost()
|
|
|
|
);
|
|
|
|
|
|
|
|
$tab->Fields()->add($gridField);
|
|
|
|
|
|
|
|
$fields->addFieldToTab('Root', $tab);
|
|
|
|
|
2015-05-09 16:33:12 +02:00
|
|
|
return $fields;
|
|
|
|
}
|
2015-03-22 23:18:02 +01:00
|
|
|
}
|