public/themes/templates/1/includes/js/user_management.js   F
last analyzed

Complexity

Total Complexity 63
Complexity/F 1.5

Size

Lines of Code 270
Function Count 42

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 174
dl 0
loc 270
rs 3.36
c 0
b 0
f 0
wmc 63
mnd 21
bc 21
fnc 42
bpm 0.5
cpm 1.5
noi 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
F user_management.js ➔ UserlistFunction 0 68 51
A user_management.js ➔ UserSearch 0 17 3
B user_management.js ➔ sendEmail 0 39 6
A user_management.js ➔ UserModify 0 25 3

How to fix   Complexity   

Complexity

Complex classes like public/themes/templates/1/includes/js/user_management.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
$(document).ready(function () {
2
    function UserSearch () {
3
        var formValues = $('#user_search').serialize();
4
5
        $.ajaxQueue({
6
            // The URL for the request
7
            url: '../user/ajax_user_management.php',
8
            data: formValues,
9
            type: 'GET',
10
            dataType: 'html',
11
12
            // Code to run if the request succeeds;
13
            // the response is passed to the function
14
            success: function (html) {
15
                $('#column_center_cpanel').html(html);
16
            }
17
        });
18
    }
19
20
    $('#jsUserBrowse').change(function () {
21
        UserSearch();
22
    });
23
    $('#js_user_search').keyup(function () {
24
        var value = $(this).val();
25
        if (value.length >= 3 || value === '') {
26
            UserSearch();
27
        }
28
    });
29
    $('#no_comments').change(function () {
30
        if ($(this).is(':checked')) {
31
            $('#with_comments').prop('checked', false);
32
        }
33
        UserSearch();
34
    });
35
    $('#with_comments').change(function () {
36
        if ($(this).is(':checked')) {
37
            $('#no_comments').prop('checked', false);
38
        }
39
        UserSearch();
40
    });
41
    $('#no_submissions').change(function () {
42
        if ($(this).is(':checked')) {
43
            $('#with_submissions').prop('checked', false);
44
        }
45
        UserSearch();
46
    });
47
    $('#with_submissions').change(function () {
48
        if ($(this).is(':checked')) {
49
            $('#no_submissions').prop('checked', false);
50
        }
51
        UserSearch();
52
    });
53
    $('#no_email').change(function () {
54
        if ($(this).is(':checked')) {
55
            $('#with_email').prop('checked', false);
56
        }
57
        UserSearch();
58
    });
59
    $('#with_email').change(function () {
60
        if ($(this).is(':checked')) {
61
            $('#no_email').prop('checked', false);
62
        }
63
        UserSearch();
64
    });
65
    $('#no_news').change(function () {
66
        if ($(this).is(':checked')) {
67
            $('#with_news').prop('checked', false);
68
        }
69
        UserSearch();
70
    });
71
    $('#with_news').change(function () {
72
        if ($(this).is(':checked')) {
73
            $('#no_news').prop('checked', false);
74
        }
75
        UserSearch();
76
    });
77
    $('#no_links').change(function () {
78
        if ($(this).is(':checked')) {
79
            $('#with_links').prop('checked', false);
80
        }
81
        UserSearch();
82
    });
83
    $('#with_links').change(function () {
84
        if ($(this).is(':checked')) {
85
            $('#no_links').prop('checked', false);
86
        }
87
        UserSearch();
88
    });
89
    $('#no_interviews').change(function () {
90
        if ($(this).is(':checked')) {
91
            $('#with_interviews').prop('checked', false);
92
        }
93
        UserSearch();
94
    });
95
    $('#with_interviews').change(function () {
96
        if ($(this).is(':checked')) {
97
            $('#no_interviews').prop('checked', false);
98
        }
99
        UserSearch();
100
    });
101
    $('#no_review').change(function () {
102
        if ($(this).is(':checked')) {
103
            $('#with_review').prop('checked', false);
104
        }
105
        UserSearch();
106
    });
107
    $('#with_review').change(function () {
108
        if ($(this).is(':checked')) {
109
            $('#no_review').prop('checked', false);
110
        }
111
        UserSearch();
112
    });
113
    $('#not_admin').change(function () {
114
        if ($(this).is(':checked')) {
115
            $('#is_admin').prop('checked', false);
116
        }
117
        UserSearch();
118
    });
119
    $('#is_admin').change(function () {
120
        if ($(this).is(':checked')) {
121
            $('#not_admin').prop('checked', false);
122
        }
123
        UserSearch();
124
    });
125
    $('#last_visit').change(function () {
126
        UserSearch();
127
    });
128
});
129
130
$(document).ajaxComplete(function () {
131
    UserlistFunction();
132
});
133
134
$(document).ready(function () {
135
    UserlistFunction();
136
});
137
138
function UserlistFunction () {
139
    // select all checkboxes in list
140
    $('#user_select_all').click(function () {
141
        $('.user_checkbox').prop('checked', $(this).prop('checked'));
142
    });
143
144
    // Are you sure question Delete
145
    $('#user_list_action').change(function () {
146
        if ($(this).val() === 'delete_user') {
147
            $('#JSGenericModal').dialog({
148
                title: 'Delete Users?',
149
                open: $('#JSGenericModalText').text('These users will be permanently deleted. Are you sure?'),
150
                resizable: false,
151
                height: 200,
152
                modal: true,
153
                buttons: {
154
                    'Delete': function () {
155
                        $(this).dialog('close');
156
                        UserModify();
157
                    },
158
                    Cancel: function () {
159
                        $(this).dialog('close');
160
                    }
161
                }
162
            });
163
        }
164
        if ($(this).val() === 'deactivate_user') {
165
            $('#JSGenericModal').dialog({
166
                title: 'Deactivate Users?',
167
                open: $('#JSGenericModalText').text('These users will be deactivated. Are you sure?'),
168
                resizable: false,
169
                height: 200,
170
                modal: true,
171
                buttons: {
172
                    'Deactivate': function () {
173
                        $(this).dialog('close');
174
                        UserModify();
175
                    },
176
                    Cancel: function () {
177
                        $(this).dialog('close');
178
                    }
179
                }
180
            });
181
        }
182
        if ($(this).val() === 'activate_user') {
183
            $('#JSGenericModal').dialog({
184
                title: 'Activate Users?',
185
                open: $('#JSGenericModalText').text('These users will be activated. Are you sure?'),
186
                resizable: false,
187
                height: 200,
188
                modal: true,
189
                buttons: {
190
                    'Activate': function () {
191
                        $(this).dialog('close');
192
                        UserModify();
193
                    },
194
                    Cancel: function () {
195
                        $(this).dialog('close');
196
                    }
197
                }
198
            });
199
        }
200
        if ($(this).val() === 'email_user') {
201
            $('#email_fields').css('display', 'block');
202
            sendEmail();
203
        }
204
    });
205
}
206
207
function UserModify () {
208
    var formValues = $('#user_list').serialize();
209
    $.ajaxQueue({
210
        url: 'db_user_management.php',
211
        data: formValues,
212
        type: 'POST',
213
        dataType: 'text',
214
        // Code to run if the request succeeds;
215
        success: function (text) {
216
            window.OSDMessageDisplay(text);
217
            // Reload userlist
218
            var formValues = $('#user_search').serialize();
219
            $.ajaxQueue({
220
                url: 'ajax_user_management.php',
221
                data: formValues,
222
                type: 'GET',
223
                dataType: 'html',
224
                // Code to run if the request succeeds;
225
                success: function (html) {
226
                    $('#ajax_usersearch').html(html);
227
                }
228
            });
229
        }
230
    });
231
}
232
function sendEmail () {
233
    $('#jsEmailBtn').click(function () {
234
        EmailSent();
235
    });
236
237
    function EmailSent () {
238
        $('#JSGenericModal').dialog({
239
            title: 'Email users?',
240
            open: $('#JSGenericModalText').text('Email users?'),
241
            resizable: false,
242
            height: 200,
243
            modal: true,
244
            buttons: {
245
                'Email user(s)': function () {
246
                    $(this).dialog('close');
247
                    var formValues = $('#user_list').serialize() + '&action=email_user';
248
                    $.ajaxQueue({
249
                        // The URL for the request
250
                        url: 'db_user_management.php',
251
                        data: formValues,
252
                        type: 'POST',
253
                        dataType: 'html',
254
255
                        // Code to run if the request succeeds;
256
                        // the response is passed to the function
257
                        success: function (html) {
258
                            window.OSDMessageDisplay(html);
259
                            $('#email_fields').css('display', 'none');
260
                        }
261
                    });
262
                },
263
                Cancel: function () {
264
                    $(this).dialog('close');
265
                    $('#email_fields').css('display', 'none');
266
                }
267
            }
268
        });
269
    }
270
}
271