You are here

function brazilianids_user in Brazilian IDs 6

Implementation of hook_user().

File

./brazilianids.module, line 95

Code

function brazilianids_user($op, &$edit, &$account, $category = NULL) {
  switch ($op) {
    case 'register':

      // Enable the form settings during user registration
      // based on previous settings
      if (!variable_get('brazilianids_registration', FALSE)) {
        return;
      }
    case 'form':
      $form['brazilianids_group'] = array(
        '#type' => 'fieldset',
        '#title' => t('Brazilian ID Number'),
        '#weight' => check_plain(variable_get('brazilianids_field_weight', 0)),
      );

      // Include the CNPJ
      if (variable_get('brazilianids_cpf', FALSE)) {
        $form['brazilianids_group']['brazilianids_cpf'] = array(
          '#description' => t('Your CPF. Only the numbers.'),
          '#default_value' => empty($account->brazilianids_cpf) ? '' : $account->brazilianids_cpf,
          '#maxlength' => 11,
          '#required' => variable_get('brazilianids_required', FALSE),
          '#size' => 20,
          '#title' => t('CPF'),
          '#type' => 'textfield',
        );
      }

      // Include the CNPJ
      if (variable_get('brazilianids_cnpj', FALSE)) {
        $form['brazilianids_group']['brazilianids_cnpj'] = array(
          '#description' => t('Your CNPJ. Only the numbers.'),
          '#default_value' => empty($account->brazilianids_cnpj) ? '' : $account->brazilianids_cnpj,
          '#maxlength' => 14,
          '#required' => variable_get('brazilianids_required', FALSE),
          '#size' => 20,
          '#title' => t('CNPJ'),
          '#type' => 'textfield',
        );
      }

      //  Include the CPF/CNPJ
      if (variable_get('brazilianids_cpfcnpj', FALSE)) {
        $form['brazilianids_group']['brazilianids_cpfcnpj'] = array(
          '#description' => t('Your CPF/CNPJ. Only the numbers.'),
          '#default_value' => empty($account->brazilianids_cpfcnpj) ? '' : $account->brazilianids_cpfcnpj,
          '#maxlength' => 14,
          '#required' => variable_get('brazilianids_required', FALSE),
          '#size' => 20,
          '#title' => t('CPF / CNPJ'),
          '#type' => 'textfield',
        );
      }

      // Hide fieldset if empty
      if (!count(element_children($form['brazilianids_group']))) {
        unset($form['brazilianids_group']);
      }
      return $form;
    case 'validate':

      // Include the validation functions
      module_load_include('check.inc', 'brazilianids', 'brazilianids');

      // Validate the CPF, if any
      if (isset($edit['brazilianids_cpf']) and !_brazilianids_validate_cpf($edit['brazilianids_cpf'])) {
        form_set_error('brazilianids_cpf', t('Your CPF is not valid or it already exists.'));
      }

      // Validate the CNPJ, if any
      if (isset($edit['brazilianids_cnpj']) and !_brazilianids_validate_cnpj($edit['brazilianids_cnpj'])) {
        form_set_error('brazilianids_cnpj', t('Your CNPJ is not valid or it already exists.'));
      }

      //  Validate the CPF OR CNPJ, if any
      if (isset($edit['brazilianids_cpfcnpj']) and !(_brazilianids_is_cpf_cnpj($edit['brazilianids_cpfcnpj']) == 'CPF' ? _brazilianids_validate_cpf($edit['brazilianids_cpfcnpj']) : _brazilianids_validate_cnpj($edit['brazilianids_cpfcnpj']))) {
        form_set_error('brazilianids_cpfcnpj', t('Your CPF/CNPJ is not valid or it already exists.'));
      }
      return;
    case 'view':

      // Include the validation functions
      module_load_include('check.inc', 'brazilianids', 'brazilianids');
      if (variable_get('brazilianids_cpf', FALSE) && !empty($account->brazilianids_cpf)) {
        $account->content['account'][t('CPF')] = array(
          '#type' => 'user_profile_item',
          '#title' => t('CPF'),
          '#value' => _brazilianids_format_cpf($account->brazilianids_cpf),
        );
      }
      if (variable_get('brazilianids_cnpj', FALSE) && !empty($account->brazilianids_cnpj)) {
        $account->content['account'][t('CNPJ')] = array(
          '#type' => 'user_profile_item',
          '#title' => t('CNPJ'),
          '#value' => _brazilianids_format_cnpj($account->brazilianids_cnpj),
        );
      }
      if (variable_get('brazilianids_cpfcnpj', FALSE) && !empty($account->brazilianids_cpfcnpj)) {
        $account->content['account'][t('CPF / CNPJ')] = array(
          '#type' => 'user_profile_item',
          '#title' => t('CPF / CNPJ'),
          '#value' => _brazilianids_is_cpf_cnpj($account->brazilianids_cpfcnpj) == 'CPF' ? _brazilianids_format_cpf($account->brazilianids_cpfcnpj) : _brazilianids_format_cnpj($account->brazilianids_cpfcnpj),
        );
      }
  }
}