[ Index ]
 

Code source de Drupal 5.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/modules/profile/ -> profile.module (source)

   1  <?php
   2  // $Id: profile.module,v 1.189.2.8 2007/07/26 19:16:48 drumm Exp $
   3  
   4  /**
   5   * @file
   6   * Support for configurable user profiles.
   7   */
   8  
   9  /**
  10   * Private field, content only available to privileged users.
  11   */
  12  define('PROFILE_PRIVATE', 1);
  13  
  14  /**
  15   * Public field, content shown on profile page but not used on member list pages.
  16   */
  17  define('PROFILE_PUBLIC', 2);
  18  
  19  /**
  20   * Public field, content shown on profile page and on member list pages.
  21   */
  22  define('PROFILE_PUBLIC_LISTINGS', 3);
  23  
  24  /**
  25   * Hidden profile field, only accessible by administrators, modules and themes.
  26   */
  27  define('PROFILE_HIDDEN', 4);
  28  
  29  /**
  30   * Implementation of hook_help().
  31   */
  32  function profile_help($section) {
  33    switch ($section) {
  34      case 'admin/help#profile':
  35        $output = '<p>'. t('The profile module allows you to define custom fields (such as country, real name, age, ...) in the user profile. This permits users of a site to share more information about themselves, and can help community-based sites to organize users around profile fields.') .'</p>';
  36        $output .= t('<p>The following types of fields can be added to the user profile:</p>
  37  <ul>
  38  <li>single-line textfield</li>
  39  <li>multi-line textfield</li>
  40  <li>checkbox</li>
  41  <li>list selection</li>
  42  <li>freeform list</li>
  43  <li>URL</li>
  44  <li>date</li>
  45  </ul>
  46  ');
  47        $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="@profile">Profile page</a>.', array('@profile' => 'http://drupal.org/handbook/modules/profile/')) .'</p>';
  48        return $output;
  49      case 'admin/user/profile':
  50        return '<p>'. t('Here you can define custom fields that users can fill in as part of their user profile (such as <em>country</em>, <em>real name</em>, <em>age</em>, ...).') .'</p>';
  51    }
  52  }
  53  
  54  /**
  55   * Implementation of hook_menu().
  56   */
  57  function profile_menu($may_cache) {
  58    $items = array();
  59  
  60    if ($may_cache) {
  61      $items[] = array('path' => 'profile',
  62        'title' => t('User list'),
  63        'callback' => 'profile_browse',
  64        'access' => user_access('access user profiles'),
  65        'type' => MENU_SUGGESTED_ITEM);
  66      $items[] = array('path' => 'admin/user/profile',
  67        'title' => t('Profiles'),
  68        'description' => t('Create customizable fields for your users.'),
  69        'callback' => 'profile_admin_overview');
  70      $items[] = array('path' => 'admin/user/profile/add',
  71        'title' => t('Add field'),
  72        'callback' => 'drupal_get_form',
  73        'callback arguments' => array('profile_field_form'),
  74        'type' => MENU_CALLBACK);
  75      $items[] = array('path' => 'admin/user/profile/autocomplete',
  76        'title' => t('Profile category autocomplete'),
  77        'callback' => 'profile_admin_settings_autocomplete',
  78        'access' => user_access('administer users'),
  79        'type' => MENU_CALLBACK);
  80      $items[] = array('path' => 'admin/user/profile/edit',
  81        'title' => t('Edit field'),
  82        'callback' => 'drupal_get_form',
  83        'callback arguments' => array('profile_field_form'),
  84        'type' => MENU_CALLBACK);
  85      $items[] = array('path' => 'admin/user/profile/delete',
  86        'title' => t('Delete field'),
  87        'callback' => 'drupal_get_form',
  88        'callback arguments' => array('profile_field_delete'),
  89        'type' => MENU_CALLBACK);
  90      $items[] = array('path' => 'profile/autocomplete', 'title' => t('Profile autocomplete'),
  91        'callback' => 'profile_autocomplete',
  92        'access' => 1,
  93        'type' => MENU_CALLBACK);
  94    }
  95  
  96    return $items;
  97  }
  98  
  99  /**
 100   * Implementation of hook_block().
 101   */
 102  function profile_block($op = 'list', $delta = 0, $edit = array()) {
 103  
 104    if ($op == 'list') {
 105       $blocks[0]['info'] = t('Author information');
 106  
 107       return $blocks;
 108    }
 109    else if ($op == 'configure' && $delta == 0) {
 110      // Compile a list of fields to show
 111      $fields = array();
 112      $result = db_query('SELECT name, title, weight, visibility FROM {profile_fields} WHERE visibility IN (%d, %d) ORDER BY weight', PROFILE_PUBLIC, PROFILE_PUBLIC_LISTINGS);
 113      while ($record = db_fetch_object($result)) {
 114        $fields[$record->name] = check_plain($record->title);
 115      }
 116      $fields['user_profile'] = t('Link to full user profile');
 117      $form['profile_block_author_fields'] = array('#type' => 'checkboxes',
 118        '#title' => t('Profile fields to display'),
 119        '#default_value' => variable_get('profile_block_author_fields', NULL),
 120        '#options' => $fields,
 121        '#description' => t('Select which profile fields you wish to display in the block. Only fields designated as public in the <a href="@profile-admin">profile field configuration</a> are available.', array('@profile-admin' => url('admin/user/profile'))),
 122      );
 123      return $form;
 124    }
 125    else if ($op == 'save' && $delta == 0) {
 126      variable_set('profile_block_author_fields', $edit['profile_block_author_fields']);
 127    }
 128    else if ($op == 'view') {
 129      if (user_access('access user profiles')) {
 130        if ((arg(0) == 'node') && is_numeric(arg(1)) && (arg(2) == NULL)) {
 131          $node = node_load(arg(1));
 132          $account = user_load(array('uid' => $node->uid));
 133  
 134          if ($use_fields = variable_get('profile_block_author_fields', array())) {
 135            // Compile a list of fields to show.
 136            $fields = array();
 137            $result = db_query('SELECT name, title, type, visibility, weight FROM {profile_fields} WHERE visibility IN (%d, %d) ORDER BY weight', PROFILE_PUBLIC, PROFILE_PUBLIC_LISTINGS);
 138            while ($record = db_fetch_object($result)) {
 139              // Ensure that field is displayed only if it is among the defined block fields and, if it is private, the user has appropriate permissions.
 140              if (isset($use_fields[$record->name]) && $use_fields[$record->name]) {
 141                $fields[] = $record;
 142              }
 143            }
 144          }
 145  
 146          if ($fields) {
 147            $profile = _profile_update_user_fields($fields, $account);
 148            $output .= theme('profile_block', $account, $profile, TRUE);
 149          }
 150  
 151          if (isset($use_fields['user_profile']) && $use_fields['user_profile']) {
 152            $output .= '<div>'. l(t('View full user profile'), 'user/'. $account->uid) .'</div>';
 153          }
 154        }
 155  
 156        if ($output) {
 157           $block['subject'] = t('About %name', array('%name' => $account->name));
 158           $block['content'] = $output;
 159           return $block;
 160        }
 161      }
 162    }
 163  }
 164  
 165  /**
 166   * Implementation of hook_user().
 167   */
 168  function profile_user($type, &$edit, &$user, $category = NULL) {
 169    switch ($type) {
 170      case 'load':
 171        return profile_load_profile($user);
 172      case 'register':
 173        return profile_form_profile($edit, $user, $category, TRUE);
 174      case 'update':
 175        return profile_save_profile($edit, $user, $category);
 176      case 'insert':
 177        return profile_save_profile($edit, $user, $category, TRUE);
 178      case 'view':
 179        return profile_view_profile($user);
 180      case 'form':
 181        return profile_form_profile($edit, $user, $category);
 182      case 'validate':
 183        return profile_validate_profile($edit, $category);
 184      case 'categories':
 185        return profile_categories();
 186      case 'delete':
 187        db_query('DELETE FROM {profile_values} WHERE uid = %d', $user->uid);
 188    }
 189  }
 190  
 191  /**
 192   * Menu callback: Generate a form to add/edit a user profile field.
 193   */
 194  function profile_field_form($arg = NULL) {
 195    if (arg(3) == 'edit') {
 196      if (is_numeric($arg)) {
 197        $fid = $arg;
 198  
 199        $edit = db_fetch_array(db_query('SELECT * FROM {profile_fields} WHERE fid = %d', $fid));
 200  
 201        if (!$edit) {
 202          drupal_not_found();
 203          return;
 204        }
 205        drupal_set_title(t('edit %title', array('%title' => $edit['title'])));
 206        $form['fid'] = array('#type' => 'value',
 207          '#value' => $fid,
 208        );
 209        $type = $edit['type'];
 210      }
 211      else {
 212        drupal_not_found();
 213        return;
 214      }
 215    }
 216    else {
 217      $types = _profile_field_types();
 218      if (!isset($types[$arg])) {
 219        drupal_not_found();
 220        return;
 221      }
 222      $type = $arg;
 223      drupal_set_title(t('add new %type', array('%type' => $types[$type])));
 224      $edit = array('name' => 'profile_');
 225      $form['type'] = array('#type' => 'value', '#value' => $type);
 226    }
 227    $form['fields'] = array('#type' => 'fieldset',
 228      '#title' => t('Field settings'),
 229    );
 230    $form['fields']['category'] = array('#type' => 'textfield',
 231      '#title' => t('Category'),
 232      '#default_value' => $edit['category'],
 233      '#autocomplete_path' => 'admin/user/profile/autocomplete',
 234      '#description' => t('The category the new field should be part of. Categories are used to group fields logically. An example category is "Personal information".'),
 235      '#required' => TRUE,
 236    );
 237    $form['fields']['title'] = array('#type' => 'textfield',
 238      '#title' => t('Title'),
 239      '#default_value' => $edit['title'],
 240      '#description' => t('The title of the new field. The title will be shown to the user. An example title is "Favorite color".'),
 241      '#required' => TRUE,
 242    );
 243    $form['fields']['name'] = array('#type' => 'textfield',
 244      '#title' => t('Form name'),
 245      '#default_value' => $edit['name'],
 246      '#description' => t('The name of the field. The form name is not shown to the user but used internally in the HTML code and URLs.
 247  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".'),
 248      '#required' => TRUE,
 249    );
 250    $form['fields']['explanation'] = array('#type' => 'textarea',
 251      '#title' => t('Explanation'),
 252      '#default_value' => $edit['explanation'],
 253      '#description' => t('An optional explanation to go with the new field. The explanation will be shown to the user.'),
 254    );
 255    if ($type == 'selection') {
 256      $form['fields']['options'] = array('#type' => 'textarea',
 257        '#title' => t('Selection options'),
 258        '#default_value' => $edit['options'],
 259        '#description' => t('A list of all options. Put each option on a separate line. Example options are "red", "blue", "green", etc.'),
 260      );
 261    }
 262    $form['fields']['weight'] = array('#type' => 'weight',
 263      '#title' => t('Weight'),
 264      '#default_value' => $edit['weight'],
 265      '#delta' => 5,
 266      '#description' => t('The weights define the order in which the form fields are shown. Lighter fields "float up" towards the top of the category.'),
 267    );
 268    $form['fields']['visibility'] = array('#type' => 'radios',
 269      '#title' => t('Visibility'),
 270      '#default_value' => isset($edit['visibility']) ? $edit['visibility'] : PROFILE_PUBLIC,
 271      '#options' => array(PROFILE_HIDDEN => t('Hidden profile field, only accessible by administrators, modules and themes.'), PROFILE_PRIVATE => t('Private field, content only available to privileged users.'), PROFILE_PUBLIC => t('Public field, content shown on profile page but not used on member list pages.'), PROFILE_PUBLIC_LISTINGS => t('Public field, content shown on profile page and on member list pages.')),
 272    );
 273    if ($type == 'selection' || $type == 'list' || $type == 'textfield') {
 274      $form['fields']['page'] = array('#type' => 'textfield',
 275        '#title' => t('Page title'),
 276        '#default_value' => $edit['page'],
 277        '#description' => t('To enable browsing this field by value, enter a title for the resulting page. The word <code>%value</code> will be substituted with the corresponding value. An example page title is "People whose favorite color is %value". This is only applicable for a public field.'),
 278      );
 279    }
 280    else if ($type == 'checkbox') {
 281      $form['fields']['page'] = array('#type' => 'textfield',
 282        '#title' => t('Page title'),
 283        '#default_value' => $edit['page'],
 284        '#description' => t('To enable browsing this field by value, enter a title for the resulting page. An example page title is "People who are employed". This is only applicable for a public field.'),
 285      );
 286    }
 287    $form['fields']['autocomplete'] = array('#type' => 'checkbox',
 288      '#title' => t('Form will auto-complete while user is typing.'),
 289      '#default_value' => $edit['autocomplete'],
 290    );
 291    $form['fields']['required'] = array('#type' => 'checkbox',
 292      '#title' => t('The user must enter a value.'),
 293      '#default_value' => $edit['required'],
 294    );
 295    $form['fields']['register'] = array('#type' => 'checkbox',
 296      '#title' => t('Visible in user registration form.'),
 297      '#default_value' => $edit['register'],
 298    );
 299    $form['submit'] = array('#type' => 'submit',
 300      '#value' => t('Save field'),
 301    );
 302    return $form;
 303  }
 304  
 305  /**
 306   * Validate profile_field_form submissions.
 307   */
 308  function profile_field_form_validate($form_id, $form_values) {
 309    // Validate the 'field name':
 310    if (preg_match('/[^a-zA-Z0-9_-]/', $form_values['name'])) {
 311      form_set_error('name', t('The specified form name contains one or more illegal characters. Spaces or any other special characters except dash (-) and underscore (_) are not allowed.'));
 312    }
 313  
 314    if (in_array($form_values['name'], user_fields())) {
 315      form_set_error('name', t('The specified form name is reserved for use by Drupal.'));
 316    }
 317    // Validate the category:
 318    if (!$form_values['category']) {
 319      form_set_error('category', t('You must enter a category.'));
 320    }
 321    if ($form_values['category'] == 'account') {
 322      form_set_error('category', t('The specified category name is reserved for use by Drupal.'));
 323    }
 324    $args1 = array($form_values['title'], $form_values['category']);
 325    $args2 = array($form_values['name']);
 326    $query_suffix = '';
 327  
 328    if (isset($form_values['fid'])) {
 329      $args1[] = $args2[] = $form_values['fid'];
 330      $query_suffix = ' AND fid != %d';
 331    }
 332  
 333    if (db_result(db_query("SELECT fid FROM {profile_fields} WHERE title = '%s' AND category = '%s'". $query_suffix, $args1))) {
 334      form_set_error('title', t('The specified title is already in use.'));
 335    }
 336    if (db_result(db_query("SELECT fid FROM {profile_fields} WHERE name = '%s'". $query_suffix, $args2))) {
 337      form_set_error('name', t('The specified name is already in use.'));
 338    }
 339  }
 340  
 341  /**
 342   * Process profile_field_form submissions.
 343   */
 344  function profile_field_form_submit($form_id, $form_values) {
 345    if (!isset($form_values['fid'])) {
 346      db_query("INSERT INTO {profile_fields} (title, name, explanation, category, type, weight, required, register, visibility, autocomplete, options, page) VALUES ('%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, %d, '%s', '%s')", $form_values['title'], $form_values['name'], $form_values['explanation'], $form_values['category'], $form_values['type'], $form_values['weight'], $form_values['required'], $form_values['register'], $form_values['visibility'], $form_values['autocomplete'], $form_values['options'], $form_values['page']);
 347  
 348      drupal_set_message(t('The field has been created.'));
 349      watchdog('profile', t('Profile field %field added under category %category.', array('%field' => $form_values['title'], '%category' => $form_values['category'])), WATCHDOG_NOTICE, l(t('view'), 'admin/user/profile'));
 350    }
 351    else {
 352      db_query("UPDATE {profile_fields} SET title = '%s', name = '%s', explanation = '%s', category = '%s', weight = %d, required = %d, register = %d, visibility = %d, autocomplete = %d, options = '%s', page = '%s' WHERE fid = %d", $form_values['title'], $form_values['name'], $form_values['explanation'], $form_values['category'], $form_values['weight'], $form_values['required'], $form_values['register'], $form_values['visibility'], $form_values['autocomplete'], $form_values['options'], $form_values['page'], $form_values['fid']);
 353  
 354      drupal_set_message(t('The field has been updated.'));
 355    }
 356    cache_clear_all();
 357  
 358    return 'admin/user/profile';
 359  }
 360  
 361  /**
 362   * Menu callback; deletes a field from all user profiles.
 363   */
 364  function profile_field_delete($fid) {
 365    $field = db_fetch_object(db_query("SELECT title FROM {profile_fields} WHERE fid = %d", $fid));
 366    if (!$field) {
 367      drupal_not_found();
 368      return;
 369    }
 370    $form['fid'] = array('#type' => 'value', '#value' => $fid);
 371    $form['title'] = array('#type' => 'value', '#value' => $field->title);
 372  
 373    return confirm_form($form,
 374      t('Are you sure you want to delete the field %field?', array('%field' => $field->title)), 'admin/user/profile',
 375      t('This action cannot be undone. If users have entered values into this field in their profile, these entries will also be deleted. If you want to keep the user-entered data, instead of deleting the field you may wish to <a href="@edit-field">edit this field</a> and change it to a hidden profile field so that it may only be accessed by administrators.', array('@edit-field' => url('admin/user/profile/edit/'. $fid))),
 376      t('Delete'), t('Cancel'));
 377  }
 378  
 379  /**
 380   * Process a field delete form submission.
 381   */
 382  function profile_field_delete_submit($form_id, $form_values) {
 383    db_query('DELETE FROM {profile_fields} WHERE fid = %d', $form_values['fid']);
 384    db_query('DELETE FROM {profile_values} WHERE fid = %d', $form_values['fid']);
 385  
 386    cache_clear_all();
 387  
 388    drupal_set_message(t('The field %field has been deleted.', array('%field' => $form_values['title'])));
 389    watchdog('profile', t('Profile field %field deleted.', array('%field' => $form_values['title'])), WATCHDOG_NOTICE, l(t('view'), 'admin/user/profile'));
 390  
 391    return 'admin/user/profile';
 392  }
 393  
 394  /**
 395   * Menu callback; display a listing of all editable profile fields.
 396   */
 397  function profile_admin_overview() {
 398  
 399    $result = db_query('SELECT title, name, type, category, fid FROM {profile_fields} ORDER BY category, weight');
 400    $rows = array();
 401    while ($field = db_fetch_object($result)) {
 402      $rows[] = array(check_plain($field->title), check_plain($field->name), _profile_field_types($field->type), check_plain($field->category), l(t('edit'), "admin/user/profile/edit/$field->fid"), l(t('delete'), "admin/user/profile/delete/$field->fid"));
 403    }
 404    if (count($rows) == 0) {
 405      $rows[] = array(array('data' => t('No fields defined.'), 'colspan' => '6'));
 406    }
 407  
 408    $header = array(t('Title'), t('Name'), t('Type'), t('Category'), array('data' => t('Operations'), 'colspan' => '2'));
 409  
 410    $output  = theme('table', $header, $rows);
 411    $output .= '<h2>'. t('Add new field') .'</h2>';
 412    $output .= '<ul>';
 413    foreach (_profile_field_types() as $key => $value) {
 414      $output .= '<li>'. l($value, "admin/user/profile/add/$key") .'</li>';
 415    }
 416    $output .= '</ul>';
 417  
 418    return $output;
 419  }
 420  
 421  /**
 422   * Menu callback; display a list of user information.
 423   */
 424  function profile_browse() {
 425    $name = arg(1);
 426    list(, , $value) = explode('/', $_GET['q'], 3);
 427  
 428    $field = db_fetch_object(db_query("SELECT DISTINCT(fid), type, title, page, visibility FROM {profile_fields} WHERE name = '%s'", $name));
 429  
 430    if ($name && $field->fid) {
 431      // Only allow browsing of fields that have a page title set.
 432      if (empty($field->page)) {
 433        drupal_not_found();
 434        return;
 435      }
 436      // Do not allow browsing of private and hidden fields by non-admins.
 437      if (!user_access('administer users') && ($field->visibility == PROFILE_PRIVATE || $field->visibility == PROFILE_HIDDEN)) {
 438         drupal_access_denied();
 439         return;
 440      }
 441  
 442      // Compile a list of fields to show.
 443      $fields = array();
 444      $result = db_query('SELECT name, title, type, weight, page FROM {profile_fields} WHERE fid != %d AND visibility = %d ORDER BY weight', $field->fid, PROFILE_PUBLIC_LISTINGS);
 445      while ($record = db_fetch_object($result)) {
 446        $fields[] = $record;
 447      }
 448  
 449      // Determine what query to use:
 450      $arguments = array($field->fid);
 451      switch ($field->type) {
 452        case 'checkbox':
 453          $query = 'v.value = 1';
 454          break;
 455        case 'textfield':
 456        case 'selection':
 457          $query = "v.value = '%s'";
 458          $arguments[] = $value;
 459          break;
 460        case 'list':
 461          $query = "v.value LIKE '%%%s%%'";
 462          $arguments[] = $value;
 463          break;
 464        default:
 465          drupal_not_found();
 466          return;
 467      }
 468  
 469      // Extract the affected users:
 470      $result = pager_query("SELECT u.uid, u.access FROM {users} u INNER JOIN {profile_values} v ON u.uid = v.uid WHERE v.fid = %d AND $query AND u.access != 0 AND u.status != 0 ORDER BY u.access DESC", 20, 0, NULL, $arguments);
 471  
 472      $output = '<div id="profile">';
 473      while ($account = db_fetch_object($result)) {
 474        $account = user_load(array('uid' => $account->uid));
 475        $profile = _profile_update_user_fields($fields, $account);
 476        $output .= theme('profile_listing', $account, $profile);
 477      }
 478      $output .= theme('pager', NULL, 20);
 479  
 480      if ($field->type == 'selection' || $field->type == 'list' || $field->type == 'textfield') {
 481        $title = strtr(check_plain($field->page), array('%value' => theme('placeholder', $value)));
 482      }
 483      else {
 484        $title = check_plain($field->page);
 485      }
 486      $output .= '</div>';
 487  
 488      drupal_set_title($title);
 489      return $output;
 490    }
 491    else if ($name && !$field->fid) {
 492      drupal_not_found();
 493    }
 494    else {
 495      // Compile a list of fields to show.
 496      $fields = array();
 497      $result = db_query('SELECT name, title, type, weight, page FROM {profile_fields} WHERE visibility = %d ORDER BY category, weight', PROFILE_PUBLIC_LISTINGS);
 498      while ($record = db_fetch_object($result)) {
 499        $fields[] = $record;
 500      }
 501  
 502      // Extract the affected users:
 503      $result = pager_query('SELECT uid, access FROM {users} WHERE uid > 0 AND status != 0 AND access != 0 ORDER BY access DESC', 20, 0, NULL);
 504  
 505      $output = '<div id="profile">';
 506      while ($account = db_fetch_object($result)) {
 507        $account = user_load(array('uid' => $account->uid));
 508        $profile = _profile_update_user_fields($fields, $account);
 509        $output .= theme('profile_listing', $account, $profile);
 510      }
 511      $output .= '</div>';
 512      $output .= theme('pager', NULL, 20);
 513  
 514      drupal_set_title(t('User list'));
 515      return $output;
 516    }
 517  }
 518  
 519  function profile_load_profile(&$user) {
 520    $result = db_query('SELECT f.name, f.type, v.value FROM {profile_fields} f INNER JOIN {profile_values} v ON f.fid = v.fid WHERE uid = %d', $user->uid);
 521    while ($field = db_fetch_object($result)) {
 522      if (empty($user->{$field->name})) {
 523        $user->{$field->name} = _profile_field_serialize($field->type) ? unserialize($field->value) : $field->value;
 524      }
 525    }
 526  }
 527  
 528  function profile_save_profile(&$edit, &$user, $category, $register = FALSE) {
 529    $result = _profile_get_fields($category, $register);
 530    while ($field = db_fetch_object($result)) {
 531      if (_profile_field_serialize($field->type)) {
 532         $edit[$field->name] = serialize($edit[$field->name]);
 533      }
 534      db_query("DELETE FROM {profile_values} WHERE fid = %d AND uid = %d", $field->fid, $user->uid);
 535      db_query("INSERT INTO {profile_values} (fid, uid, value) VALUES (%d, %d, '%s')", $field->fid, $user->uid, $edit[$field->name]);
 536      // Mark field as handled (prevents saving to user->data).
 537      $edit[$field->name] = NULL;
 538    }
 539  }
 540  
 541  function profile_view_field($user, $field) {
 542    // Only allow browsing of private fields for admins, if browsing is enabled,
 543    // and if a user has permission to view profiles. Note that this check is
 544    // necessary because a user may always see their own profile.
 545    $browse = user_access('access user profiles')
 546           && (user_access('administer users') || $field->visibility != PROFILE_PRIVATE)
 547           && !empty($field->page);
 548  
 549    if ($value = $user->{$field->name}) {
 550      switch ($field->type) {
 551        case 'textarea':
 552          return check_markup($value);
 553        case 'textfield':
 554        case 'selection':
 555          return $browse ? l($value, 'profile/'. $field->name .'/'. $value) : check_plain($value);
 556        case 'checkbox':
 557          return $browse ? l($field->title, 'profile/'. $field->name) : check_plain($field->title);
 558        case 'url':
 559          return '<a href="'. check_url($value) .'">'. check_plain($value) .'</a>';
 560        case 'date':
 561          $format = substr(variable_get('date_format_short', 'm/d/Y - H:i'), 0, 5);
 562          // Note: Avoid PHP's date() because it does not handle dates before
 563          // 1970 on Windows. This would make the date field useless for e.g.
 564          // birthdays.
 565          $replace = array('d' => sprintf('%02d', $value['day']),
 566                           'j' => $value['day'],
 567                           'm' => sprintf('%02d', $value['month']),
 568                           'M' => map_month($value['month']),
 569                           'Y' => $value['year'],
 570                           'H:i' => NULL,
 571                           'g:ia' => NULL);
 572          return strtr($format, $replace);
 573        case 'list':
 574          $values = split("[,\n\r]", $value);
 575          $fields = array();
 576          foreach ($values as $value) {
 577            if ($value = trim($value)) {
 578              $fields[] = $browse ? l($value, 'profile/'. $field->name .'/'. $value) : check_plain($value);
 579            }
 580          }
 581          return implode(', ', $fields);
 582      }
 583    }
 584  }
 585  
 586  function profile_view_profile($user) {
 587  
 588    profile_load_profile($user);
 589  
 590    // Show private fields to administrators and people viewing their own account.
 591    if (user_access('administer users') || $GLOBALS['user']->uid == $user->uid) {
 592      $result = db_query('SELECT * FROM {profile_fields} WHERE visibility != %d ORDER BY category, weight', PROFILE_HIDDEN);
 593    }
 594    else {
 595      $result = db_query('SELECT * FROM {profile_fields} WHERE visibility != %d AND visibility != %d ORDER BY category, weight', PROFILE_PRIVATE, PROFILE_HIDDEN);
 596    }
 597  
 598    while ($field = db_fetch_object($result)) {
 599      if ($value = profile_view_field($user, $field)) {
 600        $title = ($field->type != 'checkbox') ? check_plain($field->title) : NULL;
 601        $item = array('title' => $title,
 602          'value' => $value,
 603          'class' => $field->name,
 604        );
 605        $fields[$field->category][$field->name] = $item;
 606      }
 607    }
 608    return $fields;
 609  }
 610  
 611  function _profile_form_explanation($field) {
 612    $output = $field->explanation;
 613  
 614    if ($field->type == 'list') {
 615      $output .= ' '. t('Put each item on a separate line or separate them by commas. No HTML allowed.');
 616    }
 617  
 618    if ($field->visibility == PROFILE_PRIVATE) {
 619      $output .= ' '. t('The content of this field is kept private and will not be shown publicly.');
 620    }
 621  
 622    return $output;
 623  }
 624  
 625  function profile_form_profile($edit, $user, $category, $register = FALSE) {
 626    $result = _profile_get_fields($category, $register);
 627    $w = 1;
 628    while ($field = db_fetch_object($result)) {
 629      $category = $field->category;
 630      if (!isset($fields[$category])) {
 631        $fields[$category] = array('#type' => 'fieldset', '#title' => check_plain($category), '#weight' => $w++);
 632      }
 633      switch ($field->type) {
 634        case 'textfield':
 635        case 'url':
 636          $fields[$category][$field->name] = array('#type' => 'textfield',
 637            '#title' => check_plain($field->title),
 638            '#default_value' => $edit[$field->name],
 639            '#maxlength' => 255,
 640            '#description' => _profile_form_explanation($field),
 641            '#required' => $field->required,
 642          );
 643          if ($field->autocomplete) {
 644            $fields[$category][$field->name]['#autocomplete_path'] = "profile/autocomplete/". $field->fid;
 645          }
 646          break;
 647        case 'textarea':
 648          $fields[$category][$field->name] = array('#type' => 'textarea',
 649            '#title' => check_plain($field->title),
 650            '#default_value' => $edit[$field->name],
 651            '#description' => _profile_form_explanation($field),
 652            '#required' => $field->required,
 653          );
 654          break;
 655        case 'list':
 656          $fields[$category][$field->name] = array('#type' => 'textarea',
 657            '#title' => check_plain($field->title),
 658            '#default_value' => $edit[$field->name],
 659            '#description' => _profile_form_explanation($field),
 660            '#required' => $field->required,
 661          );
 662          break;
 663        case 'checkbox':
 664          $fields[$category][$field->name] = array('#type' => 'checkbox',
 665            '#title' => check_plain($field->title),
 666            '#default_value' => $edit[$field->name],
 667            '#description' => _profile_form_explanation($field),
 668            '#required' => $field->required,
 669          );
 670          break;
 671        case 'selection':
 672          $options = $field->required ? array() : array('--');
 673          $lines = split("[,\n\r]", $field->options);
 674          foreach ($lines as $line) {
 675            if ($line = trim($line)) {
 676              $options[$line] = $line;
 677            }
 678          }
 679          $fields[$category][$field->name] = array('#type' => 'select',
 680            '#title' => check_plain($field->title),
 681            '#default_value' => $edit[$field->name],
 682            '#options' => $options,
 683            '#description' => _profile_form_explanation($field),
 684            '#required' => $field->required,
 685          );
 686          break;
 687        case 'date':
 688          $fields[$category][$field->name] = array('#type' => 'date',
 689            '#title' => check_plain($field->title),
 690            '#default_value' => $edit[$field->name],
 691            '#description' => _profile_form_explanation($field),
 692            '#required' => $field->required,
 693          );
 694          break;
 695      }
 696    }
 697    return $fields;
 698  }
 699  
 700  /**
 701   * Callback to allow autocomplete of profile text fields.
 702   */
 703  function profile_autocomplete($field, $string) {
 704    if (db_result(db_query("SELECT COUNT(*) FROM {profile_fields} WHERE fid = %d AND autocomplete = 1", $field))) {
 705      $matches = array();
 706      $result = db_query_range("SELECT value FROM {profile_values} WHERE fid = %d AND LOWER(value) LIKE LOWER('%s%%') GROUP BY value ORDER BY value ASC", $field, $string, 0, 10);
 707      while ($data = db_fetch_object($result)) {
 708        $matches[$data->value] = check_plain($data->value);
 709      }
 710  
 711      print drupal_to_js($matches);
 712    }
 713    exit();
 714  }
 715  
 716  /**
 717   * Helper function: update an array of user fields by calling profile_view_field
 718   */
 719  function _profile_update_user_fields($fields, $account) {
 720    foreach ($fields as $key => $field) {
 721      $fields[$key]->value = profile_view_field($account, $field);
 722    }
 723    return $fields;
 724  }
 725  
 726  function profile_validate_profile($edit, $category) {
 727    $result = _profile_get_fields($category);
 728    while ($field = db_fetch_object($result)) {
 729      if ($edit[$field->name]) {
 730        if ($field->type == 'url') {
 731          if (!valid_url($edit[$field->name], TRUE)) {
 732            form_set_error($field->name, t('The value provided for %field is not a valid URL.', array('%field' => $field->title)));
 733          }
 734        }
 735      }
 736      else if ($field->required && !user_access('administer users')) {
 737        form_set_error($field->name, t('The field %field is required.', array('%field' => $field->title)));
 738      }
 739    }
 740  
 741    return $edit;
 742  }
 743  
 744  function profile_categories() {
 745    $result = db_query("SELECT DISTINCT(category) FROM {profile_fields}");
 746    while ($category = db_fetch_object($result)) {
 747      $data[] = array('name' => $category->category, 'title' => $category->category, 'weight' => 3);
 748    }
 749    return $data;
 750  }
 751  
 752  function theme_profile_block($account, $fields = array()) {
 753  
 754    $output .= theme('user_picture', $account);
 755  
 756    foreach ($fields as $field) {
 757      if ($field->value) {
 758        if ($field->type == 'checkbox') {
 759          $output .= "<p>$field->value</p>\n";
 760        }
 761        else {
 762          $output .= '<p><strong>'. check_plain($field->title) ."</strong><br />$field->value</p>\n";
 763        }
 764      }
 765    }
 766  
 767    return $output;
 768  }
 769  
 770  function theme_profile_listing($account, $fields = array()) {
 771  
 772    $output  = "<div class=\"profile\">\n";
 773    $output .= theme('user_picture', $account);
 774    $output .= ' <div class="name">'. theme('username', $account) ."</div>\n";
 775  
 776    foreach ($fields as $field) {
 777      if ($field->value) {
 778        $output .= " <div class=\"field\">$field->value</div>\n";
 779      }
 780    }
 781  
 782    $output .= "</div>\n";
 783  
 784    return $output;
 785  }
 786  
 787  function _profile_field_types($type = NULL) {
 788    $types = array('textfield' => t('single-line textfield'),
 789                   'textarea' => t('multi-line textfield'),
 790                   'checkbox' => t('checkbox'),
 791                   'selection' => t('list selection'),
 792                   'list' => t('freeform list'),
 793                   'url' => t('URL'),
 794                   'date' => t('date'));
 795    return isset($type) ? $types[$type] : $types;
 796  }
 797  
 798  function _profile_field_serialize($type = NULL) {
 799    return $type == 'date';
 800  }
 801  
 802  function _profile_get_fields($category, $register = FALSE) {
 803    $args = array();
 804    $sql = 'SELECT * FROM {profile_fields} WHERE ';
 805    $filters = array();
 806    if ($register) {
 807      $filters[] = 'register = 1';
 808    }
 809    else {
 810      // Use LOWER('%s') instead of PHP's strtolower() to avoid UTF-8 conversion issues.
 811      $filters[] = "LOWER(category) = LOWER('%s')";
 812      $args[] = $category;
 813    }
 814    if (!user_access('administer users')) {
 815      $filters[] = 'visibility != %d';
 816      $args[] = PROFILE_HIDDEN;
 817    }
 818    $sql .= implode(' AND ', $filters);
 819    $sql .= ' ORDER BY category, weight';
 820    return db_query($sql, $args);
 821  }
 822  
 823  /**
 824   * Retrieve a pipe delimited string of autocomplete suggestions for profile categories
 825   */
 826  function profile_admin_settings_autocomplete($string) {
 827    $matches = array();
 828    $result = db_query_range("SELECT category FROM {profile_fields} WHERE LOWER(category) LIKE LOWER('%s%%')", $string, 0, 10);
 829    while ($data = db_fetch_object($result)) {
 830      $matches[$data->category] = check_plain($data->category);
 831    }
 832    print drupal_to_js($matches);
 833    exit();
 834  }


Généré le : Fri Nov 30 16:20:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics