Completed
Push — master ( 4ac15e...dcd29c )
by Sathish
47:10 queued 32:15
created

getCustomerGroupID()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5.0729

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 4
nop 0
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
crap 5.0729
rs 9.6111
c 0
b 0
f 0
1
<?php
2
namespace UserManagement\Extension;
3
4
use SilverStripe\CMS\Model\SiteTree;
5
use SilverStripe\Forms\FieldList;
6
use SilverStripe\Forms\Tab;
7
use SilverStripe\Forms\TreeDropdownField;
8
use SilverStripe\ORM\DataExtension;
9
use SilverStripe\Security\Group;
10
use SilverStripe\SiteConfig\SiteConfig;
11
use SilverStripe\Forms\TextareaField;
12
use SilverStripe\Forms\CheckboxField;
13
use SilverStripe\Forms\ListboxField;
14
use SilverStripe\Security\Member;
15
16
/**
17
 * Class UserManagementConfigExtension
18
 *
19
 * @package user-management
20
 */
21
class UserManagementConfigExtension extends DataExtension
22
{
23
    /**
24
     * DB Fields
25
     * @var array
26
     */
27
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
28
        "ProfileUpdateSuccess" => 'Text',
29
        "ProfileUpdatError" => 'Text',
30
        "EnableSpamProtection" => 'Boolean',
31
        "ExportFields" => 'Text'
32
33
    ];
34
35
    /**
36
     * One to many relationship
37
     * @var array
38
     */
39
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
40
        'LoginCallBackUrl' => SiteTree::class,
41
        'LoginUrl' => SiteTree::class,
42
        'LostPasswordUrl' => SiteTree::class,
43
        'CustomerGroup' => Group::class
44
    ];
45
    
46
    /**
47
     * Update the CMS fields
48
     *
49
     * @return \SilverStripe\Forms\FieldList
50
     */
51
    public function updateCMSFields(FieldList $fields)
52
    {
53
        $fields->insertBefore('Access', $usertab = Tab::create('UserManagement', 'User Management'));
54
        $fields->addFieldToTab(
55
            'Root.UserManagement',
56
            TreeDropdownField::create(
57
                'CustomerGroupID',
58
                _t(__CLASS__ . '.CustomerGroup', 'Group to add new customers to'),
59
                Group::class
60
            )
61
        );
62
        $fields->addFieldToTab(
63
            'Root.UserManagement',
64
            TreeDropdownField::create(
65
                'LoginUrlID',
66
                _t(__CLASS__ . '.LoginUrl', 'Login Url'),
67
                SiteTree::class
68
            )
69
        );
70
        $fields->addFieldToTab(
71
            'Root.UserManagement',
72
            TreeDropdownField::create(
73
                'LoginCallBackUrlID',
74
                _t(__CLASS__ . '.LoginCallBackUrl', 'Login Call Back Url'),
75
                SiteTree::class
76
            )
77
        );
78
        $fields->addFieldToTab(
79
            'Root.UserManagement',
80
            TreeDropdownField::create(
81
                'LostPasswordUrlID',
82
                _t(__CLASS__ . '.LostPasswordUrl', 'Lost Password Url'),
83
                SiteTree::class
84
            )
85
        );
86
        $fields->addFieldToTab(
87
            'Root.UserManagement',
88
            TextareaField::create(
89
                'ProfileUpdateSuccess',
90
                _t(__CLASS__ . '.ProfileUpdateSuccess', 'Profile update Success Message')
91
            )
92
        );
93
        $fields->addFieldToTab(
94
            'Root.UserManagement',
95
            TextareaField::create(
96
                'ProfileUpdatError',
97
                _t(__CLASS__ . '.ProfileUpdatError', 'Profile update Error Message')
98
            )
99
        );
100
        $fields->addFieldToTab(
101
            'Root.UserManagement',
102
            CheckboxField::create(
103
                'EnableSpamProtection',
104
                _t(__CLASS__ . '.EnableSpamProtection', 'Enable Spam Protection')
105
            )
106
        );
107
        
108
        $fields->addFieldToTab(
109
            'Root.UserManagement',
110
            ListboxField::create(
111
                'ExportFields',
112
                'Select Data fields for the report',
113
                $this->getExportFieldNames(),
114
                json_decode($this->owner->ExportFields)
115
            )
116
        );
117
    }
118
    
119
    /**
120
     * Returns login page id
121
     *
122
     * @return integer
123
     */
124 2
    public function getLoginUrlID()
125
    {
126 2
        if (!$this->owner->LoginUrl()->ID && $this->getDBstatus()) {
127 1
            $LoginUrl = SiteTree::get()
128 1
                ->filter('ClassName', 'UserManagement\Page\UserLoginPage');
129 1
            if ($LoginUrl->count() > 0) {
130
                return $LoginUrl->first()->ID;
131
            } else {
132 1
                return;
133
            }
134
        } else {
135 1
            return $this->owner->LoginUrl()->ID;
136
        }
137
    }
138
    
139
    /**
140
     * Returns call back page id
141
     *
142
     * @return integer
143
     */
144 2
    public function getLoginCallBackUrlID()
145
    {
146 2
        if (!$this->owner->LoginCallBackUrl()->ID && $this->getDBstatus()) {
147 1
            $LoginCallBackUrl = SiteTree::get()
148 1
            ->filter('ClassName', 'UserManagement\Page\UserProfilePage');
149 1
            if ($LoginCallBackUrl->count() > 0) {
150
                return $LoginCallBackUrl->first()->ID;
151
            } else {
152 1
                return;
153
            }
154
        } else {
155 1
            return $this->owner->LoginCallBackUrl()->ID;
156
        }
157
    }
158
159
    /**
160
     * Returns lost password page id
161
     *
162
     * @return integer | null
163
     */
164 2
    public function getLostPasswordUrlID()
165
    {
166 2
        if (!$this->owner->LostPasswordUrl()->ID && $this->getDBstatus()) {
167 1
            $LostPasswordUrl = SiteTree::get()->filter('ClassName', 'UserManagement\Page\LostPasswordPage');
168 1
            if ($LostPasswordUrl->count() > 0) {
169
                return $LostPasswordUrl->first()->ID;
170
            } else {
171 1
                return;
172
            }
173
        } else {
174 1
            return $this->owner->LostPasswordUrl()->ID;
175
        }
176
    }
177
    
178
    
179
    /**
180
     * Returns customer group id
181
     *
182
     * @return integer | null
183
     */
184 2
    /*public function getCustomerGroupID()
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
185
    {
186 2
        if (!$this->owner->CustomerGroup()->ID && $this->getDBstatus()) {
187 1
            $group = Group::get()->filter('Title', 'general');
188 1
            if ($group->count() > 0) {
189
                return $group->first()->ID;
190
            } else {
191 1
                return;
192
            }
193
        } else if ($this->getDBstatus()) {
194 1
            return $this->owner->CustomerGroup()->ID;
195
        }
196
        return;
197
    }*/
198
    
199
    /**
200
     * Returns the DB fields exist for Member
201
     *
202
     * @return array
203 1
     */
204
    public function getExportFieldNames()
205 1
    {
206 1
        $memberFields = Member::create()->getFrontEndFields()->dataFieldNames();
207
        $memberFields = array_diff($memberFields, ["FirstName", "Surname", "Email",
208
            "TempIDHash", "TempIDExpired", "AutoLoginHash", "AutoLoginExpired",
209
            "PasswordEncryption", "Salt", "Locale", "FailedLoginCount",
210 1
            "LockedOutUntil", "Password", "PasswordExpiry"]);
211
        return array_combine($memberFields, $memberFields);
212
    }
213
214
    /**
215
     * Checks the DB status
216
     *
217
     * @return bool
218 6
     */
219
    public function getDBstatus()
220 6
    {
221 1
        if (\SilverStripe\ORM\DB::is_active() && count(\SilverStripe\ORM\DB::table_list()) > 0) {
222
            return true;
223 5
        }
224
        return false;
225
    }
226
}
227