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

Contents of /contributions/modules/rolecontact/rolecontact.module

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


Revision 1.1 - (show annotations) (download) (as text)
Mon Nov 12 10:23:38 2007 UTC (2 years ago) by dlane
Branch: MAIN
CVS Tags: DRUPAL-5--1-0, HEAD
Branch point for: DRUPAL-5
File MIME type: text/x-php
Initial commit of rolecontact module.  With rolecontact, members of an admin-specified role are listed on a 'staff' page, with links to a themed subset of their profile data, ordered based on per-user weights.  Each user also has a themed profile page, offering a (potentially different) subset of their profile details along with a link to the user's contact form: a contact form category is created for each user allowing any site visitor to send them email without revealing the user's email address.
1 <?php
2 // $Id: rolecontact.module,v 0.1 2007/05/13 22:59:00 dave Exp $
3
4 define(DEFAULT_WEIGHT,0);
5 define(DEFAULT_CONFIRMATION,"Thank you for your message. If appropriate, I will respond as soon as possible.");
6
7 /**
8 * Implementation of hook_menu().
9 */
10 function rolecontact_menu($may_cache) {
11 $items = array();
12
13 if (!$may_cache) {
14 $items[] = array(
15 'path' => 'rolecontact_list',
16 'title' => t(check_plain(variable_get('rolecontact_profile_list_title','Staff list'))),
17 'callback' => 'profile_list',
18 'access' => 1,
19 'type' => MENU_SUGGESTED_ITEM
20 );
21 $items[] = array(
22 'path' => 'admin/settings/rolecontact',
23 'title' => t('Role Contact'),
24 'description' => t('Allows admin to select a role for which users are given an automatic contact form category'),
25 'callback' => 'drupal_get_form',
26 'callback arguments' => array('rolecontact_settings')
27 );
28 $items[] = array(
29 'path' => 'admin/settings/rolecontact/role',
30 'title' => t('Role Contact'),
31 'type' => MENU_DEFAULT_LOCAL_TASK,
32 );
33 $items[] = array(
34 'path' => 'admin/settings/rolecontact/weights',
35 'title' => t('User weights'),
36 'description' => t('Allow admins to set users role contact list weights to determine display order'),
37 'callback' => 'drupal_get_form',
38 'callback arguments' => array('rolecontact_users'),
39 'type' => MENU_LOCAL_TASK,
40 'weight' => 1,
41 );
42 $items[] = array(
43 'path' => 'admin/settings/rolecontact/fields',
44 'title' => t('Display fields'),
45 'description' => t('Fields to display on the staff page'),
46 'callback' => 'drupal_get_form',
47 'callback arguments' => array('rolecontact_fields'),
48 'type' => MENU_LOCAL_TASK,
49 'weight' => 1,
50 );
51 $items[] = array(
52 'path' => 'admin/settings/rolecontact/data',
53 'title' => t('Extra data'),
54 'description' => t('Extra data to display'),
55 'callback' => 'drupal_get_form',
56 'callback arguments' => array('rolecontact_data'),
57 'type' => MENU_LOCAL_TASK,
58 'weight' => 1,
59 );
60 }
61 return $items;
62 }
63
64 /**
65 * Implementation of hook_perm().
66 */
67 function rolecontact_perm() {
68 return array('administer role contact module', 'access role contact forms', 'change own weight');
69 }
70
71
72 /**
73 * Administrative settings for RoleContact Module
74 */
75 function rolecontact_settings() {
76 if (user_access('administer role contact module')) {
77 $roles = user_roles(1); // it only makes sense to offer this for roles with users, so skip anonymous...
78 $form['rolecontact_role_id'] = array(
79 '#type' => 'radios',
80 '#title' => t('Roles'),
81 '#default_value' => variable_get('rolecontact_role_id',NULL),
82 '#options' => $roles,
83 '#description' => t('Select a role - each user in that role will automatically have a contact form category created for him/her.')
84 );
85 $form['rolecontact_default_confirmation'] = array(
86 '#type' => 'textarea',
87 '#title' => t('Default confirmation text'),
88 '#default_value' => variable_get('rolecontact_default_confirmation',DEFAULT_CONFIRMATION),
89 '#rows' => 2,
90 '#description' => t("This message is displayed to the user after successfully submitting a contact form for a user."),
91 );
92 $form['rolecontact_title'] = array(
93 '#type' => 'textfield',
94 '#title' => t('Page title'),
95 '#required' => TRUE,
96 '#default_value' => variable_get('rolecontact_profile_list_title', 'Staff List'),
97 );
98 $form['rolecontact_path'] = array(
99 '#type' => 'textfield',
100 '#title' => t('Path alias'),
101 '#default_value' => db_result(db_query("SELECT dst FROM {url_alias} WHERE src='rolecontact_list'")),
102 '#description' => t('The location the list will be avaliable from.'),
103 );
104 return system_settings_form($form);
105 } else {
106 drupal_access_denied();
107 }
108 }
109
110 /**
111 * This is called in conjunction with the system settings form...
112 */
113 function rolecontact_settings_submit($form_id, $form_values) {
114 debug(__FUNCTION__." form_id = $form_id, form_values = ".print_r($form_values,true));
115 if ($form_id == 'rolecontact_settings') {
116 $old_role = variable_get('rolecontact_role_id',NULL);
117 $current_role = $form_values['rolecontact_role_id'];
118
119 variable_set('rolecontact_role_id',$current_role);
120 variable_set('rolecontact_default_confirmation',$form_values['rolecontact_default_confirmation']);
121
122 // Set the page title
123 variable_set('rolecontact_profile_list_title', $form_values['rolecontact_title']);
124
125 // Set the path alias
126 path_set_alias('rolecontact_list');
127 if (isset($form_values['rolecontact_path']) && $form_values['rolecontact_path'] != '') {
128 path_set_alias('rolecontact_list', $form_values['rolecontact_path']);
129 }
130
131 if ($current_role != $old_role) {
132 debug(__FUNCTION__." role changed from $old_role to $current_role");
133 drupal_set_message(t('All users in the selected role now have contact categories'));
134 if ($old_role != NULL) {
135 _delete_all_user_contact_categories($old_role);
136 drupal_set_message(t('All users in the previous role have been removed'));
137 }
138 _add_all_user_contact_categories($current_role);
139 }
140 }
141 }
142
143 /**
144 * Changes the weights of each user in role contact
145 */
146 function rolecontact_users() {
147 $role_id = variable_get('rolecontact_role_id',NULL); // staff role
148 if ($role_id == NULL) {
149 drupal_set_message(t('No role has been selected'), 'error');
150
151 return;
152 }
153
154 /* Get a list of all users to display in the role contact list */
155 $sql = "SELECT u.*, IF (rc.weight IS NULL, 0, rc.weight) weight FROM {users} u INNER JOIN {users_roles} ur ON ur.uid = u.uid LEFT JOIN {rolecontact} rc ON rc.uid = u.uid WHERE ur.rid = " . $role_id . " ORDER BY weight ASC, u.name ASC";
156 $results = db_query($sql);
157 for ($i = -10; $i <= 10; $i++) {
158 $options[$i] = $i;
159 }
160 $form['#tree'] = TRUE;
161 while($user = db_fetch_object($results)) {
162 $profile = profile_view_profile($user);
163 $name = _rolecontact_get_name($profile, $user);
164
165 $form['user'][$user->uid] = array('#type' => 'markup', '#value' => theme('username', $user));
166 $form['name'][$user->uid] = array('#value' => $name);
167 $form['weight'][$user->uid] = array(
168 '#type' => 'select',
169 '#options' => $options,
170 '#default_value' => $user->weight,
171 );
172 }
173
174 $form['submit'] = array(
175 '#type' => 'submit',
176 '#value' => t('Update'),
177 );
178
179 return $form;
180 }
181
182 function rolecontact_users_submit($form_id, $form) {
183 $sql = "SELECT * FROM {users} WHERE uid=%d";
184 foreach($form['weight'] as $key => $value) {
185 $user = db_fetch_object(db_query($sql, $key));
186 $cid = _get_user_contact_category($user->mail);
187 if ($cid == FALSE) {
188 _add_user_contact_category($user);
189 $cid = _get_user_contact_category($user->mail);
190 }
191 _rolecontact_user_update($key, $cid, $value);
192 }
193 drupal_set_message(t('The users weights have been updated'));
194 }
195
196 function theme_rolecontact_users($form) {
197 $header = array(
198 array('data' => t('Username')),
199 array('data' => t('Name')),
200 array('data' => t('Weight')),
201 );
202
203 $rows = array();
204 if (isset($form['user']) && is_array($form['user'])) {
205 foreach (element_children($form['user']) as $key) {
206 $row = array();
207 $row['data'][] = drupal_render($form['user'][$key]);
208 $row['data'][] = drupal_render($form['name'][$key]);
209 $row['data'][] = drupal_render($form['weight'][$key]);
210 $rows[] = $row;
211 }
212 }
213 $output = theme('table', $header, $rows);
214 $output .= drupal_render($form);
215
216 return $output;
217 }
218
219 /**
220 * Get a user's real name from the profile or use their username if that failed
221 */
222 function rolecontact_fields() {
223 $header = array(
224 array('data' => t('Show field')),
225 array('data' => t('Field title')),
226 );
227 $form['head'] = array('#type' => 'value', '#value' => $header);
228
229 $role_id = variable_get('rolecontact_role_id',NULL); // staff role
230
231 /* Get a list of all fields avaliable */
232 $sql = "SELECT pf.fid, pf.title, pf.name FROM {profile_fields} pf ORDER BY pf.title";
233 $results = db_query($sql);
234
235 $form['#tree'] = TRUE;
236 $form['global']['head'] = array('#value' => t('Fields to display when viewing all members profiles'));
237 $form['user']['head'] = array('#value' => t('Fields to display when viewing one members profile'));
238
239 while($field = db_fetch_object($results)) {
240 $form['global']['shown'][$field->fid] = array('#type' => 'checkbox', '#default_value' => variable_get($field->name . '_show', 0));
241 $form['global']['title'][$field->fid] = array('#value' => $field->title);
242 $form['global']['name'][$field->fid] = array('#type' => 'value', '#value' => $field->name);
243
244 $form['user']['shown'][$field->fid] = array('#type' => 'checkbox', '#default_value' => variable_get($field->name . '_user_show', 0));
245 $form['user']['title'][$field->fid] = array('#value' => $field->title);
246 $form['user']['name'][$field->fid] = array('#type' => 'value', '#value' => $field->name);
247 }
248
249 $form['submit'] = array(
250 '#type' => 'submit',
251 '#value' => t('Update'),
252 );
253
254 return $form;
255 }
256
257 function rolecontact_fields_submit($form_id, $form_values) {
258 foreach($form_values['global']['shown'] as $key => $value) {
259 $name = $form_values['global']['name'][$key];
260 variable_set($name . '_show', $value);
261 }
262
263 foreach($form_values['user']['shown'] as $key => $value) {
264 $name = $form_values['user']['name'][$key];
265 variable_set($name . '_user_show', $value);
266 }
267
268 drupal_set_message(t('The displayed fields have been updated'));
269 }
270
271 function theme_rolecontact_fields($form) {
272 $header = $form['head']['#value'];
273 $output = '';
274
275 $output .= '<div class="rolecontact_field_select">' . theme('rolecontact_field_admin', $header, $form['global']) . '</div>';
276 $output .= '<div class="rolecontact_field_select">' . theme('rolecontact_field_admin', $header, $form['user']) . '</div>';
277
278 unset($form['global']);
279 unset($form['user']);
280
281 $output .= drupal_render($form);
282 return $output;
283 }
284
285 /**
286 * Theme the table where the profiles can be edited
287 */
288 function theme_rolecontact_field_admin($header, $fields) {
289 $output = drupal_render($fields['head']);
290 if (isset($fields['title']) && is_array($fields['title'])) {
291 $rows = array();
292 foreach (element_children($fields['title']) as $key) {
293 $row = array();
294 $row['data'][] = drupal_render($fields['shown'][$key]);
295 $row['data'][] = drupal_render($fields['title'][$key]);
296 $rows[] = $row;
297 }
298 $output .= theme('table', $header, $rows);
299 } else {
300 $output .= t('No fields defined');
301 }
302
303 return $output;
304 }
305
306 /**
307 * Allow the administrator to define a header and footer for the rolecontact_list page
308 */
309 function rolecontact_data() {
310 $form['#tree'] = TRUE;
311 $form['header'] = array(
312 '#type' => 'textarea',
313 '#title' => t('Header'),
314 '#default_value' => variable_get('rolecontact_header', ''),
315 );
316
317 $form['footer'] = array(
318 '#type' => 'textarea',
319 '#title' => t('Footer'),
320 '#default_value' => variable_get('rolecontact_footer', ''),
321 );
322 $form['format'] = filter_form(variable_get('rolecontact_filter', FILTER_FORMAT_DEFAULT));
323
324 $form['submit'] = array('#type' => 'submit', '#value' => t('Update'));
325
326 return $form;
327 }
328
329 function rolecontact_data_submit($form_id, $form_values) {
330 variable_set('rolecontact_filter', $form_values['format']);
331 variable_set('rolecontact_header', check_markup($form_values['header'], $form_values['format']));
332 variable_set('rolecontact_footer', check_markup($form_values['footer'], $form_values['format']));
333 }
334
335 /**
336 * Implementation of hook_help
337 */
338 function rolecontact_help($section = '') {
339 switch ($section) {
340
341 case 'admin/modules#description':
342 return t('A module to add all users in a designated Role to the contact form list. Requires Contact module.');
343 case 'admin/settings/rolecontact/weights':
344 return t('Lighter weights float to the top, heavier weights sink to the bottom. Users with the same weight are sorted alphabetically by username.');
345 break;
346 }
347 }
348
349 /**
350 * Implement hook_form_alter().
351 */
352 function rolecontact_form_alter($form_id, &$form) {
353 global $user;
354
355 switch ($form_id) {
356 case 'contact_mail_page':
357 $form['contact_information']['#weight'] = -2;
358 $form['cid']['#weight'] = -1;
359 $form['cid']['#title'] = 'Send message to';
360
361 $arg1 = arg(1);
362 if (!empty($arg1)) {
363 if (isset($form['cid']['#options'])) {
364 foreach ($form['cid']['#options'] as $key => $option) {
365 //debug(__FUNCTION__ .': key='. $key .', option='. $option);
366 if ($arg1 == strtolower($key)) {
367 $form['cid']['#default_value'] = $key;
368 }
369 }
370 }
371 }
372
373 if (is_array($form['cid']['#options'])) {
374 $rolecontact_options = array();
375 $contact_options = array();
376 foreach($form['cid']['#options'] as $key => $item) {
377 $result = false;
378 $result = db_num_rows(db_query("SELECT uid from {rolecontact} WHERE cid=%d", $key));
379 debug(__FUNCTION__." result = $result for key = $key");
380 if ($result) {
381 $rolecontact_options[$key] = "Staff - ".$form['cid']['#options'][$key];
382 } else {
383 $contact_options[$key] = $form['cid']['#options'][$key];
384 }
385 }
386 /*$options = $contact_options;
387 $options['---'] = $rolecontact_options;*/
388 //$options = array_merge($contact_options,$rolecontact_options);
389 //$options = $contact_options + array('---') + $rolecontact_options;
390 $options = $rolecontact_options + array('---') + $contact_options;
391 $form['cid']['#options'] = $options;
392 }
393 break;
394
395 case 'user_edit':
396 /* We allow users with 'change own weight' to change their own weight or administrators to change anybody's weight */
397 if ((user_access('change own weight') && $user->uid == arg(1))|| user_access('administer role contact module')) {
398 /* Add a form to let the user change their weight */
399 $rc_data = db_fetch_array(db_query("SELECT weight FROM {rolecontact} WHERE uid=%d", arg(1)));
400 for ($i = -10; $i <= 10; $i++) {
401 $options[$i] = $i;
402 }
403 $form['contact']['rolecontact_weight'] = array(
404 '#type' => 'select',
405 '#title' => t('Rolecontact list weight'),
406 '#options' => $options,
407 '#default_value' => (isset($rc_data['weight']) ? $rc_data['weight'] : 0),
408 );
409 }
410 break;
411 }
412 }
413
414 /**
415 * Updates the user's weight and Contact ID in the rolecontact table
416 */
417 function _rolecontact_user_update($uid, $cid, $weight) {
418 if (db_fetch_array(db_query("SELECT * FROM {rolecontact} WHERE uid=%d", $uid))) {
419 db_query("UPDATE {rolecontact} SET weight=%d, cid=%d WHERE uid=%d", $weight, $cid, $uid);
420 } else {
421 db_query("INSERT INTO {rolecontact} (uid, cid, weight) VALUES (%d, %d, %d)", $uid, $cid, $weight);
422 }
423 }
424
425 /**
426 * Updates the user's Contact ID in the rolecontact table if it previously had a different Contact ID
427 */
428 function _rolecontact_user_cid_update($uid, $cid) {
429 if (db_fetch_array(db_query("SELECT * FROM {rolecontact} WHERE uid=%d", $uid))) {
430 debug(__FUNCTION__." updating cid for uid = $uid to $cid");
431 db_query("UPDATE {rolecontact} SET cid=%d WHERE uid=%d", $cid, $uid);
432 }
433 }
434
435
436 /**
437 * Implementation of hook_user().
438 *
439 * Updates the contact category when a user is changed or deleted.
440 */
441 function rolecontact_user($type, &$edit, &$user, $category = NULL) {
442 $rolecontact_rid = variable_get('rolecontact_role_id',NULL);
443 switch ($type) {
444 case 'view':
445 debug(__FUNCTION__.' view - edit object: '.print_r($edit,true));
446 if ($cid = _get_user_contact_category($user->mail)) {
447 $items['contact_link'] = array('title' => t('Contact Link'),
448 'value' => _create_user_rolecontact_link($cid),
449 'class' => 'contact-link',
450 );
451 debug(__FUNCTION__.' items: '.print_r($items,true));
452 return array(t('Contact Link') => $items);
453 }
454 break;
455 case 'update':
456 debug(__FUNCTION__.' update - edit object: '.print_r($edit,true));
457 debug(__FUNCTION__.' update - user object: '.print_r($user,true));
458
459 $roles = (isset($edit['roles']) ? $edit['roles'] : $user->roles);
460 $cid = _get_user_contact_category($user->mail);
461 if (isset($roles[$rolecontact_rid])) {
462 debug(__FUNCTION__.' in role $rolecontact_rid');
463 if ($cid == false) {
464 _add_user_contact_category($user);
465 }
466 } else {
467 if ($cid != false) {
468 _delete_user_contact_category($cid);
469 }
470 }
471
472 // Get the users cid
473 $cid = _get_user_contact_category($user->mail);
474 if ($cid) {
475 if (db_fetch_array(db_query("SELECT * FROM {rolecontact} WHERE uid=%d", $user->uid))) {
476 db_query("UPDATE {rolecontact} SET weight=%d, cid=%d WHERE uid=%d", $edit['rolecontact_weight'], $cid, $user->uid);
477 } else {
478 db_query("INSERT INTO {rolecontact} (uid, cid, weight) VALUES (%d, %d, %d)", $user->uid, $cid, $edit['rolecontact_weight']);
479 }
480 }
481
482 break;
483 case 'delete':
484 if ($cid = _get_user_contact_category($user->mail)) {
485 return _delete_user_contact_category($cid);
486 }
487 break;
488 default:
489 break;
490 }
491 }
492
493 function _create_user_rolecontact_link($cid) {
494 return '<a class="contact-link" href="/contact/'.$cid.'">contact form</a>';
495 }
496
497 /**
498 * Internal method - determines whether the user is already in the contact category table
499 * Params $mail - user email address string
500 * Returns the user's contact category id, if present, or false if not.
501 */
502 function _get_user_contact_category($mail) {
503 $result = db_query("SELECT cid FROM {contact} WHERE recipients = '%s'", $mail);
504 while ($category = db_fetch_object($result)) {
505 return $category->cid;
506 }
507 return false;
508 }
509
510 /**
511 * Internal method - adds a category for the user to the contact category table
512 * Params $user - user object reference
513 * Returns true on success, false on failure
514 */
515 function _add_user_contact_category(&$user) {
516 $category = _get_user_category_string($user);
517 $recipients = $user->mail;
518 $reply = variable_get('rolecontact_default_confirmation',DEFAULT_CONFIRMATION);
519 $weight = DEFAULT_WEIGHT;
520 $selected = "false";
521 if (db_query("INSERT INTO {contact} (category, recipients, reply, weight, selected) VALUES ('%s','%s','%s',%d, %d)", $category, $recipients, $reply, $weight, $selected)) {
522 debug(__FUNCTION__." added user $category");
523 // make sure that the user has an updated cid in the rolecontact table if one previously existed...
524 $cid = _get_user_contact_category($recipients);
525 _rolecontact_user_cid_update($user->uid,$cid);
526 watchdog('rolecontact', t('Role Contact: user %category added.', array('%category' => $category)), WATCHDOG_NOTICE);
527 } else {
528 watchdog('rolecontact', t('Role Contact: failed to add user %category.', array('%category' => $category)), WATCHDOG_WARNING);
529 }
530 }
531
532 /**
533 * Internal method - adds a contact category for all the users in the selected role
534 * Params $rid - a role id
535 * Returns true on success, false on failure
536 */
537 function _add_all_user_contact_categories($rid) {
538 $users = _get_users_in_role($rid);
539 foreach ($users as $user) {
540 _add_user_contact_category($user);
541 }
542 }
543
544 /**
545 * Internal method - removes a category for the user from the contact category table
546 *
547 * Returns nothin'
548 */
549 function _delete_user_contact_category($cid) {
550 $result = db_query("SELECT category FROM {contact} WHERE cid = %d", $cid);
551 $category = false;
552 if ($row = db_fetch_object($result)) {
553 if (db_query("DELETE FROM {contact} WHERE cid = %d", $cid)) {
554 debug(__FUNCTION__."deleting user $row->category");
555 watchdog('rolecontact', t('Role Contact: user %category removed.', array('%category' => $row->category)), WATCHDOG_NOTICE);
556 } else {
557 watchdog('rolecontact', t('Role Contact: failed to remove user %category.', array('%category' => $row->category)), WATCHDOG_WARNING);
558 }
559 }
560 }
561
562 /**
563 * Internal method - when disabling the module, removes all user role categories
564 * Params $rid - a role id
565 * Returns nothin'
566 */
567 function _delete_all_user_contact_categories($rid) {
568 $users = _get_users_in_role($rid);
569 foreach ($users as $user) {
570 $category = _get_user_category_string($user);
571 $mail = $user->mail;
572 debug(__FUNCTION__." deleting user $category, with email $mail");
573 if (db_query("DELETE FROM {contact} WHERE category = '%s' AND recipients = '%s'", $category, $mail)) {
574 debug(__FUNCTION__." deleted user $category, with email $mail");
575 watchdog('rolecontact', t('Role Contact: removing all users - user %category removed.', array('%category' => $category)), WATCHDOG_NOTICE);
576 } else {
577 debug(__FUNCTION__." failed to delete user $category, with email $mail");
578 watchdog('rolecontact', t('Role Contact: removing all users - failed to remove user %category.', array('%category' => $category)), WATCHDOG_WARNING);
579 }
580 }
581 }
582
583 /**
584 * Internal method - returns the user's contact category string based on rules
585 *
586 * Returns a string on success, false on failure
587 */
588 function _get_user_category_string(&$user) {
589 //debug(__FUNCTION__.' user object: '.print_r($user,true));
590 $name = false;
591 if (isset($user->profile_firstname) && isset($user->profile_lastname)) {
592 $name = $user->profile_firstname.' '.$user->profile_lastname;
593 } elseif (isset($user->profile_fullname)) {
594 $name = $user->profile_fullname;
595 } else {
596 $name = $user->name;
597 }
598 debug(__FUNCTION__.' name = '.$name);
599 return $name;
600 }
601
602 /**
603 * Internal method - returns an array of role user objects
604 * Argument - a role ID
605 * Returns an array of users, empty array otherwise
606 */
607 function _get_users_in_role($role_id) {
608 $users = array();
609 $result = db_query('SELECT DISTINCT u.uid FROM {users} u INNER JOIN {users_roles} r ON u.uid = r.uid WHERE r.rid = %s', $role_id);
610 while($account = db_fetch_object($result)) {
611 $users[] = user_load(array('uid'=>$account->uid));
612 }
613 //debug(__FUNCTION__." all users in role $role_id: ".print_r($users,true));
614 return $users;
615 }
616
617 /**
618 * Menu callback; display a list of user information.
619 */
620 function profile_list() {
621 global $user;
622 $name = arg(1);
623 list(, , $value) = explode('/', $_GET['q'], 3);
624
625 $field = db_fetch_object(db_query("SELECT DISTINCT(fid), type, title, page, visibility FROM {profile_fields} WHERE name = '%s' ORDER BY weight ASC ", $name));
626 $role_id = variable_get('rolecontact_role_id',NULL); // staff role
627
628 if ($name && $field->fid) {
629 // Only allow browsing of fields that have a page title set.
630 if (empty($field->page)) {
631 drupal_not_found();
632 return;
633 }
634 // Do not allow browsing of private fields by non-admins.
635 if (!user_access('administer users') && $field->visibility == PROFILE_PRIVATE) {
636 drupal_access_denied();
637 return;
638 }
639
640 // Compile a list of fields to show.
641 $fields = array();
642 $result = db_query('SELECT name, title, type, weight, page FROM {profile_fields} WHERE fid != %d AND visibility = %d ORDER BY weight ASC', $field->fid, PROFILE_PUBLIC_LISTINGS);
643 while ($record = db_fetch_object($result)) {
644 $fields[] = $record;
645 }
646
647 // Determine what query to use:
648 $arguments = array($field->fid);
649 switch ($field->type) {
650 case 'checkbox':
651 $query = 'v.value = 1';
652 break;
653 case 'textfield':
654 case 'selection':
655 $query = "v.value = '%s'";
656 $arguments[] = $value;
657 break;
658 case 'list':
659 $query = "v.value LIKE '%%%s%%'";
660 $arguments[] = $value;
661 break;
662 default:
663 drupal_not_found();
664 return;
665 }
666
667 // Extract the affected users:
668 $result = pager_query("SELECT u.uid, u.access FROM {users} u INNER JOIN {profile_values} v ON u.uid = v.uid WHERE v.fid = %d AND $query AND u.access != 0 AND u.status != 0 ORDER BY u.access DESC", 20,0, NULL, $arguments);
669 $output = '<div id="staff_profile">';
670 while ($account = db_fetch_object($result)) {
671 $account = user_load(array('uid' => $account->uid));
672 if (isset($account->roles[$role_id])) {
673 $profile = _profile_update_user_fields($fields, $account);
674 $output .= theme('profile_listing', $account, $profile);
675 }
676 else {
677 $output .= "not including ".$account->uid."...";
678 }
679 }
680 $output .= theme('pager', NULL, 20);
681
682 if ($field->type == 'selection' || $field->type == 'list' || $field->type == 'textfield') {
683 $title = strtr(check_plain($field->page), array('%value' => theme('placeholder', $value)));
684 } else {
685 $title = check_plain($field->page);
686 }
687 $output .= '</div>';
688 drupal_set_title($title);
689 return $output;
690 }
691 // for a specific user
692 else if ($name) {
693 $result = db_query("SELECT u.uid, u.name, u.status, u.created, u.access FROM {users} u INNER JOIN {users_roles} ur ON u.uid=ur.uid WHERE ur.rid = $role_id AND u.name='%s'", $name);
694 $account = db_fetch_object($result);
695 if (!$account) {
696 drupal_not_found();
697 } else {
698 $account = user_load(array('uid' => $account->uid));
699
700 $profile = profile_view_profile($account); // get the profile fields array
701
702 if (is_array($profile)) {
703 // Remove unwanted profile items
704 foreach($profile as $details_key => $details) {
705 foreach($details as $key => $item) {
706 if (variable_get($key . '_user_show', 0) == 0) {
707 unset($profile[$details_key][$key]);
708 }
709 }
710 }
711 }
712 $account->profile_fullname = _rolecontact_get_name($profile,$account);
713 drupal_set_title('Information about ' . $account->profile_fullname);
714 //$output = '<div class="content">';
715 //$output .= '<p>To obtain more details about a staff member, please click on their name below.</p>';
716 $output .= '<table id="rolecontact_list">';
717 $output .= theme('rolecontact_profile',$account,$profile,false);
718 $output .= '</table>';
719 return $output;
720 }
721 }
722 // for a list of all users in the role
723 else {
724 $fields = array();
725 // if ( user in private role
726 $fields_selector = PROFILE_PUBLIC_LISTINGS;
727 $result = db_query('SELECT name, title, type, weight FROM {profile_fields} WHERE visibility = %d ORDER BY category DESC, weight', $fields_selector);
728 while ($record = db_fetch_object($result)) {
729 $fields[] = $record;
730 }
731 debug(__FUNCTION__.': staff field array ='.print_r($fields,true));
732 debug(__FUNCTION__.': user array ='.print_r($account,true));
733
734 // Find the profile_rolecontact_weight profile field
735 // TODO: Make profile_rolecontact_weight admin configurable
736 $result = db_query("SELECT fid FROM {profile_fields} WHERE name = 'profile_rolecontact_weight'");
737 $id = db_fetch_object($result);
738
739 $result = pager_query("SELECT u.uid, u.name, u.status, u.created, u.access, IF (rc.weight IS NULL, 0, rc.weight) weight FROM {users} u INNER JOIN {users_roles} ur ON u.uid=ur.uid LEFT JOIN {rolecontact} rc ON rc.uid = u.uid WHERE ur.rid = $role_id ORDER BY weight ASC, u.name ASC",
740 20, 0, NULL);
741 $status = array(t('blocked'), t('active'));
742 $output = '<div class="content">';
743 $output .= '<div class="rolecontact_header">' . variable_get('rolecontact_header', '') . '</div>';
744 //$output .= '<p>To obtain more details about a staff member, please click on their name below.</p>';
745 $output .= '<table id="rolecontact_list">';
746 $info = array();
747 $i = 0;
748 while ($account = db_fetch_object($result)) {
749 $account = user_load(array('uid' => $account->uid));
750 debug(__FUNCTION__.': staff field array ='.print_r($fields,true));
751 debug(__FUNCTION__.': user array ='.print_r($account,true));
752 //$profile = _profile_update_user_fields($fields, $account);
753 $profile = profile_view_profile($account); // get the profile fields array
754
755 if (is_array($profile)) {
756 // Remove unwanted profile items
757 foreach($profile as $details_key => $details) {
758 foreach($details as $key => $item) {
759 if (variable_get($key . '_show', 0) == 0) {
760 unset($profile[$details_key][$key]);
761 }
762 }
763 }
764 }
765 $account->profile_fullname = _rolecontact_get_name($profile,$account);
766 $output .= theme('rolecontact_profile',$account,$profile);
767 }
768
769 $output .= '</table>';
770 $output .= '<div class="rolecontact_footer">' . variable_get('rolecontact_footer', '') . '</div>';
771 $output .= '</div>';
772 $output .= theme('pager', NULL, 20);
773
774 drupal_set_title(t(check_plain(variable_get('rolecontact_profile_list_title','Staff List'))));
775 return $output;
776 }
777 }
778
779 /**
780 * Find the name of a user from their profile. If that fails use their username
781 */
782 function _rolecontact_get_name($profile, &$user) {
783 if (is_array($profile)) {
784 foreach ($profile as $item) {
785 if (isset($item['profile_fullname']['value'])) {
786 if (!isset($fullname)) {
787 $fullname = $item['profile_fullname']['value'];
788 }
789 }
790 if (isset($item['profile_firstname']['value'])) {
791 if (!isset($firstname)) {
792 $firstname = $item['profile_firstname']['value'];
793 }
794 }
795 if (isset($item['profile_lastname']['value'])) {
796 if (!isset($lastname)) {
797 $lastname = $item['profile_lastname']['value'];
798 }
799 }
800 }
801
802 if (isset($fullname)) {
803 return $fullname;
804 } elseif (isset($firstname)) {
805 $name = $firstname;
806 if (isset($lastname)) {
807 $name .= ' ' . $lastname;
808 }
809 return $name;
810 }
811 }
812 return $user->name;
813 }
814
815 function theme_rolecontact_profile($account,$profile,$show_link=true) {
816 $output = ' <tr>';
817 $output .= ' <td class="picture">';
818 if ($account->picture) {
819 $output .= theme('user_picture', $account);
820 }
821 $output .= ' </td>';
822 $output .= ' <td class="profile">';
823
824 if ($profile) {
825 foreach ($profile as $profile_section => $fields) {
826 debug(__FUNCTION__.' processing '.$profile_section.' now...');
827 debug(__FUNCTION__.' profile object: '.print_r($fields,true));
828 $name_added = false;
829 $contact_added = false;
830 foreach ($fields as $field) {
831 if ($field['class'] == 'profile_lastname' or $field['class'] == 'profile_firstname' or $field['class'] == 'profile_fullname') {
832 if (!$name_added) {
833 $name = _rolecontact_get_name($profile, $user);
834 $output .= ' <div class="fields fields-fullname">';
835 $output .= ' '.$name.' ';
836 if ($show_link) {
837 $url = "/rolecontact_list/".$account->name;
838 $output .= ' (<a href="'.$url.'">profile</a>)';
839 }
840 $output .= ' </div>';
841 $name_added = true;
842 }
843 } else {
844 debug(__FUNCTION__.' field : '.print_r($field,true));
845 $output .= ' <div class="fields fields-'.$field['class'].'"><span class="label">'.$field['title'].'</span>: '.$field['value'].'</div>';
846 debug(__FUNCTION__.' profile field '.$field['title'].' is '.$field['value']);
847 }
848
849 // add the contact info after the user's name
850 if ($name_added and !$contact_added and $account->contact == 1) {
851 $output .= ' <div class="fields fields-contact"><span class="label">Email:</span>';
852 debug(__FUNCTION__.' user object: '.print_r($account,true));
853 debug(__FUNCTION__.' profile object: '.print_r($profile,true));
854 $uid = $account->uid;
855 $cid = _get_user_contact_category($account->mail);
856 $url = _create_user_rolecontact_link($cid);
857 $output .= ' '.$url;
858 $output .= ' </div>';
859 $contact_added = true;
860 }
861 }
862 }
863 }
864
865 $output .= ' </td>';
866 $output .= ' </tr>';
867 return $output;
868 }

  ViewVC Help
Powered by ViewVC 1.1.2