You are here

function cck_phone_field_settings in Phone Number 6

Implementation of hook_field_settings().

File

./cck_phone.module, line 68
Defines phone number fields for CCK. Provide some verifications on the phone numbers

Code

function cck_phone_field_settings($op, $field) {
  switch ($op) {
    case 'form':
      drupal_add_css(drupal_get_path('module', 'cck_phone') . '/cck_phone.css');
      drupal_add_js(drupal_get_path('module', 'cck_phone') . '/cck_phone.js');
      $form = array();
      $form['default_country'] = array(
        '#type' => 'select',
        '#title' => t('Default country code'),
        '#default_value' => isset($field['default_country']) && $field['default_country'] !== '' ? $field['default_country'] : NULL,
        '#options' => _cck_phone_cc_options(TRUE),
      );
      $form['all_country_codes'] = array(
        '#type' => 'checkbox',
        '#title' => t('Show all country codes.'),
        '#default_value' => isset($field['all_country_codes']) && $field['all_country_codes'] !== '' ? $field['all_country_codes'] : TRUE,
        '#description' => t('Uncheck this to select the country to be displayed.'),
      );

      // Country codes settings
      $form['country_codes'] = array(
        '#title' => 'Country selection',
        '#type' => 'fieldset',
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#attributes' => array(
          'class' => 'cck-phone-settings',
        ),
      );
      $form['country_codes']['country_selection'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Select country codes to be included'),
        '#default_value' => isset($field['country_selection']) && !empty($field['country_selection']) ? $field['country_selection'] : array(
          $field['default_country'] => $field['default_country'],
        ),
        '#options' => _cck_phone_cc_options(TRUE),
        '#description' => t('Country marks with <em>*</em> has custom country code settings and/or validation.'),
      );
      $form['enable_custom_country'] = array(
        '#type' => 'checkbox',
        '#title' => t('Enable country level validation'),
        '#default_value' => isset($field['enable_custom_country']) && $field['enable_custom_country'] !== '' ? $field['enable_custom_country'] : TRUE,
        '#description' => t('Uncheck this to disable stringent country phone number validation.'),
      );
      $form['enable_extension'] = array(
        '#type' => 'checkbox',
        '#title' => t('Enable phone extension support'),
        '#default_value' => isset($field['enable_extension']) && $field['enable_extension'] !== '' ? $field['enable_extension'] : FALSE,
        '#description' => t('Check this to enable phone number extension field.'),
      );
      $form['enable_mobile'] = array(
        '#type' => 'checkbox',
        '#title' => t('Enable mobile device detection'),
        '#default_value' => isset($field['enable_mobile']) && $field['enable_mobile'] !== '' ? $field['enable_mobile'] : FALSE,
        '#description' => t('Check this to enable phone number link on mobile browsers (RFC3966).'),
      );

      // Display country specific settings
      foreach (_cck_phone_custom_cc() as $cc) {
        $function = $cc . '_phone_field_settings';
        if (function_exists($function)) {
          $country_settings = $function($op, $field);
          if (isset($country_settings) && !empty($country_settings)) {
            $country_codes = cck_phone_countrycodes($cc);

            // Wrap with fieldset
            $wrapper = array(
              '#title' => $country_codes['country'] . ' specific settings',
              '#type' => 'fieldset',
              '#collapsible' => TRUE,
              '#collapsed' => TRUE,
              '#attributes' => array(
                'class' => 'cck-phone-settings cck-phone-settings-' . $cc,
              ),
            );
            $wrapper[] = $country_settings;
            array_push($form, $wrapper);
          }
        }
      }
      return $form;
    case 'validate':

      // Validate country specific settings
      foreach (_cck_phone_custom_cc() as $cc) {
        $function = $cc . '_phone_field_settings';
        if (function_exists($function)) {
          $function($op, $field);
        }
      }
      break;
    case 'save':
      $settings = array(
        'default_country',
        'all_country_codes',
        'country_selection',
        'enable_custom_country',
        'enable_extension',
        'enable_mobile',
      );

      // Save country specific settings
      foreach (_cck_phone_custom_cc() as $cc) {
        $function = $cc . '_phone_field_settings';
        if (function_exists($function)) {
          array_push($settings, $function($op, $field));
        }
      }
      return $settings;

    // TODO: filters for phone number?
    //    case 'filters':
    //      break;
    case 'database columns':
      return array(
        'number' => array(
          'type' => 'varchar',
          'length' => CCK_PHONE_PHONE_MAX_LENGTH,
          'not null' => FALSE,
        ),
        'country_codes' => array(
          'type' => 'varchar',
          'length' => CCK_PHONE_CC_MAX_LENGTH,
          'not null' => FALSE,
        ),
        'extension' => array(
          'type' => 'varchar',
          'length' => CCK_PHONE_EXTENSION_MAX_LENGTH,
          'not null' => FALSE,
        ),
      );
  }
}