Completed
Push — master ( b78daa...7b55f4 )
by Sathish
09:24 queued 06:44
created

getCustomerGroupID()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.0466

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 3
nop 0
dl 0
loc 10
ccs 6
cts 7
cp 0.8571
crap 4.0466
rs 10
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
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
24
        "ProfileUpdateSuccess" => 'Text',
25
        "ProfileUpdatError" => 'Text',
26
        "EnableSpamProtection" => 'Boolean',
27
        "ExportFields" => 'Text'
28
29
    ];
30
    
31
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
32
        'LoginCallBackUrl' => SiteTree::class,
33
        'LoginUrl' => SiteTree::class,
34
        'LostPasswordUrl' => SiteTree::class,
35
        'CustomerGroup' => Group::class
36
    ];
37
    
38
    public function updateCMSFields(FieldList $fields)
39
    {
40
        $fields->insertBefore('Access', $usertab = Tab::create('UserManagement', 'User Management'));
41
        $fields->addFieldToTab(
42
            'Root.UserManagement',
43
            TreeDropdownField::create(
44
                'CustomerGroupID',
45
                _t(__CLASS__ . '.CustomerGroup', 'Group to add new customers to'),
46
                Group::class
47
            )
48
        );
49
        $fields->addFieldToTab(
50
            'Root.UserManagement',
51
            TreeDropdownField::create(
52
                'LoginUrlID',
53
                _t(__CLASS__ . '.LoginUrl', 'Login Url'),
54
                SiteTree::class
55
            )
56
        );
57
        $fields->addFieldToTab(
58
            'Root.UserManagement',
59
            TreeDropdownField::create(
60
                'LoginCallBackUrlID',
61
                _t(__CLASS__ . '.LoginCallBackUrl', 'Login Call Back Url'),
62
                SiteTree::class
63
            )
64
        );
65
        $fields->addFieldToTab(
66
            'Root.UserManagement',
67
            TreeDropdownField::create(
68
                'LostPasswordUrlID',
69
                _t(__CLASS__ . '.LostPasswordUrl', 'Lost Password Url'),
70
                SiteTree::class
71
            )
72
        );
73
        $fields->addFieldToTab(
74
            'Root.UserManagement',
75
            TextareaField::create(
76
                'ProfileUpdateSuccess',
77
                _t(__CLASS__ . '.ProfileUpdateSuccess', 'Profile update Success Message')
78
            )
79
        );
80
        $fields->addFieldToTab(
81
            'Root.UserManagement',
82
            TextareaField::create(
83
                'ProfileUpdatError',
84
                _t(__CLASS__ . '.ProfileUpdatError', 'Profile update Error Message')
85
            )
86
        );
87
        $fields->addFieldToTab(
88
            'Root.UserManagement',
89
            CheckboxField::create(
90
                'EnableSpamProtection',
91
                _t(__CLASS__ . '.EnableSpamProtection', 'Enable Spam Protection')
92
            )
93
        );
94
        
95
        $fields->addFieldToTab(
96
            'Root.UserManagement',
97
            ListboxField::create(
98
                'ExportFields',
99
                'Select Data fields for the report',
100
                $this->getExportFieldNames(),
101
                json_decode($this->owner->ExportFields)
102
            )
103
        );
104
    }
105
    
106
    /**
107
     * Returns login page id
108
     *
109
     * @return integer
110
     */
111 2
    public function getLoginUrlID()
112
    {
113 2
        if (!$this->owner->LoginUrl()->ID && $this->getDBstatus()) {
114 1
            $LoginUrl = SiteTree::get()
115 1
                ->filter('ClassName', 'UserManagement\Page\UserLoginPage');
116 1
            if($LoginUrl->count() > 0)
117
                return $LoginUrl->first()->ID;
118
            else
119 1
                return;
120
        } else {
121 1
            return $this->owner->LoginUrl()->ID;
122
        }
123
    }
124
    
125
    /**
126
     * Returns call back page id
127
     *
128
     * @return integer
129
     */
130 2
    public function getLoginCallBackUrlID()
131
    {
132 2
        if (!$this->owner->LoginCallBackUrl()->ID && $this->getDBstatus()) {
133 1
            $LoginCallBackUrl = SiteTree::get()
134 1
            ->filter('ClassName', 'UserManagement\Page\UserProfilePage');
135 1
            if($LoginCallBackUrl->count() > 0)
136
                return $LoginCallBackUrl->first()->ID;
137
            else
138 1
                return;
139
        } else {
140 1
            return $this->owner->LoginCallBackUrl()->ID;
141
        }
142
    }
143
144
    /**
145
     * Returns lost password page id
146
     *
147
     * @return integer
148
     */
149 2
    public function getLostPasswordUrlID()
150
    {
151 2
        if (!$this->owner->LostPasswordUrl()->ID && $this->getDBstatus()) {
152 1
            $LostPasswordUrl = SiteTree::get()->filter('ClassName', 'UserManagement\Page\LostPasswordPage');
153 1
            if($LostPasswordUrl->count() > 0)
154
                return $LostPasswordUrl->first()->ID;
155
            else
156 1
                return;
157
        } else {
158 1
            return $this->owner->LostPasswordUrl()->ID;
159
        }
160
    }
161
    
162
    
163
    /**
164
     * Returns customer group id
165
     *
166
     * @return integer
167
     */
168 2
    public function getCustomerGroupID()
169
    {
170 2
        if (!$this->owner->CustomerGroup()->ID && $this->getDBstatus()) {
171 1
            $group = Group::get()->filter('Title', 'General');
172 1
            if($group->count() > 0)
173
                return $group->first()->ID;
174
            else
175 1
                return;
176
        } else {
177 1
            return $this->owner->CustomerGroup()->ID;
178
        }
179
    }
180
181 1
    public function getExportFieldNames()
182
    {
183 1
        $memberFields = Member::create()->getFrontEndFields()->dataFieldNames();
184 1
        $memberFields = array_diff($memberFields, ["FirstName", "Surname", "Email", "TempIDHash", "TempIDExpired", "AutoLoginHash", "AutoLoginExpired","PasswordEncryption","Salt","Locale", "FailedLoginCount", "LockedOutUntil", "Password", "PasswordExpiry"]);
185 1
        return array_combine($memberFields, $memberFields);
186
    }
187
188 5
    public function getDBstatus(){
189 5
        if(\SilverStripe\ORM\DB::is_active() && count(\SilverStripe\ORM\DB::table_list()) > 0){
190 1
            return true;
191
        }
192 4
        return false;
193
    }
194
}
195