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

Diff of /contributions/modules/profile_csv/profile_csv.module

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

revision 1.10.2.2, Mon Oct 26 15:52:34 2009 UTC revision 1.10.2.3, Tue Nov 10 22:59:59 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: profile_csv.module,v 1.10.2.1 2009/07/07 23:43:55 kbahey Exp $  // $Id: profile_csv.module,v 1.10.2.1 2009/07/07 23:43:55 kbahey Exp $
3    
4    /**
5     * @file
6     * Profile CSV
7     *
8     * This is a profile module export user and profile data to a comma separated variable file (CSV).
9     */
10    
11  define('PROFILE_CSV_STATUS',         'profile_csv_status');  define('PROFILE_CSV_STATUS',         'profile_csv_status');
12  define('PROFILE_CSV_PARAM',          'profile_csv_param_');  define('PROFILE_CSV_PARAM',          'profile_csv_param_');
# Line 15  function profile_csv_help($path, $arg) { Line 21  function profile_csv_help($path, $arg) {
21    }    }
22    return $output;    return $output;
23  }  }
24    
25  function profile_csv_perm() {  function profile_csv_perm() {
26    return array(PROFILE_CSV_PERM_DOWNLOAD);    return array(PROFILE_CSV_PERM_DOWNLOAD);
27  }  }
# Line 41  function profile_csv_menu() { Line 47  function profile_csv_menu() {
47    return $items;    return $items;
48  }  }
49    
50  function profile_csv_admin_settings() {  function profile_csv_admin_settings() {
51    
52    $form['extra'] = array(    $form['extra'] = array(
53      '#type'        => 'markup',      '#type'        => 'markup',
54      '#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'),      '#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'),
# Line 54  function profile_csv_admin_settings() { Line 61  function profile_csv_admin_settings() {
61      '#collapsible' => TRUE,      '#collapsible' => TRUE,
62      '#collapsed'   => TRUE,      '#collapsed'   => TRUE,
63    );    );
64      $roles = array(
65        DRUPAL_ANONYMOUS_RID,
66        DRUPAL_AUTHENTICATED_RID,
67      );
68    foreach (user_roles($membersonly = TRUE) as $rid => $name) {    foreach (user_roles($membersonly = TRUE) as $rid => $name) {
69      $role = PROFILE_CSV_ROLE . $rid;      $role = PROFILE_CSV_ROLE . $rid;
70      $form[$set][$role] = array(      $form[$set][$role] = array(
71        '#type'          => 'checkbox',        '#type'          => 'checkbox',
72        '#title'         => $name,        '#title'         => in_array($rid, $roles) ? $name : t($name),
73        '#return_value'  => 1,        '#return_value'  => 1,
74        '#default_value' => variable_get($role, 0),        '#default_value' => variable_get($role, 0),
75      );      );
# Line 73  function profile_csv_admin_settings() { Line 83  function profile_csv_admin_settings() {
83      '#collapsed'   => TRUE,      '#collapsed'   => TRUE,
84    );    );
85    $options = array(    $options = array(
86      1 => t('Active'),    1 => t('Active'),
87      0 => t('Blocked'),    0 => t('Blocked'),
88      2 => t('Both'),    2 => t('Both'),
89    );    );
90    $form[$set][PROFILE_CSV_STATUS] = array(    $form[$set][PROFILE_CSV_STATUS] = array(
91      '#type'          => 'select',      '#type'          => 'select',
# Line 83  function profile_csv_admin_settings() { Line 93  function profile_csv_admin_settings() {
93      '#options'       => $options,      '#options'       => $options,
94      '#description'   => t(''),      '#description'   => t(''),
95    );    );
96    
97    $set = 'fields';    $set = 'fields';
98    $form[$set] = array(    $form[$set] = array(
99      '#type'          => 'fieldset',      '#type'          => 'fieldset',
# Line 92  function profile_csv_admin_settings() { Line 102  function profile_csv_admin_settings() {
102      '#collapsible'   => TRUE,      '#collapsible'   => TRUE,
103      '#collapsed'     => TRUE,      '#collapsed'     => TRUE,
104    );    );
105    $form[$set][PROFILE_CSV_PARAM . 'uid'] = array(    $schema = drupal_get_schema('users');
106      '#type'          => 'checkbox',    $options = array();
107      '#title'         => t('User ID'),    foreach ($schema['fields'] as $field_name => $field) {
108      '#return_value'  => 1,      $options[$field_name] = _profile_csv_users_map_column_name($field_name) .'<div class="description clear-block">'. filter_xss_admin(t($field['description'])) .'</div>';
109      '#default_value' => variable_get(PROFILE_CSV_PARAM .'uid', 0),    }
110    );    $form[$set][PROFILE_CSV_PARAM .'fields'] = array(
111    $form[$set][PROFILE_CSV_PARAM . 'name'] = array(      '#type'          => 'checkboxes',
112      '#type'          => 'checkbox',      '#options'       => $options,
113      '#title'         => t('User Name'),      '#title'         => t('Fields'),
114      '#return_value'  => 1,      '#default_value' => variable_get(PROFILE_CSV_PARAM .'fields', array()),
     '#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),  
115    );    );
116    
117    $set = 'profile';    $set = 'profile';
118    $form[$set] = array(    $form[$set] = array(
119      '#type'        => 'fieldset',      '#type'        => 'fieldset',
# Line 119  function profile_csv_admin_settings() { Line 122  function profile_csv_admin_settings() {
122      '#collapsible' => TRUE,      '#collapsible' => TRUE,
123      '#collapsed'   => TRUE,      '#collapsed'   => TRUE,
124    );    );
125    
126    $result = db_query("SELECT pf.fid, pf.name, pf.title, pf.category    $result = db_query("SELECT pf.fid, pf.name, pf.title, pf.category
127      FROM {profile_fields} pf      FROM {profile_fields} pf
128      ORDER BY pf.category, pf.weight, pf.title");      ORDER BY pf.category, pf.weight, pf.title");
129    while ($row = db_fetch_object($result)) {    while ($row = db_fetch_object($result)) {
130      $fld = PROFILE_CSV_PARAM .'profile_'. $row->fid;      $fld = PROFILE_CSV_PARAM .'profile_'. $row->fid;
131      if (!isset($form[$set][$row->category])) {      if (!isset($form[$set][$row->category])) {
132        $form[$set][$row->category] = array(        $form[$set][$row->category] = array(
133          '#type'        => 'fieldset',          '#type'        => 'fieldset',
134          '#title'       => $row->category,          '#title'       => check_plain(t($row->category)),
135          '#collapsible' => TRUE,          '#collapsible' => TRUE,
136        );        );
137      }      }
138    
139       $form[$set][$row->category][$fld] = array(      $form[$set][$row->category][$fld] = array(
140          '#type'          => 'checkbox',        '#type'          => 'checkbox',
141          '#title'         => $row->title,        '#title'         => check_plain(t($row->title)),
142          '#return_value'  => 1,        '#return_value'  => 1,
143          '#default_value' => variable_get($fld, 0),        '#default_value' => variable_get($fld, 0),
144        );      );
145    }    }
146    return system_settings_form($form);    return system_settings_form($form);
147  }  }
148    
149  function profile_csv_page() {  function profile_csv_page() {
150    header("Content-type: text/plain; charset=UTF-8");    @ini_set('memory_limit', '750M');
151    header("Content-Disposition: attachment; filename=userlist.csv");    @set_time_limit(240);
152    header("Pragma: no-cache");  
153    header("Expires: 0");    header('Content-type: text/plain; charset=UTF-8');
154      header('Content-Disposition: attachment; filename=userlist_' . time() . '.csv');
155      header('Pragma: no-cache');
156      header('Expires: 0');
157    
158    print _profile_csv_header();    print _profile_csv_header();
159    
160    $user_status = variable_get(PROFILE_CSV_STATUS, 2);    $user_status = variable_get(PROFILE_CSV_STATUS, 2);
161    if (variable_get(PROFILE_CSV_ROLE .'2', 0)) {    if (variable_get(PROFILE_CSV_ROLE . '2', 0)) {
162      $result = db_query("SELECT u.uid, u.status FROM {users} u  WHERE u.uid > 1");      $result = db_query("SELECT u.uid, u.status FROM {users} u  WHERE u.uid > 1");
163      while ($row = db_fetch_object($result)) {      while ($row = db_fetch_object($result)) {
164        if ($user_status == 2) {        if ($user_status == 2) {
165          print _profile_csv_format_user($row->uid );          print _profile_csv_format_user($row->uid);
166        }        }
167        else{        else {
168          if ($user_status == $row->status) {          if ($user_status == $row->status) {
169            print _profile_csv_format_user($row->uid );            print _profile_csv_format_user($row->uid);
170          }          }
171        }        }
172      }      }
# Line 169  function profile_csv_page() { Line 176  function profile_csv_page() {
176        FROM {users} u        FROM {users} u
177        INNER JOIN {users_roles} ur ON u.uid = ur.uid        INNER JOIN {users_roles} ur ON u.uid = ur.uid
178        WHERE u.uid > 1");        WHERE u.uid > 1");
179      $prev_uid = 0;      $prev_uid = 0;
180      while ($row = db_fetch_object($result)) {      while ($row = db_fetch_object($result)) {
181        if ((variable_get(PROFILE_CSV_ROLE . $row->rid, 0)) && ($row->uid != $prev_uid)) {        if ((variable_get(PROFILE_CSV_ROLE . $row->rid, 0)) && ($row->uid != $prev_uid)) {
182          if ($user_status == 2) {          if ($user_status == 2) {
183            print  _profile_csv_format_user($row->uid );            print _profile_csv_format_user($row->uid);
184          }          }
185          else{          else {
186            if ($user_status == $row->status) {            if ($user_status == $row->status) {
187              print  _profile_csv_format_user($row->uid );              print _profile_csv_format_user($row->uid);
188            }            }
189          }          }
190         $prev_uid = $row->uid;          $prev_uid = $row->uid;
191        }        }
192      }      }
193    }    }
194      exit();
195    exit();  }
 }  
196    
197  function _profile_csv_get_profile_fields() {  function _profile_csv_get_profile_fields() {
198    static $fields;    static $fields;
199    
200    if (!isset($fields)) {    if (!isset($fields)) {
201      $fields = array();      $fields = array();
202      $result = db_query('SELECT pf.fid, pf.name, pf.title, pf.type, pf.visibility      $result = db_query('SELECT pf.fid, pf.name, pf.title, pf.type, pf.visibility
# Line 202  function _profile_csv_get_profile_fields Line 208  function _profile_csv_get_profile_fields
208        }        }
209      }      }
210    }    }
211    return $fields;    return $fields;
212  }  }
213    
214  function _profile_csv_format_user($uid = 0) {  function _profile_csv_format_user($uid = 0) {
215    $user_data = _profile_csv_get_user($uid);    $user_data = _profile_csv_get_user($uid);
216    $profile_data = _profile_csv_get_profile($uid, $user_data['data']);    $profile_data = _profile_csv_get_profile($uid, $user_data['data']);
217    unset($user_data['data']);    unset($user_data['data']);
218    $info = array_merge($user_data, $profile_data);    $info = array_merge($user_data, $profile_data);
219    //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
220    
221    foreach ($info as $value) {    foreach ($info as $value) {
222      $new_info[] = '"'. str_replace('"', '""', $value) .'"';      $new_info[] = '"'. str_replace('"', '""', $value) .'"';
223    }    }
224    if (isset($new_info)) {    if (isset($new_info)) {
225      $line = implode(",", $new_info);      $line = implode(",", $new_info);
226    }    }
227    $data .= trim($line) ."\n";    $data .= trim($line) ."\n";
228    
229   return $data;    return $data;
230  }  }
231    
232  function _profile_csv_get_user($uid = 0) {  function _profile_csv_get_user($uid) {
233     $users = array();    $user = array();
234     $result = db_query('SELECT u.uid, u.name, u.mail, u.data FROM {users} u WHERE u.uid = %d', $uid);    $fields = _profile_csv_users_selected_fields();
235     while ($row = db_fetch_object($result)) {    //Verify that the columns haven't been deleted since the last save or the query will fail
236      if (variable_get(PROFILE_CSV_PARAM .'uid', 0)) {    $schema = drupal_get_schema('users');
237        $users[] = $row->uid;    foreach ($fields as $field_name => $value) {
238        if (!$schema['fields'][$field_name]) {
239          unset($fields[$field_name]);
240      }      }
241      if (variable_get(PROFILE_CSV_PARAM .'name', 0)) {    }
242        $users[] = $row->name;    $cols = implode(', ', $fields);
243      }    $result = db_query("SELECT $cols FROM {users} u WHERE u.uid = %d", $uid);
244      if (variable_get(PROFILE_CSV_PARAM .'mail', 0)) {    while ($row = db_fetch_object($result)) {
245        $users[] = $row->mail;      foreach ($fields as $col) {
246          $user[$col] = ($col == 'data') ? unserialize($row->$col) : $row->$col;
247      }      }
248      $users['data'] = unserialize($row->data);    };
249    }    return $user;
250    return $users;  }
251  }  
   
252  function _profile_csv_get_profile($uid=0, $user_data=NULL) {  function _profile_csv_get_profile($uid=0, $user_data=NULL) {
253    $profile_fields = _profile_csv_get_profile_fields();    $profile_fields = _profile_csv_get_profile_fields();
254    $profile_result = array();    $profile_result = array();
255    foreach ($profile_fields  as $profile_field) {    foreach ($profile_fields  as $profile_field) {
256      if ($profile_field ['visibility'] == 4) {      if ($profile_field ['visibility'] == 4) {
257        // Try to get it from the $user_data        // Try to get it from the $user_data
258        $value = $user_data[$profile_field['name']];        $value = $user_data[$profile_field['name']];
259      }      }
260      else {      else {
261        $value = db_result(db_query("SELECT pv.value        $value = db_result(db_query("SELECT pv.value
262          FROM {profile_fields} pf, {profile_values} pv          FROM {profile_fields} pf, {profile_values} pv
# Line 256  function _profile_csv_get_profile($uid=0 Line 264  function _profile_csv_get_profile($uid=0
264          AND pf.name = '%s'          AND pf.name = '%s'
265          AND pv.uid = %d", $profile_field['name'], $uid));          AND pv.uid = %d", $profile_field['name'], $uid));
266      }      }
267    
268      if ($profile_field['type'] == 'date') {      if ($profile_field['type'] == 'date') {
269        if ($value !== 0) {        if ($value !== 0) {
270          $value = unserialize($value);          $value = unserialize($value);
271          $value = $value['year'] .'-'. $value['month'] .'-'. $value['day'];          $value = $value['year'] .'-'. $value['month'] .'-'. $value['day'];
272        }        }
273      }      }
274      $profile_result[] = $value;      $profile_result[] = $value;
275    }    }
276    return $profile_result;    return $profile_result;
# Line 270  function _profile_csv_get_profile($uid=0 Line 278  function _profile_csv_get_profile($uid=0
278    
279  function _profile_csv_header() {  function _profile_csv_header() {
280    $row = array();    $row = array();
281      $fields = _profile_csv_users_selected_fields();
282    if (variable_get(PROFILE_CSV_PARAM .'uid', 0)) {    foreach ($fields as $field_name => $value) {
283      $row[] = '"uid"';      $row[] = '"'. _profile_csv_users_map_column_name($field_name) .'"';
   }  
   if (variable_get(PROFILE_CSV_PARAM .'name', 0)) {  
     $row[] = '"name"';  
   }  
   if (variable_get(PROFILE_CSV_PARAM .'mail', 0)) {  
     $row[] = '"mail"';  
284    }    }
285    foreach (_profile_csv_get_profile_fields() as $field) {    foreach (_profile_csv_get_profile_fields() as $field) {
286      $row[] = '"'. $field['title'] .'"';      $row[] = '"'. $field['title'] .'"';
287    }    }
288    
289    return implode(",", $row) ."\n";    return implode(",", $row) ."\n";
290    }
291    
292    function _profile_csv_users_selected_fields() {
293      static $data = NULL;
294      if (!$data) {
295        $fields = variable_get(PROFILE_CSV_PARAM .'fields', array());
296        foreach ($fields as $field_name => $value) {
297          if ($value === 0) {
298            unset($fields[$field_name]);
299          }
300        }
301        $data = $fields;
302      }
303      return $data;
304    }
305    
306    function _profile_csv_users_map_column_name($field_name) {
307      //map with some translation already in Drupal
308      $names = array(
309        'uid'           => t('User ID'),
310        'name'          => t('Username'),
311        'mail'          => t('E-mail address'),
312        'pass'          => t('Password'),
313        'status'        => t('Status'),
314        'signature'     => t('Signature'),
315        'created'       => t('Member for'),
316        'access'        => t('Last access'),
317        'language'      => t('Language'),
318        'timezone_name' => t('Timezone'),
319        'data'          => t('Data'),
320      );
321      if (isset($names[$field_name])) {
322        return $names[$field_name];
323      }
324      return $field_name;
325  }  }

Legend:
Removed from v.1.10.2.2  
changed lines
  Added in v.1.10.2.3

  ViewVC Help
Powered by ViewVC 1.1.2