View source
<?php
if (module_exists('content')) {
module_load_include('inc', 'brazilianids', 'brazilianids_cck');
}
function _brazilianids_admin() {
$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)),
);
$form['brazilianids_required'] = array(
'#default_value' => variable_get('brazilianids_required', FALSE),
'#description' => t('Are these fields required?'),
'#title' => t('Required'),
'#type' => 'checkbox',
);
$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);
}
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;
}
function brazilianids_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'register':
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)),
);
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',
);
}
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',
);
}
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',
);
}
if (!count(element_children($form['brazilianids_group']))) {
unset($form['brazilianids_group']);
}
return $form;
case 'validate':
module_load_include('check.inc', 'brazilianids', 'brazilianids');
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.'));
}
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.'));
}
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':
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),
);
}
}
}
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');
}
}
}