View source
<?php
function onepageprofile_menu() {
$items = array();
$items['admin/settings/onepageprofile'] = array(
'title' => 'One page profile',
'description' => 'Assign weight to profile categories',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'onepageprofile_settings',
),
'access arguments' => array(
'administer site configuration',
),
);
return $items;
}
function onepageprofile_menu_alter(&$items) {
}
function onepageprofile_theme() {
return array(
'onepageprofile_settings' => array(
'arguments' => array(
'form' => array(),
),
),
);
}
function onepageprofile_form_user_profile_form_alter(&$form, &$form_state) {
if ($weights = variable_get('onepageprofile_weights', '')) {
foreach (element_children($form) as $field) {
if (!empty($weights[$field])) {
$form[$field]['#weight'] = $weights[$field];
}
}
uasort($form, 'element_sort');
}
}
function onepageprofile_user_update(&$edit, $account, $category = NULL) {
}
function onepageprofile_profile_alter(&$account) {
$categories = _onepageprofile_get_categories();
foreach ($categories as $key => $category) {
if (isset($account->content[$key])) {
$account->content[$key]['#weight'] = $category['#weight'];
}
}
}
function onepageprofile_settings() {
$form = array();
$form['onepageprofile_categories'] = array(
'#tree' => TRUE,
);
$categories = _onepageprofile_get_categories();
$tree =& $form['onepageprofile_categories'];
foreach ($categories as $key => $category) {
$tree[$key]['title'] = array(
'#value' => $category['#title'],
);
$tree[$key]['weight'] = array(
'#type' => 'weight',
'#delta' => max(10, count($categories)),
'#default_value' => $category['#weight'],
);
}
$form['onepageprofile_submit'] = array(
'#type' => 'submit',
'#value' => t('Save configuration'),
'#submit' => array(
'onepageprofile_settings_submit',
),
);
$form['onepageprofile_reset'] = array(
'#type' => 'submit',
'#value' => t('Reset to defaults'),
'#submit' => array(
'onepageprofile_settings_reset',
),
);
return $form;
}
function onepageprofile_settings_submit($form, $form_state) {
$weights = array();
foreach ($form_state['values']['onepageprofile_categories'] as $category => $value) {
$weights[$category] = (int) $value['weight'];
}
variable_set('onepageprofile_weights', $weights);
drupal_set_message(t('Category weights have been saved.'));
}
function onepageprofile_settings_reset($form, $form_state) {
variable_del('onepageprofile_weights');
drupal_set_message(t('Category weights have been reset.'));
}
function onepageprofile_domainignore() {
return array(
'profile_form_profile',
);
}
function theme_onepageprofile_settings($form) {
drupal_add_tabledrag('onepageprofile-categories', 'order', 'sibling', 'onepageprofile-element');
$header = array(
t('Category'),
t('Weight'),
);
$rows = array();
$tree =& $form['onepageprofile_categories'];
foreach (element_children($tree) as $key) {
$tree[$key]['weight']['#attributes']['class'] = 'onepageprofile-element';
$row = array();
$row[] = drupal_render($tree[$key]['title']);
$row[] = drupal_render($tree[$key]['weight']);
$rows[] = array(
'data' => $row,
'class' => 'draggable',
);
}
$output = '<p>' . t('Use this form to set the order of the fieldsets appearing on the user profile editing page.') . '</p>';
$output .= theme('table', $header, $rows, array(
'id' => 'onepageprofile-categories',
));
$output .= drupal_render($form);
return $output;
}
function _onepageprofile_check_access($account, $category) {
$map = array(
'user',
$account,
'edit',
$category['name'],
);
$category['access_callback'] = $category['access callback'];
$category['access_arguments'] = serialize($category['access arguments']);
_menu_check_access($category, $map);
$menu['access'] = TRUE;
if (function_exists('profile_role_access_category')) {
$menu['access'] = profile_role_access_category($account, $category['name']);
}
return $menu['access'] && $category['access'];
}
function _onepageprofile_get_categories() {
module_load_include('inc', 'user', 'user.pages');
$form_state = array();
$account = $GLOBALS['user'];
$form = user_profile_form($form_state, $account);
$categories = array();
foreach (element_children($form) as $field) {
if ($field == 'theme_select' && isset($form[$field])) {
$categories[$field] = array(
'#weight' => $form[$field]['#weight'],
'#title' => $form[$field]['themes']['#title'],
);
continue;
}
if (!isset($form[$field]['#type']) || $form[$field]['#type'] !== 'fieldset') {
continue;
}
$categories[$field] = array(
'#weight' => $form[$field]['#weight'],
'#title' => $form[$field]['#title'],
);
foreach (variable_get('onepageprofile_weights', array()) as $field => $weight) {
if (isset($categories[$field])) {
$categories[$field]['#weight'] = $weight;
}
}
}
uasort($categories, 'element_sort');
return $categories;
}