Passed
Push — 1.0 ( 6a0590...8cd863 )
by Morven
01:50
created

ContactLocation::getCMSFields()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 1
nop 0
1
<?php
2
3
namespace SilverCommerce\ContactAdmin\Model;
4
5
use SilverStripe\ORM\DataObject;
6
use SilverStripe\Security\Member;
7
use SilverStripe\Security\Permission;
8
use SilverStripe\Versioned\Versioned;
9
use SilverStripe\Forms\RequiredFields;
10
use SilverStripe\Forms\GridField\GridField;
11
use SilverStripe\Security\PermissionProvider;
12
use SilverStripe\ORM\FieldType\DBHTMLText as HTMLText;
13
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
14
use SilverCommerce\VersionHistoryField\Forms\VersionHistoryField;
15
16
/**
17
 * Details on a particular contact
18
 * 
19
 * @author ilateral
20
 * @package Contacts
21
 */
22
class ContactLocation extends DataObject implements PermissionProvider
23
{
24
    private static $table_name = 'ContactLocation';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
25
26
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
27
        "Address1" => "Varchar(255)",
28
        "Address2" => "Varchar(255)",
29
        "City" => "Varchar(255)",
30
        "County" => "Varchar(255)",
31
        "Country" => "Varchar(255)",
32
        "PostCode" => "Varchar(10)",
33
        "Default" => "Boolean"
34
    ];
35
    
36
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
37
        "Contact" => Contact::class
38
    ];
39
    
40
    private static $casting = [
0 ignored issues
show
introduced by
The private property $casting is not used, and could be removed.
Loading history...
41
        "Title" => "Varchar",
42
        "Address" => "Text"
43
    ];
44
45
    private static $frontend_fields = [
0 ignored issues
show
introduced by
The private property $frontend_fields is not used, and could be removed.
Loading history...
46
        "Address1",
47
        "Address2",
48
        "City",
49
        "County",
50
        "Country",
51
        "PostCode",
52
        "Default"
53
    ];
54
55
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
56
        "Address1",
57
        "Address2",
58
        "City",
59
        "County",
60
        "Country",
61
        "PostCode",
62
        "Default"
63
    ];
64
65
    /**
66
     * Add extension classes
67
     *
68
     * @var array
69
     * @config
70
     */
71
    private static $extensions = [
0 ignored issues
show
introduced by
The private property $extensions is not used, and could be removed.
Loading history...
72
        Versioned::class . '.versioned',
73
    ];
74
75
    /**
76
     * Declare version history
77
     *
78
     * @var array
79
     * @config
80
     */
81
    private static $versioning = [
0 ignored issues
show
introduced by
The private property $versioning is not used, and could be removed.
Loading history...
82
        "History"
83
    ];
84
85
    public function getTitle()
86
    {
87
        $title = $this->Address1 . " (" . $this->PostCode . ")";
0 ignored issues
show
Bug Best Practice introduced by
The property Address1 does not exist on SilverCommerce\ContactAdmin\Model\ContactLocation. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property PostCode does not exist on SilverCommerce\ContactAdmin\Model\ContactLocation. Since you implemented __get, consider adding a @property annotation.
Loading history...
88
89
        $this->extend("updateTitle", $title);
90
91
        return $title;
92
    }
93
94
    public function getAddress() 
95
    {
96
        $return = [];
97
        $return[] = $this->Address1;
0 ignored issues
show
Bug Best Practice introduced by
The property Address1 does not exist on SilverCommerce\ContactAdmin\Model\ContactLocation. Since you implemented __get, consider adding a @property annotation.
Loading history...
98
        
99
		if (!empty($this->Address2)) {
0 ignored issues
show
Bug Best Practice introduced by
The property Address2 does not exist on SilverCommerce\ContactAdmin\Model\ContactLocation. Since you implemented __get, consider adding a @property annotation.
Loading history...
100
            $return[] = $this->Address2;
101
        }
102
        
103
        $return[] = $this->City;
0 ignored issues
show
Bug Best Practice introduced by
The property City does not exist on SilverCommerce\ContactAdmin\Model\ContactLocation. Since you implemented __get, consider adding a @property annotation.
Loading history...
104
105
		if (!empty($this->County)) {
0 ignored issues
show
Bug Best Practice introduced by
The property County does not exist on SilverCommerce\ContactAdmin\Model\ContactLocation. Since you implemented __get, consider adding a @property annotation.
Loading history...
106
            $return[] = $this->County;
107
        }
108
109
        $return[] = $this->Country;
0 ignored issues
show
Bug Best Practice introduced by
The property Country does not exist on SilverCommerce\ContactAdmin\Model\ContactLocation. Since you implemented __get, consider adding a @property annotation.
Loading history...
110
        $return[] = $this->PostCode;
0 ignored issues
show
Bug Best Practice introduced by
The property PostCode does not exist on SilverCommerce\ContactAdmin\Model\ContactLocation. Since you implemented __get, consider adding a @property annotation.
Loading history...
111
112
        $this->extend("updateAddress", $return);
113
        
114
		return implode(",\n", $return);
115
    }
116
    
117
    public function getCMSFields()
118
    {
119
        $self = $this;
120
        $this->beforeUpdateCMSFields(function ($fields) use ($self) {
121
            if ($self->exists()) {
122
                $fields->addFieldToTab(
123
                    "Root.History",
124
                    VersionHistoryField::create(
125
                        "History",
126
                        _t("SilverCommerce\VersionHistoryField.History", "History"),
127
                        $this
128
                    )->addExtraClass("stacked")
129
                );
130
            }
131
        });
132
133
        return parent::getCMSFields();
134
    }
135
136
    public function getCMSValidator()
137
    {
138
        return new RequiredFields(array(
139
            "Address1",
140
            "City",
141
            "Country",
142
            "PostCode"
143
        ));
144
    }
145
    
146
    public function providePermissions()
147
    {
148
        return [
149
            "CONTACTS_MANAGE" => [
150
                'name' => _t(
151
                    'Contacts.PERMISSION_MANAGE_CONTACTS_DESCRIPTION',
152
                    'Manage contacts'
153
                ),
154
                'help' => _t(
155
                    'Contacts.PERMISSION_MANAGE_CONTACTS_HELP',
156
                    'Allow creation and editing of contacts'
157
                ),
158
                'category' => _t('Contacts.Contacts', 'Contacts')
159
            ],
160
            "CONTACTS_DELETE" => [
161
                'name' => _t(
162
                    'Contacts.PERMISSION_DELETE_CONTACTS_DESCRIPTION',
163
                    'Delete contacts'
164
                ),
165
                'help' => _t(
166
                    'Contacts.PERMISSION_DELETE_CONTACTS_HELP',
167
                    'Allow deleting of contacts'
168
                ),
169
                'category' => _t('Contacts.Contacts', 'Contacts')
170
            ]
171
        ];
172
    }
173
    
174
    public function canView($member = null)
175
    {
176
        $extended = $this->extendedCan(__FUNCTION__, $member);
177
178
        if ($extended !== null) {
179
            return $extended;
180
        }
181
182
        return $this->Contact()->canView($member);
0 ignored issues
show
Bug introduced by
The method Contact() does not exist on SilverCommerce\ContactAdmin\Model\ContactLocation. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

182
        return $this->/** @scrutinizer ignore-call */ Contact()->canView($member);
Loading history...
183
    }
184
185
    public function canCreate($member = null, $context = [])
186
    {
187
        $extended = $this->extendedCan(__FUNCTION__, $member, $context);
188
189
        if ($extended !== null) {
190
            return $extended;
191
        }
192
193
        if (!$member) {
194
            $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

194
            $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...
195
        }
196
197
        if ($member && Permission::checkMember($member->ID, "CONTACTS_MANAGE")) {
198
            return true;
199
        }
200
201
        return false;
202
    }
203
204
    public function canEdit($member = null)
205
    {
206
        $extended = $this->extendedCan(__FUNCTION__, $member);
207
208
        if ($extended !== null) {
209
            return $extended;
210
        }
211
212
        return $this->Contact()->canEdit($member);
213
    }
214
215
    public function canDelete($member = null, $context = [])
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

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

215
    public function canDelete($member = null, /** @scrutinizer ignore-unused */ $context = [])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
216
    {
217
        $extended = $this->extendedCan(__FUNCTION__, $member);
218
219
        if ($extended !== null) {
220
            return $extended;
221
        }
222
223
        return $this->Contact()->canDelete($member);
224
    }
225
226
    /**
227
     * If we have assigned this as a default location, loop through
228
     * other locations and disable default.
229
     *
230
     * @return void
231
     */
232
    public function onAfterWrite()
233
    {
234
        parent::onAfterWrite();
235
236
        if ($this->Default) {
237
            foreach ($this->Contact()->Locations() as $location) {
238
                if ($location->ID != $this->ID && $location->Default) {
239
                    $location->Default = false;
240
                    $location->write();
241
                }
242
            }
243
        }
244
    }
245
}
246