/[drupal]/contributions/modules/user_types/user_types.module
ViewVC logotype

Contents of /contributions/modules/user_types/user_types.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Tue Sep 23 16:29:36 2008 UTC (14 months ago) by swentel
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
Changes since 1.1: +217 -16 lines
File MIME type: text/x-php
#196938 by domidc: documentation and help. #218132 by thePanz: uninstall hook. Adding README and CHANGELOG file
1 <?php
2 // $Id: user_types.module,v 1.1.2.15 2008/03/24 18:37:45 drumm Exp $
3
4 /**
5 * @file
6 * Different user types with customizable profile fields.
7 *
8 * @author Kristof De Jaeger - http://drupal.org/user/107403 - http://realize.be
9 * Originally by Neil Drumm.
10 * @version this is the drupal 5.x version
11 */
12
13 /**
14 * Implementation of hook_menu().
15 */
16 function user_types_menu($may_cache) {
17 $items = array();
18
19 if ($may_cache) {
20 $items[] = array(
21 'path' => 'admin/user/user_types',
22 'title' => t('User types'),
23 'description' => t('List, edit, and add user types.'),
24 'access' => user_access('administer users'),
25 'callback' => 'user_types_admin_page',
26 );
27 $items[] = array(
28 'path' => 'admin/user/user_types/list',
29 'title' => t('List'),
30 'type' => MENU_DEFAULT_LOCAL_TASK,
31 );
32 $items[] = array(
33 'path' => 'admin/user/user_types/settings',
34 'title' => t('Settings'),
35 'callback' => 'drupal_get_form',
36 'callback arguments' => array('user_types_settings'),
37 'type' => MENU_LOCAL_TASK,
38 'weight' => 2,
39 );
40 $items[] = array(
41 'path' => 'admin/user/user_types/add',
42 'title' => t('Add user type'),
43 'callback' => 'drupal_get_form',
44 'callback arguments' => array('user_types_add_form'),
45 'type' => MENU_LOCAL_TASK,
46 'weight' => 1,
47 );
48
49 foreach (user_types_list() as $user_type_id => $name) {
50 $items[] = array(
51 'path' => 'admin/user/user_types/'. check_plain($name),
52 'title' => $name,
53 'callback' => 'user_types_type_page',
54 'callback arguments' => array($user_type_id),
55 );
56 $items[] = array(
57 'path' => 'admin/user/user_types/'. check_plain($name) .'/list',
58 'title' => t('List users'),
59 'type' => MENU_DEFAULT_LOCAL_TASK,
60 );
61 $items[] = array(
62 'path' => 'admin/user/user_types/'. check_plain($name) .'/list/quick',
63 'title' => t('Quick‐view'),
64 'type' => MENU_DEFAULT_LOCAL_TASK,
65 );
66 $items[] = array(
67 'path' => 'admin/user/user_types/'. check_plain($name) .'/list/detail',
68 'title' => t('Detail‐view'),
69 'type' => MENU_LOCAL_TASK,
70 'callback' => 'user_types_type_page',
71 'callback arguments' => array($user_type_id, TRUE),
72 'weight' => 1,
73 );
74 $items[] = array(
75 'path' => 'admin/user/user_types/'. check_plain($name) .'/list/'. check_plain($name) .'.csv',
76 'title' => t('CSV'),
77 'type' => MENU_CALLBACK,
78 'callback' => 'user_types_type_page',
79 'callback arguments' => array($user_type_id, TRUE, TRUE),
80 );
81 $items[] = array(
82 'path' => 'admin/user/user_types/'. check_plain($name) .'/edit',
83 'title' => t('Edit'),
84 'callback' => 'drupal_get_form',
85 'callback arguments' => array('user_types_edit_form', $user_type_id),
86 'type' => MENU_LOCAL_TASK,
87 'weight' => 1,
88 );
89 }
90
91 $items[] = array(
92 'path' => 'admin/user/user_types/delete',
93 'title' => t('Delete user type'),
94 'callback' => 'drupal_get_form',
95 'callback arguments' => array('user_types_delete_form'),
96 'type' => MENU_CALLBACK,
97 );
98 foreach (array('user/register', 'admin/user/user/create') as $base_path) {
99 $items[] = array(
100 'path' => $base_path .'/untyped',
101 'title' => variable_get('user_types_untyped_name', t('Normal user')),
102 'type' => MENU_DEFAULT_LOCAL_TASK,
103 'weight' => '-1',
104 );
105 foreach (user_types_list(TRUE) as $user_type_id => $name) {
106 $items[] = array(
107 'path' => $base_path .'/'. check_plain($name),
108 'title' => $name,
109 'type' => MENU_LOCAL_TASK,
110 );
111 }
112 }
113 }
114 else {
115 if (arg(0) == 'user' && is_numeric(arg(1)) && arg(2) == 'edit') {
116 $items[] = array(
117 'path' => 'user/'. arg(1) .'/edit/user_types',
118 'title' => t('User type'),
119 'callback' => 'drupal_get_form',
120 'callback arguments' => array('user_types_user_edit', arg(1)),
121 'access' => user_access('switch user types'),
122 'type' => MENU_LOCAL_TASK,
123 'weight' => 100,
124 );
125 }
126 }
127
128 return $items;
129 }
130
131 /**
132 * Implementation of hook_perm().
133 */
134 function user_types_perm() {
135 return array('switch user types');
136 }
137
138 /**
139 * Implementation of hook_help().
140 */
141 function user_types_help($section = NULL) {
142 if (preg_match('!^user/[0-9]*/edit/user_types$!', $section)) {
143 return '<p>'. t('Changing the user type will <strong>lose data</strong> in fields which are removed.') .'</p>';
144 }
145 if ($section == 'admin/help#user_types') {
146 // we make it easy by reading in the README.txt
147 $path = drupal_get_path('module', 'user_types');
148 $readme = file_get_contents($path .'/README.txt');
149 return '<p>'. nl2br($readme) .'</p>';
150 }
151 }
152
153 function user_types_list($only_enabled = FALSE) {
154 if ($only_enabled) {
155 $result = db_query('SELECT user_type_id, name FROM {user_types_type} WHERE enable_registration ORDER BY name');
156 }
157 else {
158 $result = db_query('SELECT user_type_id, name FROM {user_types_type} ORDER BY name');
159 }
160 $user_types = array();
161 while ($row = db_fetch_object($result)) {
162 $user_types[$row->user_type_id] = $row->name;
163 }
164 return $user_types;
165 }
166
167 /**
168 * Get the current user type ID for create user pages.
169 */
170 function user_types_add_page_id() {
171 static $user_type_id;
172
173 if (!isset($user_type_id)) {
174 $menu = menu_get_menu();
175 $user_type_id = db_result(db_query("SELECT user_type_id FROM {user_types_type} WHERE name = '%s'", $menu['items'][menu_get_active_item()]['title']));
176 if (empty($user_type_id)) {
177 $user_type_id = -1;
178 }
179 }
180
181 return $user_type_id;
182 }
183
184 /**
185 * Implementation of hook_form_alter().
186 */
187 function user_types_form_alter($form_id, &$form) {
188 switch ($form_id) {
189 case 'profile_field_form':
190 // Get list of user types
191 $user_types = array(
192 -1 => check_plain(variable_get('user_types_untyped_name', t('Normal user'))),
193 );
194 $user_types = $user_types + array_map('check_plain', user_types_list());
195
196 // Get default value
197 if (isset($form['fid'])) {
198 $result = db_query('SELECT user_type_id, enabled FROM {user_types_profile_fields} WHERE fid = %d', $form['fid']['#value']);
199 $default_value = array();
200 while ($row = db_fetch_object($result)) {
201 $default_value[$row->user_type_id] = $row->enabled * $row->user_type_id;
202 }
203 }
204 else {
205 $default_value = drupal_map_assoc(array(-1));
206 }
207
208 $form['fields']['user_types'] = array(
209 '#type' => 'checkboxes',
210 '#title' => t('User types'),
211 '#options' => $user_types,
212 '#default_value' => $default_value,
213 );
214 $form['fields']['user_types_quick_view'] = array(
215 '#type' => 'checkbox',
216 '#title' => t('Show in quick‐view'),
217 '#default_value' => db_result(db_query('SELECT enabled FROM {user_types_quick_view} WHERE fid = %d', $form['fid']['#value'])),
218 );
219
220 $form['#submit']['user_types_profile_field_form_submit'] = array();
221 break;
222
223 case 'user_edit':
224 $user_type_id = db_result(db_query('SELECT user_type_id FROM {user_types_user} WHERE uid = %d', $form['_account']['#value']->uid));
225 if (empty($user_type_id)) {
226 $user_type_id = -1;
227 }
228 case 'user_register':
229 if (!isset($user_type_id)) {
230 $user_type_id = user_types_add_page_id();
231 }
232
233 // Remove disabled fields for this type.
234 $result = db_query('SELECT pf.name, pf.category, utpf.enabled FROM {user_types_profile_fields} utpf INNER JOIN {profile_fields} pf ON pf.fid = utpf.fid WHERE utpf.user_type_id = %d', $user_type_id);
235 $categories = array();
236 while ($row = db_fetch_object($result)) {
237 if (!$row->enabled) {
238 unset($form[$row->category][$row->name]);
239 $categories[] = $row->category;
240 }
241 }
242
243 // Remove empty fieldsets.
244 foreach (array_unique($categories) as $category) {
245 if (count(element_children($form[$category])) == 0) {
246 if ($form_id == 'user_edit' && isset($form[$category])) {
247 // We can not remove a category added by profile module, so the
248 // best we can do is confirm the page is supposed to be empty.
249 drupal_set_message(t('There are no fields in this category.'));
250 }
251 unset($form[$category]);
252 }
253 }
254 break;
255 }
256 }
257
258 /**
259 * Implementation of hook_user().
260 */
261 function user_types_user($op, &$edit, &$account, $category = NULL) {
262 switch ($op) {
263 case 'insert':
264 db_query('INSERT INTO {user_types_user} (uid, user_type_id) VALUES (%d, %d)', $account->uid, user_types_add_page_id());
265 break;
266 }
267 }
268
269 function user_types_profile_field_form_submit($form_id, $form_values) {
270 if (isset($form_values['fid'])) {
271 $fid = $form_values['fid'];
272 }
273 else {
274 $fid = db_result(db_query("SELECT fid FROM {profile_fields} WHERE name = '%s'", $form_values['name']));
275 }
276
277 db_query('DELETE FROM {user_types_profile_fields} WHERE fid = %d', $fid);
278 foreach ($form_values['user_types'] as $user_type_id => $enabled) {
279 db_query('INSERT INTO {user_types_profile_fields} (user_type_id, fid, enabled) VALUES (%d, %d, %d)', $user_type_id, $fid, (bool) $enabled);
280 }
281
282 db_query('DELETE FROM {user_types_quick_view} WHERE fid = %d', $fid);
283 db_query('INSERT INTO {user_types_quick_view} (fid, enabled) VALUES (%d, %d)', $fid, $form_values['user_types_quick_view']);
284 }
285
286 function user_types_admin_page() {
287 $output = '';
288
289 $user_types = user_types_list();
290 if (count($user_types) > 0) {
291 $output .= '<ul>';
292 foreach ($user_types as $user_type_id => $name) {
293 $output .= '<li>'. l($name, 'admin/user/user_types/'. check_plain($name)) .'</li>';
294 }
295 $output .= '</ul>';
296 }
297 else {
298 $output .= '<p><em>'. t('There are no user types. You can <a href="!url">add a user type</a>.', array('!url' => url('admin/user/user_types/add'))) .'</em></p>';
299 }
300
301 return $output;
302 }
303
304 function user_types_settings() {
305 $form = array();
306
307 $form['user_types_untyped_name'] = array(
308 '#type' => 'textfield',
309 '#title' => t('Name for untyped users'),
310 '#default_value' => variable_get('user_types_untyped_name', t('Normal user')),
311 '#required' => TRUE,
312 );
313 $form['submit'] = array(
314 '#type' => 'submit',
315 '#value' => t('Save settings'),
316 );
317
318 return $form;
319 }
320
321 function user_types_settings_submit($form_id, $form_values) {
322 variable_set('user_types_untyped_name', $form_values['user_types_untyped_name']);
323 cache_clear_all('*', 'cache_menu', TRUE);
324 drupal_set_message('Saved user types settings.');
325 return 'admin/user/user_types';
326 }
327
328 function user_types_add_form() {
329 $form = array();
330
331 $form['name'] = array(
332 '#type' => 'textfield',
333 '#title' => t('Name'),
334 '#required' => TRUE,
335 );
336 $form['enable_registration'] = array(
337 '#type' => 'checkbox',
338 '#title' => t('Enable registration'),
339 );
340 $form['submit'] = array(
341 '#type' => 'submit',
342 '#value' => t('Add user type'),
343 );
344
345 return $form;
346 }
347
348 function user_types_add_form_submit($form_id, $form_values) {
349 db_query("INSERT INTO {user_types_type} (user_type_id, name, enable_registration) VALUES (%d, '%s', %d)", db_next_id('{user_types_type}_user_type_id'), $form_values['name'], $form_values['enable_registration']);
350 cache_clear_all('*', 'cache_menu', TRUE);
351 drupal_set_message(t('Added %name user type.', array('%name' => $form_values['name'])));
352 return 'admin/user/user_types/'. check_plain($form_values['name']);
353 }
354
355 function user_types_edit_form($user_type_id) {
356 $user_type = db_fetch_object(db_query('SELECT user_type_id, name, enable_registration FROM {user_types_type} WHERE user_type_id = %d', $user_type_id));
357
358 $form = array();
359
360 $form['user_type_id'] = array(
361 '#type' => 'value',
362 '#value' => $user_type->user_type_id,
363 );
364 $form['name'] = array(
365 '#type' => 'textfield',
366 '#title' => t('Name'),
367 '#default_value' => $user_type->name,
368 '#required' => TRUE,
369 );
370 $form['enable_registration'] = array(
371 '#type' => 'checkbox',
372 '#title' => t('Enable registration'),
373 '#default_value' => $user_type->enable_registration,
374 );
375 $form[] = array('#value' => '<div class="container-inline">');
376 $form['submit'] = array(
377 '#type' => 'submit',
378 '#value' => t('Save user type'),
379 );
380 $form['delete'] = array(
381 '#value' => l(t('Delete'), 'admin/user/user_types/delete/'. $user_type->user_type_id),
382 );
383 $form[] = array('#value' => '</div>');
384
385 return $form;
386 }
387
388 function user_types_edit_form_submit($form_id, $form_values) {
389 db_query("UPDATE {user_types_type} SET name = '%s', enable_registration = %d WHERE user_type_id = %d", $form_values['name'], $form_values['enable_registration'], $form_values['user_type_id']);
390 cache_clear_all('*', 'cache_menu', TRUE);
391 drupal_set_message(t('Saved %name user type.', array('%name' => $form_values['name'])));
392 return 'admin/user/user_types/'. check_plain($form_values['name']);
393 }
394
395 function user_types_delete_form($user_type_id) {
396 $user_type = db_fetch_object(db_query('SELECT user_type_id, name FROM {user_types_type} WHERE user_type_id = %d', $user_type_id));
397 if (empty($user_type)) {
398 drupal_set_message(t('Invalid user type.'), 'error');
399 return array();
400 }
401
402 $form = array();
403
404 $form['user_type_id'] = array(
405 '#type' => 'value',
406 '#value' => $user_type->user_type_id,
407 );
408 $form['name'] = array(
409 '#type' => 'value',
410 '#value' => $user_type->name,
411 );
412
413 return confirm_form($form, t('Are you sure you want to delete the %name user type?', array('%name' => $user_type->name)), 'admin/user/user_types/edit/'. $user_type->user_type_id);
414 }
415
416 function user_types_delete_form_submit($form_id, $form_values) {
417 db_query("DELETE FROM {user_types_type} WHERE user_type_id = %d", $form_values['user_type_id']);
418 cache_clear_all('*', 'cache_menu', TRUE);
419 drupal_set_message(t('Deleted %name user type.', array('%name' => $form_values['name'])));
420 return 'admin/user/user_types';
421 }
422
423 function user_types_type_page($user_type_id, $detail_view = FALSE, $csv = FALSE) {
424 $output = '';
425
426 $joins = array();
427 if (!$detail_view) {
428 $joins[] = 'INNER JOIN {user_types_quick_view} utqv ON utpf.fid = utqv.fid AND utqv.enabled';
429 }
430 $orders = array(
431 'pf.category',
432 'pf.weight',
433 'pf.fid',
434 );
435 foreach (module_implements('user_types_table_alter') as $module) {
436 // Use pass by reference in implementations. Useful for reordering columns.
437 $function = $module .'_user_types_table_alter';
438 $function($joins, $orders);
439 }
440 $result = db_query('SELECT pf.name, pf.title, pf.fid, pf.category FROM {user_types_profile_fields} utpf INNER JOIN {profile_fields} pf ON pf.fid = utpf.fid '. implode(' ', $joins) .' WHERE utpf.enabled AND utpf.user_type_id = %d ORDER BY '. implode(', ', $orders), $user_type_id);
441 $header = array(
442 array(
443 'data' => t('User'),
444 'field' => 'u.name',
445 'sort' => 'asc',
446 ),
447 );
448 $fields = array();
449 while ($row = db_fetch_object($result)) {
450 $header[] = array(
451 'data' => $row->title,
452 'field' => 'pv'. $row->fid .'.value',
453 'category' => $row->category,
454 );
455 $fields[] = $row->name;
456 }
457
458 $tablesort_order = tablesort_get_order($header);
459 $join = '';
460 if (preg_match('/^pv([0-9]*)/', $tablesort_order['sql'], $matches)) {
461 $join = 'INNER JOIN {profile_values} pv'. $matches[1] .' ON pv'. $matches[1] .'.uid = utu.uid AND pv'. $matches[1] .'.fid = '. $matches[1];
462 }
463 if ($csv) {
464 $result = db_query('SELECT utu.uid FROM {user_types_user} utu INNER JOIN {users} u ON u.uid = utu.uid '. $join .' WHERE utu.user_type_id = %d'. tablesort_sql($header), $user_type_id);
465 }
466 else {
467 $result = pager_query('SELECT utu.uid FROM {user_types_user} utu INNER JOIN {users} u ON u.uid = utu.uid '. $join .' WHERE utu.user_type_id = %d'. tablesort_sql($header), 20, 0, NULL, $user_type_id);
468 }
469
470 $rows = array();
471 while ($row = db_fetch_object($result)) {
472 $account = user_load(array('uid' => $row->uid));
473 $r = array(
474 theme('username', $account),
475 );
476 foreach ($fields as $field) {
477 $r[] = $account->$field;
478 }
479 $rows[] = $r;
480 }
481
482 if ($csv) {
483 drupal_set_header('Content-type: text/csv');
484
485 // Category header
486 $categories = array_map(create_function('$item', 'return $item[\'category\'];'), $header);
487 $categories[0] = t('Category');
488 $output .= user_types_to_csv($categories);
489
490 // Field header
491 $output .= user_types_to_csv(array_map(create_function('$item', 'return $item[\'data\'];'), $header));
492
493 // Row data
494 foreach ($rows as $row) {
495 $output .= user_types_to_csv($row);
496 }
497 print $output;
498 }
499 else {
500 if (count($rows) > 0) {
501 $output .= theme('table', $header, $rows);
502 }
503 else {
504 $output .= '<p><em>'. t('There are no users of this type. You can <a href="!url">add a user</a>.', array('!url' => url('admin/user/user/create/'. drupal_get_title()))) .'</em></p>';
505 }
506 $output .= theme('pager', 20, 0);
507 $output .= l(t('Download CSV'), 'admin/user/user_types/'. arg(3) .'/list/'. arg(3) .'.csv');
508
509 return $output;
510 }
511 }
512
513 function user_types_to_csv($array) {
514 $output = '"'. user_types_quote_csv(array_shift($array)) .'"';
515 foreach ($array as $item) {
516 $output .= ',"'. user_types_quote_csv($item) .'"';
517 }
518 $output .= "\n";
519
520 return $output;
521 }
522
523 function user_types_quote_csv($string) {
524 $string = str_replace('"', '""', strip_tags($string));
525 $string = str_replace("\r\n", "\n", $string);
526 $string = str_replace("\r", "\n", $string);
527
528 return $string;
529 }
530
531 function user_types_user_edit($uid) {
532 $account = user_load(array('uid' => $uid));
533 drupal_set_title($account->name);
534 $user_type_id = db_result(db_query('SELECT user_type_id FROM {user_types_user} WHERE uid = %d', $uid));
535
536 $user_types = array(
537 -1 => check_plain(variable_get('user_types_untyped_name', t('Normal user'))),
538 );
539 $user_types = $user_types + array_map('check_plain', user_types_list());
540
541 $form = array();
542
543 $form['uid'] = array(
544 '#type' => 'value',
545 '#value' => $uid,
546 );
547 $form['user_type'] = array(
548 '#type' => 'select',
549 '#title' => t('User type'),
550 '#options' => $user_types,
551 '#default_value' => $user_type_id,
552 );
553 $form['submit'] = array(
554 '#type' => 'submit',
555 '#value' => t('Change user type'),
556 );
557
558 return $form;
559 }
560
561 function user_types_user_edit_submit($form_id, $form_values) {
562 $user_types = array(
563 -1 => check_plain(variable_get('user_types_untyped_name', t('Normal user'))),
564 );
565 $user_types = $user_types + array_map('check_plain', user_types_list());
566
567 db_query('UPDATE {user_types_user} SET user_type_id = %d WHERE uid = %d', $form_values['user_type'], $form_values['uid']);
568 drupal_set_message(t('Changed user type to %type.', array('%type' => $user_types[$form_values['user_type']])));
569 return 'user/'. $form_values['uid'];
570 }

  ViewVC Help
Powered by ViewVC 1.1.2