'Profile CSV', 'type' => MENU_NORMAL_ITEM, 'description' => 'Profile CSV user data selection', 'page callback' => 'drupal_get_form', 'page arguments' => array('profile_csv_admin_settings'), 'access arguments' => array('administer site configuration'), ); $items['profile_csv'] = array( 'title' => 'Profile CSV Export', 'type' => MENU_SUGGESTED_ITEM, // Optional 'page callback' => 'profile_csv_page', 'access arguments' => array(PROFILE_CSV_PERM_DOWNLOAD), ); return $items; } function profile_csv_admin_settings() { $form['extra'] = array( '#type' => 'markup', '#value' => t('Remember to enable the profile_csv menu item so users who have the permission to download profile data have this item on their menus'), ); $set = 'roles'; $form[$set] = array( '#type' => 'fieldset', '#title' => t('Roles'), '#description' => t('Select one or more roles.'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); foreach (user_roles($membersonly = TRUE) as $rid => $name) { $role = PROFILE_CSV_ROLE . $rid; $form[$set][$role] = array( '#type' => 'checkbox', '#title' => $name, '#return_value' => 1, '#default_value' => variable_get($role, 0), ); } $set = 'status'; $form[$set] = array( '#type' => 'fieldset', '#title' => t('User status'), '#description' => t('Select one status.'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); $options = array( 1 => t('Active'), 0 => t('Blocked'), 2 => t('Both'), ); $form[$set][PROFILE_CSV_STATUS] = array( '#type' => 'select', '#default_value' => variable_get(PROFILE_CSV_STATUS, 2), '#options' => $options, '#description' => t(''), ); $set = 'fields'; $form[$set] = array( '#type' => 'fieldset', '#title' => t('General'), '#description' => t('Select one or more profile fields.'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); $form[$set][PROFILE_CSV_PARAM . 'uid'] = array( '#type' => 'checkbox', '#title' => t('User ID'), '#return_value' => 1, '#default_value' => variable_get(PROFILE_CSV_PARAM .'uid', 0), ); $form[$set][PROFILE_CSV_PARAM . 'name'] = array( '#type' => 'checkbox', '#title' => t('User Name'), '#return_value' => 1, '#default_value' => variable_get(PROFILE_CSV_PARAM .'name', 0), ); $form[$set][PROFILE_CSV_PARAM . 'mail'] = array( '#type' => 'checkbox', '#title' => t('User Email'), '#return_value' => 1, '#default_value' => variable_get(PROFILE_CSV_PARAM .'mail', 0), ); $form[$set][PROFILE_CSV_PARAM . 'member_since'] = array( '#type' => 'checkbox', '#title' => t('User Member Since'), '#return_value' => 1, '#default_value' => variable_get(PROFILE_CSV_PARAM .'member_since', 0), ); $form[$set][PROFILE_CSV_PARAM . 'last_access'] = array( '#type' => 'checkbox', '#title' => t('User Last Access'), '#return_value' => 1, '#default_value' => variable_get(PROFILE_CSV_PARAM .'last_access', 0), ); $set = 'profile'; $form[$set] = array( '#type' => 'fieldset', '#title' => t('Categories'), '#description' => t('Select one or more profile fields from categories below.'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); $result = db_query("SELECT pf.fid, pf.name, pf.title, pf.category FROM {profile_fields} pf ORDER BY pf.category, pf.weight, pf.title"); while ($row = db_fetch_object($result)) { $fld = PROFILE_CSV_PARAM .'profile_'. $row->fid; if (!isset($form[$set][$row->category])) { $form[$set][$row->category] = array( '#type' => 'fieldset', '#title' => $row->category, '#collapsible' => TRUE, ); } $form[$set][$row->category][$fld] = array( '#type' => 'checkbox', '#title' => $row->title, '#return_value' => 1, '#default_value' => variable_get($fld, 0), ); } return system_settings_form($form); } function profile_csv_page() { header("Content-type: text/plain; charset=UTF-8"); header("Content-Disposition: attachment; filename=userlist.csv"); header("Pragma: no-cache"); header("Expires: 0"); print _profile_csv_header(); $user_status = variable_get(PROFILE_CSV_STATUS, 2); if (variable_get(PROFILE_CSV_ROLE .'2', 0)) { $result = db_query("SELECT u.uid, u.status FROM {users} u WHERE u.uid > 1"); while ($row = db_fetch_object($result)) { if ($user_status == 2) { print _profile_csv_format_user($row->uid ); } else{ if ($user_status == $row->status) { print _profile_csv_format_user($row->uid ); } } } } else { $result = db_query("SELECT u.uid, u.status, ur.rid FROM {users} u INNER JOIN {users_roles} ur ON u.uid = ur.uid WHERE u.uid > 1"); $prev_uid = 0; while ($row = db_fetch_object($result)) { if ((variable_get(PROFILE_CSV_ROLE . $row->rid, 0)) && ($row->uid != $prev_uid)) { if ($user_status == 2) { print _profile_csv_format_user($row->uid ); } else{ if ($user_status == $row->status) { print _profile_csv_format_user($row->uid ); } } $prev_uid = $row->uid; } } } exit(); } function _profile_csv_get_profile_fields() { static $fields; if (!isset($fields)) { $fields = array(); $result = db_query('SELECT pf.fid, pf.name, pf.title, pf.type, pf.visibility FROM {profile_fields} pf ORDER BY pf.category, pf.weight, pf.title'); while ($row = db_fetch_object($result)) { if (variable_get(PROFILE_CSV_PARAM .'profile_'. $row->fid, 0)) { $fields[] = array('name' => $row->name, 'title' => $row->title, 'type' => $row->type, 'visibility' => $row->visibility); } } } return $fields; } function _profile_csv_format_user($uid = 0) { $user_data = _profile_csv_get_user($uid); $profile_data = _profile_csv_get_profile($uid, $user_data['data']); unset($user_data['data']); $info = array_merge($user_data, $profile_data); //all of the valid fields in ['data'] should have been picked out in _profile_csv_get_profile, so unset it foreach ($info as $value) { $new_info[] = '"'. str_replace('"', '""', $value) .'"'; } if (isset($new_info)) { $line = implode(",", $new_info); } $data .= trim($line) ."\n"; return $data; } function _profile_csv_get_user($uid = 0) { $users = array(); $result = db_query('SELECT u.uid, u.name, u.mail, u.data, u.created, u.access FROM {users} u WHERE u.uid = %d', $uid); while ($row = db_fetch_object($result)) { if (variable_get(PROFILE_CSV_PARAM .'uid', 0)) { $users[] = $row->uid; } if (variable_get(PROFILE_CSV_PARAM .'name', 0)) { $users[] = $row->name; } if (variable_get(PROFILE_CSV_PARAM .'mail', 0)) { $users[] = $row->mail; } if (variable_get(PROFILE_CSV_PARAM .'member_since', 0)) { $users[] = format_date($row->created, 'small'); } if (variable_get(PROFILE_CSV_PARAM .'last_access', 0)) { $users[] = format_date($row->access, 'small'); } $users['data'] = unserialize($row->data); } return $users; } function _profile_csv_get_profile($uid=0, $user_data=NULL) { $profile_fields = _profile_csv_get_profile_fields(); $profile_result = array(); foreach ($profile_fields as $profile_field) { if ($profile_field ['visibility'] == 4) { // Try to get it from the $user_data $value = $user_data[$profile_field['name']]; } else { $value = db_result(db_query("SELECT pv.value FROM {profile_fields} pf, {profile_values} pv WHERE pv.fid = pf.fid AND pf.name = '%s' AND pv.uid = %d", $profile_field['name'], $uid)); } if ($profile_field['type'] == 'date') { if ($value !== 0) { $value = unserialize($value); $value = $value['year'] .'-'. $value['month'] .'-'. $value['day']; } } $profile_result[] = $value; } return $profile_result; } function _profile_csv_header() { $row = array(); if (variable_get(PROFILE_CSV_PARAM .'uid', 0)) { $row[] = '"uid"'; } if (variable_get(PROFILE_CSV_PARAM .'name', 0)) { $row[] = '"name"'; } if (variable_get(PROFILE_CSV_PARAM .'mail', 0)) { $row[] = '"mail"'; } if (variable_get(PROFILE_CSV_PARAM .'member_since', 0)) { $row[] = '"member_since"'; } if (variable_get(PROFILE_CSV_PARAM .'last_access', 0)) { $row[] = '"last_access"'; } foreach (_profile_csv_get_profile_fields() as $field) { $row[] = '"'. $field['title'] .'"'; } return implode(",", $row) ."\n"; }