<?php
// $Id: user_readonly.module,v 1.1.6.3 2007/03/02 18:34:37 cainan Exp $

/**
 * @file
 * 
 * This module provides restrictions on user account/profile fields.
 */

/**
 * Implementation of hook_menu().
 */
function user_readonly_menu($may_cache) {
  $items = array();

  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/user_readonly',
      'description' => t('User ReadOnly settings.'),
      'title' => t('User ReadOnly'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('user_readonly_settings')
    );
  }
  return $items;
}

/**
 * Implementation of hook_help().
 */
function user_readonly_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Restrict user/profile editing.');
      break;
    case 'admin/settings/user_readonly':
      return t('<p>Here you can define modification restrictions for user/profile editing.</p>');
      break;
  }
}

/**
 * Implementation of hook_settings().
 */
function user_readonly_settings() {
 
  $settings = variable_get('user_readonly',array());

  $user_fields = array(
    'name'      => t('Name'),
    'mail'      => t('Mail'),
    'pass'      => t('Password'),
    'theme'     => t('Theme'),
    'signature' => t('Signature'),
    'timezone'  => t('Timezone'), 
    'language'  => t('Language'),
    'picture_upload'   => t('Picture Upload'),
    'picture_delete'   => t('Picture Delete'),
  );

  if (module_exists('contact')) {
    $user_fields['contact'] = t('Contact');
  }

  $options = array(
    1 => t('only <b>DENY</b> changes to selected fields.'),
    0 => t('only <b>ALLOW</b> changes to selected fields.'),
  );

  $form['group1'] = array(
    '#type'        => 'fieldset',
    '#title'       => t('Restrictions Mode'),
  );
  $form['group1']['user_readonly_mode'] = array(
    '#type'          => 'radios',
    '#default_value' => $settings['user_readonly_mode'],
    '#options'       => $options,
    '#description'   => t('Define the restriction rules.<br>To deny all changes, choose ALLOW, but do not select any fields below.'),
  );

  $form['group2'] = array(
    '#type'        => 'fieldset',
    '#title'       => t('User Fields'),
  );
  $form['group2']['user_readonly_ufields'] = array(
    '#type'          => 'checkboxes',
    '#default_value' => $settings['user_readonly_ufields'],
    '#required'      => FALSE,
    '#options'       => $user_fields,
  );

  if (module_exists('profile')) {
    $result = db_query('SELECT * from {profile_fields}');
    while ($obj = db_fetch_object($result)) {
      $profile_fields[$obj->name] = t($obj->title);
    }
    if ($profile_fields) {
      $form['group3'] = array(
        '#type'        => 'fieldset',
        '#title'       => t('Profile Fields'),
      );
      $form['group3']['user_readonly_pfields'] = array(
        '#type'          => 'checkboxes',
        '#default_value' => $settings['user_readonly_pfields'],
        '#required'      => FALSE,
        '#options'       => $profile_fields,
      );
    }
  }

  $form['submit'] = array(
    '#type'	=> 'submit',
    '#value'	=> t('Save')
  );
  return $form;
}

function user_readonly_settings_submit($form_id, $form_values) {

  $data = array(
    'user_readonly_mode'    => $form_values['user_readonly_mode'],
    'user_readonly_ufields' => $form_values['user_readonly_ufields'],
    'user_readonly_pfields' => $form_values['user_readonly_pfields'],
  );

  variable_set('user_readonly',$data);
  drupal_set_message('Settings saved.');
  drupal_goto('admin/settings/user_readonly');
}



/**
 * general field validation.
 */
function user_readonly_validate($form_id, &$form_values, &$form) {
  global $user;

  $mode = 1; 
  $settings = variable_get('user_readonly',array());

  $field_list = array_merge(
    $settings['user_readonly_ufields'],
    $settings['user_readonly_pfields']
  );

  // make sure the profile data is loaded.
  if (module_exists('profile')) {
    profile_load_profile($user);
  }

  $mode = $settings['user_readonly_mode'];
  foreach ($field_list as $key => $value) {
    if (($mode && $field_list[$key]) || (!$mode && !$field_list[$key])) {
      if ($form_values[$key] && ($form_values[$key] != $user->$key)) {
        form_set_error($key,t('You are not authorized to edit this field.'));
      }
    }
  }
}

/** 
 * Prevent the user from attempting to edit the field,
 * thus preventing confusion.
 * insert a hidden field w/ the appropriate value, since
 * disabled fields do not get posted.
 */
function user_readonly_form_alter($form_id, &$form) { 

  if (user_access('administer users') || $form_id != 'user_edit') {
    return;
  }
  $settings = variable_get('user_readonly',array());

  $field_list = array_merge(
    $settings['user_readonly_ufields'],
    $settings['user_readonly_pfields']
  );

  $mode = $settings['user_readonly_mode'];

  // hook into the validation process.
  $form['#validate']['user_readonly_validate'] = array();

  foreach ($form as $group => $data) {
    if (!is_array($data)) {
      continue;
    }
    foreach ($data as $key => $value) {
      // only restrict access to fields controlled by this module.
      if (!array_key_exists($key,$field_list)) {
        continue;
      }

      if (($mode && $field_list[$key]) || (!$mode && !$field_list[$key])) {
	      switch ($form[$group][$key]['#type']) {
	        case 'date':
	        case 'password_confirm':
	          // due to the complex nature of these fields, 
            // we simply delete them from the form.
   	        unset($form[$group][$key]);
            break;
          default:
            // fields must have a hidden field w/ proper value.
            // as disabled fields are not submitted.
            $form[$group]['user_readonly'][$key] = $form[$group][$key];
            $form[$group]['user_readonly'][$key]['#type'] = 'hidden';
	    if (!is_set ($form[$group][$key]['#attributes'])) {
	      $form[$group][$key]['#attributes'] = array('disabled'=> TRUE);
	    }
	    else {
	      $form[$group][$key]['#attributes']['disabled']=TRUE;
	    }
            break;
        }
	    }
    }
  }
}
