--- profile_csv.module 2009-07-08 01:43:55.000000000 +0200 +++ profile_csv.module 2009-10-22 17:15:22.463200000 +0200 @@ -1,6 +1,12 @@ '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'), @@ -54,12 +61,15 @@ function profile_csv_admin_settings() { '#collapsible' => TRUE, '#collapsed' => TRUE, ); - + $roles = array( + DRUPAL_ANONYMOUS_RID, + DRUPAL_AUTHENTICATED_RID, + ); foreach (user_roles($membersonly = TRUE) as $rid => $name) { $role = PROFILE_CSV_ROLE . $rid; $form[$set][$role] = array( '#type' => 'checkbox', - '#title' => $name, + '#title' => in_array($rid, $roles) ? $name : t($name), '#return_value' => 1, '#default_value' => variable_get($role, 0), ); @@ -73,9 +83,9 @@ function profile_csv_admin_settings() { '#collapsed' => TRUE, ); $options = array( - 1 => t('Active'), - 0 => t('Blocked'), - 2 => t('Both'), + 1 => t('Active'), + 0 => t('Blocked'), + 2 => t('Both'), ); $form[$set][PROFILE_CSV_STATUS] = array( '#type' => 'select', @@ -83,7 +93,7 @@ function profile_csv_admin_settings() { '#options' => $options, '#description' => t(''), ); - + $set = 'fields'; $form[$set] = array( '#type' => 'fieldset', @@ -92,25 +102,18 @@ function profile_csv_admin_settings() { '#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), + $schema = drupal_get_schema('users'); + $options = array(); + foreach ($schema['fields'] as $field_name => $field) { + $options[$field_name] = _profile_csv_users_map_column_name($field_name) .'
'. filter_xss_admin(t($field['description'])) .'
'; + } + $form[$set][PROFILE_CSV_PARAM .'fields'] = array( + '#type' => 'checkboxes', + '#options' => $options, + '#title' => t('Fields'), + '#default_value' => variable_get(PROFILE_CSV_PARAM .'fields', array()), ); - + $set = 'profile'; $form[$set] = array( '#type' => 'fieldset', @@ -119,27 +122,27 @@ function profile_csv_admin_settings() { '#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"); + 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, + '#title' => check_plain(t($row->category)), '#collapsible' => TRUE, ); } - - $form[$set][$row->category][$fld] = array( - '#type' => 'checkbox', - '#title' => $row->title, - '#return_value' => 1, - '#default_value' => variable_get($fld, 0), - ); - } + + $form[$set][$row->category][$fld] = array( + '#type' => 'checkbox', + '#title' => check_plain(t($row->title)), + '#return_value' => 1, + '#default_value' => variable_get($fld, 0), + ); + } return system_settings_form($form); } @@ -150,11 +153,11 @@ function profile_csv_page() { $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) { - $data .= _profile_csv_format_user($row->uid ); + $data .= _profile_csv_format_user($row->uid); } - else{ + else{ if ($user_status == $row->status) { - $data .= _profile_csv_format_user($row->uid ); + $data .= _profile_csv_format_user($row->uid); } } } @@ -164,32 +167,33 @@ function profile_csv_page() { FROM {users} u INNER JOIN {users_roles} ur ON u.uid = ur.uid WHERE u.uid > 1"); - $prev_uid = 0; + $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) { - $data .= _profile_csv_format_user($row->uid ); + $data .= _profile_csv_format_user($row->uid); } - else{ + else{ if ($user_status == $row->status) { - $data .= _profile_csv_format_user($row->uid ); + $data .= _profile_csv_format_user($row->uid); } - } - $prev_uid = $row->uid; - } - } + } + $prev_uid = $row->uid; + } + } } header("Content-type: text/plain; charset=UTF-8"); - header("Content-Disposition: attachment; filename=userlist.csv"); + $token = time(); + header("Content-Disposition: attachment; filename=userlist_${token}.csv"); header("Pragma: no-cache"); header("Expires: 0"); print $data; - exit(); -} + 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 @@ -201,53 +205,55 @@ function _profile_csv_get_profile_fields } } } - return $fields; -} + return $fields; +} -function _profile_csv_format_user($uid = 0) { +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 - + //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)) { + if (isset($new_info)) { $line = implode(",", $new_info); - } + } $data .= trim($line) ."\n"; - - return $data; + + return $data; } -function _profile_csv_get_user($uid = 0) { - $users = array(); - $result = db_query('SELECT u.uid, u.name, u.mail, u.data 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; +function _profile_csv_get_user($uid) { + $user = array(); + $fields = _profile_csv_users_selected_fields(); + //Verify that the columns haven't been deleted since the last save or the query will fail + $schema = drupal_get_schema('users'); + foreach ($fields as $field_name => $value) { + if (!$schema['fields'][$field_name]) { + unset($fields[$field_name]); } - if (variable_get(PROFILE_CSV_PARAM .'mail', 0)) { - $users[] = $row->mail; + } + $cols = implode(', ', $fields); + $result = db_query_range("SELECT $cols FROM {users} u WHERE u.uid = %d", $uid, O, 1); + while ($row = db_fetch_object($result)) { + foreach ($fields as $col) { + $user[$col] = ($col == 'data') ? unserialize($row->$col) : $row->$col; } - $users['data'] = unserialize($row->data); - } - return $users; -} - + }; + return $user; +} + function _profile_csv_get_profile($uid=0, $user_data=NULL) { - $profile_fields = _profile_csv_get_profile_fields(); + $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 @@ -255,13 +261,13 @@ function _profile_csv_get_profile($uid=0 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; @@ -269,15 +275,9 @@ function _profile_csv_get_profile($uid=0 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"'; + $fields = _profile_csv_users_selected_fields(); + foreach ($fields as $field_name => $value) { + $row[] = '"'. _profile_csv_users_map_column_name($field_name) .'"'; } foreach (_profile_csv_get_profile_fields() as $field) { $row[] = '"'. $field['title'] .'"'; @@ -285,3 +285,38 @@ function _profile_csv_header() { return implode(",", $row) ."\n"; } + +function _profile_csv_users_selected_fields() { + static $data = NULL; + if (!$data) { + $fields = variable_get(PROFILE_CSV_PARAM .'fields', array()); + foreach ($fields as $field_name => $value) { + if ($value === 0) { + unset($fields[$field_name]); + } + } + $data = $fields; + } + return $data; +} + +function _profile_csv_users_map_column_name($field_name) { + //map with some translation already in Drupal + $names = array( + 'uid' => t('User ID'), + 'name' => t('Username'), + 'mail' => t('E-mail address'), + 'pass' => t('Password'), + 'status' => t('Status'), + 'signature' => t('Signature'), + 'created' => t('Member for'), + 'access' => t('Last access'), + 'language' => t('Language'), + 'timezone_name' => t('Timezone'), + 'data' => t('Data'), + ); + if (isset($names[$field_name])) { + return $names[$field_name]; + } + return $field_name; +} \ No newline at end of file