2002-08-08 18:52:55 +00:00
<?php
2004-03-13 06:10:20 +00:00
// $Id$
2002-08-08 18:52:55 +00:00
2004-03-11 20:33:59 +00:00
// TODO: add a 'date' field so we can migrate the birthday information.
2003-08-23 18:27:05 +00:00
2004-06-18 15:04:37 +00:00
/**
* Implementation of hook_help().
*/
2004-03-11 20:33:59 +00:00
function profile_help($section) {
2003-08-23 18:27:05 +00:00
switch ($section) {
2004-06-18 15:04:37 +00:00
case 'admin/modules#description':
return t('Support for configurable user profiles.');
2003-08-23 18:27:05 +00:00
}
}
2004-04-21 13:56:38 +00:00
/**
2004-06-18 15:04:37 +00:00
* Implementation of hook_menu().
2004-04-21 13:56:38 +00:00
*/
2004-06-18 15:04:37 +00:00
function profile_menu() {
$items = array();
$items[] = array('path' => 'profile', 'title' => t('browse'),
'callback' => 'profile_browse',
'access' => TRUE,
'type' => MENU_SUGGESTED_ITEM);
$items[] = array('path' => 'admin/user/configure/profile', 'title' => t('profiles'),
'callback' => 'profile_admin_overview',
'access' => user_access('administer users'),
'type' => MENU_LOCAL_SUBTASK);
$items[] = array('path' => 'admin/user/configure/profile/add', 'title' => t('add field'),
'callback' => 'profile_browse',
'access' => user_access('administer users'),
'type' => MENU_CALLBACK);
$items[] = array('path' => 'admin/user/configure/profile/edit', 'title' => t('edit field'),
'callback' => 'profile_browse',
'access' => user_access('administer users'),
'type' => MENU_CALLBACK);
$items[] = array('path' => 'admin/user/configure/profile/delete', 'title' => t('delete field'),
'callback' => 'profile_browse',
'access' => user_access('administer users'),
'type' => MENU_CALLBACK);
return $items;
2004-03-11 20:33:59 +00:00
}
2002-08-08 18:52:55 +00:00
2004-06-18 15:04:37 +00:00
/**
* Menu callback; display a list of user information.
*/
2004-03-11 20:33:59 +00:00
function profile_browse() {
2003-12-26 23:03:21 +00:00
2004-06-01 21:58:46 +00:00
$name = strip_tags(arg(1));
$value = strip_tags(arg(2));
2004-02-07 16:59:34 +00:00
2004-06-01 21:58:46 +00:00
$field = db_fetch_object(db_query("SELECT DISTINCT(fid), type, title, page FROM {profile_fields} WHERE name = '%s'", $name));
2002-08-08 18:52:55 +00:00
2004-03-11 20:33:59 +00:00
if ($field->fid) {
2004-06-01 21:58:46 +00:00
// Compile a list of fields to show
2004-03-11 20:33:59 +00:00
$fields = array();
2004-06-18 15:04:37 +00:00
$result = db_query('SELECT name, title, type FROM {profile_fields} WHERE fid != %d AND overview = 1', $field->fid);
2004-03-11 20:33:59 +00:00
while ($record = db_fetch_object($result)) {
$fields[] = $record;
}
2002-08-08 18:52:55 +00:00
2004-03-21 12:46:06 +00:00
// Determine what query to use:
switch ($field->type) {
case 'checkbox':
$query = 'v.value = 1';
break;
case 'selection':
2004-06-01 21:58:46 +00:00
$query = "v.value = '". check_query($value) ."'";
2004-03-21 12:46:06 +00:00
break;
case 'list':
2004-06-01 21:58:46 +00:00
$query = "v.value LIKE '%". check_query($value) ."%'";
2004-03-21 12:46:06 +00:00
break;
}
2004-03-11 20:33:59 +00:00
// Extract the affected users:
2004-03-21 12:46:06 +00:00
$result = pager_query("SELECT u.uid FROM {users} u INNER JOIN {profile_values} v ON u.uid = v.uid WHERE v.fid = $field->fid AND $query ORDER BY u.changed DESC", 20);
2002-08-08 18:52:55 +00:00
2004-03-20 19:41:42 +00:00
$output = '<div id="profile">';
2004-03-11 20:33:59 +00:00
while ($account = db_fetch_object($result)) {
$output .= theme('profile_profile', user_load(array('uid' => $account->uid)), $fields);
}
$output .= theme('pager', NULL, 20);
2004-03-21 12:46:06 +00:00
if ($field->type == 'selection' || $field->type == 'list') {
2004-06-01 21:58:46 +00:00
$title = strtr($field->page, array('%value' => $value));
2004-03-11 20:33:59 +00:00
}
else {
2004-03-20 19:41:42 +00:00
$title = $field->page;
2004-03-11 20:33:59 +00:00
}
2004-06-18 15:04:37 +00:00
$output .= '</div>';
2004-03-11 20:33:59 +00:00
print theme('page', $output, $title);
}
else {
drupal_not_found();
2002-08-08 18:52:55 +00:00
}
2004-03-11 20:33:59 +00:00
}
2002-08-08 18:52:55 +00:00
2004-03-11 20:33:59 +00:00
function profile_load_profile(&$user) {
$result = db_query('SELECT f.name, 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} = $field->value;
}
2002-10-22 18:46:43 +00:00
}
2002-08-08 18:52:55 +00:00
}
2004-05-05 21:17:25 +00:00
function profile_save_profile(&$edit, &$user) {
2004-03-11 20:33:59 +00:00
db_query('DELETE FROM {profile_values} WHERE uid = %d', $user->uid);
2004-03-27 11:56:01 +00:00
$result = db_query('SELECT fid, name FROM {profile_fields}');
2004-03-11 20:33:59 +00:00
while ($field = db_fetch_object($result)) {
if ($edit[$field->name]) {
db_query("INSERT INTO {profile_values} (fid, uid, value) VALUES (%d, %d, '%s')", $field->fid, $user->uid, $edit[$field->name]);
2004-05-05 21:17:25 +00:00
unset($edit[$field->name], $user->{$field->name});
2004-03-11 20:33:59 +00:00
}
}
2002-08-08 18:52:55 +00:00
}
2004-03-21 10:28:10 +00:00
function profile_view_field($user, $field) {
if ($value = $user->{$field->name}) {
switch ($field->type) {
case 'textfield':
case 'textarea':
return check_output($value);
case 'selection':
2004-03-21 12:46:06 +00:00
return l($value, "profile/$field->name/". htmlentities($value));
2004-03-21 10:28:10 +00:00
case 'checkbox':
2004-03-21 12:46:06 +00:00
return l($field->title, "profile/$field->name");
2004-03-21 10:28:10 +00:00
case 'url':
2004-06-18 15:04:37 +00:00
return '<a href="'. check_url(strip_tags($value)) .'">'. strip_tags($value) .'</a>'; case 'list':
2004-03-21 12:46:06 +00:00
$values = split("[\n\r]", $value);
$fields = array();
foreach ($values as $value) {
if ($value = trim(strip_tags($value))) {
$fields[] = l($value, "profile/$field->name/". htmlentities($value));
}
}
return implode(', ', $fields);
2004-03-21 10:28:10 +00:00
}
}
}
2004-03-11 20:33:59 +00:00
function profile_view_profile($user) {
2002-08-08 18:52:55 +00:00
2004-03-29 18:19:10 +00:00
profile_load_profile($user);
2002-08-08 18:52:55 +00:00
2004-03-11 20:33:59 +00:00
$result = db_query('SELECT * FROM {profile_fields} ORDER BY category, weight');
while ($field = db_fetch_object($result)) {
2004-03-21 10:28:10 +00:00
if ($value = profile_view_field($user, $field)) {
if ($field->type == 'checkbox') {
2004-03-21 14:28:15 +00:00
$fields[$field->category] .= "<p>$value</p>";
2004-03-21 10:28:10 +00:00
}
else {
2004-03-21 14:28:15 +00:00
$fields[$field->category] .= form_item($field->title, check_output($value));
2004-03-11 20:33:59 +00:00
}
2002-08-08 18:52:55 +00:00
}
}
2004-03-21 14:28:15 +00:00
return $fields;
2004-03-11 20:33:59 +00:00
}
2002-08-08 18:52:55 +00:00
2004-03-11 20:33:59 +00:00
function profile_edit_profile($edit, $user) {
$result = db_query('SELECT * FROM {profile_fields} ORDER BY category, weight');
while ($field = db_fetch_object($result)) {
switch ($field->type) {
case 'textfield':
2004-03-21 10:28:10 +00:00
case 'url':
2004-04-24 15:39:31 +00:00
$fields[$field->category] .= form_textfield($field->title, $field->name, $edit[$field->name], 70, 255, $field->explanation, NULL, $field->required);
2004-03-11 20:33:59 +00:00
break;
case 'textarea':
2004-04-24 15:39:31 +00:00
$fields[$field->category] .= form_textarea($field->title, $field->name, $edit[$field->name], 60, 5, $field->explanation, NULL, $field->required);
2004-03-21 12:46:06 +00:00
break;
case 'list':
2004-06-18 15:04:37 +00:00
$fields[$field->category] .= form_textarea($field->title, $field->name, $edit[$field->name], 60, 5, $field->explanation .' '. t('Put each entry on a separate line. No HTML allowed.'), NULL, $field->required);
2004-03-11 20:33:59 +00:00
break;
case 'checkbox':
2004-04-24 15:39:31 +00:00
$fields[$field->category] .= form_checkbox($field->title, $field->name, 1, $edit[$field->name], $field->explanation, NULL, $field->required);
2004-03-11 20:33:59 +00:00
break;
case 'selection':
$options = array('--');
$lines = split("[\n\r]", $field->options);
foreach ($lines as $line) {
if ($line = trim($line)) {
$options[$line] = $line;
}
}
2004-04-24 15:39:31 +00:00
$fields[$field->category] .= form_select($field->title, $field->name, $edit[$field->name], $options, $field->explanation, 0, 0, $field->required);
2004-03-11 20:33:59 +00:00
break;
2002-08-08 18:52:55 +00:00
}
}
2004-03-11 20:33:59 +00:00
return $fields;
2002-08-08 18:52:55 +00:00
}
2004-03-21 10:28:10 +00:00
function profile_validate_profile($edit) {
$result = db_query('SELECT * FROM {profile_fields} ORDER BY category, weight');
while ($field = db_fetch_object($result)) {
2004-03-27 11:56:01 +00:00
if ($edit[$field->name]) {
if ($field->type == 'url' && !valid_url($edit[$field->name], true)) {
2004-06-18 15:04:37 +00:00
form_set_error($field->name, t('The value provided for "%field" is not a valid URL.', array('%field' => $field->title)));
2004-03-21 10:28:10 +00:00
}
}
2004-03-27 14:50:56 +00:00
else if ($field->required) {
2004-06-18 15:04:37 +00:00
form_set_error($field->name, t('The field "%field" is required.', array('%field' => $field->title)));
2004-03-27 14:50:56 +00:00
}
2004-03-21 10:28:10 +00:00
}
return $edit;
}
2004-06-18 15:04:37 +00:00
/**
* Implementation of hook_user().
*/
2004-05-05 21:17:25 +00:00
function profile_user($type, &$edit, &$user) {
2004-03-11 20:33:59 +00:00
switch ($type) {
case 'load':
return profile_load_profile($user);
case 'update':
2004-04-29 23:27:15 +00:00
case 'insert':
2004-03-11 20:33:59 +00:00
return profile_save_profile($edit, $user);
case 'view':
return profile_view_profile($user);
2004-04-29 22:39:55 +00:00
case 'form':
2004-03-11 20:33:59 +00:00
return profile_edit_profile($edit, $user);
case 'validate':
2004-03-21 10:28:10 +00:00
return profile_validate_profile($edit);
2004-03-11 20:33:59 +00:00
}
}
2003-04-29 20:31:21 +00:00
2004-03-11 20:33:59 +00:00
function profile_validate_form($edit) {
2002-08-08 18:52:55 +00:00
2004-03-11 20:33:59 +00:00
// Validate the title:
2002-08-08 18:52:55 +00:00
2004-03-11 20:33:59 +00:00
if (!$edit['title']) {
2004-05-31 09:40:56 +00:00
form_set_error('title', t('You must enter a title.'));
2002-08-08 18:52:55 +00:00
}
2004-03-11 20:33:59 +00:00
// Validate the 'form name':
2002-08-08 18:52:55 +00:00
2004-03-11 20:33:59 +00:00
if (eregi('[^a-z0-9_-]', $edit['name'])) {
2004-05-31 09:40:56 +00:00
form_set_error('name', t('The specified form name contains one or more illegal characters. Spaces or any other special characters expect dash (-) and underscore (_) are not allowed.'));
2002-08-08 18:52:55 +00:00
}
2004-03-11 20:33:59 +00:00
if (in_array($edit['name'], user_fields())) {
2004-05-31 09:40:56 +00:00
form_set_error('name', t('The specified form name is reserved for use by Drupal.'));
2002-08-08 18:52:55 +00:00
}
2004-03-11 20:33:59 +00:00
// Validate the category:
if (!$edit['category']) {
2004-05-31 09:40:56 +00:00
form_set_error('category', t('You must enter a category.'));
2004-03-11 20:33:59 +00:00
}
2002-08-08 18:52:55 +00:00
}
2004-06-18 15:04:37 +00:00
/**
* Menu callback; adds a new field to all user profiles.
*/
2004-03-11 20:33:59 +00:00
function profile_admin_add($type) {
$type = _profile_field_types($type);
2002-08-08 18:52:55 +00:00
2003-06-03 18:04:47 +00:00
2004-03-11 20:33:59 +00:00
if ($_POST['op']) {
$data = $_POST['edit'];
2002-08-08 18:52:55 +00:00
2004-05-31 09:40:56 +00:00
// Validate the form:
profile_validate_form($data);
if (db_result(db_query("SELECT fid FROM {profile_fields} WHERE title = '%s'", $data['title']))) {
form_set_error('title', t('the specified title is already in use.'));
2004-05-28 20:02:11 +00:00
}
2004-05-31 09:40:56 +00:00
if (db_result(db_query("SELECT fid FROM {profile_fields} WHERE name = '%s'", $data['name']))) {
form_set_error('name', t('the specified name is already in use.'));
2004-05-28 20:02:11 +00:00
}
2004-05-31 09:40:56 +00:00
if (!form_has_errors()) {
2004-03-27 14:50:56 +00:00
db_query("INSERT INTO {profile_fields} (title, name, explanation, category, type, weight, required, overview, options, page) VALUES ('%s', '%s', '%s', '%s', '%s', %d, %d, %d, '%s', '%s')", $data['title'], $data['name'], $data['explanation'], $data['category'], $type, $data['weight'], $data['required'], $data['overview'], $data['options'], $data['page']);
2002-08-08 18:52:55 +00:00
2004-03-11 20:33:59 +00:00
drupal_set_message(t('the field has been created.'));
}
2003-12-26 23:03:21 +00:00
}
2004-03-11 20:33:59 +00:00
else {
$data = array('name' => 'profile_');
}
print theme('page', _profile_field_form($type, $data), t('Add new %type', array('%type' => $type)));
2003-12-26 23:03:21 +00:00
}
2004-06-18 15:04:37 +00:00
/**
* Menu callback; displays the profile field editing form.
*/
2004-03-11 20:33:59 +00:00
function profile_admin_edit($fid) {
2002-08-08 18:52:55 +00:00
2004-03-11 20:33:59 +00:00
if ($_POST['op']) {
$data = $_POST['edit'];
2002-08-08 18:52:55 +00:00
2004-05-31 09:40:56 +00:00
// Validate form:
profile_validate_form($data);
2002-08-08 18:52:55 +00:00
2004-05-31 09:40:56 +00:00
if (!form_has_errors()) {
2004-03-27 14:50:56 +00:00
db_query("UPDATE {profile_fields} SET title = '%s', name = '%s', explanation = '%s', category = '%s', weight = %d, required = %d, overview = %d, options = '%s', page = '%s' WHERE fid = %d", $data['title'], $data['name'], $data['explanation'], $data['category'], $data['weight'], $data['required'], $data['overview'], $data['options'], $data['page'], $fid);
2004-03-11 20:33:59 +00:00
drupal_set_message(t('the field has been updated.'));
}
2003-12-26 23:03:21 +00:00
}
else {
2004-03-11 20:33:59 +00:00
$data = db_fetch_array(db_query('SELECT * FROM {profile_fields} WHERE fid = %d', $fid));
2002-08-08 18:52:55 +00:00
}
2004-03-11 20:33:59 +00:00
print theme('page', _profile_field_form($data['type'], $data), t('Edit %type', array('%type' => $edit['type'])));
2002-08-08 18:52:55 +00:00
}
2004-06-18 15:04:37 +00:00
/**
* Menu callback; deletes a field from all user profiles.
*/
2004-03-11 20:33:59 +00:00
function profile_admin_delete($fid) {
db_query('DELETE FROM {profile_fields} WHERE fid = %d', $fid);
drupal_set_message(t('the field has been deleted.'));
print theme('page', '', t('Delete field'));
2002-08-08 18:52:55 +00:00
}
2004-03-11 20:33:59 +00:00
function _profile_field_form($type, $edit = array()) {
2004-06-18 15:04:37 +00:00
$group = form_textfield(t('Category'), 'category', $edit['category'], 70, 128, t('The category the new field should be part of. Categories are used to group fields logically. An example category is "Personal information".'));
$group .= form_textfield(t('Title'), 'title', $edit['title'], 70, 128, t('The title of the new field. The title will be shown to the user. An example title is "Favorite color".'));
$group .= form_textfield(t('Form name'), 'name', $edit['name'], 70, 128, t('The name of the field. The form name is not shown to the user but used internally in the HTML code and URLs.
Unless you know what you are doing, it is highly recommended that you prefix the form name with <code>profile_</code> to avoid name clashes with other fields. Spaces or any other special characters except dash (-) and underscore (_) are not allowed. An example name is "profile_favorite_color" or perhaps just "profile_color".'));
$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.'));
2004-03-20 19:41:42 +00:00
if ($type == 'selection') {
2004-06-18 15:04:37 +00:00
$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.'));
2004-03-20 19:41:42 +00:00
}
2004-06-18 15:04:37 +00:00
$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.'));
2004-03-27 14:50:56 +00:00
$group .= form_checkbox(t('Required field.'), 'required', 1, $edit['required']);
2004-03-20 19:41:42 +00:00
$output = form_group(t('Field settings'), $group);
2002-08-08 18:52:55 +00:00
2004-03-20 19:41:42 +00:00
$group = '';
2004-03-21 12:46:06 +00:00
if ($type == 'selection' || $type == 'list') {
2004-06-18 15:04:37 +00:00
$group .= form_textfield(t('Page title'), 'page', $edit['page'], 70, 128, t('The title of the page showing all users with the specified field. The word <code>%value</code> will be substituted with the corresponding value. An example page title is "People whose favorite color is %value".'));
2004-03-20 19:41:42 +00:00
}
else {
2004-06-18 15:04:37 +00:00
$group .= form_textfield(t('Page title'), 'page', $edit['page'], 70, 128, t('The title of the page showing all users with the specified field.'));
2002-08-08 18:52:55 +00:00
}
2004-03-20 19:41:42 +00:00
$group .= form_checkbox(t('Should this field be shown on the member listing pages.'), 'overview', 1, $edit['overview']);
2002-08-08 18:52:55 +00:00
2004-03-20 19:41:42 +00:00
$output .= form_group(t('Browsability'), $group);
2004-03-11 20:33:59 +00:00
$output .= form_submit(t('Save field'));
return form($output);
}
2004-06-18 15:04:37 +00:00
/**
* Menu callback; display a listing of all editable profile fields.
*/
2004-03-11 20:33:59 +00:00
function profile_admin_overview() {
$result = db_query('SELECT * FROM {profile_fields} ORDER BY category, weight');
while ($field = db_fetch_object($result)) {
2004-06-18 15:04:37 +00:00
$rows[] = array($field->title, $field->name, $field->type, $field->category, l(t('edit'), "admin/user/configure/profile/edit/$field->fid"), l(t('delete'), "admin/user/configure/profile/delete/$field->fid"));
2002-08-08 18:52:55 +00:00
}
2004-03-11 20:33:59 +00:00
$header = array(t('title'), t('name'), t('type'), t('category'), array('data' => t('operations'), 'colspan' => '2'));
$output = theme('table', $header, $rows);
$output .= '<h2>'. t('Create new field') .'</h2>';
$output .= '<ul>';
foreach (_profile_field_types() as $key => $value) {
2004-06-18 15:04:37 +00:00
$output .= '<li>'. l(t('Add new %type', array('%type' => $value)), "admin/user/configure/profile/add/$key") .'</li>';
2002-08-08 18:52:55 +00:00
}
2004-03-11 20:33:59 +00:00
$output .= '</ul>';
print theme('page', $output);
2002-08-08 18:52:55 +00:00
}
2004-03-11 20:33:59 +00:00
function theme_profile_profile($user, $fields = array()) {
$output = "<div class=\"profile\">\n";
$output .= theme('user_picture', $user);
2004-06-18 15:04:37 +00:00
$output .= ' <div class="name">'. format_name($user) ."</div>\n";
2004-03-11 20:33:59 +00:00
foreach ($fields as $field) {
2004-03-21 10:28:10 +00:00
if ($value = profile_view_field($user, $field)) {
$output .= " <div class=\"field\">$value</div>\n";
2002-08-08 18:52:55 +00:00
}
}
2004-03-11 20:33:59 +00:00
$output .= "</div>\n";
return $output;
}
function _profile_field_types($type = NULL) {
2004-03-21 12:46:06 +00:00
$types = array('textfield', 'textarea', 'checkbox', 'selection', 'list', 'url');
2004-03-11 20:33:59 +00:00
return isset($type) ? $types[$type] : $types;
2002-08-08 18:52:55 +00:00
}
2003-03-16 12:36:07 +00:00
?>