<?php
/**
* Account Settings - Basics
*/
function account_basics_form($form_state, $profile = NULL) {
if (!$profile) {
global $user;
$profile = $user;
}
$form['#attributes']['enctype'] = 'multipart/form-data';
$form['#attributes']['class'] = 'standard-form stacked';
$form['profile'] = array('#type' => 'value', '#value' => $profile);
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#default_value' => $profile->name,
'#maxlength' => USERNAME_MAX_LENGTH,
'#required' => TRUE,
);
$form['mail'] = array(
'#type' => 'item',
'#title' => t('Email'),
'#value' => $profile->mail,
);
$form['picture'] = array(
'#type' => 'file',
'#title' => t('Photo'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#attributes' => array('class' => 'btn-primary'),
);
return $form;
}
function account_basics_form_validate($form, &$form_state) {
$profile = $form_state['values']['profile'];
if ($error = user_validate_name($form_state['values']['name'])) {
form_set_error('name', $error);
}
else if (db_result(db_query("SELECT COUNT(*) FROM {users} WHERE uid != %d AND LOWER(name) = LOWER('%s')", $profile->uid, $form_state['values']['name'])) > 0) {
form_set_error('name', t('The name %name is already taken.', array('%name' => $form_state['values']['name'])));
}
else if (drupal_is_denied('user', $form_state['values']['name'])) {
form_set_error('name', t('The name %name has been denied access.', array('%name' => $form_state['values']['name'])));
}
// Validate the uploaded picture.
$validators = array(
'file_validate_is_image' => array(),
'file_validate_image_resolution' => array('85x85'),
'file_validate_size' => array(30 * 1024),
);
if ($file = file_save_upload('picture', $validators)) {
// Remove the old picture.
if (isset($profile->picture) && file_exists($profile->picture)) {
file_delete($profile->picture);
}
// The image was saved using file_save_upload() and was added to the
// files table as a temporary file. We'll make a copy and let the garbage
// collector delete the original upload.
$info = image_get_info($file->filepath);
$destination = 'user-pictures/picture-'. $profile->uid .'.'. $info['extension'];
if (file_copy($file, $destination, FILE_EXISTS_REPLACE)) {
$form_state['values']['picture'] = $file->filepath;
}
else {
form_set_error('picture', t("Failed to upload the picture image; the %directory directory doesn't exist or is not writable.", array('%directory' => 'user-pictures')));
}
}
}
function account_basics_form_submit($form, &$form_state) {
user_save($form_state['values']['profile'], array(
'name' => $form_state['values']['name'],
'picture' => $form_state['values']['picture'],
));
set_message(t('Basic account information has been saved.'));
}
/**
* Account Settings - Profile
*/
function account_profile_form($form_state, $profile = NULL) {
if (!$profile) {
global $user;
$profile = $user;
}
$form['#attributes']['class'] = 'standard-form stacked';
$form['profile'] = array('#type' => 'value', '#value' => $profile);
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Job title'),
'#default_value' => $profile->title,
);
$form['department'] = array(
'#type' => 'textfield',
'#title' => t('Department'),
'#default_value' => $profile->department,
);
$form['location'] = array(
'#type' => 'textfield',
'#title' => t('Location'),
'#default_value' => $profile->location,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#attributes' => array('class' => 'btn-primary'),
);
return $form;
}
function account_profile_form_submit($form, &$form_state) {
user_save($form_state['values']['profile'], array(
'title' => $form_state['values']['title'],
'department' => $form_state['values']['department'],
'location' => $form_state['values']['location'],
));
set_message(t('Profile has been saved.'));
}
/**
* Account Settings - Password
*/
function account_password_form($form_state) {
_user_password_dynamic_validation();
$form['#attributes']['class'] = 'standard-form stacked';
global $user;
$form['account']['pass'] = array('#type' => 'password_confirm',
'#description' => t('To change the current user password, enter the new password in both fields.'),
'#required' => TRUE,
'#size' => 25,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#attributes' => array('class' => 'btn-primary'),
);
return $form;
}
function account_password_form_submit($form, &$form_state) {
global $user;
user_save($user, array(
'pass' => $form_state['values']['pass'],
));
set_message(t('Your password has been saved.'));
}