-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_bulk_delete.module
252 lines (191 loc) · 7.85 KB
/
user_bulk_delete.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
//$Id$
/**
* @file
* Form that lists user registration details including added fields
* and allow for bulk deletion to cope with spam registrations
*/
/**
* @todo
*
* create bulk delete form
* integrate output string into form
* refactor
* make drupal freindly
*/
/**
* Implementation of hook_menu().
*/
function user_bulk_delete_menu() {
$items['user_bulk_delete'] = array(
'title' => 'Bulk Delete Users',
'page callback' => 'drupal_get_form',
'page arguments' => array('user_bulk_delete_form'),
'access arguments' => array('administer users'),
);
return $items;
}
function start_output(&$output_string){
$output_string = "<ul class='user_bulk_delete_list'>\n";
}
function indent_output(&$output_string, $label){
$output_string .= "<li>" . $label . "\n <ul class='user_bulk_delete_indented_list'>\n";
}
function outdent_output(&$output_string){
$output_string .= "</ul></li>\n";
}
function end_output(&$output_string){
$output_string .= "</ul>\n";
}
function grow_output(&$output_string = "",$label= "",$value=""){
if ($label == "" && $value == ""){
$output_string .= "<li class='blank_line'></li>";
}
else{
$output_string .= "<li>" . $label . " : " . $value . "</li>\n" ;
}
}
function get_field_item_values(&$output_string,$field_values,$user_field){
foreach($field_values as $field_values_index){
if (isset($field_values_index['value'])){
// add the field label and the field value to the output string
grow_output($output_string,$user_field['label'],
$field_values_index['value']);
}
}
}
function get_field_collection_item_values(&$output_string,$field_collection_item_key,$field_collection_item_value,$user_field,$field_collection_fields){
// if the key starts with "field_" it contains values
// unless its "field_name"
if(preg_match('/^field_/', $field_collection_item_key) &&
($field_collection_item_key != "field_name")){
if(array_key_exists('und', $field_collection_item_value)){
foreach($field_collection_item_value['und']
as $field_colection_value){
$output_string_label =
$field_collection_fields[$user_field['field_name']][$field_collection_item_key]['label'];
$output_string_value =
$field_colection_value['value'];
grow_output($output_string,$output_string_label,$output_string_value);
$foo = "barr";
}
}
$foo = "barr";
}
}
function get_field_collection_details(&$output_string,$field_values_index,$user_field,$field_collection_fields){
if (isset($field_values_index['value'])){
// Add the name of this field collection field to output string
indent_output($output_string,$user_field['label']);
//load the field collection for this reference
$loaded_field_collection =
field_collection_item_load($field_values_index['value']);
// for each field in the filed collection
foreach($loaded_field_collection as
$field_collection_item_key => $field_collection_item_value){
get_field_collection_item_values($output_string,$field_collection_item_key,$field_collection_item_value,$user_field,$field_collection_fields);
}
outdent_output($output_string);
$foo = "barr";
}
}
function get_field_details(&$output_string,$user_field,$selected_user,$field_collection_fields,$user_fields,$entity){
// if this is a field collection field
if(array_key_exists($user_field['field_name'],$field_collection_fields)){
// attempt to load the field collection indexes referenced by this field
$field_values = field_get_items('user',$entity,$user_field['field_name']);
// if there are refernece values for this field
if(is_array($field_values)){
// for each reference value for this field
foreach($field_values as $field_values_index){
// if it has a value get its details
get_field_collection_details($output_string,$field_values_index,$user_field,$field_collection_fields);
}
}
}
else{
// if this is not a field collection field.
// get all the values for this field
$field_values = field_get_items('user',$entity,$user_field['field_name']);
// if there are values for this field
if(is_array($field_values)){
// for each value
get_field_item_values($output_string,$field_values,$user_field);
}
}
}
function get_user_details($selected_user){
// Get list of fields for user entity type
$user_fields = field_info_instances('user');
// get list of field clooection fields
$field_collection_fields = field_info_instances('field_collection_item');
// start creating the list of fields for this user
$output_string = "";
start_output($output_string);
grow_output($output_string,'Username', $selected_user->name);
grow_output($output_string,'email', $selected_user->mail);
//load entity information for this user
$entity = entity_load_single('user',$selected_user->uid);
// loop throught the fileds for this user
foreach ($user_fields['user'] as $user_field){
// if field is a field collection field
get_field_details($output_string,$user_field,$selected_user,$field_collection_fields,$user_fields,$entity);
}
return $output_string;
}
/**
* Define the form.
*/
function user_bulk_delete_form($form, &$form_submit){
# the options to display in our checkboxes
$user_options = array(
'delete' => t('Delete'),
'authorize' => t('Authorize'),
'ignore' => t('Ignore')
);
// disable form caching
$form['#cache'] = TRUE;
// Get all the users on the system
$users = entity_load('user');
//loop through the users of the system
foreach($users as $selected_user){
// check to see if user access is blocked
if ($selected_user->status =="0" &&
in_array("authenticated user", $selected_user-> roles)){
$form[$selected_user->name] = array(
'#title' => $selected_user->name,
'#type' => 'radios',
'#description' => get_user_details($selected_user),
'#options' => $user_options,
);
}
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
/**
* Validate the form.
*/
function user_bulk_delete_form_validate($form, &$form_state){
if ($form_state['values']['user_name'] == 'King Kong') {
// We notify the form API that this field has failed validation.
form_set_error('user_name', t('King Kong is not allowed to use this form.'));
}
}
/**
* Handle post validation form submission.
*/
function user_bulk_delete_form_submit($form, &$form_state){
foreach($form_state['values'] as $form_state_name => $form_state_value){
if(is_array($form_state_value)){
//check if key exists
// if ()
$foo = 'barr';
}
drupal_set_message(t('Thanks for filling out the form, %name',
array('%name' => $name)));
}
}