/[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.9, Mon Feb 26 03:11:47 2007 UTC revision 1.10, Mon Jul 21 00:47:04 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2  //$Id: profile_csv.module,v  Exp $  // $Id$
3    
 // Copyright 2006 http://2bits.com  
4    
5  define('PROFILE_CSV_STATUS',         'profile_csv_status');  define('PROFILE_CSV_STATUS',         'profile_csv_status');
6  define('PROFILE_CSV_PARAM',          'profile_csv_param_');  define('PROFILE_CSV_PARAM',          'profile_csv_param_');
7  define('PROFILE_CSV_ROLE',           'profile_csv_role');  define('PROFILE_CSV_ROLE',           'profile_csv_role');
8  define('PROFILE_CSV_PERM_DOWNLOAD',  'download profiles');  define('PROFILE_CSV_PERM_DOWNLOAD',  'download profiles');
9    
10  function profile_csv_help($section='') {  function profile_csv_help($path, $arg) {
11    switch ($section) {    switch ($path) {
12      case "admin/modules#description":      case "admin/modules#description":
13        $output = t("Allows exporting profile data in comma separated variable format.");        $output = t("Allows exporting profile data in comma separated variable format.");
14        break;        break;
# Line 48  function profile_csv_admin_settings() { Line 47  function profile_csv_admin_settings() {
47    );    );
48     $form[$set][PROFILE_CSV_STATUS] = array(     $form[$set][PROFILE_CSV_STATUS] = array(
49      '#type' => 'select',      '#type' => 'select',
50      '#default_value' => variable_get(PROFILE_CSV_STATUS,2),      '#default_value' => variable_get(PROFILE_CSV_STATUS, 2),
51      '#options' => array( 1 => t('active'), 0 => t('blocked'), 2 => t('both')),      '#options' => array( 1 => t('active'), 0 => t('blocked'), 2 => t('both')),
52      '#description' => t(''),      '#description' => t(''),
53    );    );
# Line 61  function profile_csv_admin_settings() { Line 60  function profile_csv_admin_settings() {
60      '#collapsible' => true,      '#collapsible' => true,
61      '#collapsed' => true,      '#collapsed' => true,
62    );    );
63    $form[$set][PROFILE_CSV_PARAM . 'uid'] = array(    $form[$set][PROFILE_CSV_PARAM .'uid'] = array(
64      '#type' => 'checkbox',      '#type' => 'checkbox',
65      '#title' => t('User ID'),      '#title' => t('User ID'),
66      '#return_value' => 1,      '#return_value' => 1,
67      '#default_value' => variable_get(PROFILE_CSV_PARAM . 'uid', 0),      '#default_value' => variable_get(PROFILE_CSV_PARAM .'uid', 0),
68    );    );
69    $form[$set][PROFILE_CSV_PARAM . 'name'] = array(    $form[$set][PROFILE_CSV_PARAM .'name'] = array(
70      '#type' => 'checkbox',      '#type' => 'checkbox',
71      '#title' => t('User Name'),      '#title' => t('User Name'),
72      '#return_value' => 1,      '#return_value' => 1,
73      '#default_value' => variable_get(PROFILE_CSV_PARAM . 'name', 0),      '#default_value' => variable_get(PROFILE_CSV_PARAM .'name', 0),
74    );    );
75    $form[$set][PROFILE_CSV_PARAM . 'mail'] = array(    $form[$set][PROFILE_CSV_PARAM .'mail'] = array(
76      '#type' => 'checkbox',      '#type' => 'checkbox',
77      '#title' => t('User Email'),      '#title' => t('User Email'),
78      '#return_value' => 1,      '#return_value' => 1,
79      '#default_value' => variable_get(PROFILE_CSV_PARAM . 'mail', 0),      '#default_value' => variable_get(PROFILE_CSV_PARAM .'mail', 0),
80    );    );
81    
82    $set = 'profile';    $set = 'profile';
# Line 91  function profile_csv_admin_settings() { Line 90  function profile_csv_admin_settings() {
90    
91    $result = db_query("SELECT pf.fid, pf.name, pf.title, pf.category FROM {profile_fields} pf ORDER BY pf.category, pf.weight, pf.title");    $result = db_query("SELECT pf.fid, pf.name, pf.title, pf.category FROM {profile_fields} pf ORDER BY pf.category, pf.weight, pf.title");
92    while ($row = db_fetch_object($result)) {    while ($row = db_fetch_object($result)) {
93      $fld = PROFILE_CSV_PARAM . 'profile_' . $row->fid;      $fld = PROFILE_CSV_PARAM .'profile_'. $row->fid;
94    
95      if (!isset($form[$set][$row->category])) {      if (!isset($form[$set][$row->category])) {
96        $form[$set][$row->category] = array(        $form[$set][$row->category] = array(
# Line 112  function profile_csv_admin_settings() { Line 111  function profile_csv_admin_settings() {
111  }  }
112    
113  function profile_csv_perm() {  function profile_csv_perm() {
114    return array (PROFILE_CSV_PERM_DOWNLOAD);    return array(PROFILE_CSV_PERM_DOWNLOAD);
115  }  }
116    
117  function profile_csv_menu($may_cache) {  function profile_csv_menu() {
118    $items = array();    $items = array();
119    
120    $items[] = array(    $items['admin/settings/profile_csv'] = array(
121        'path' => 'admin/settings/profile_csv',        'title' => 'profile csv',
122        'title' => t('profile csv'),        'description' => 'profile csv settings',
123        'description' => t('profile csv settings.'),        'page callback' => 'drupal_get_form',
124        'callback' => 'drupal_get_form',        'page arguments' => array('profile_csv_admin_settings'),
125        'callback arguments' => array('profile_csv_admin_settings'),        'access callback' => 'user_access',
126        'access' => user_access('administer site configuration'),        'access arguments' => array('administer site configuration'),
127        'type' => MENU_NORMAL_ITEM, // optional        'type' => MENU_NORMAL_ITEM, // optional
128    );    );
129        $items['profile_csv'] = array(
130    if ($may_cache) {        'title' => 'Profile Export CSV',
131      $items[] = array(        'page callback' => 'profile_csv_page',
132        'path'     => 'profile_csv',        'access callback' => 'user_access',
133        'title'    => t('Profile Export CSV'),        'access arguments' => array(PROFILE_CSV_PERM_DOWNLOAD),
134        'callback' => 'profile_csv_page',        'type' => MENU_NORMAL_ITEM,
       'access'   => user_access(PROFILE_CSV_PERM_DOWNLOAD),  
       'type'     => MENU_NORMAL_ITEM,  
135      );      );
   }  
136    return $items;    return $items;
137  }  }
138    
139  function profile_csv_page() {  function profile_csv_page() {
140    
141    $data = _profile_csv_header();    $data = _profile_csv_header();
142    $user_status = variable_get(PROFILE_CSV_STATUS,2);    $user_status = variable_get(PROFILE_CSV_STATUS, 2);
143    if (variable_get(PROFILE_CSV_ROLE . '2', 0)) {    if (variable_get(PROFILE_CSV_ROLE .'2', 0)) {
144      $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");
145      while ($row = db_fetch_object($result)) {      while ($row = db_fetch_object($result)) {
146        if ($user_status == 2){        if ($user_status == 2) {
147          $data .= _profile_csv_format_user($row->uid );          $data .= _profile_csv_format_user($row->uid );
148        }        }
149        else{        else{
# Line 157  function profile_csv_page() { Line 153  function profile_csv_page() {
153        }        }
154      }      }
155    }    }
156    else{    else {
157      $result = db_query("SELECT u.uid, u.status, ur.rid  FROM {users} u      $result = db_query("SELECT u.uid, u.status, ur.rid  FROM {users} u
158              INNER JOIN {users_roles} ur ON u.uid = ur.uid              INNER JOIN {users_roles} ur ON u.uid = ur.uid
159              WHERE u.uid > 1");              WHERE u.uid > 1");
160      $prev_uid = 0;      $prev_uid = 0;
161      while ($row = db_fetch_object($result)) {      while ($row = db_fetch_object($result)) {
162        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)) {
163          if ($user_status == 2){          if ($user_status == 2) {
164            $data .= _profile_csv_format_user($row->uid );            $data .= _profile_csv_format_user($row->uid );
165          }          }
166          else{          else{
# Line 191  function _profile_csv_get_profile_fields Line 187  function _profile_csv_get_profile_fields
187      $fields = array();      $fields = array();
188      $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');      $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');
189      while ($row = db_fetch_object($result)) {      while ($row = db_fetch_object($result)) {
190        if (variable_get(PROFILE_CSV_PARAM . 'profile_' . $row->fid, 0)) {        if (variable_get(PROFILE_CSV_PARAM .'profile_'. $row->fid, 0)) {
191          $fields[] = array('name' => $row->name, 'title' => $row->title, 'type' => $row->type, 'visibility' => $row->visibility);          $fields[] = array('name' => $row->name, 'title' => $row->title, 'type' => $row->type, 'visibility' => $row->visibility);
192        }        }
193      }      }
# Line 199  function _profile_csv_get_profile_fields Line 195  function _profile_csv_get_profile_fields
195    return $fields;    return $fields;
196  }  }
197    
   
198  function _profile_csv_format_user($uid = 0) {  function _profile_csv_format_user($uid = 0) {
199    $user_data = _profile_csv_get_user($uid);    $user_data = _profile_csv_get_user($uid);
200    $profile_data = _profile_csv_get_profile($uid,$user_data['data']);    $profile_data = _profile_csv_get_profile($uid, $user_data['data']);
201    unset($user_data['data']);    unset($user_data['data']);
202    $info = array_merge($user_data, $profile_data);    $info = array_merge($user_data, $profile_data);
203    //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
204    
205    foreach($info as $value) {    foreach ($info as $value) {
206      $new_info[] = '"' . str_replace('"', '""', $value) . '"';      $new_info[] = '"'. str_replace('"', '""', $value) .'"';
207    }    }
208    if (isset($new_info)) {    if (isset($new_info)) {
209      $line = implode(",", $new_info);      $line = implode(",", $new_info);
210    }    }
211    $data .= trim($line) . "\n";    $data .= trim($line) ."\n";
212    
213   return $data;   return $data;
214  }  }
# Line 222  function _profile_csv_get_user($uid = 0) Line 217  function _profile_csv_get_user($uid = 0)
217     $users = array();     $users = array();
218     $result = db_query('SELECT u.uid, u.name, u.mail, u.data FROM {users} u WHERE u.uid = %d', $uid);     $result = db_query('SELECT u.uid, u.name, u.mail, u.data FROM {users} u WHERE u.uid = %d', $uid);
219     while ($row = db_fetch_object($result)) {     while ($row = db_fetch_object($result)) {
220      if (variable_get(PROFILE_CSV_PARAM . 'uid', 0))      if (variable_get(PROFILE_CSV_PARAM .'uid', 0))
221        $users[] = $row->uid;        $users[] = $row->uid;
222    
223      if (variable_get(PROFILE_CSV_PARAM . 'name', 0))      if (variable_get(PROFILE_CSV_PARAM .'name', 0))
224        $users[] = $row->name;        $users[] = $row->name;
225    
226      if (variable_get(PROFILE_CSV_PARAM . 'mail', 0))      if (variable_get(PROFILE_CSV_PARAM .'mail', 0))
227        $users[] = $row->mail;        $users[] = $row->mail;
228        $users['data'] = unserialize($row->data);        $users['data'] = unserialize($row->data);
229    }    }
230    return $users;    return $users;
231  }  }
232    
233  function _profile_csv_get_profile($uid=0,$user_data=NULL) {  function _profile_csv_get_profile($uid=0, $user_data=NULL) {
234    $profile_fields = _profile_csv_get_profile_fields();    $profile_fields = _profile_csv_get_profile_fields();
235    $profile_result = array();    $profile_result = array();
236    foreach($profile_fields  as $profile_field) {    foreach ($profile_fields  as $profile_field) {
237      if ($profile_field ['visibility'] == 4) {      if ($profile_field ['visibility'] == 4) {
238        //$value = try to get it from the $user_data        //$value = try to get it from the $user_data
239        $value = $user_data[$profile_field['name']];        $value = $user_data[$profile_field['name']];
# Line 248  function _profile_csv_get_profile($uid=0 Line 243  function _profile_csv_get_profile($uid=0
243          WHERE pv.fid = pf.fid          WHERE pv.fid = pf.fid
244          AND pf.name = '%s'          AND pf.name = '%s'
245          AND pv.uid = %d", $profile_field['name'], $uid));          AND pv.uid = %d", $profile_field['name'], $uid));
246      }      }
247    
248      if ($profile_field['type'] == 'date') {      if ($profile_field['type'] == 'date') {
249        if ($value != 0) {        if ($value !== 0) {
250          $value = unserialize($value);          $value = unserialize($value);
251          $value = $value['year'] .'-'. $value['month'] .'-'. $value['day'];          $value = $value['year'] .'-'. $value['month'] .'-'. $value['day'];
252        }        }
# Line 263  function _profile_csv_get_profile($uid=0 Line 259  function _profile_csv_get_profile($uid=0
259  function _profile_csv_header() {  function _profile_csv_header() {
260    $row = array();    $row = array();
261    
262    if (variable_get(PROFILE_CSV_PARAM . 'uid', 0))    if (variable_get(PROFILE_CSV_PARAM .'uid', 0))
263      $row[] = '"uid"';      $row[] = '"uid"';
264    
265    if (variable_get(PROFILE_CSV_PARAM . 'name', 0))    if (variable_get(PROFILE_CSV_PARAM .'name', 0))
266      $row[] = '"name"';      $row[] = '"name"';
267    
268    if (variable_get(PROFILE_CSV_PARAM . 'mail', 0))    if (variable_get(PROFILE_CSV_PARAM .'mail', 0))
269      $row[] = '"mail"';      $row[] = '"mail"';
270    
271    foreach(_profile_csv_get_profile_fields() as $field) {    foreach (_profile_csv_get_profile_fields() as $field) {
272      $row[] = '"'. $field['title'] .'"';      $row[] = '"'. $field['title'] .'"';
273    }    }
274    

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.10

  ViewVC Help
Powered by ViewVC 1.1.2