Code Duplication    Length = 173-174 lines in 2 locations

code/model/BlogCategory.php 1 location

@@ 14-187 (lines=174) @@
11
 * @property string $URLSegment
12
 * @property int $BlogID
13
 */
14
class BlogCategory extends DataObject implements CategorisationObject
15
{
16
17
	/**
18
	 * Use an exception code so that attempted writes can continue on
19
	 * duplicate errors. 600 is completely arbitrary.
20
	 *
21
	 * @const int
22
	 */
23
	const DUPLICATE_EXCEPTION = 600;
24
25
    /**
26
     * @var array
27
     */
28
    private static $db = array(
29
        'Title' => 'Varchar(255)',
30
    );
31
32
    /**
33
     * @var array
34
     */
35
    private static $has_one = array(
36
        'Blog' => 'Blog',
37
    );
38
39
    /**
40
     * @var array
41
     */
42
    private static $belongs_many_many = array(
43
        'BlogPosts' => 'BlogPost',
44
    );
45
46
    /**
47
     * @var array
48
     */
49
    private static $extensions = array(
50
        'URLSegmentExtension',
51
    );
52
    
53
    /**
54
     * @return DataList
55
     */
56
    public function BlogPosts()
57
    {
58
        $blogPosts = parent::BlogPosts();
59
    
60
        $this->extend("updateGetBlogPosts", $blogPosts);
61
    
62
        return $blogPosts;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getCMSFields()
69
    {
70
        $fields = new FieldList(
71
            TextField::create('Title', _t('BlogCategory.Title', 'Title'))
72
        );
73
74
        $this->extend('updateCMSFields', $fields);
75
76
        return $fields;
77
    }
78
79
	/**
80
	 * {@inheritdoc}
81
	 */
82
	protected function validate() {
83
		$validation = parent::validate();
84
		if($validation->valid()) {
85
			// Check for duplicate categories
86
			$blog = $this->Blog();
87
			if($blog && $blog->exists()) {
88
				$existing = $blog->Categories()->filter('Title', $this->Title);
89
				if($this->ID) {
90
					$existing = $existing->exclude('ID', $this->ID);
91
				}
92
				if($existing->count() > 0) {
93
					$validation->error(_t(
94
						'BlogCategory.Duplicate', 
95
						'A blog category already exists with that name'
96
					), BlogCategory::DUPLICATE_EXCEPTION);
97
				}
98
			}
99
		}
100
		return $validation;
101
	}
102
103
    /**
104
     * Returns a relative link to this category.
105
     *
106
     * @return string
107
     */
108
    public function getLink()
109
    {
110
        return Controller::join_links($this->Blog()->Link(), 'category', $this->URLSegment);
111
	}
112
    
113
114
    /**
115
     * Inherits from the parent blog or can be overwritten using a DataExtension.
116
     *
117
     * @param null|Member $member
118
     *
119
     * @return bool
120
     */
121
    public function canView($member = null)
122
    {
123
        $extended = $this->extendedCan(__FUNCTION__, $member);
124
125
        if ($extended !== null) {
126
            return $extended;
127
        }
128
129
        return $this->Blog()->canView($member);
130
    }
131
132
    /**
133
     * Inherits from the parent blog or can be overwritten using a DataExtension.
134
     *
135
     * @param null|Member $member
136
     *
137
     * @return bool
138
     */
139
    public function canCreate($member = null)
140
    {
141
        $extended = $this->extendedCan(__FUNCTION__, $member);
142
143
        if ($extended !== null) {
144
            return $extended;
145
        }
146
147
        $permission = Blog::config()->grant_user_permission;
148
149
        return Permission::checkMember($member, $permission);
150
    }
151
152
    /**
153
     * Inherits from the parent blog or can be overwritten using a DataExtension.
154
     *
155
     * @param null|Member $member
156
     *
157
     * @return bool
158
     */
159
    public function canDelete($member = null)
160
    {
161
        $extended = $this->extendedCan(__FUNCTION__, $member);
162
163
        if ($extended !== null) {
164
            return $extended;
165
        }
166
167
        return $this->Blog()->canEdit($member);
168
    }
169
170
    /**
171
     * Inherits from the parent blog or can be overwritten using a DataExtension.
172
     *
173
     * @param null|Member $member
174
     *
175
     * @return bool
176
     */
177
    public function canEdit($member = null)
178
    {
179
        $extended = $this->extendedCan(__FUNCTION__, $member);
180
181
        if ($extended !== null) {
182
            return $extended;
183
        }
184
185
        return $this->Blog()->canEdit($member);
186
    }
187
}
188

code/model/BlogTag.php 1 location

@@ 15-187 (lines=173) @@
12
 * @property string $URLSegment
13
 * @property int $BlogID
14
 */
15
class BlogTag extends DataObject implements CategorisationObject
16
{
17
18
	/**
19
	 * Use an exception code so that attempted writes can continue on
20
	 * duplicate errors. 600 is completely arbitrary.
21
	 *
22
	 * @const int
23
	 */
24
	const DUPLICATE_EXCEPTION = 600;
25
26
    /**
27
     * @var array
28
     */
29
    private static $db = array(
30
        'Title' => 'Varchar(255)',
31
    );
32
33
    /**
34
     * @var array
35
     */
36
    private static $has_one = array(
37
        'Blog' => 'Blog',
38
    );
39
40
    /**
41
     * @var array
42
     */
43
    private static $belongs_many_many = array(
44
        'BlogPosts' => 'BlogPost',
45
    );
46
47
    /**
48
     * @var array
49
     */
50
    private static $extensions = array(
51
        'URLSegmentExtension',
52
    );
53
54
    /**
55
     * @return DataList
56
     */
57
    public function BlogPosts()
58
    {
59
        $blogPosts = parent::BlogPosts();
60
61
        $this->extend("updateGetBlogPosts", $blogPosts);
62
63
        return $blogPosts;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getCMSFields()
70
    {
71
        $fields = new FieldList(
72
            TextField::create('Title', _t('BlogTag.Title', 'Title'))
73
        );
74
75
        $this->extend('updateCMSFields', $fields);
76
77
        return $fields;
78
    }
79
80
	/**
81
	 * {@inheritdoc}
82
	 */
83
	protected function validate() {
84
		$validation = parent::validate();
85
		if($validation->valid()) {
86
			// Check for duplicate tags
87
			$blog = $this->Blog();
88
			if($blog && $blog->exists()) {
89
				$existing = $blog->Tags()->filter('Title', $this->Title);
90
				if($this->ID) {
91
					$existing = $existing->exclude('ID', $this->ID);
92
				}
93
				if($existing->count() > 0) {
94
					$validation->error(_t(
95
						'BlogTag.Duplicate', 
96
						'A blog tags already exists with that name'
97
					), BlogTag::DUPLICATE_EXCEPTION);
98
				}
99
			}
100
		}
101
		return $validation;
102
	}
103
104
    /**
105
     * Returns a relative URL for the tag link.
106
     *
107
     * @return string
108
     */
109
    public function getLink()
110
    {
111
        return Controller::join_links($this->Blog()->Link(), 'tag', $this->URLSegment);
112
    }
113
114
    /**
115
     * Inherits from the parent blog or can be overwritten using a DataExtension.
116
     *
117
     * @param null|Member $member
118
     *
119
     * @return bool
120
     */
121
    public function canView($member = null)
122
    {
123
        $extended = $this->extendedCan(__FUNCTION__, $member);
124
125
        if ($extended !== null) {
126
            return $extended;
127
        }
128
129
        return $this->Blog()->canView($member);
130
    }
131
132
    /**
133
     * Inherits from the parent blog or can be overwritten using a DataExtension.
134
     *
135
     * @param null|Member $member
136
     *
137
     * @return bool
138
     */
139
    public function canCreate($member = null)
140
    {
141
        $extended = $this->extendedCan(__FUNCTION__, $member);
142
143
        if ($extended !== null) {
144
            return $extended;
145
        }
146
147
        $permission = Blog::config()->grant_user_permission;
148
149
        return Permission::checkMember($member, $permission);
150
    }
151
152
    /**
153
     * Inherits from the parent blog or can be overwritten using a DataExtension.
154
     *
155
     * @param null|Member $member
156
     *
157
     * @return bool
158
     */
159
    public function canDelete($member = null)
160
    {
161
        $extended = $this->extendedCan(__FUNCTION__, $member);
162
163
        if ($extended !== null) {
164
            return $extended;
165
        }
166
167
        return $this->Blog()->canEdit($member);
168
    }
169
170
    /**
171
     * Inherits from the parent blog or can be overwritten using a DataExtension.
172
     *
173
     * @param null|Member $member
174
     *
175
     * @return bool
176
     */
177
    public function canEdit($member = null)
178
    {
179
        $extended = $this->extendedCan(__FUNCTION__, $member);
180
181
        if ($extended !== null) {
182
            return $extended;
183
        }
184
185
        return $this->Blog()->canEdit($member);
186
    }
187
}
188