Completed
Push — master ( 187618...8c43e0 )
by Will
12s
created

src/Admin/CommentAdmin.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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';
24
25
    private static $url_rule = '/$Action';
26
27
    private static $menu_title = 'Comments';
28
29
    private static $allowed_actions = array(
0 ignored issues
show
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);
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