Completed
Pull Request — master (#421)
by Robbie
02:31
created

BlogMemberExtension   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 10
dl 0
loc 106
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A onBeforeWrite() 0 11 2
A generateURLSegment() 0 12 4
A validURLSegment() 0 10 2
B updateCMSFields() 0 28 1
1
<?php
2
3
namespace SilverStripe\Blog\Model;
4
5
use SilverStripe\Blog\Forms\GridField\GridFieldConfig_BlogPost;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\GridField\GridField;
8
use SilverStripe\Forms\Tab;
9
use SilverStripe\ORM\DataExtension;
10
use SilverStripe\Security\Member;
11
use SilverStripe\View\Parsers\URLSegmentFilter;
12
use SilverStripe\View\Requirements;
13
14
/**
15
 * This class is responsible for add Blog specific behaviour to Members.
16
 *
17
 * @package silverstripe
18
 * @subpackage blog
19
 */
20
class BlogMemberExtension extends DataExtension
21
{
22
    /**
23
     * @var array
24
     */
25
    private static $db = array(
0 ignored issues
show
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
26
        'URLSegment'         => 'Varchar',
27
        'BlogProfileSummary' => 'Text'
28
    );
29
30
    /**
31
     * @var array
32
     */
33
    private static $has_one = array(
0 ignored issues
show
Unused Code introduced by
The property $has_one is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
34
        'BlogProfileImage' => 'SilverStripe\\Assets\\Image'
35
    );
36
37
    /**
38
     * @var array
39
     */
40
    private static $belongs_many_many = array(
0 ignored issues
show
Unused Code introduced by
The property $belongs_many_many is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
41
        'BlogPosts' => 'SilverStripe\\Blog\\Model\\BlogPost'
42
    );
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function onBeforeWrite()
48
    {
49
        $count = 1;
50
51
        $this->owner->URLSegment = $this->generateURLSegment();
52
53
        while (!$this->validURLSegment()) {
54
            $this->owner->URLSegment = preg_replace('/-[0-9]+$/', null, $this->owner->URLSegment) . '-' . $count;
55
            $count++;
56
        }
57
    }
58
59
    /**
60
     * Generate a unique URL segment based on the Member's name.
61
     *
62
     * @return string
63
     */
64
    public function generateURLSegment()
65
    {
66
        $filter = URLSegmentFilter::create();
67
        $name = $this->owner->FirstName . ' ' . $this->owner->Surname;
68
        $urlSegment = $filter->filter($name);
69
70
        if (!$urlSegment || $urlSegment == '-' || $urlSegment == '-1') {
71
            $urlSegment = 'profile-' . $this->owner->ID;
72
        }
73
74
        return $urlSegment;
75
    }
76
77
    /**
78
     * Returns TRUE if this object has a URL segment value that does not conflict with any other
79
     * objects.
80
     *
81
     * @return bool
82
     */
83
    public function validURLSegment()
84
    {
85
        $conflict = Member::get()->filter('URLSegment', $this->owner->URLSegment);
86
87
        if ($this->owner->ID) {
88
            $conflict = $conflict->exclude('ID', $this->owner->ID);
89
        }
90
91
        return $conflict->count() == 0;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function updateCMSFields(FieldList $fields)
98
    {
99
        $fields->removeByName('URLSegment');
100
101
        // Remove the automatically-generated posts tab.
102
103
        $fields->removeFieldFromTab('Root', 'BlogPosts');
104
105
        // Construct a better posts tab.
106
107
        Requirements::css(BLOGGER_DIR . '/css/cms.css');
108
        Requirements::javascript(BLOGGER_DIR . '/js/cms.js');
109
110
        $tab = Tab::create('BlogPosts', 'Blog Posts');
111
112
        $gridField = GridField::create(
113
            'BlogPosts',
114
            'Blog Posts',
115
            $this->owner->BlogPosts(),
116
            GridFieldConfig_BlogPost::create()
117
        );
118
119
        $tab->Fields()->add($gridField);
120
121
        $fields->addFieldToTab('Root', $tab);
122
123
        return $fields;
124
    }
125
}
126