ContactList::canCreate()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 17
rs 9.6111
c 0
b 0
f 0
cc 5
nc 5
nop 2
1
<?php
2
3
namespace SilverCommerce\ContactAdmin\Model;
4
5
use SilverStripe\ORM\DataObject;
6
use SilverStripe\Security\Member;
7
use Colymba\BulkManager\BulkManager;
8
use SilverStripe\Security\Permission;
9
use SilverStripe\Security\PermissionProvider;
10
use Colymba\BulkManager\BulkAction\EditHandler;
11
use Colymba\BulkManager\BulkAction\DeleteHandler;
12
13
/**
14
 * A container for grouping contacts
15
 *
16
 * @property string Title
17
 *
18
 * @method \SilverStripe\ORM\ManyManyList Contacts
19
 *
20
 * @author  ilateral
21
 * @package Contacts
22
 */
23
class ContactList extends DataObject implements PermissionProvider
24
{
25
    private static $table_name = 'ContactList';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
26
27
    private static $singular_name = 'List';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
28
29
    private static $plural_name = 'Lists';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
30
31
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
32
        'Title' => "Varchar(255)",
33
    ];
34
35
    private static $many_many = [
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
36
        'Contacts' => Contact::class,
37
    ];
38
39
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
40
        'Title',
41
        'Contacts.Count'
42
    ];
43
44
    private static $searchable_fields = [
0 ignored issues
show
introduced by
The private property $searchable_fields is not used, and could be removed.
Loading history...
45
        'Title'
46
    ];
47
48
    public function fieldLabels($includelrelations = true)
49
    {
50
        $labels = parent::fieldLabels($includelrelations);
51
        $labels["Title"] = _t('Contacts.FieldTitle', "Title");
52
        $labels["FullTitle"] = _t('Contacts.FieldTitle', "Title");
53
        $labels["ActiveRecipients.Count"] = _t('Contacts.Recipients', "Recipients");
54
        return $labels;
55
    }
56
57
    public function getCMSFields()
58
    {
59
        $fields = parent::getCMSFields();
60
61
        // Move contacts field to main tab
62
        $contacts_field = $fields->dataFieldByName("Contacts");
63
        $fields->removeByName("Contacts");
64
65
        if ($contacts_field) {
0 ignored issues
show
introduced by
$contacts_field is of type SilverStripe\Forms\FormField, thus it always evaluated to true.
Loading history...
66
            $manager = new BulkManager();
67
            $manager->removeBulkAction(DeleteHandler::class);
68
            $manager->removeBulkAction(EditHandler::class);
69
            
70
            $config = $contacts_field->getConfig();
71
            $config->addComponent($manager);
72
73
            $fields->addFieldToTab(
74
                'Root.Main',
75
                $contacts_field
76
            );
77
        }
78
        
79
        $this->extend("updateCMSFields", $fields);
80
81
        return $fields;
82
    }
83
    
84
    public function providePermissions()
85
    {
86
        return [
87
            "CONTACTS_LISTS_MANAGE" => [
88
                'name' => _t(
89
                    'Contacts.PERMISSION_MANAGE_CONTACTS_LISTS_DESCRIPTION',
90
                    'Manage contact lists'
91
                ),
92
                'help' => _t(
93
                    'Contacts.PERMISSION_MANAGE_CONTACTS_LISTS_HELP',
94
                    'Allow creation and editing of contact lists'
95
                ),
96
                'category' => _t('Contacts.Contacts', 'Contacts')
97
            ],
98
            "CONTACTS_LISTS_DELETE" => [
99
                'name' => _t(
100
                    'Contacts.PERMISSION_DELETE_CONTACTS_LISTS_DESCRIPTION',
101
                    'Delete contact lists'
102
                ),
103
                'help' => _t(
104
                    'Contacts.PERMISSION_DELETE_CONTACTS_LISTS_HELP',
105
                    'Allow deleting of contact lists'
106
                ),
107
                'category' => _t('Contacts.Contacts', 'Contacts')
108
            ]
109
        ];
110
    }
111
    
112
    public function canView($member = null)
113
    {
114
        $extended = $this->extendedCan(__FUNCTION__, $member);
115
116
        if ($extended !== null) {
117
            return $extended;
118
        }
119
120
        if (!$member) {
121
            $member = Member::currentUser();
0 ignored issues
show
Deprecated Code introduced by
The function SilverStripe\Security\Member::currentUser() has been deprecated: 5.0.0 use Security::getCurrentUser() ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

121
            $member = /** @scrutinizer ignore-deprecated */ Member::currentUser();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
122
        }
123
124
        if ($member && Permission::checkMember($member->ID, "CONTACTS_LISTS_MANAGE")) {
125
            return true;
126
        }
127
128
        return false;
129
    }
130
131
    public function canCreate($member = null, $context = [])
132
    {
133
        $extended = $this->extendedCan(__FUNCTION__, $member, $context);
134
135
        if ($extended !== null) {
136
            return $extended;
137
        }
138
139
        if (!$member) {
140
            $member = Member::currentUser();
0 ignored issues
show
Deprecated Code introduced by
The function SilverStripe\Security\Member::currentUser() has been deprecated: 5.0.0 use Security::getCurrentUser() ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

140
            $member = /** @scrutinizer ignore-deprecated */ Member::currentUser();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
141
        }
142
143
        if ($member && Permission::checkMember($member->ID, "CONTACTS_LISTS_MANAGE")) {
144
            return true;
145
        }
146
147
        return false;
148
    }
149
150
    public function canEdit($member = null)
151
    {
152
        $extended = $this->extendedCan(__FUNCTION__, $member);
153
154
        if ($extended !== null) {
155
            return $extended;
156
        }
157
158
        if (!$member) {
159
            $member = Member::currentUser();
0 ignored issues
show
Deprecated Code introduced by
The function SilverStripe\Security\Member::currentUser() has been deprecated: 5.0.0 use Security::getCurrentUser() ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

159
            $member = /** @scrutinizer ignore-deprecated */ Member::currentUser();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
160
        }
161
   
162
        if ($member && Permission::checkMember($member->ID, "CONTACTS_LISTS_MANAGE")) {
163
            return true;
164
        }
165
166
        return false;
167
    }
168
169
    public function canDelete($member = null)
170
    {
171
        $extended = $this->extendedCan(__FUNCTION__, $member);
172
173
        if ($extended !== null) {
174
            return $extended;
175
        }
176
177
        if (!$member) {
178
            $member = Member::currentUser();
0 ignored issues
show
Deprecated Code introduced by
The function SilverStripe\Security\Member::currentUser() has been deprecated: 5.0.0 use Security::getCurrentUser() ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

178
            $member = /** @scrutinizer ignore-deprecated */ Member::currentUser();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
179
        }
180
   
181
        if ($member && Permission::checkMember($member->ID, "CONTACTS_LISTS_DELETE")) {
182
            return true;
183
        }
184
185
        return false;
186
    }
187
}
188