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

Contents of /contributions/modules/profile_csv/profile_csv.module

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


Revision 1.10 - (show annotations) (download) (as text)
Mon Jul 21 00:47:04 2008 UTC (16 months, 1 week ago) by kbahey
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
Changes since 1.9: +48 -52 lines
File MIME type: text/x-php
#281167 by jwolf, port to Drupal 6.
1 <?php
2 // $Id$
3
4
5 define('PROFILE_CSV_STATUS', 'profile_csv_status');
6 define('PROFILE_CSV_PARAM', 'profile_csv_param_');
7 define('PROFILE_CSV_ROLE', 'profile_csv_role');
8 define('PROFILE_CSV_PERM_DOWNLOAD', 'download profiles');
9
10 function profile_csv_help($path, $arg) {
11 switch ($path) {
12 case "admin/modules#description":
13 $output = t("Allows exporting profile data in comma separated variable format.");
14 break;
15 }
16 return $output;
17 }
18
19 function profile_csv_admin_settings() {
20
21 $set = 'roles';
22 $form[$set] = array(
23 '#type' => 'fieldset',
24 '#title' => t('Roles'),
25 '#description' => t('Select one or more roles.'),
26 '#collapsible' => true,
27 '#collapsed' => true,
28 );
29
30 $u_role = user_roles($membersonly = TRUE);
31 foreach ( $u_role as $rid => $name) {
32 $role = PROFILE_CSV_ROLE . $rid;
33 $form[$set][PROFILE_CSV_ROLE . $rid] = array(
34 '#type' => 'checkbox',
35 '#title' => $name,
36 '#return_value' => 1,
37 '#default_value' => variable_get($role, 0),
38 );
39 }
40 $set = 'status';
41 $form[$set] = array(
42 '#type' => 'fieldset',
43 '#title' => t('User status'),
44 '#description' => t('Select one status.'),
45 '#collapsible' => true,
46 '#collapsed' => true,
47 );
48 $form[$set][PROFILE_CSV_STATUS] = array(
49 '#type' => 'select',
50 '#default_value' => variable_get(PROFILE_CSV_STATUS, 2),
51 '#options' => array( 1 => t('active'), 0 => t('blocked'), 2 => t('both')),
52 '#description' => t(''),
53 );
54
55 $set = 'fields';
56 $form[$set] = array(
57 '#type' => 'fieldset',
58 '#title' => t('General'),
59 '#description' => t('Select one or more profile fields.'),
60 '#collapsible' => true,
61 '#collapsed' => true,
62 );
63 $form[$set][PROFILE_CSV_PARAM .'uid'] = array(
64 '#type' => 'checkbox',
65 '#title' => t('User ID'),
66 '#return_value' => 1,
67 '#default_value' => variable_get(PROFILE_CSV_PARAM .'uid', 0),
68 );
69 $form[$set][PROFILE_CSV_PARAM .'name'] = array(
70 '#type' => 'checkbox',
71 '#title' => t('User Name'),
72 '#return_value' => 1,
73 '#default_value' => variable_get(PROFILE_CSV_PARAM .'name', 0),
74 );
75 $form[$set][PROFILE_CSV_PARAM .'mail'] = array(
76 '#type' => 'checkbox',
77 '#title' => t('User Email'),
78 '#return_value' => 1,
79 '#default_value' => variable_get(PROFILE_CSV_PARAM .'mail', 0),
80 );
81
82 $set = 'profile';
83 $form[$set] = array(
84 '#type' => 'fieldset',
85 '#title' => t('Categories'),
86 '#description' => t('Select one or more profile fields from categories below.'),
87 '#collapsible' => true,
88 '#collapsed' => true,
89 );
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");
92 while ($row = db_fetch_object($result)) {
93 $fld = PROFILE_CSV_PARAM .'profile_'. $row->fid;
94
95 if (!isset($form[$set][$row->category])) {
96 $form[$set][$row->category] = array(
97 '#type' => 'fieldset',
98 '#title' => $row->category,
99 '#collapsible' => true,
100 );
101 }
102
103 $form[$set][$row->category][$fld] = array(
104 '#type' => 'checkbox',
105 '#title' => $row->title,
106 '#return_value' => 1,
107 '#default_value' => variable_get($fld, 0),
108 );
109 }
110 return system_settings_form($form);
111 }
112
113 function profile_csv_perm() {
114 return array(PROFILE_CSV_PERM_DOWNLOAD);
115 }
116
117 function profile_csv_menu() {
118 $items = array();
119
120 $items['admin/settings/profile_csv'] = array(
121 'title' => 'profile csv',
122 'description' => 'profile csv settings',
123 'page callback' => 'drupal_get_form',
124 'page arguments' => array('profile_csv_admin_settings'),
125 'access callback' => 'user_access',
126 'access arguments' => array('administer site configuration'),
127 'type' => MENU_NORMAL_ITEM, // optional
128 );
129 $items['profile_csv'] = array(
130 'title' => 'Profile Export CSV',
131 'page callback' => 'profile_csv_page',
132 'access callback' => 'user_access',
133 'access arguments' => array(PROFILE_CSV_PERM_DOWNLOAD),
134 'type' => MENU_NORMAL_ITEM,
135 );
136 return $items;
137 }
138
139 function profile_csv_page() {
140
141 $data = _profile_csv_header();
142 $user_status = variable_get(PROFILE_CSV_STATUS, 2);
143 if (variable_get(PROFILE_CSV_ROLE .'2', 0)) {
144 $result = db_query("SELECT u.uid, u.status FROM {users} u WHERE u.uid > 1");
145 while ($row = db_fetch_object($result)) {
146 if ($user_status == 2) {
147 $data .= _profile_csv_format_user($row->uid );
148 }
149 else{
150 if ($user_status == $row->status) {
151 $data .= _profile_csv_format_user($row->uid );
152 }
153 }
154 }
155 }
156 else {
157 $result = db_query("SELECT u.uid, u.status, ur.rid FROM {users} u
158 INNER JOIN {users_roles} ur ON u.uid = ur.uid
159 WHERE u.uid > 1");
160 $prev_uid = 0;
161 while ($row = db_fetch_object($result)) {
162 if ((variable_get(PROFILE_CSV_ROLE . $row->rid, 0)) && ($row->uid != $prev_uid)) {
163 if ($user_status == 2) {
164 $data .= _profile_csv_format_user($row->uid );
165 }
166 else{
167 if ($user_status == $row->status) {
168 $data .= _profile_csv_format_user($row->uid );
169 }
170 }
171 $prev_uid = $row->uid;
172 }
173 }
174 }
175 header("Content-type: text/plain; charset=UTF-8");
176 header("Content-Disposition: attachment; filename=userlist.csv");
177 header("Pragma: no-cache");
178 header("Expires: 0");
179 print $data;
180 exit();
181 }
182
183 function _profile_csv_get_profile_fields() {
184 static $fields;
185
186 if (!isset($fields)) {
187 $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');
189 while ($row = db_fetch_object($result)) {
190 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);
192 }
193 }
194 }
195 return $fields;
196 }
197
198 function _profile_csv_format_user($uid = 0) {
199 $user_data = _profile_csv_get_user($uid);
200 $profile_data = _profile_csv_get_profile($uid, $user_data['data']);
201 unset($user_data['data']);
202 $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
204
205 foreach ($info as $value) {
206 $new_info[] = '"'. str_replace('"', '""', $value) .'"';
207 }
208 if (isset($new_info)) {
209 $line = implode(",", $new_info);
210 }
211 $data .= trim($line) ."\n";
212
213 return $data;
214 }
215
216 function _profile_csv_get_user($uid = 0) {
217 $users = array();
218 $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)) {
220 if (variable_get(PROFILE_CSV_PARAM .'uid', 0))
221 $users[] = $row->uid;
222
223 if (variable_get(PROFILE_CSV_PARAM .'name', 0))
224 $users[] = $row->name;
225
226 if (variable_get(PROFILE_CSV_PARAM .'mail', 0))
227 $users[] = $row->mail;
228 $users['data'] = unserialize($row->data);
229 }
230 return $users;
231 }
232
233 function _profile_csv_get_profile($uid=0, $user_data=NULL) {
234 $profile_fields = _profile_csv_get_profile_fields();
235 $profile_result = array();
236 foreach ($profile_fields as $profile_field) {
237 if ($profile_field ['visibility'] == 4) {
238 //$value = try to get it from the $user_data
239 $value = $user_data[$profile_field['name']];
240 }
241 else {
242 $value = db_result(db_query("SELECT pv.value FROM {profile_fields} pf, {profile_values} pv
243 WHERE pv.fid = pf.fid
244 AND pf.name = '%s'
245 AND pv.uid = %d", $profile_field['name'], $uid));
246 }
247
248 if ($profile_field['type'] == 'date') {
249 if ($value !== 0) {
250 $value = unserialize($value);
251 $value = $value['year'] .'-'. $value['month'] .'-'. $value['day'];
252 }
253 }
254 $profile_result[] = $value;
255 }
256 return $profile_result;
257 }
258
259 function _profile_csv_header() {
260 $row = array();
261
262 if (variable_get(PROFILE_CSV_PARAM .'uid', 0))
263 $row[] = '"uid"';
264
265 if (variable_get(PROFILE_CSV_PARAM .'name', 0))
266 $row[] = '"name"';
267
268 if (variable_get(PROFILE_CSV_PARAM .'mail', 0))
269 $row[] = '"mail"';
270
271 foreach (_profile_csv_get_profile_fields() as $field) {
272 $row[] = '"'. $field['title'] .'"';
273 }
274
275 return implode(",", $row) ."\n";
276 }

  ViewVC Help
Powered by ViewVC 1.1.2