Index: modules/profile.module =================================================================== RCS file: /cvs/drupal/drupal/modules/profile.module,v retrieving revision 1.88 diff -U3 -r1.88 profile.module --- modules/profile.module 18 Mar 2005 07:07:04 -0000 1.88 +++ modules/profile.module 24 Mar 2005 04:04:23 -0000 @@ -144,20 +144,30 @@ } 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); + $result = db_query('SELECT f.name, f.type, f.multiple, 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})) { + if ($field->multiple) { + $multiple_values[$field->name][] = $field->value; + } + elseif (empty($user->{$field->name})) { $user->{$field->name} = _profile_field_serialize($field->type) ? unserialize($field->value) : $field->value; } } + + // Add fields with multiple values. + foreach ($multiple_values as $name => $values) { + if (empty($user->{$name})) { + $user->{$name} = $values; + } + } } function profile_save_profile(&$edit, &$user, $category) { if (($_GET['q'] == 'user/register') ? 1 : 0) { - $result = db_query('SELECT fid, name, type FROM {profile_fields} WHERE register = 1 ORDER BY category, weight'); + $result = db_query('SELECT fid, name, type, multiple FROM {profile_fields} WHERE register = 1 ORDER BY category, weight'); } else { - $result = db_query("SELECT fid, name, type FROM {profile_fields} WHERE LOWER(category) = LOWER('%s')", $category); + $result = db_query("SELECT fid, name, type, multiple FROM {profile_fields} WHERE LOWER(category) = LOWER('%s')", $category); // We use LOWER('%s') instead of PHP's strtolower() to avoid UTF-8 conversion issues. } while ($field = db_fetch_object($result)) { @@ -165,7 +175,15 @@ $edit[$field->name] = serialize($edit[$field->name]); } 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]); + // Flatten out multiple selects. + if (is_array($edit[$field->name]) && $field->multiple) { + foreach ($edit[$field->name] as $key => $value) { + db_query("INSERT INTO {profile_values} (fid, uid, value) VALUES (%d, %d, '%s')", $field->fid, $user->uid, $value); + } + } + else { + 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). $edit[$field->name] = null; } @@ -182,7 +200,15 @@ case 'textarea': return check_output($value); case 'selection': - return $browse ? l(drupal_specialchars($value), "profile/$field->name/". check_url($value)) : drupal_specialchars($value); + if (is_array($value)) { + foreach($value as $v) { + $values[] = $browse ? l(drupal_specialchars($v), "profile/$field->name/". check_url($v)) : drupal_specialchars($v); + } + return implode(', ', $values); + } + else { + return $browse ? l(drupal_specialchars($value), "profile/$field->name/". check_url($value)) : drupal_specialchars($value); + } case 'checkbox': return $browse ? l(strip_tags($field->title), "profile/$field->name") : drupal_specialchars($field->title); case 'url': @@ -276,7 +302,7 @@ $fields[$category] .= form_checkbox($field->title, $field->name, 1, $edit[$field->name], _profile_form_explanation($field), NULL, $field->required); break; case 'selection': - $options = array('--'); + $options[-1] = '--'; $lines = split("[,\n\r]", $field->options); foreach ($lines as $line) { if ($line = trim($line)) { @@ -284,7 +310,7 @@ } } - $fields[$category] .= form_select($field->title, $field->name, $edit[$field->name], $options, _profile_form_explanation($field), 0, 0, $field->required); + $fields[$category] .= form_select($field->title, $field->name, $edit[$field->name], $options, _profile_form_explanation($field), 0, $field->multiple, $field->required); break; case 'date': $fields[$category] .= _profile_date_field($field, $edit); @@ -448,7 +474,7 @@ } if (!form_get_errors()) { - db_query("INSERT INTO {profile_fields} (title, name, explanation, category, type, weight, required, register, visibility, options, page) VALUES ('%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, '%s', '%s')", $data['title'], $data['name'], $data['explanation'], $data['category'], $type, $data['weight'], $data['required'], $data['register'], $data['visibility'], $data['options'], $data['page']); + db_query("INSERT INTO {profile_fields} (title, name, explanation, category, type, weight, multiple, required, register, visibility, options, page) VALUES ('%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, %d, '%s', '%s')", $data['title'], $data['name'], $data['explanation'], $data['category'], $type, $data['weight'], $data['multiple'], $data['required'], $data['register'], $data['visibility'], $data['options'], $data['page']); cache_clear_all(); @@ -476,7 +502,7 @@ profile_validate_form($data); if (!form_get_errors()) { - db_query("UPDATE {profile_fields} SET title = '%s', name = '%s', explanation = '%s', category = '%s', weight = %d, required = %d, register = %d, visibility = %d, options = '%s', page = '%s' WHERE fid = %d", $data['title'], $data['name'], $data['explanation'], $data['category'], $data['weight'], $data['required'], $data['register'], $data['visibility'], $data['options'], $data['page'], $fid); + db_query("UPDATE {profile_fields} SET title = '%s', name = '%s', explanation = '%s', category = '%s', weight = %d, multiple = %d, required = %d, register = %d, visibility = %d, options = '%s', page = '%s' WHERE fid = %d", $data['title'], $data['name'], $data['explanation'], $data['category'], $data['weight'], $data['multiple'], $data['required'], $data['register'], $data['visibility'], $data['options'], $data['page'], $fid); cache_clear_all(); @@ -513,6 +539,7 @@ $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_checkbox(t('Multiple select'), 'multiple', 1, $edit['multiple'], t('Allows users to select more than one item in this field.')); } $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_radios(t('Visibility'), 'visibility', $edit['visibility'], array(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.')));