summaryrefslogtreecommitdiff
path: root/modules/profile.module
blob: d3b2c6201ab6b2d555ac20b1cf173f0f8aa4e8d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<?php
// $Id$

// TODO: add a 'date' field so we can migrate the birthday information.

/**
 * Implementation of hook_help().
 */
function profile_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Support for configurable user profiles.');
  }
}

/**
 * Implementation of hook_menu().
 */
function profile_menu() {
  $items = array();
  $items[] = array('path' => 'profile', 'title' => t('browse'),
    'callback' => 'profile_browse',
    'access' => TRUE,
    'type' => MENU_SUGGESTED_ITEM);
  $items[] = array('path' => 'admin/user/configure/profile', 'title' => t('profiles'),
    'callback' => 'profile_admin_overview',
    'access' => user_access('administer users'),
    'type' => MENU_LOCAL_SUBTASK);
  $items[] = array('path' => 'admin/user/configure/profile/add', 'title' => t('add field'),
    'callback' => 'profile_browse',
    'access' => user_access('administer users'),
    'type' => MENU_CALLBACK);
  $items[] = array('path' => 'admin/user/configure/profile/edit', 'title' => t('edit field'),
    'callback' => 'profile_browse',
    'access' => user_access('administer users'),
    'type' => MENU_CALLBACK);
  $items[] = array('path' => 'admin/user/configure/profile/delete', 'title' => t('delete field'),
    'callback' => 'profile_browse',
    'access' => user_access('administer users'),
    'type' => MENU_CALLBACK);
  return $items;
}

/**
 * Menu callback; display a list of user information.
 */
function profile_browse() {

  $name = strip_tags(arg(1));
  $value = strip_tags(arg(2));

  $field = db_fetch_object(db_query("SELECT DISTINCT(fid), type, title, page FROM {profile_fields} WHERE name = '%s'", $name));

  if ($field->fid) {
    // Compile a list of fields to show
    $fields = array();
    $result = db_query('SELECT name, title, type FROM {profile_fields} WHERE fid != %d AND overview = 1', $field->fid);
    while ($record = db_fetch_object($result)) {
      $fields[] = $record;
    }

    // Determine what query to use:
    switch ($field->type) {
      case 'checkbox':
        $query = 'v.value = 1';
        break;
      case 'selection':
        $query = "v.value = '". check_query($value) ."'";
        break;
      case 'list':
        $query = "v.value LIKE '%". check_query($value) ."%'";
        break;
    }

    // Extract the affected users:
    $result = pager_query("SELECT u.uid FROM {users} u INNER JOIN {profile_values} v ON u.uid = v.uid WHERE v.fid = $field->fid AND $query ORDER BY u.changed DESC", 20);

    $output = '<div id="profile">';
    while ($account = db_fetch_object($result)) {
      $output .= theme('profile_profile', user_load(array('uid' => $account->uid)), $fields);
    }
    $output .= theme('pager', NULL, 20);

    if ($field->type == 'selection' || $field->type == 'list') {
      $title = strtr($field->page, array('%value' => $value));
    }
    else {
      $title = $field->page;
    }
    $output .= '</div>';

    print theme('page', $output, $title);
  }
  else {
    drupal_not_found();
  }
}

function profile_load_profile(&$user) {
  $result = db_query('SELECT f.name, v.value FROM {profile_fields} f INNER JOIN {profile_values} v ON f.fid = v.fid WHERE uid = %d', $user->uid);
  while ($field = db_fetch_object($result)) {
    if (empty($user->{$field->name})) {
      $user->{$field->name} = $field->value;
    }
  }
}

function profile_save_profile(&$edit, &$user) {
  db_query('DELETE FROM {profile_values} WHERE uid = %d', $user->uid);
  $result = db_query('SELECT fid, name FROM {profile_fields}');
  while ($field = db_fetch_object($result)) {
    if ($edit[$field->name]) {
      db_query("INSERT INTO {profile_values} (fid, uid, value) VALUES (%d, %d, '%s')", $field->fid, $user->uid, $edit[$field->name]);
      unset($edit[$field->name], $user->{$field->name});
    }
  }
}

function profile_view_field($user, $field) {
  if ($value = $user->{$field->name}) {
    switch ($field->type) {
      case 'textfield':
      case 'textarea':
        return check_output($value);
      case 'selection':
        return l($value, "profile/$field->name/". htmlentities($value));
      case 'checkbox':
        return l($field->title, "profile/$field->name");
      case 'url':
        return '<a href="'. check_url(strip_tags($value)) .'">'. strip_tags($value) .'</a>';      case 'list':
        $values = split("[\n\r]", $value);
        $fields = array();
        foreach ($values as $value) {
          if ($value = trim(strip_tags($value))) {
            $fields[] = l($value, "profile/$field->name/". htmlentities($value));
          }
        }
        return implode(', ', $fields);
    }
  }
}

function profile_view_profile($user) {

  profile_load_profile($user);

  $result = db_query('SELECT * FROM {profile_fields} ORDER BY category, weight');
  while ($field = db_fetch_object($result)) {
    if ($value = profile_view_field($user, $field)) {
      if ($field->type == 'checkbox') {
        $fields[$field->category] .= "<p>$value</p>";
      }
      else {
        $fields[$field->category] .= form_item($field->title, check_output($value));
      }
    }
  }

  return $fields;
}

function profile_edit_profile($edit, $user) {

  $result = db_query('SELECT * FROM {profile_fields} ORDER BY category, weight');

  while ($field = db_fetch_object($result)) {
    switch ($field->type) {
      case 'textfield':
      case 'url':
        $fields[$field->category] .= form_textfield($field->title, $field->name, $edit[$field->name], 70, 255, $field->explanation, NULL, $field->required);
        break;
      case 'textarea':
        $fields[$field->category] .= form_textarea($field->title, $field->name, $edit[$field->name], 60, 5, $field->explanation, NULL, $field->required);
        break;
      case 'list':
        $fields[$field->category] .= form_textarea($field->title, $field->name, $edit[$field->name], 60, 5, $field->explanation .' '. t('Put each entry on a separate line.  No HTML allowed.'), NULL, $field->required);
        break;
      case 'checkbox':
        $fields[$field->category] .= form_checkbox($field->title, $field->name, 1, $edit[$field->name], $field->explanation, NULL, $field->required);
        break;
      case 'selection':
        $options = array('--');
        $lines = split("[\n\r]", $field->options);
        foreach ($lines as $line) {
          if ($line = trim($line)) {
            $options[$line] = $line;
          }
        }

        $fields[$field->category] .= form_select($field->title, $field->name, $edit[$field->name], $options, $field->explanation, 0, 0, $field->required);
        break;
    }
  }

  return $fields;
}

function profile_validate_profile($edit) {
  $result = db_query('SELECT * FROM {profile_fields} ORDER BY category, weight');

  while ($field = db_fetch_object($result)) {
    if ($edit[$field->name]) {
      if ($field->type == 'url' && !valid_url($edit[$field->name], true)) {
        form_set_error($field->name, t('The value provided for "%field" is not a valid URL.', array('%field' => $field->title)));
      }
    }
    else if ($field->required) {
      form_set_error($field->name, t('The field "%field" is required.', array('%field' => $field->title)));
    }
  }

  return $edit;
}

/**
 * Implementation of hook_user().
 */
function profile_user($type, &$edit, &$user) {
  switch ($type) {
    case 'load':
      return profile_load_profile($user);
    case 'update':
    case 'insert':
      return profile_save_profile($edit, $user);
    case 'view':
      return profile_view_profile($user);
    case 'form':
      return profile_edit_profile($edit, $user);
    case 'validate':
      return profile_validate_profile($edit);
  }
}

function profile_validate_form($edit) {

  // Validate the title:

  if (!$edit['title']) {
    form_set_error('title', t('You must enter a title.'));
  }

  // Validate the 'form name':

  if (eregi('[^a-z0-9_-]', $edit['name'])) {
    form_set_error('name', t('The specified form name contains one or more illegal characters.  Spaces or any other special characters expect dash (-) and underscore (_) are not allowed.'));
  }

  if (in_array($edit['name'], user_fields())) {
    form_set_error('name', t('The specified form name is reserved for use by Drupal.'));
  }

  // Validate the category:
  if (!$edit['category']) {
    form_set_error('category', t('You must enter a category.'));
  }
}

/**
 * Menu callback; adds a new field to all user profiles.
 */
function profile_admin_add($type) {
  $type = _profile_field_types($type);


  if ($_POST['op']) {
    $data = $_POST['edit'];

    // Validate the form:
    profile_validate_form($data);

    if (db_result(db_query("SELECT fid FROM {profile_fields} WHERE title = '%s'", $data['title']))) {
      form_set_error('title', t('the specified title is already in use.'));
    }

    if (db_result(db_query("SELECT fid FROM {profile_fields} WHERE name = '%s'", $data['name']))) {
      form_set_error('name', t('the specified name is already in use.'));
    }

    if (!form_has_errors()) {
      db_query("INSERT INTO {profile_fields} (title, name, explanation, category, type, weight, required, overview, options, page) VALUES ('%s', '%s', '%s', '%s', '%s', %d, %d, %d, '%s', '%s')", $data['title'], $data['name'], $data['explanation'], $data['category'], $type, $data['weight'], $data['required'], $data['overview'], $data['options'], $data['page']);

      drupal_set_message(t('the field has been created.'));
    }
  }
  else {
    $data = array('name' => 'profile_');
  }

  print theme('page', _profile_field_form($type, $data), t('Add new %type', array('%type' => $type)));
}

/**
 * Menu callback; displays the profile field editing form.
 */
function profile_admin_edit($fid) {

  if ($_POST['op']) {
    $data = $_POST['edit'];

    // Validate form:
    profile_validate_form($data);

    if (!form_has_errors()) {
      db_query("UPDATE {profile_fields} SET title = '%s', name = '%s', explanation = '%s', category = '%s', weight = %d, required = %d, overview = %d, options = '%s', page = '%s' WHERE fid = %d", $data['title'], $data['name'], $data['explanation'], $data['category'], $data['weight'], $data['required'], $data['overview'], $data['options'], $data['page'], $fid);

      drupal_set_message(t('the field has been updated.'));
    }
  }
  else {
    $data = db_fetch_array(db_query('SELECT * FROM {profile_fields} WHERE fid = %d', $fid));
  }

  print theme('page', _profile_field_form($data['type'], $data), t('Edit %type', array('%type' => $edit['type'])));
}

/**
 * Menu callback; deletes a field from all user profiles.
 */
function profile_admin_delete($fid) {
  db_query('DELETE FROM {profile_fields} WHERE fid = %d', $fid);
  drupal_set_message(t('the field has been deleted.'));
  print theme('page', '', t('Delete field'));
}

function _profile_field_form($type, $edit = array()) {

  $group  = form_textfield(t('Category'), 'category', $edit['category'], 70, 128, t('The category the new field should be part of.  Categories are used to group fields logically.  An example category is "Personal information".'));
  $group .= form_textfield(t('Title'), 'title', $edit['title'], 70, 128, t('The title of the new field.  The title will be shown to the user.  An example title is "Favorite color".'));
  $group .= form_textfield(t('Form name'), 'name', $edit['name'], 70, 128, t('The name of the field.  The form name is not shown to the user but used internally in the HTML code and URLs.
Unless you know what you are doing, it is highly recommended that you prefix the form name with <code>profile_</code> to avoid name clashes with other fields.  Spaces or any other special characters except dash (-) and underscore (_) are not allowed. An example name is "profile_favorite_color" or perhaps just "profile_color".'));
  $group .= form_textarea(t('Explanation'), 'explanation', $edit['explanation'], 70, 3, t('An optional explanation to go with the new field.  The explanation will be shown to the user.'));
  if ($type == 'selection') {
    $group .= form_textarea(t('Selection options'), 'options', $edit['options'], 70, 8, t('A list of all options.  Put each option on a separate line.  Example options are "red", "blue", "green", etc.'));
  }
  $group .= form_weight(t('Weight'), 'weight', $edit['weight'], 5, t('The weights define the order in which the form fields are shown.  Lighter fields "float up" towards the top of the category.'));
  $group .= form_checkbox(t('Required field.'), 'required', 1, $edit['required']);
  $output = form_group(t('Field settings'), $group);

  $group = '';
  if ($type == 'selection' || $type == 'list') {
    $group .= form_textfield(t('Page title'), 'page', $edit['page'], 70, 128, t('The title of the page showing all users with the specified field.  The word <code>%value</code> will be substituted with the corresponding value.  An example page title is "People whose favorite color is %value".'));
  }
  else {
    $group .= form_textfield(t('Page title'), 'page', $edit['page'], 70, 128, t('The title of the page showing all users with the specified field.'));
  }
  $group .= form_checkbox(t('Should this field be shown on the member listing pages.'), 'overview', 1, $edit['overview']);

  $output .= form_group(t('Browsability'), $group);
  $output .= form_submit(t('Save field'));

  return form($output);
}

/**
 * Menu callback; display a listing of all editable profile fields.
 */
function profile_admin_overview() {

  $result = db_query('SELECT * FROM {profile_fields} ORDER BY category, weight');
  while ($field = db_fetch_object($result)) {
    $rows[] = array($field->title, $field->name, $field->type, $field->category, l(t('edit'), "admin/user/configure/profile/edit/$field->fid"), l(t('delete'), "admin/user/configure/profile/delete/$field->fid"));
  }

  $header = array(t('title'), t('name'), t('type'), t('category'), array('data' => t('operations'), 'colspan' => '2'));

  $output  = theme('table', $header, $rows);
  $output .= '<h2>'. t('Create new field') .'</h2>';
  $output .= '<ul>';
  foreach (_profile_field_types() as $key => $value) {
    $output .= '<li>'. l(t('Add new %type', array('%type' => $value)), "admin/user/configure/profile/add/$key") .'</li>';
  }
  $output .= '</ul>';

  print theme('page', $output);
}

function theme_profile_profile($user, $fields = array()) {

  $output  = "<div class=\"profile\">\n";
  $output .= theme('user_picture', $user);
  $output .= ' <div class="name">'. format_name($user) ."</div>\n";

  foreach ($fields as $field) {
    if ($value = profile_view_field($user, $field)) {
      $output .= " <div class=\"field\">$value</div>\n";
    }
  }

  $output .= "</div>\n";

  return $output;
}

function _profile_field_types($type = NULL) {
  $types = array('textfield', 'textarea', 'checkbox', 'selection', 'list', 'url');
  return isset($type) ? $types[$type] : $types;
}

?>