You are here

brazilianids.module in Brazilian IDs 6

File

brazilianids.module
View source
<?php

/**
 * @file
 * brazilianids.module
 *
 * Deal with brazilian IDs like CPF and CNPJ.
 *
 * @note this file only have the hook functions.
 */
if (module_exists('content')) {
  module_load_include('inc', 'brazilianids', 'brazilianids_cck');
}

/**
 * Module settings. Let users should which fields are needed
 *
 * @return
 *   Array. The settings form
 */
function _brazilianids_admin() {

  // Choose which of these fields should be used
  $form = array();
  $form['brazilianids_enable_cpf'] = array(
    '#default_value' => variable_get('brazilianids_enable_cpf', FALSE),
    '#description' => t('Enable the CPF on user profiles.'),
    '#title' => t('Enable the CPF.'),
    '#type' => 'checkbox',
  );
  $form['brazilianids_enable_cnpj'] = array(
    '#default_value' => variable_get('brazilianids_enable_cnpj', FALSE),
    '#description' => t('Enable the CNPJ on user profiles.'),
    '#title' => t('Enable the CNPJ.'),
    '#type' => 'checkbox',
  );
  $form['brazilianids_enable_cpfcnpj'] = array(
    '#default_value' => variable_get('brazilianids_enable_cpfcnpj', FALSE),
    '#description' => t('This option works only when fields CPF and CNPJ are enabled.'),
    '#title' => t('Use the same field for CPF or CNPJ.'),
    '#type' => 'checkbox',
  );
  $form['brazilianids_field_weight'] = array(
    '#type' => 'select',
    '#title' => t('Field weight'),
    '#description' => t('The field weight in the form.'),
    '#default_value' => check_plain(variable_get('brazilianids_field_weight', 0)),
    '#options' => drupal_map_assoc(range(-20, 20)),
  );

  // It will determine if these Brazilian IDs are
  // required fields
  $form['brazilianids_required'] = array(
    '#default_value' => variable_get('brazilianids_required', FALSE),
    '#description' => t('Are these fields required?'),
    '#title' => t('Required'),
    '#type' => 'checkbox',
  );

  // It will determine if these Brazilian IDs should
  // displayed during user registration
  $form['brazilianids_registration'] = array(
    '#default_value' => variable_get('brazilianids_registration', FALSE),
    '#description' => t('Enable the these fields during user registration?'),
    '#title' => t('User registration'),
    '#type' => 'checkbox',
  );
  $form['#submit'][] = 'brazilianids_settings_submit';
  return system_settings_form($form);
}

/**
 * Implementation of hook_menu().
 */
function brazilianids_menu() {
  $items = array();
  $items['admin/user/brazilianids'] = array(
    'access arguments' => array(
      'administer users',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      '_brazilianids_admin',
    ),
    'title' => 'Brazilian IDs',
    'type=' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implementation of hook_user().
 */
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),
        );
      }
  }
}

/**
 * Implementation of hook_settings_submit().
 */
function brazilianids_settings_submit($form, &$form_state) {
  foreach ($form_state['values'] as $key => $form_value) {
    if ($key != 'brazilianids_registration' and $key != 'brazilianids_required') {
      eval('$' . $key . '=$form_value;');
    }
  }
  if ($brazilianids_enable_cpf == TRUE and $brazilianids_enable_cnpj == TRUE and $brazilianids_enable_cpfcnpj == TRUE) {
    variable_set('brazilianids_cpfcnpj', TRUE);
    $form_state['values']['brazilianids_enable_cpfcnpj'] = TRUE;
    variable_set('brazilianids_cpf', FALSE);
    $form_state['values']['brazilianids_enable_cpf'] = TRUE;
    variable_set('brazilianids_cnpj', FALSE);
    $form_state['values']['brazilianids_enable_cnpj'] = TRUE;
  }
  else {
    variable_set('brazilianids_cpf', $brazilianids_enable_cpf);
    $form_state['values']['brazilianids_enable_cpf'] = $brazilianids_enable_cpf;
    variable_set('brazilianids_cnpj', $brazilianids_enable_cnpj);
    $form_state['values']['brazilianids_enable_cnpj'] = $brazilianids_enable_cnpj;
    variable_set('brazilianids_cpfcnpj', FALSE);
    $form_state['values']['brazilianids_enable_cpfcnpj'] = FALSE;
    if ($brazilianids_enable_cpf != $brazilianids_enable_cnpj and $brazilianids_enable_cpfcnpj == TRUE) {
      drupal_set_message(t('"Use the same field for CPF or CNPJ" was disabled because you enabled only one that fields, CPF or CNPJ.'), 'warning');
    }
  }
}

Functions

Namesort descending Description
brazilianids_menu Implementation of hook_menu().
brazilianids_settings_submit Implementation of hook_settings_submit().
brazilianids_user Implementation of hook_user().
_brazilianids_admin Module settings. Let users should which fields are needed