Completed
Pull Request — master (#196)
by Robbie
13:10
created

CommentAdmin::providePermissions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\Comments\Admin;
4
5
use SilverStripe\Admin\LeftAndMain;
6
use SilverStripe\Comments\Admin\CommentsGridField;
7
use SilverStripe\Comments\Model\Comment;
8
use SilverStripe\Forms\Tab;
9
use SilverStripe\Forms\TabSet;
10
use SilverStripe\Forms\FieldList;
11
use SilverStripe\Forms\Form;
12
use SilverStripe\Security\PermissionProvider;
13
use SilverStripe\Security\Security;
14
15
/**
16
 * Comment administration system within the CMS
17
 *
18
 * @package comments
19
 */
20
class CommentAdmin extends LeftAndMain implements PermissionProvider
21
{
22
23
    private static $url_segment = 'comments';
0 ignored issues
show
Unused Code introduced by
The property $url_segment 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...
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
24
25
    private static $url_rule = '/$Action';
0 ignored issues
show
Unused Code introduced by
The property $url_rule 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...
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
26
27
    private static $menu_title = 'Comments';
0 ignored issues
show
Unused Code introduced by
The property $menu_title 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...
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
28
29
    private static $allowed_actions = array(
0 ignored issues
show
Unused Code introduced by
The property $allowed_actions 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...
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
30
        'approvedmarked',
31
        'deleteall',
32
        'deletemarked',
33
        'hammarked',
34
        'showtable',
35
        'spammarked',
36
        'EditForm',
37
        'unmoderated'
38
    );
39
40
    public function providePermissions()
41
    {
42
        return array(
43
            "CMS_ACCESS_CommentAdmin" => array(
44
                'name' => _t('CommentAdmin.ADMIN_PERMISSION', "Access to 'Comments' section"),
45
                'category' => _t('Permission.CMS_ACCESS_CATEGORY', 'CMS Access')
46
            )
47
        );
48
    }
49
50
    /**
51
     * @return Form
52
     */
53
    public function getEditForm($id = null, $fields = null)
54
    {
55
        if (!$id) {
56
            $id = $this->currentPageID();
57
        }
58
59
        $form = parent::getEditForm($id);
0 ignored issues
show
Unused Code introduced by
$form is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
60
        $record = $this->getRecord($id);
61
62
        if ($record && !$record->canView()) {
63
            return Security::permissionFailure($this);
64
        }
65
66
        $newComments = Comment::get()->filter('Moderated', 0);
67
68
        $newGrid = new CommentsGridField(
69
            'NewComments',
70
            _t('CommentsAdmin.NewComments', 'New'),
71
            $newComments,
72
            CommentsGridFieldConfig::create()
73
        );
74
75
        $approvedComments = Comment::get()->filter('Moderated', 1)->filter('IsSpam', 0);
76
77
        $approvedGrid = new CommentsGridField(
78
            'ApprovedComments',
79
            _t('CommentsAdmin.ApprovedComments', 'Approved'),
80
            $approvedComments,
81
            CommentsGridFieldConfig::create()
82
        );
83
84
        $spamComments = Comment::get()->filter('Moderated', 1)->filter('IsSpam', 1);
85
86
        $spamGrid = new CommentsGridField(
87
            'SpamComments',
88
            _t('CommentsAdmin.SpamComments', 'Spam'),
89
            $spamComments,
90
            CommentsGridFieldConfig::create()
91
        );
92
93
        $newCount = '(' . count($newComments) . ')';
94
        $approvedCount = '(' . count($approvedComments) . ')';
95
        $spamCount = '(' . count($spamComments) . ')';
96
97
        $fields = new FieldList(
98
            $root = new TabSet(
99
                'Root',
100
                new Tab('NewComments', _t('CommentAdmin.NewComments', 'New') . ' ' . $newCount,
101
                    $newGrid
102
                ),
103
                new Tab('ApprovedComments', _t('CommentAdmin.ApprovedComments', 'Approved') . ' ' . $approvedCount,
104
                    $approvedGrid
105
                ),
106
                new Tab('SpamComments', _t('CommentAdmin.SpamComments', 'Spam') . ' ' . $spamCount,
107
                    $spamGrid
108
                )
109
            )
110
        );
111
112
        $root->setTemplate('CMSTabSet');
113
114
        $actions = new FieldList();
115
116
        $form = new Form(
117
            $this,
118
            'EditForm',
119
            $fields,
120
            $actions
121
        );
122
123
        $form->addExtraClass('cms-edit-form');
124
        $form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
125
126
        if ($form->Fields()->hasTabset()) {
127
            $form->Fields()->findOrMakeTab('Root')->setTemplate('CMSTabSet');
128
            $form->addExtraClass('center ss-tabset cms-tabset ' . $this->BaseCSSClasses());
129
        }
130
131
        $this->extend('updateEditForm', $form);
132
133
        return $form;
134
    }
135
}
136