302 lines
11 KiB
Plaintext
302 lines
11 KiB
Plaintext
<?php
|
|
// $Id$
|
|
|
|
// TODO: add a 'date' field so we can migrate the birthday information.
|
|
|
|
function profile_help($section) {
|
|
switch ($section) {
|
|
case 'admin/system/modules#description':
|
|
$output = t("Support for configurable user profiles.");
|
|
break;
|
|
}
|
|
return $output;
|
|
|
|
|
|
}
|
|
|
|
function profile_link($type) {
|
|
if ($type == 'system') {
|
|
menu('profile', t('browse'), 'profile_browse', 0, MENU_HIDE);
|
|
|
|
if (user_access('administer users')) {
|
|
menu('admin/system/modules/profile', t('profile'), 'profile_admin_overview');
|
|
menu('admin/system/modules/profile/add', NULL, 'profile_admin_add', 0, MENU_HIDE);
|
|
menu('admin/system/modules/profile/edit', NULL, 'profile_admin_edit', 0, MENU_HIDE);
|
|
menu('admin/system/modules/profile/delete', NULL, 'profile_admin_delete', 0, MENU_HIDE);
|
|
}
|
|
}
|
|
}
|
|
|
|
function profile_browse() {
|
|
|
|
$value = arg(2) ? arg(2) : 1;
|
|
|
|
// Determine the field to group users by:
|
|
$field = db_fetch_object(db_query("SELECT DISTINCT(f.fid), f.type, f.title FROM {profile_fields} f INNER JOIN {profile_values} v ON f.fid = v.fid WHERE f.name = '%s' AND v.value = '%s' ORDER BY f.category, f.weight", arg(1), $value));
|
|
|
|
if ($field->fid) {
|
|
// Compile a list of fields to show:
|
|
$fields = array();
|
|
$result = db_query("SELECT name, title, type FROM {profile_fields} WHERE fid != %d AND overview = 1", $field->fid);
|
|
while ($record = db_fetch_object($result)) {
|
|
$fields[] = $record;
|
|
}
|
|
|
|
// Extract the affected users:
|
|
$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 v.value = '". check_query($value) ."' ORDER BY u.changed DESC", 20);
|
|
|
|
$output = '';
|
|
while ($account = db_fetch_object($result)) {
|
|
$output .= theme('profile_profile', user_load(array('uid' => $account->uid)), $fields);
|
|
}
|
|
$output .= theme('pager', NULL, 20);
|
|
|
|
if ($field->type == "selection") {
|
|
$title = arg(2);
|
|
}
|
|
else {
|
|
$title = $field->title;
|
|
}
|
|
|
|
print theme('page', $output, $title);
|
|
}
|
|
else {
|
|
drupal_not_found();
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
function profile_save_profile($edit, $user) {
|
|
db_query('DELETE FROM {profile_values} WHERE uid = %d', $user->uid);
|
|
$result = db_query('SELECT fid, name FROM profile_fields');
|
|
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]);
|
|
unset($edit[$field->name]);
|
|
}
|
|
}
|
|
}
|
|
|
|
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>';
|
|
}
|
|
}
|
|
}
|
|
|
|
return $output;
|
|
}
|
|
|
|
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':
|
|
$fields[$field->category] .= form_textfield($field->title, $field->name, $edit[$field->name], 70, 255, $field->explanation);
|
|
break;
|
|
case 'textarea':
|
|
$fields[$field->category] .= form_textarea($field->title, $field->name, $edit[$field->name], 60, 4, $field->explanation);
|
|
break;
|
|
case 'checkbox':
|
|
$fields[$field->category] .= form_checkbox($field->title, $field->name, 1, $edit[$field->name], $field->explanation);
|
|
break;
|
|
case 'selection':
|
|
$options = array('--');
|
|
$lines = split("[\n\r]", $field->options);
|
|
foreach ($lines as $line) {
|
|
if ($line = trim($line)) {
|
|
$options[$line] = $line;
|
|
}
|
|
}
|
|
|
|
$fields[$field->category] .= form_select($field->title, $field->name, $edit[$field->name], $options, $field->explanation);
|
|
break;
|
|
}
|
|
}
|
|
|
|
return $fields;
|
|
}
|
|
|
|
function profile_user($type, $edit, &$user) {
|
|
switch ($type) {
|
|
case 'load':
|
|
return profile_load_profile($user);
|
|
case 'update':
|
|
return profile_save_profile($edit, $user);
|
|
case 'view':
|
|
return profile_view_profile($user);
|
|
case 'edit':
|
|
return profile_edit_profile($edit, $user);
|
|
case 'validate':
|
|
return $edit;
|
|
}
|
|
}
|
|
|
|
function profile_validate_form($edit) {
|
|
|
|
// Validate the title:
|
|
|
|
if (!$edit['title']) {
|
|
return t('You must enter a title.');
|
|
}
|
|
|
|
// Validate the 'form name':
|
|
|
|
if (eregi('[^a-z0-9_-]', $edit['name'])) {
|
|
return t('The specified form name contains one or more illegal characters. Spaces or any other special characters expect dash (-) and underscore (_) are not allowed.');
|
|
}
|
|
|
|
if (in_array($edit['name'], user_fields())) {
|
|
return t('The specified form name is reserved for use by Drupal.');
|
|
}
|
|
|
|
// Validate the category:
|
|
|
|
if (!$edit['category']) {
|
|
return t('You must enter a category.');
|
|
}
|
|
}
|
|
|
|
function profile_admin_add($type) {
|
|
$type = _profile_field_types($type);
|
|
|
|
|
|
if ($_POST['op']) {
|
|
$data = $_POST['edit'];
|
|
|
|
if ($error = profile_validate_form($data)) {
|
|
drupal_set_message($error, 'error');
|
|
}
|
|
else {
|
|
db_query("INSERT INTO {profile_fields} (title, name, explanation, category, type, weight, overview, options) VALUES ('%s', '%s', '%s', '%s', '%s', %d, %d, '%s')", $data['title'], $data['name'], $data['explanation'], $data['category'], $type, $data['weight'], $data['overview'], $data['options']);
|
|
|
|
drupal_set_message(t('the field has been created.'));
|
|
}
|
|
}
|
|
else {
|
|
$data = array('name' => 'profile_');
|
|
}
|
|
|
|
print theme('page', _profile_field_form($type, $data), t('Add new %type', array('%type' => $type)));
|
|
}
|
|
|
|
function profile_admin_edit($fid) {
|
|
|
|
if ($_POST['op']) {
|
|
$data = $_POST['edit'];
|
|
|
|
if ($error = profile_validate_form($data)) {
|
|
drupal_set_message($error, 'error');
|
|
|
|
}
|
|
else {
|
|
db_query("UPDATE {profile_fields} SET title = '%s', name = '%s', explanation = '%s', category = '%s', weight = %d, overview = %d, options = '%s' WHERE fid = %d", $data['title'], $data['name'], $data['explanation'], $data['category'], $data['weight'], $data['overview'], $data['options'], $fid);
|
|
|
|
drupal_set_message(t('the field has been updated.'));
|
|
}
|
|
}
|
|
else {
|
|
$data = db_fetch_array(db_query('SELECT * FROM {profile_fields} WHERE fid = %d', $fid));
|
|
}
|
|
|
|
print theme('page', _profile_field_form($data['type'], $data), t('Edit %type', array('%type' => $edit['type'])));
|
|
}
|
|
|
|
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'));
|
|
}
|
|
|
|
function _profile_field_form($type, $edit = array()) {
|
|
|
|
$output = 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'."), NULL, FORM_REQUIRED);
|
|
$output .= 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'."));
|
|
$output .= 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."));
|
|
$output .= 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'."));
|
|
$output .= 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."));
|
|
$output .= form_checkbox(t('Display this field in member lists'), 'overview', 1, $edit['overview']);
|
|
|
|
if ($type == 'selection') {
|
|
$output .= 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."));
|
|
}
|
|
|
|
$output .= form_submit(t('Save field'));
|
|
|
|
return form($output);
|
|
}
|
|
|
|
function profile_admin_overview() {
|
|
|
|
$result = db_query('SELECT * FROM {profile_fields} ORDER BY category, weight');
|
|
while ($field = db_fetch_object($result)) {
|
|
$rows[] = array($field->title, $field->name, $field->type, $field->category, l(t('edit'), "admin/system/modules/profile/edit/$field->fid"), l(t('delete'), "admin/system/modules/profile/delete/$field->fid"));
|
|
}
|
|
|
|
$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) {
|
|
$output .= "<li>". l(t('Add new %type', array('%type' => $value)), "admin/system/modules/profile/add/$key") ."</li>";
|
|
}
|
|
$output .= '</ul>';
|
|
|
|
print theme('page', $output);
|
|
}
|
|
|
|
function theme_profile_profile($user, $fields = array()) {
|
|
|
|
$output = "<div class=\"profile\">\n";
|
|
$output .= theme('user_picture', $user);
|
|
$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>";
|
|
}
|
|
else {
|
|
$output .= "<div class=\"field\">". $user->{$field->name} ."</div>";
|
|
}
|
|
}
|
|
}
|
|
|
|
$output .= "</div>\n";
|
|
|
|
return $output;
|
|
}
|
|
|
|
function _profile_field_types($type = NULL) {
|
|
$types = array('textfield', 'textarea', 'checkbox', 'selection');
|
|
return isset($type) ? $types[$type] : $types;
|
|
}
|
|
|
|
?>
|