Completed
Pull Request — master (#421)
by Robbie
03:13 queued 52s
created

BlogMemberExtension::onBeforeWrite()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
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(
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...
28
        'URLSegment'         => 'Varchar',
29
        'BlogProfileSummary' => 'Text'
30
    );
31
32
    /**
33
     * @var array
34
     */
35
    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...
36
        'BlogProfileImage' => Image::class
37
    );
38
39
    /**
40
     * @var array
41
     */
42
    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...
43
        'BlogPosts' => BlogPost::class
44
    );
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function onBeforeWrite()
50
    {
51
        $count = 1;
52
53
        $this->owner->URLSegment = $this->generateURLSegment();
54
55
        while (!$this->validURLSegment()) {
56
            $this->owner->URLSegment = preg_replace('/-[0-9]+$/', null, $this->owner->URLSegment) . '-' . $count;
57
            $count++;
58
        }
59
    }
60
61
    /**
62
     * Generate a unique URL segment based on the Member's name.
63
     *
64
     * @return string
65
     */
66
    public function generateURLSegment()
67
    {
68
        $filter = URLSegmentFilter::create();
69
        $name = $this->owner->FirstName . ' ' . $this->owner->Surname;
70
        $urlSegment = $filter->filter($name);
71
72
        if (!$urlSegment || $urlSegment == '-' || $urlSegment == '-1') {
73
            $urlSegment = 'profile-' . $this->owner->ID;
74
        }
75
76
        return $urlSegment;
77
    }
78
79
    /**
80
     * Returns TRUE if this object has a URL segment value that does not conflict with any other
81
     * objects.
82
     *
83
     * @return bool
84
     */
85
    public function validURLSegment()
86
    {
87
        $conflict = Member::get()->filter('URLSegment', $this->owner->URLSegment);
88
89
        if ($this->owner->ID) {
90
            $conflict = $conflict->exclude('ID', $this->owner->ID);
91
        }
92
93
        return $conflict->count() == 0;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function updateCMSFields(FieldList $fields)
100
    {
101
        $fields->removeByName('URLSegment');
102
103
        // Remove the automatically-generated posts tab.
104
105
        $fields->removeFieldFromTab('Root', 'BlogPosts');
106
107
        // Construct a better posts tab.
108
109
        Requirements::css(BLOGGER_DIR . '/css/cms.css');
110
        Requirements::javascript(BLOGGER_DIR . '/js/cms.js');
111
112
        $tab = Tab::create('BlogPosts', 'Blog Posts');
113
114
        $gridField = GridField::create(
115
            'BlogPosts',
116
            'Blog Posts',
117
            $this->owner->BlogPosts(),
118
            GridFieldConfig_BlogPost::create()
119
        );
120
121
        $tab->Fields()->add($gridField);
122
123
        $fields->addFieldToTab('Root', $tab);
124
125
        return $fields;
126
    }
127
}
128