diff options
author | Dries Buytaert <dries@buytaert.net> | 2004-03-21 10:28:10 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2004-03-21 10:28:10 +0000 |
commit | 93cf70d72e0e4ac8859afa11811da4d3df738460 (patch) | |
tree | e4a0ea7b105794d6e3588ae720434b43a9d66ea4 | |
parent | b5c441a926bf71f7fb4b0553f13c49c66cf23ca7 (diff) | |
download | brdo-93cf70d72e0e4ac8859afa11811da4d3df738460.tar.gz brdo-93cf70d72e0e4ac8859afa11811da4d3df738460.tar.bz2 |
- 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.
-rw-r--r-- | database/updates.inc | 8 | ||||
-rw-r--r-- | includes/common.inc | 9 | ||||
-rw-r--r-- | modules/profile.module | 62 | ||||
-rw-r--r-- | modules/profile/profile.module | 62 |
4 files changed, 93 insertions, 48 deletions
diff --git a/database/updates.inc b/database/updates.inc index 371606ba3..663bccf61 100644 --- a/database/updates.inc +++ b/database/updates.inc @@ -836,7 +836,7 @@ function update_80() { array("MSN messenger ID", "msn", "textfield", NULL, 0), array("Yahoo messenger ID", "yahoo", "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("Interests", "interests", "textarea", NULL, 0), array("Public key", "publickey", "textarea", NULL, 0) @@ -880,16 +880,16 @@ function update_80() { // Save the update record: user_save($account, $edit); } - + return $ret; } function update_81() { $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; } - + function update_sql($sql) { $edit = $_POST["edit"]; $result = db_query($sql); diff --git a/includes/common.inc b/includes/common.inc index e50aa5caf..350458bef 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -561,8 +561,13 @@ function valid_email_address($mail) { * * @param $url an URL */ -function valid_url($url) { - return preg_match("/^[a-zA-z0-9\/:_\-_\.,]+$/", $url); +function valid_url($url, $absolute = false) { + 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) { diff --git a/modules/profile.module b/modules/profile.module index 68676923b..53c458555 100644 --- a/modules/profile.module +++ b/modules/profile.module @@ -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) { profile_load_profile(&$user); $result = db_query('SELECT * FROM {profile_fields} ORDER BY category, weight'); while ($field = db_fetch_object($result)) { - if ($value = $user->{$field->name}) { - switch ($field->type) { - case 'textfield': - case 'textarea': - $output .= form_item($field->title, check_output($value)); - break; - 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>'; + if ($value = profile_view_field($user, $field)) { + if ($field->type == 'checkbox') { + $output .= "<p>$value</p>"; + } + else { + $output .= form_item($field->title, check_output($value)); } } } @@ -117,6 +128,7 @@ function profile_edit_profile($edit, $user) { while ($field = db_fetch_object($result)) { switch ($field->type) { case 'textfield': + case 'url': $fields[$field->category] .= form_textfield($field->title, $field->name, $edit[$field->name], 70, 255, $field->explanation); break; case 'textarea': @@ -142,6 +154,20 @@ function profile_edit_profile($edit, $user) { 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) { switch ($type) { case 'load': @@ -153,7 +179,7 @@ function profile_user($type, $edit, &$user) { case 'edit': return profile_edit_profile($edit, $user); case 'validate': - return $edit; + return profile_validate_profile($edit); } } @@ -176,7 +202,6 @@ function profile_validate_form($edit) { } // Validate the category: - if (!$edit['category']) { return t('You must enter a category.'); } @@ -288,13 +313,8 @@ function theme_profile_profile($user, $fields = array()) { $output .= " <div class=\"name\">". format_name($user) ."</div>\n"; foreach ($fields as $field) { - if ($user->{$field->name}) { - if ($field->type == 'checkbox') { - $output .= " <div class=\"field\">". $field->title ."</div>\n"; - } - else { - $output .= " <div class=\"field\">". $user->{$field->name} ."</div>\n"; - } + if ($value = profile_view_field($user, $field)) { + $output .= " <div class=\"field\">$value</div>\n"; } } @@ -304,7 +324,7 @@ function theme_profile_profile($user, $fields = array()) { } 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; } diff --git a/modules/profile/profile.module b/modules/profile/profile.module index 68676923b..53c458555 100644 --- a/modules/profile/profile.module +++ b/modules/profile/profile.module @@ -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) { profile_load_profile(&$user); $result = db_query('SELECT * FROM {profile_fields} ORDER BY category, weight'); while ($field = db_fetch_object($result)) { - if ($value = $user->{$field->name}) { - switch ($field->type) { - case 'textfield': - case 'textarea': - $output .= form_item($field->title, check_output($value)); - break; - 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>'; + if ($value = profile_view_field($user, $field)) { + if ($field->type == 'checkbox') { + $output .= "<p>$value</p>"; + } + else { + $output .= form_item($field->title, check_output($value)); } } } @@ -117,6 +128,7 @@ function profile_edit_profile($edit, $user) { while ($field = db_fetch_object($result)) { switch ($field->type) { case 'textfield': + case 'url': $fields[$field->category] .= form_textfield($field->title, $field->name, $edit[$field->name], 70, 255, $field->explanation); break; case 'textarea': @@ -142,6 +154,20 @@ function profile_edit_profile($edit, $user) { 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) { switch ($type) { case 'load': @@ -153,7 +179,7 @@ function profile_user($type, $edit, &$user) { case 'edit': return profile_edit_profile($edit, $user); case 'validate': - return $edit; + return profile_validate_profile($edit); } } @@ -176,7 +202,6 @@ function profile_validate_form($edit) { } // Validate the category: - if (!$edit['category']) { return t('You must enter a category.'); } @@ -288,13 +313,8 @@ function theme_profile_profile($user, $fields = array()) { $output .= " <div class=\"name\">". format_name($user) ."</div>\n"; foreach ($fields as $field) { - if ($user->{$field->name}) { - if ($field->type == 'checkbox') { - $output .= " <div class=\"field\">". $field->title ."</div>\n"; - } - else { - $output .= " <div class=\"field\">". $user->{$field->name} ."</div>\n"; - } + if ($value = profile_view_field($user, $field)) { + $output .= " <div class=\"field\">$value</div>\n"; } } @@ -304,7 +324,7 @@ function theme_profile_profile($user, $fields = array()) { } 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; } |