Commit 93cf70d7 authored by Dries's avatar Dries

- Profile module improvements: added a URL-type field to the profile module.

  We can use this for the 'URL of homepage' field on drupal.org.  URL fields
  are rendered as links and are being validated.
parent b5c441a9
...@@ -836,7 +836,7 @@ function update_80() { ...@@ -836,7 +836,7 @@ function update_80() {
array("MSN messenger ID", "msn", "textfield", NULL, 0), array("MSN messenger ID", "msn", "textfield", NULL, 0),
array("Yahoo messenger ID", "yahoo", "textfield", NULL, 0), array("Yahoo messenger ID", "yahoo", "textfield", NULL, 0),
array("AIM messenger ID", "aim", "textfield", NULL, 0), array("AIM messenger ID", "aim", "textfield", NULL, 0),
array("URL of homepage", "homepage", "textfield", NULL, 1), array("URL of homepage", "homepage", "url", NULL, 1),
array("Biography", "biography", "textarea", NULL, 0), array("Biography", "biography", "textarea", NULL, 0),
array("Interests", "interests", "textarea", NULL, 0), array("Interests", "interests", "textarea", NULL, 0),
array("Public key", "publickey", "textarea", NULL, 0) array("Public key", "publickey", "textarea", NULL, 0)
...@@ -880,16 +880,16 @@ function update_80() { ...@@ -880,16 +880,16 @@ function update_80() {
// Save the update record: // Save the update record:
user_save($account, $edit); user_save($account, $edit);
} }
return $ret; return $ret;
} }
function update_81() { function update_81() {
$ret[] = update_sql('ALTER TABLE {profile_fields} ADD page varchar(255) default NULL'); $ret[] = update_sql('ALTER TABLE {profile_fields} ADD page varchar(255) default NULL');
$ret[] = update_sql("UPDATE {profile_fields} SET type = 'url' WHERE name = 'homepage'");
return $ret; return $ret;
} }
function update_sql($sql) { function update_sql($sql) {
$edit = $_POST["edit"]; $edit = $_POST["edit"];
$result = db_query($sql); $result = db_query($sql);
......
...@@ -561,8 +561,13 @@ function valid_email_address($mail) { ...@@ -561,8 +561,13 @@ function valid_email_address($mail) {
* *
* @param $url an URL * @param $url an URL
*/ */
function valid_url($url) { function valid_url($url, $absolute = false) {
return preg_match("/^[a-zA-z0-9\/:_\-_\.,]+$/", $url); if ($absolute) {
return preg_match("/^http:\/\/[a-zA-z0-9\/:_\-_\.,]+$/", $url);
}
else {
return preg_match("/^[a-zA-z0-9\/:_\-_\.,]+$/", $url);
}
} }
function valid_input_data($data) { function valid_input_data($data) {
......
...@@ -86,23 +86,34 @@ function profile_save_profile($edit, $user) { ...@@ -86,23 +86,34 @@ function profile_save_profile($edit, $user) {
} }
} }
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/$value");
case 'checkbox':
return l($field->title, "profile/$field->name/");
case 'url':
return "<a href=\"". check_url(strip_tags($value)) ."\">". strip_tags($value) ."</a>";
}
}
}
function profile_view_profile($user) { function profile_view_profile($user) {
profile_load_profile(&$user); profile_load_profile(&$user);
$result = db_query('SELECT * FROM {profile_fields} ORDER BY category, weight'); $result = db_query('SELECT * FROM {profile_fields} ORDER BY category, weight');
while ($field = db_fetch_object($result)) { while ($field = db_fetch_object($result)) {
if ($value = $user->{$field->name}) { if ($value = profile_view_field($user, $field)) {
switch ($field->type) { if ($field->type == 'checkbox') {
case 'textfield': $output .= "<p>$value</p>";
case 'textarea': }
$output .= form_item($field->title, check_output($value)); else {
break; $output .= form_item($field->title, check_output($value));
case 'selection':
$output .= form_item($field->title, l($value, "profile/$field->name/$value"));
break;
case 'checkbox':
$output .= '<p>'. l($field->title, "profile/$field->name/") .'</p>';
} }
} }
} }
...@@ -117,6 +128,7 @@ function profile_edit_profile($edit, $user) { ...@@ -117,6 +128,7 @@ function profile_edit_profile($edit, $user) {
while ($field = db_fetch_object($result)) { while ($field = db_fetch_object($result)) {
switch ($field->type) { switch ($field->type) {
case 'textfield': case 'textfield':
case 'url':
$fields[$field->category] .= form_textfield($field->title, $field->name, $edit[$field->name], 70, 255, $field->explanation); $fields[$field->category] .= form_textfield($field->title, $field->name, $edit[$field->name], 70, 255, $field->explanation);
break; break;
case 'textarea': case 'textarea':
...@@ -142,6 +154,20 @@ function profile_edit_profile($edit, $user) { ...@@ -142,6 +154,20 @@ function profile_edit_profile($edit, $user) {
return $fields; 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 ($field->type == 'url') {
if ($edit[$field->name] && !valid_url($edit[$field->name], true)) {
return t("The value provided for '%field' is not a valid URL.", array('%field' => $field->title));
}
}
}
return $edit;
}
function profile_user($type, $edit, &$user) { function profile_user($type, $edit, &$user) {
switch ($type) { switch ($type) {
case 'load': case 'load':
...@@ -153,7 +179,7 @@ function profile_user($type, $edit, &$user) { ...@@ -153,7 +179,7 @@ function profile_user($type, $edit, &$user) {
case 'edit': case 'edit':
return profile_edit_profile($edit, $user); return profile_edit_profile($edit, $user);
case 'validate': case 'validate':
return $edit; return profile_validate_profile($edit);
} }
} }
...@@ -176,7 +202,6 @@ function profile_validate_form($edit) { ...@@ -176,7 +202,6 @@ function profile_validate_form($edit) {
} }
// Validate the category: // Validate the category:
if (!$edit['category']) { if (!$edit['category']) {
return t('You must enter a category.'); return t('You must enter a category.');
} }
...@@ -288,13 +313,8 @@ function theme_profile_profile($user, $fields = array()) { ...@@ -288,13 +313,8 @@ function theme_profile_profile($user, $fields = array()) {
$output .= " <div class=\"name\">". format_name($user) ."</div>\n"; $output .= " <div class=\"name\">". format_name($user) ."</div>\n";
foreach ($fields as $field) { foreach ($fields as $field) {
if ($user->{$field->name}) { if ($value = profile_view_field($user, $field)) {
if ($field->type == 'checkbox') { $output .= " <div class=\"field\">$value</div>\n";
$output .= " <div class=\"field\">". $field->title ."</div>\n";
}
else {
$output .= " <div class=\"field\">". $user->{$field->name} ."</div>\n";
}
} }
} }
...@@ -304,7 +324,7 @@ function theme_profile_profile($user, $fields = array()) { ...@@ -304,7 +324,7 @@ function theme_profile_profile($user, $fields = array()) {
} }
function _profile_field_types($type = NULL) { function _profile_field_types($type = NULL) {
$types = array('textfield', 'textarea', 'checkbox', 'selection'); $types = array('textfield', 'textarea', 'checkbox', 'selection', 'url');
return isset($type) ? $types[$type] : $types; return isset($type) ? $types[$type] : $types;
} }
......
...@@ -86,23 +86,34 @@ function profile_save_profile($edit, $user) { ...@@ -86,23 +86,34 @@ function profile_save_profile($edit, $user) {
} }
} }
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/$value");
case 'checkbox':
return l($field->title, "profile/$field->name/");
case 'url':
return "<a href=\"". check_url(strip_tags($value)) ."\">". strip_tags($value) ."</a>";
}
}
}
function profile_view_profile($user) { function profile_view_profile($user) {
profile_load_profile(&$user); profile_load_profile(&$user);
$result = db_query('SELECT * FROM {profile_fields} ORDER BY category, weight'); $result = db_query('SELECT * FROM {profile_fields} ORDER BY category, weight');
while ($field = db_fetch_object($result)) { while ($field = db_fetch_object($result)) {
if ($value = $user->{$field->name}) { if ($value = profile_view_field($user, $field)) {
switch ($field->type) { if ($field->type == 'checkbox') {
case 'textfield': $output .= "<p>$value</p>";
case 'textarea': }
$output .= form_item($field->title, check_output($value)); else {
break; $output .= form_item($field->title, check_output($value));
case 'selection':
$output .= form_item($field->title, l($value, "profile/$field->name/$value"));
break;
case 'checkbox':
$output .= '<p>'. l($field->title, "profile/$field->name/") .'</p>';
} }
} }
} }
...@@ -117,6 +128,7 @@ function profile_edit_profile($edit, $user) { ...@@ -117,6 +128,7 @@ function profile_edit_profile($edit, $user) {
while ($field = db_fetch_object($result)) { while ($field = db_fetch_object($result)) {
switch ($field->type) { switch ($field->type) {
case 'textfield': case 'textfield':
case 'url':
$fields[$field->category] .= form_textfield($field->title, $field->name, $edit[$field->name], 70, 255, $field->explanation); $fields[$field->category] .= form_textfield($field->title, $field->name, $edit[$field->name], 70, 255, $field->explanation);
break; break;
case 'textarea': case 'textarea':
...@@ -142,6 +154,20 @@ function profile_edit_profile($edit, $user) { ...@@ -142,6 +154,20 @@ function profile_edit_profile($edit, $user) {
return $fields; 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 ($field->type == 'url') {
if ($edit[$field->name] && !valid_url($edit[$field->name], true)) {
return t("The value provided for '%field' is not a valid URL.", array('%field' => $field->title));
}
}
}
return $edit;
}
function profile_user($type, $edit, &$user) { function profile_user($type, $edit, &$user) {
switch ($type) { switch ($type) {
case 'load': case 'load':
...@@ -153,7 +179,7 @@ function profile_user($type, $edit, &$user) { ...@@ -153,7 +179,7 @@ function profile_user($type, $edit, &$user) {
case 'edit': case 'edit':
return profile_edit_profile($edit, $user); return profile_edit_profile($edit, $user);
case 'validate': case 'validate':
return $edit; return profile_validate_profile($edit);
} }
} }
...@@ -176,7 +202,6 @@ function profile_validate_form($edit) { ...@@ -176,7 +202,6 @@ function profile_validate_form($edit) {
} }
// Validate the category: // Validate the category:
if (!$edit['category']) { if (!$edit['category']) {
return t('You must enter a category.'); return t('You must enter a category.');
} }
...@@ -288,13 +313,8 @@ function theme_profile_profile($user, $fields = array()) { ...@@ -288,13 +313,8 @@ function theme_profile_profile($user, $fields = array()) {
$output .= " <div class=\"name\">". format_name($user) ."</div>\n"; $output .= " <div class=\"name\">". format_name($user) ."</div>\n";
foreach ($fields as $field) { foreach ($fields as $field) {
if ($user->{$field->name}) { if ($value = profile_view_field($user, $field)) {
if ($field->type == 'checkbox') { $output .= " <div class=\"field\">$value</div>\n";
$output .= " <div class=\"field\">". $field->title ."</div>\n";
}
else {
$output .= " <div class=\"field\">". $user->{$field->name} ."</div>\n";
}
} }
} }
...@@ -304,7 +324,7 @@ function theme_profile_profile($user, $fields = array()) { ...@@ -304,7 +324,7 @@ function theme_profile_profile($user, $fields = array()) {
} }
function _profile_field_types($type = NULL) { function _profile_field_types($type = NULL) {
$types = array('textfield', 'textarea', 'checkbox', 'selection'); $types = array('textfield', 'textarea', 'checkbox', 'selection', 'url');
return isset($type) ? $types[$type] : $types; return isset($type) ? $types[$type] : $types;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment