ContactTag::canView()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 8
c 2
b 0
f 0
dl 0
loc 17
rs 9.6111
cc 5
nc 5
nop 1
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 tag for keyword descriptions of a contact.
15
 *
16
 * @property string Title
17
 *
18
 * @method \SilverStripe\ORM\ManyManyList Contacts
19
 *
20
 * @package    silverstripe
21
 * @subpackage contacts
22
 */
23
class ContactTag extends DataObject implements PermissionProvider
24
{
25
    private static $table_name = 'ContactTag';
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 = 'Tag';
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 = 'Tags';
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 $belongs_many_many = [
0 ignored issues
show
introduced by
The private property $belongs_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
    public function getCMSFields()
45
    {
46
        $fields = parent::getCMSFields();
47
48
        // Move contacts field to main tab
49
        $contacts_field = $fields->dataFieldByName("Contacts");
50
        $fields->removeByName("Contacts");
51
52
        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...
53
            $manager = new BulkManager();
54
            $manager->removeBulkAction(DeleteHandler::class);
55
            $manager->removeBulkAction(EditHandler::class);
56
            
57
            $config = $contacts_field->getConfig();
58
            $config->addComponent($manager);
59
60
            $fields->addFieldToTab(
61
                'Root.Main',
62
                $contacts_field
63
            );
64
        }
65
66
        $this->extend('updateCMSFields', $fields);
67
68
        return $fields;
69
    }
70
    
71
    public function providePermissions()
72
    {
73
        return [
74
            "CONTACTS_TAGS_MANAGE" => [
75
                'name' => _t(
76
                    'Contacts.PERMISSION_MANAGE_CONTACTS_TAGS_DESCRIPTION',
77
                    'Manage contact tags'
78
                ),
79
                'help' => _t(
80
                    'Contacts.PERMISSION_MANAGE_CONTACTS_TAGS_HELP',
81
                    'Allow creation and editing of contact lists'
82
                ),
83
                'category' => _t('Contacts.Contacts', 'Contacts')
84
            ],
85
            "CONTACTS_TAGS_DELETE" => [
86
                'name' => _t(
87
                    'Contacts.PERMISSION_DELETE_CONTACTS_TAGS_DESCRIPTION',
88
                    'Delete contact lists'
89
                ),
90
                'help' => _t(
91
                    'Contacts.PERMISSION_DELETE_CONTACTS_TAGS_HELP',
92
                    'Allow deleting of contact lists'
93
                ),
94
                'category' => _t('Contacts.Contacts', 'Contacts')
95
            ]
96
        ];
97
    }
98
99
    public function canView($member = null)
100
    {
101
        $extended = $this->extendedCan(__FUNCTION__, $member);
102
103
        if ($extended !== null) {
104
            return $extended;
105
        }
106
        
107
        if (!$member) {
108
            $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

108
            $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...
109
        }
110
            
111
        if ($member && Permission::checkMember($member->ID, "CONTACTS_TAGS_MANAGE")) {
112
            return true;
113
        }
114
115
        return false;
116
    }
117
118
    public function canCreate($member = null, $context = [])
119
    {
120
        $extended = $this->extendedCan(__FUNCTION__, $member, $context);
121
122
        if ($extended !== null) {
123
            return $extended;
124
        }
125
        
126
        if (!$member) {
127
            $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

127
            $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...
128
        }
129
            
130
        if ($member && Permission::checkMember($member->ID, "CONTACTS_TAGS_MANAGE")) {
131
            return true;
132
        }
133
134
        return false;
135
    }
136
137
    public function canEdit($member = null)
138
    {
139
        $extended = $this->extendedCan(__FUNCTION__, $member);
140
141
        if ($extended !== null) {
142
            return $extended;
143
        }
144
        
145
        if (!$member) {
146
            $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

146
            $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...
147
        }
148
            
149
        if ($member && Permission::checkMember($member->ID, "CONTACTS_TAGS_MANAGE")) {
150
            return true;
151
        }
152
153
        return false;
154
    }
155
156
    public function canDelete($member = null)
157
    {
158
        $extended = $this->extendedCan(__FUNCTION__, $member);
159
160
        if ($extended !== null) {
161
            return $extended;
162
        }
163
        
164
        if (!$member) {
165
            $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

165
            $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...
166
        }
167
            
168
        if ($member && Permission::checkMember($member->ID, "CONTACTS_TAGS_DELETE")) {
169
            return true;
170
        }
171
172
        return false;
173
    }
174
}
175