Index: modules/profile/profile.module =================================================================== RCS file: /cvs/drupal/drupal/modules/profile/profile.module,v retrieving revision 1.222 diff -u -p -r1.222 profile.module --- modules/profile/profile.module 24 Oct 2007 13:27:12 -0000 1.222 +++ modules/profile/profile.module 26 Oct 2007 01:57:30 -0000 @@ -38,6 +38,8 @@ function profile_help($path, $arg) {
  • single-line textfield
  • multi-line textfield
  • checkbox
  • +
  • radio buttons
  • +
  • checkbox selection
  • list selection
  • freeform list
  • URL
  • @@ -285,7 +287,7 @@ Unless you know what you are doing, it i '#default_value' => $edit['explanation'], '#description' => t('An optional explanation to go with the new field. The explanation will be shown to the user.'), ); - if ($type == 'selection') { + if ($type == 'selection' || $type == 'radios' || $type == 'checkboxes') { $form['fields']['options'] = array('#type' => 'textarea', '#title' => t('Selection options'), '#default_value' => isset($edit['options']) ? $edit['options'] : '', @@ -303,7 +305,7 @@ Unless you know what you are doing, it i '#default_value' => isset($edit['visibility']) ? $edit['visibility'] : PROFILE_PUBLIC, '#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.')), ); - if ($type == 'selection' || $type == 'list' || $type == 'textfield') { + if ($type == 'selection' || $type == 'list' || $type == 'textfield' || $type == 'radios' || $type == 'checkboxes') { $form['fields']['page'] = array('#type' => 'textfield', '#title' => t('Page title'), '#default_value' => $edit['page'], @@ -504,9 +506,13 @@ function profile_browse() { break; case 'textfield': case 'selection': + case 'radios': $query = "v.value = '%s'"; $arguments[] = $value; break; + case 'checkboxes': + $query = "v.value LIKE '%%\"%s\"%%'"; + $arguments[] = $value; case 'list': $query = "v.value LIKE '%%%s%%'"; $arguments[] = $value; @@ -528,7 +534,7 @@ function profile_browse() { $output = theme('profile_wrapper', $content); $output .= theme('pager', NULL, 20); - if ($field->type == 'selection' || $field->type == 'list' || $field->type == 'textfield') { + if ($field->type == 'selection' || $field->type == 'list' || $field->type == 'textfield' || $field->type == 'radios') { $title = strtr(check_plain($field->page), array('%value' => theme('placeholder', $value))); } else { @@ -570,7 +576,7 @@ function profile_load_profile(&$user) { $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); while ($field = db_fetch_object($result)) { if (empty($user->{$field->name})) { - $user->{$field->name} = _profile_field_serialize($field->type) ? unserialize($field->value) : $field->value; + $user->{$field->name} = _profile_field_unserialize($field); } } } @@ -578,9 +584,7 @@ function profile_load_profile(&$user) { function profile_save_profile(&$edit, &$user, $category, $register = FALSE) { $result = _profile_get_fields($category, $register); while ($field = db_fetch_object($result)) { - if (_profile_field_serialize($field->type)) { - $edit[$field->name] = serialize($edit[$field->name]); - } + _profile_field_serialize($field,$edit); db_query("DELETE FROM {profile_values} WHERE fid = %d AND uid = %d", $field->fid, $user->uid); db_query("INSERT INTO {profile_values} (fid, uid, value) VALUES (%d, %d, '%s')", $field->fid, $user->uid, $edit[$field->name]); // Mark field as handled (prevents saving to user->data). @@ -600,6 +604,7 @@ function profile_view_field($user, $fiel switch ($field->type) { case 'textarea': return check_markup($value); + case 'radios': case 'textfield': case 'selection': return $browse ? l($value, 'profile/'. $field->name .'/'. $value) : check_plain($value); @@ -621,7 +626,8 @@ function profile_view_field($user, $fiel 'g:ia' => NULL); return strtr($format, $replace); case 'list': - $values = split("[,\n\r]", $value); + case 'checkboxes': + $values = is_array($value) ? $value : split("[,\n\r]", $value); $fields = array(); foreach ($values as $value) { if ($value = trim($value)) { @@ -730,15 +736,18 @@ function profile_form_profile($edit, $us '#required' => $field->required, ); break; + case 'checkboxes': + case 'radios': case 'selection': - $options = $field->required ? array() : array('--'); + $element_type = ('selection' == $field->type) ? 'select' : $field->type; + $options = ('selection' != $field->type || $field->required) ? array() : array('--'); $lines = split("[,\n\r]", $field->options); foreach ($lines as $line) { if ($line = trim($line)) { $options[$line] = $line; } } - $fields[$category][$field->name] = array('#type' => 'select', + $fields[$category][$field->name] = array('#type' => $element_type, '#title' => check_plain($field->title), '#default_value' => isset($edit[$field->name]) ? $edit[$field->name] : '', '#options' => $options, @@ -881,6 +890,8 @@ function _profile_field_types($type = NU $types = array('textfield' => t('single-line textfield'), 'textarea' => t('multi-line textfield'), 'checkbox' => t('checkbox'), + 'radios' => t('radio buttons'), + 'checkboxes' => t('checkbox selection'), 'selection' => t('list selection'), 'list' => t('freeform list'), 'url' => t('URL'), @@ -888,8 +899,35 @@ function _profile_field_types($type = NU return isset($type) ? $types[$type] : $types; } -function _profile_field_serialize($type = NULL) { - return $type == 'date'; +function _profile_field_unserialize($field) { + switch($field->type) { + case 'date': + return unserialize($field->value); + case 'checkboxes': + $values = unserialize($field->value); + $return = array(); + foreach($values as $value) { + $return[$value] = $value; + } + return $return; + } + return $field->value; +} + +function _profile_field_serialize($field,&$edit) { + switch($field->type) { + case 'date': + $edit[$field->name] = serialize($edit[$field->name]); + break; + case 'checkboxes': + $values = array(); + foreach($edit[$field->name] as $key => $value) { + if ($value) { + $values[] = $value; + } + } + $edit[$field->name] = serialize($values); + } } function _profile_get_fields($category, $register = FALSE) {