home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / CMS / drupal-6.0.exe / drupal-6.0 / modules / profile / profile.module < prev    next >
Encoding:
Text File  |  2008-02-03  |  20.7 KB  |  576 lines

  1. <?php
  2. // $Id: profile.module,v 1.236 2008/02/03 19:36:46 goba 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($path, $arg) {
  33.   switch ($path) {
  34.     case 'admin/help#profile':
  35.       $output = '<p>'. t('The profile module allows custom fields (such as country, full name, or age) to be defined and displayed in the <em>My Account</em> section. This permits users of a site to share more information about themselves, and can help community-based sites organize users around specific information.') .'</p>';
  36.       $output .= '<p>'. t('The following types of fields can be added to a user profile:') .'</p>';
  37.       $output .= '<ul><li>'. t('single-line textfield') .'</li>';
  38.       $output .= '<li>'. t('multi-line textfield') .'</li>';
  39.       $output .= '<li>'. t('checkbox') .'</li>';
  40.       $output .= '<li>'. t('list selection') .'</li>';
  41.       $output .= '<li>'. t('freeform list') .'</li>';
  42.       $output .= '<li>'. t('URL') .'</li>';
  43.       $output .= '<li>'. t('date') .'</li></ul>';
  44.       $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@profile">Profile module</a>.', array('@profile' => 'http://drupal.org/handbook/modules/profile/')) .'</p>';
  45.       return $output;
  46.     case 'admin/user/profile':
  47.       return '<p>'. t("This page displays a list of the existing custom profile fields to be displayed on a user's <em>My Account</em> page. To provide structure, similar or related fields may be placed inside a category. To add a new category (or edit an existing one), edit a profile field and provide a new category name. To change the category of a field or the order of fields within a category, grab a drag-and-drop handle under the Title column and drag the field to a new location in the list. (Grab a handle by clicking and holding the mouse while hovering over a handle icon.) Remember that your changes will not be saved until you click the <em>Save configuration</em> button at the bottom of the page.") .'</p>';
  48.   }
  49. }
  50.  
  51. /**
  52.  * Implementation of hook_theme()
  53.  */
  54. function profile_theme() {
  55.   return array(
  56.     'profile_block' => array(
  57.       'arguments' => array('account' => NULL, 'fields' => array()),
  58.       'template' => 'profile-block',
  59.     ),
  60.     'profile_listing' => array(
  61.       'arguments' => array('account' => NULL, 'fields' => array()),
  62.       'template' => 'profile-listing',
  63.     ),
  64.     'profile_wrapper' => array(
  65.       'arguments' => array('content' => NULL),
  66.       'template' => 'profile-wrapper',
  67.     ),
  68.     'profile_admin_overview' => array(
  69.       'arguments' => array('form' => NULL),
  70.       'file' => 'profile.admin.inc',
  71.     )
  72.   );
  73. }
  74.  
  75. /**
  76.  * Implementation of hook_menu().
  77.  */
  78. function profile_menu() {
  79.   $items['profile'] = array(
  80.     'title' => 'User list',
  81.     'page callback' => 'profile_browse',
  82.     'access arguments' => array('access user profiles'),
  83.     'type' => MENU_SUGGESTED_ITEM,
  84.     'file' => 'profile.pages.inc',
  85.   );
  86.   $items['admin/user/profile'] = array(
  87.     'title' => 'Profiles',
  88.     'description' => 'Create customizable fields for your users.',
  89.     'page callback' => 'drupal_get_form',
  90.     'page arguments' => array('profile_admin_overview'),
  91.     'access arguments' => array('administer users'),
  92.     'file' => 'profile.admin.inc',
  93.   );
  94.   $items['admin/user/profile/add'] = array(
  95.     'title' => 'Add field',
  96.     'page callback' => 'drupal_get_form',
  97.     'page arguments' => array('profile_field_form'),
  98.     'type' => MENU_CALLBACK,
  99.     'file' => 'profile.admin.inc',
  100.   );
  101.   $items['admin/user/profile/autocomplete'] = array(
  102.     'title' => 'Profile category autocomplete',
  103.     'page callback' => 'profile_admin_settings_autocomplete',
  104.     'type' => MENU_CALLBACK,
  105.     'file' => 'profile.admin.inc',
  106.   );
  107.   $items['admin/user/profile/edit'] = array(
  108.     'title' => 'Edit field',
  109.     'page callback' => 'drupal_get_form',
  110.     'page arguments' => array('profile_field_form'),
  111.     'type' => MENU_CALLBACK,
  112.     'file' => 'profile.admin.inc',
  113.   );
  114.   $items['admin/user/profile/delete'] = array(
  115.     'title' => 'Delete field',
  116.     'page callback' => 'drupal_get_form',
  117.     'page arguments' => array('profile_field_delete'),
  118.     'type' => MENU_CALLBACK,
  119.     'file' => 'profile.admin.inc',
  120.   );
  121.   $items['profile/autocomplete'] = array(
  122.     'title' => 'Profile autocomplete',
  123.     'page callback' => 'profile_autocomplete',
  124.     'access arguments' => array('access user profiles'),
  125.     'type' => MENU_CALLBACK,
  126.     'file' => 'profile.pages.inc',
  127.   );
  128.   return $items;
  129. }
  130.  
  131. /**
  132.  * Implementation of hook_block().
  133.  */
  134. function profile_block($op = 'list', $delta = 0, $edit = array()) {
  135.  
  136.   if ($op == 'list') {
  137.     $blocks[0]['info'] = t('Author information');
  138.     $blocks[0]['cache'] = BLOCK_CACHE_PER_PAGE | BLOCK_CACHE_PER_ROLE;
  139.     return $blocks;
  140.   }
  141.   else if ($op == 'configure' && $delta == 0) {
  142.     // Compile a list of fields to show
  143.     $fields = array();
  144.     $result = db_query('SELECT name, title, weight, visibility FROM {profile_fields} WHERE visibility IN (%d, %d) ORDER BY weight', PROFILE_PUBLIC, PROFILE_PUBLIC_LISTINGS);
  145.     while ($record = db_fetch_object($result)) {
  146.       $fields[$record->name] = check_plain($record->title);
  147.     }
  148.     $fields['user_profile'] = t('Link to full user profile');
  149.     $form['profile_block_author_fields'] = array('#type' => 'checkboxes',
  150.       '#title' => t('Profile fields to display'),
  151.       '#default_value' => variable_get('profile_block_author_fields', NULL),
  152.       '#options' => $fields,
  153.       '#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'))),
  154.     );
  155.     return $form;
  156.   }
  157.   else if ($op == 'save' && $delta == 0) {
  158.     variable_set('profile_block_author_fields', $edit['profile_block_author_fields']);
  159.   }
  160.   else if ($op == 'view') {
  161.     if (user_access('access user profiles')) {
  162.       $output = '';
  163.       if ((arg(0) == 'node') && is_numeric(arg(1)) && (arg(2) == NULL)) {
  164.         $node = node_load(arg(1));
  165.         $account = user_load(array('uid' => $node->uid));
  166.  
  167.         if ($use_fields = variable_get('profile_block_author_fields', array())) {
  168.           // Compile a list of fields to show.
  169.           $fields = array();
  170.           $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);
  171.           while ($record = db_fetch_object($result)) {
  172.             // Ensure that field is displayed only if it is among the defined block fields and, if it is private, the user has appropriate permissions.
  173.             if (isset($use_fields[$record->name]) && $use_fields[$record->name]) {
  174.               $fields[] = $record;
  175.             }
  176.           }
  177.         }
  178.  
  179.         if (!empty($fields)) {
  180.           $profile = _profile_update_user_fields($fields, $account);
  181.           $output .= theme('profile_block', $account, $profile, TRUE);
  182.         }
  183.  
  184.         if (isset($use_fields['user_profile']) && $use_fields['user_profile']) {
  185.           $output .= '<div>'. l(t('View full user profile'), 'user/'. $account->uid) .'</div>';
  186.         }
  187.       }
  188.  
  189.       if ($output) {
  190.         $block['subject'] = t('About %name', array('%name' => $account->name));
  191.         $block['content'] = $output;
  192.         return $block;
  193.       }
  194.     }
  195.   }
  196. }
  197.  
  198. /**
  199.  * Implementation of hook_user().
  200.  */
  201. function profile_user($type, &$edit, &$user, $category = NULL) {
  202.   switch ($type) {
  203.     case 'load':
  204.       return profile_load_profile($user);
  205.     case 'register':
  206.       return profile_form_profile($edit, $user, $category, TRUE);
  207.     case 'update':
  208.     return profile_save_profile($edit, $user, $category);
  209.     case 'insert':
  210.       return profile_save_profile($edit, $user, $category, TRUE);
  211.     case 'view':
  212.       return profile_view_profile($user);
  213.     case 'form':
  214.       return profile_form_profile($edit, $user, $category);
  215.     case 'validate':
  216.       return profile_validate_profile($edit, $category);
  217.     case 'categories':
  218.       return profile_categories();
  219.     case 'delete':
  220.       db_query('DELETE FROM {profile_values} WHERE uid = %d', $user->uid);
  221.   }
  222. }
  223.  
  224. function profile_load_profile(&$user) {
  225.   $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);
  226.   while ($field = db_fetch_object($result)) {
  227.     if (empty($user->{$field->name})) {
  228.       $user->{$field->name} = _profile_field_serialize($field->type) ? unserialize($field->value) : $field->value;
  229.     }
  230.   }
  231. }
  232.  
  233. function profile_save_profile(&$edit, &$user, $category, $register = FALSE) {
  234.   $result = _profile_get_fields($category, $register);
  235.   while ($field = db_fetch_object($result)) {
  236.     if (_profile_field_serialize($field->type)) {
  237.       $edit[$field->name] = serialize($edit[$field->name]);
  238.     }
  239.     db_query("DELETE FROM {profile_values} WHERE fid = %d AND uid = %d", $field->fid, $user->uid);
  240.     db_query("INSERT INTO {profile_values} (fid, uid, value) VALUES (%d, %d, '%s')", $field->fid, $user->uid, $edit[$field->name]);
  241.     // Mark field as handled (prevents saving to user->data).
  242.     $edit[$field->name] = NULL;
  243.   }
  244. }
  245.  
  246. function profile_view_field($user, $field) {
  247.   // Only allow browsing of private fields for admins, if browsing is enabled,
  248.   // and if a user has permission to view profiles. Note that this check is
  249.   // necessary because a user may always see their own profile.
  250.   $browse = user_access('access user profiles')
  251.          && (user_access('administer users') || $field->visibility != PROFILE_PRIVATE)
  252.          && !empty($field->page);
  253.  
  254.   if (isset($user->{$field->name}) && $value = $user->{$field->name}) {
  255.     switch ($field->type) {
  256.       case 'textarea':
  257.         return check_markup($value);
  258.       case 'textfield':
  259.       case 'selection':
  260.         return $browse ? l($value, 'profile/'. $field->name .'/'. $value) : check_plain($value);
  261.       case 'checkbox':
  262.         return $browse ? l($field->title, 'profile/'. $field->name) : check_plain($field->title);
  263.       case 'url':
  264.         return '<a href="'. check_url($value) .'">'. check_plain($value) .'</a>';
  265.       case 'date':
  266.         $format = substr(variable_get('date_format_short', 'm/d/Y - H:i'), 0, 5);
  267.         // Note: Avoid PHP's date() because it does not handle dates before
  268.         // 1970 on Windows. This would make the date field useless for e.g.
  269.         // birthdays.
  270.         $replace = array(
  271.           'd' => sprintf('%02d', $value['day']),
  272.           'j' => $value['day'],
  273.           'm' => sprintf('%02d', $value['month']),
  274.           'M' => map_month($value['month']),
  275.           'Y' => $value['year'],
  276.           'H:i' => NULL,
  277.           'g:ia' => NULL,
  278.         );
  279.         return strtr($format, $replace);
  280.       case 'list':
  281.         $values = split("[,\n\r]", $value);
  282.         $fields = array();
  283.         foreach ($values as $value) {
  284.           if ($value = trim($value)) {
  285.             $fields[] = $browse ? l($value, 'profile/'. $field->name .'/'. $value) : check_plain($value);
  286.           }
  287.         }
  288.         return implode(', ', $fields);
  289.     }
  290.   }
  291. }
  292.  
  293. function profile_view_profile(&$user) {
  294.  
  295.   profile_load_profile($user);
  296.  
  297.   // Show private fields to administrators and people viewing their own account.
  298.   if (user_access('administer users') || $GLOBALS['user']->uid == $user->uid) {
  299.     $result = db_query('SELECT * FROM {profile_fields} WHERE visibility != %d ORDER BY category, weight', PROFILE_HIDDEN);
  300.   }
  301.   else {
  302.     $result = db_query('SELECT * FROM {profile_fields} WHERE visibility != %d AND visibility != %d ORDER BY category, weight', PROFILE_PRIVATE, PROFILE_HIDDEN);
  303.   }
  304.  
  305.   $fields = array();
  306.   while ($field = db_fetch_object($result)) {
  307.     if ($value = profile_view_field($user, $field)) {
  308.       $title = ($field->type != 'checkbox') ? check_plain($field->title) : NULL;
  309.  
  310.       // Create a single fieldset for each category.
  311.       if (!isset($user->content[$field->category])) {
  312.         $user->content[$field->category] = array(
  313.           '#type' => 'user_profile_category',
  314.           '#title' => $field->category,
  315.         );
  316.       }
  317.  
  318.       $user->content[$field->category][$field->name] = array(
  319.         '#type' => 'user_profile_item',
  320.         '#title' => $title,
  321.         '#value' => $value,
  322.         '#weight' => $field->weight,
  323.         '#attributes' => array('class' => 'profile-'. $field->name),
  324.       );
  325.     }
  326.   }
  327. }
  328.  
  329. function _profile_form_explanation($field) {
  330.   $output = $field->explanation;
  331.  
  332.   if ($field->type == 'list') {
  333.     $output .= ' '. t('Put each item on a separate line or separate them by commas. No HTML allowed.');
  334.   }
  335.  
  336.   if ($field->visibility == PROFILE_PRIVATE) {
  337.     $output .= ' '. t('The content of this field is kept private and will not be shown publicly.');
  338.   }
  339.  
  340.   return $output;
  341. }
  342.  
  343. function profile_form_profile($edit, $user, $category, $register = FALSE) {
  344.   $result = _profile_get_fields($category, $register);
  345.   $weight = 1;
  346.   $fields = array();
  347.   while ($field = db_fetch_object($result)) {
  348.     $category = $field->category;
  349.     if (!isset($fields[$category])) {
  350.       $fields[$category] = array('#type' => 'fieldset', '#title' => check_plain($category), '#weight' => $weight++);
  351.     }
  352.     switch ($field->type) {
  353.       case 'textfield':
  354.       case 'url':
  355.         $fields[$category][$field->name] = array('#type' => 'textfield',
  356.           '#title' => check_plain($field->title),
  357.           '#default_value' => isset($edit[$field->name]) ? $edit[$field->name] : '',
  358.           '#maxlength' => 255,
  359.           '#description' => _profile_form_explanation($field),
  360.           '#required' => $field->required,
  361.         );
  362.         if ($field->autocomplete) {
  363.           $fields[$category][$field->name]['#autocomplete_path'] = "profile/autocomplete/". $field->fid;
  364.         }
  365.         break;
  366.       case 'textarea':
  367.         $fields[$category][$field->name] = array('#type' => 'textarea',
  368.           '#title' => check_plain($field->title),
  369.           '#default_value' => isset($edit[$field->name]) ? $edit[$field->name] : '',
  370.           '#description' => _profile_form_explanation($field),
  371.           '#required' => $field->required,
  372.         );
  373.         break;
  374.       case 'list':
  375.         $fields[$category][$field->name] = array('#type' => 'textarea',
  376.           '#title' => check_plain($field->title),
  377.           '#default_value' => isset($edit[$field->name]) ? $edit[$field->name] : '',
  378.           '#description' => _profile_form_explanation($field),
  379.           '#required' => $field->required,
  380.         );
  381.         break;
  382.       case 'checkbox':
  383.         $fields[$category][$field->name] = array('#type' => 'checkbox',
  384.           '#title' => check_plain($field->title),
  385.           '#default_value' => isset($edit[$field->name]) ? $edit[$field->name] : '',
  386.           '#description' => _profile_form_explanation($field),
  387.           '#required' => $field->required,
  388.         );
  389.         break;
  390.       case 'selection':
  391.         $options = $field->required ? array() : array('--');
  392.         $lines = split("[,\n\r]", $field->options);
  393.         foreach ($lines as $line) {
  394.           if ($line = trim($line)) {
  395.             $options[$line] = $line;
  396.           }
  397.         }
  398.         $fields[$category][$field->name] = array('#type' => 'select',
  399.           '#title' => check_plain($field->title),
  400.           '#default_value' => isset($edit[$field->name]) ? $edit[$field->name] : '',
  401.           '#options' => $options,
  402.           '#description' => _profile_form_explanation($field),
  403.           '#required' => $field->required,
  404.         );
  405.         break;
  406.       case 'date':
  407.         $fields[$category][$field->name] = array('#type' => 'date',
  408.           '#title' => check_plain($field->title),
  409.           '#default_value' => isset($edit[$field->name]) ? $edit[$field->name] : '',
  410.           '#description' => _profile_form_explanation($field),
  411.           '#required' => $field->required,
  412.         );
  413.         break;
  414.     }
  415.   }
  416.   return $fields;
  417. }
  418.  
  419. /**
  420.  * Helper function: update an array of user fields by calling profile_view_field
  421.  */
  422. function _profile_update_user_fields($fields, $account) {
  423.   foreach ($fields as $key => $field) {
  424.     $fields[$key]->value = profile_view_field($account, $field);
  425.   }
  426.   return $fields;
  427. }
  428.  
  429. function profile_validate_profile($edit, $category) {
  430.   $result = _profile_get_fields($category);
  431.   while ($field = db_fetch_object($result)) {
  432.     if ($edit[$field->name]) {
  433.       if ($field->type == 'url') {
  434.         if (!valid_url($edit[$field->name], TRUE)) {
  435.           form_set_error($field->name, t('The value provided for %field is not a valid URL.', array('%field' => $field->title)));
  436.         }
  437.       }
  438.     }
  439.     else if ($field->required && !user_access('administer users')) {
  440.       form_set_error($field->name, t('The field %field is required.', array('%field' => $field->title)));
  441.     }
  442.   }
  443.  
  444.   return $edit;
  445. }
  446.  
  447. function profile_categories() {
  448.   $result = db_query("SELECT DISTINCT(category) FROM {profile_fields}");
  449.   $data = array();
  450.   while ($category = db_fetch_object($result)) {
  451.     $data[] = array(
  452.       'name' => $category->category,
  453.       'title' => $category->category,
  454.       'weight' => 3,
  455.       'access callback' => 'profile_category_access',
  456.       'access arguments' => array($category->category)
  457.     );
  458.   }
  459.   return $data;
  460. }
  461.  
  462. /**
  463.  * Menu item access callback - check if a user has access to a profile category.
  464.  */
  465. function profile_category_access($category) {
  466.   if (user_access('administer users')) {
  467.     return TRUE;
  468.   }
  469.   else {
  470.     return db_result(db_query("SELECT COUNT(*) FROM {profile_fields} WHERE category = '%s' AND visibility <> %d", $category, PROFILE_HIDDEN));
  471.   }
  472. }
  473.  
  474. /**
  475.  * Process variables for profile-block.tpl.php.
  476.  *
  477.  * The $variables array contains the following arguments:
  478.  * - $account
  479.  * - $fields
  480.  *
  481.  * @see profile-block.tpl.php
  482.  */
  483. function template_preprocess_profile_block(&$variables) {
  484.  
  485.   $variables['picture'] = theme('user_picture', $variables['account']);
  486.   $variables['profile'] = array();
  487.   // Supply filtered version of $fields that have values.
  488.   foreach ($variables['fields'] as $field) {
  489.     if ($field->value) {
  490.       $variables['profile'][$field->name]->title = check_plain($field->title);
  491.       $variables['profile'][$field->name]->value = $field->value;
  492.       $variables['profile'][$field->name]->type = $field->type;
  493.     }
  494.   }
  495.  
  496. }
  497.  
  498. /**
  499.  * Process variables for profile-listing.tpl.php.
  500.  *
  501.  * The $variables array contains the following arguments:
  502.  * - $account
  503.  * - $fields
  504.  *
  505.  * @see profile-listing.tpl.php
  506.  */
  507. function template_preprocess_profile_listing(&$variables) {
  508.  
  509.   $variables['picture'] = theme('user_picture', $variables['account']);
  510.   $variables['name'] = theme('username', $variables['account']);
  511.   $variables['profile'] = array();
  512.   // Supply filtered version of $fields that have values.
  513.   foreach ($variables['fields'] as $field) {
  514.     if ($field->value) {
  515.       $variables['profile'][$field->name]->title = $field->title;
  516.       $variables['profile'][$field->name]->value = $field->value;
  517.       $variables['profile'][$field->name]->type = $field->type;
  518.     }
  519.   }
  520.  
  521. }
  522.  
  523. /**
  524.  * Process variables for profile-wrapper.tpl.php.
  525.  *
  526.  * The $variables array contains the following arguments:
  527.  * - $content
  528.  *
  529.  * @see profile-wrapper.tpl.php
  530.  */
  531. function template_preprocess_profile_wrapper(&$variables) {
  532.   $variables['current_field'] = '';
  533.   if ($field = arg(1)) {
  534.     $variables['current_field'] = $field;
  535.     // Supply an alternate template suggestion based on the browsable field.
  536.     $variables['template_files'][] = 'profile-wrapper-'. $field;
  537.   }
  538. }
  539.  
  540. function _profile_field_types($type = NULL) {
  541.   $types = array('textfield' => t('single-line textfield'),
  542.                  'textarea' => t('multi-line textfield'),
  543.                  'checkbox' => t('checkbox'),
  544.                  'selection' => t('list selection'),
  545.                  'list' => t('freeform list'),
  546.                  'url' => t('URL'),
  547.                  'date' => t('date'));
  548.   return isset($type) ? $types[$type] : $types;
  549. }
  550.  
  551. function _profile_field_serialize($type = NULL) {
  552.   return $type == 'date';
  553. }
  554.  
  555. function _profile_get_fields($category, $register = FALSE) {
  556.   $args = array();
  557.   $sql = 'SELECT * FROM {profile_fields} WHERE ';
  558.   $filters = array();
  559.   if ($register) {
  560.     $filters[] = 'register = 1';
  561.   }
  562.   else {
  563.     // Use LOWER('%s') instead of PHP's strtolower() to avoid UTF-8 conversion issues.
  564.     $filters[] = "LOWER(category) = LOWER('%s')";
  565.     $args[] = $category;
  566.   }
  567.   if (!user_access('administer users')) {
  568.     $filters[] = 'visibility != %d';
  569.     $args[] = PROFILE_HIDDEN;
  570.   }
  571.   $sql .= implode(' AND ', $filters);
  572.   $sql .= ' ORDER BY category, weight';
  573.   return db_query($sql, $args);
  574. }
  575.  
  576.