You are here

webform_phone.components.inc in Webform Phone Number 7.2

Same filename and directory in other branches
  1. 7 webform_phone.components.inc

Webform Component information for a phone number field type

File

webform_phone.components.inc
View source
<?php

/**
 * @file
 * Webform Component information for a phone number field type
 */

//Save us from having to figure this out in multiple places
define('PHONE_MODULE_PATH', drupal_get_path('module', 'phone'));

/**
 * Specify the default properties of a component.
 * @return
 *   An array defining the default structure of a component.
 */
function _webform_defaults_phone() {
  return array(
    'name' => '',
    'form_key' => NULL,
    'required' => 0,
    'mandatory' => 0,
    'pid' => 0,
    'weight' => 0,
    'value' => '',
    'extra' => array(
      'title_display' => 0,
      'width' => '',
      'disabled' => FALSE,
      'private' => FALSE,
      'attributes' => array(),
      'description' => '',
      'widget_settings' => array(
        //Below are specific to phone field
        'country_options' => array(
          'enable_default_country' => TRUE,
          'default_country' => NULL,
          'all_country_codes' => TRUE,
          'country_codes' => array(
            'hide_single_cc' => FALSE,
            'country_selection' => array(),
          ),
          'country_code_position' => 'after',
        ),
        'enable_comment' => TRUE,
        'comment_allowed_values_position' => 'before',
        'enable_extension' => FALSE,
        'use_tel_input' => TRUE,
      ),
      'field_settings' => array(
        'number_size' => 15,
        'extension_size' => 6,
        'comment_allowed_values_type' => 'custom',
        'comment_allowed_values' => array(
          'home' => t('Home'),
          'work' => t('Work'),
          'mobile' => t('Mobile'),
          'fax' => t('Fax'),
        ),
        'comment_allowed_values_function' => '',
      ),
      'display_settings' => array(
        'as_tel_link' => FALSE,
        'full_hcard' => FALSE,
        'allow_alpha' => FALSE,
        'components' => array(
          'comment' => 'comment',
          'extension' => 'extension',
        ),
      ),
    ),
  );
}

/**
 * Implements _webform_theme_component().
 */
function _webform_theme_phone() {
  return array(
    'webform_display_phonefield' => array(
      'render element' => 'element',
    ),
  );
}

/**
 * Generate the form for editing a component.
 * Create a set of form elements to be displayed on the form for editing this
 * component. Use care naming the form items, as this correlates directly to the
 * database schema. The component "Name" and "Description" fields are added to
 * every component type and are not necessary to specify here (although they
 * may be overridden if desired).
 *
 * @param $component
 *   A Webform component array.
 *
 * @return
 *   An array of form items to be displayed on the edit component page
 */
function _webform_edit_phone($component) {
  require_once PHONE_MODULE_PATH . '/phone.module';

  //not really needed, since .modules are always in the namespace
  $field_widget_instance = array(
    'widget' => array(
      'type' => 'phone',
      'settings' => $component['extra']['widget_settings'],
    ),
  );
  $field_instance = array(
    'type' => 'phone',
    'settings' => $component['extra']['field_settings'],
  );
  $display_instance = array(
    'display' => array(
      'webform' => array(
        'settings' => $component['extra']['display_settings'],
      ),
    ),
  );
  $phone_widget_settings_form = phone_field_widget_settings_form(array(), $field_widget_instance);
  $phone_field_settings_form = phone_field_settings_form($field_instance, NULL, FALSE);
  $fake_form_state = array();
  $phone_display_settings_form = phone_field_formatter_settings_form(NULL, $display_instance, 'webform', NULL, $fake_form_state);

  //Copy the format used by the field portion of the Phone module...so their 'state' attrs will still work
  $form = array(
    'instance' => array(
      '#type' => 'fieldset',
      '#collapsible' => FALSE,
      '#title' => t('Phone Settings'),
      'widget' => array(
        'settings' => $phone_widget_settings_form,
      ),
      'field' => array(
        '#type' => 'fieldset',
        '#collapsible' => FALSE,
        '#title' => t('Widget Settings'),
        'settings' => $phone_field_settings_form,
      ),
      'display' => array(
        '#type' => 'fieldset',
        '#collapsible' => FALSE,
        '#title' => t('Display Settings'),
        '#description' => t('Settings for how this field\'s value will be rendered.'),
        'settings' => $phone_display_settings_form,
      ),
    ),
  );
  return $form;
}

/**
 * Modify a Webform component before it is saved to the database.
 * Note that most of the time this hook is not necessary, because Webform will
 * automatically add data to the component based on the component form. Using
 * hook_form_alter() will be sufficient in most cases.
 * @see hook_form_alter()
 * @see webform_component_edit_form()
 *
 * @param $component
 *   The Webform component being saved.
 */
function webform_phone_webform_component_presave(&$component) {
  if ($component['type'] == 'phone' && isset($component['instance'])) {

    //Move the settings to where they belong
    $component['extra']['widget_settings'] = $component['instance']['widget']['settings'];
    $component['extra']['field_settings'] = $component['instance']['field']['settings'];
    $component['extra']['display_settings'] = $component['instance']['display']['settings'];

    //Remove their old location
    unset($component['instance']['widget']['settings']);
    unset($component['instance']['field']['settings']);
    unset($component['instance']['display']['settings']);
  }
}

/**
 * Render a Webform component to be part of a form.
 *
 * @param $component
 *   A Webform component array.
 * @param $value
 *   If editing an existing submission or resuming a draft, this will contain
 *   an array of values to be shown instead of the default in the component
 *   configuration. This value will always be an array, keyed numerically for
 *   each value saved in this field.
 * @param $filter
 *   Whether or not to filter the contents of descriptions and values when
 *   rendering the component. Values need to be unfiltered to be editable by
 *   Form Builder.
 *
 * @see _webform_client_form_add_component()
 */
function _webform_render_phone($component, $value = NULL, $filter = TRUE) {
  require_once PHONE_MODULE_PATH . '/includes/phone.element.inc';
  drupal_add_css(drupal_get_path('module', 'webform_phone') . '/webform_phone.css');
  $phone_settings = array_merge($component['extra']['widget_settings'], $component['extra']['field_settings']);
  $phone_element = array(
    '#phone_settings' => $phone_settings,
    '#value' => $value,
    '#required' => $component['required'] || $component['mandatory'],
    '#delta' => 0,
  );
  $form_state = array();
  $form_item = phone_element_process($phone_element, $form_state, array());
  $form_item['#element_validate'] = array(
    'webform_validate_phone',
  );
  $form_item['#theme_wrappers'] = array(
    'webform_element',
  );
  $form_item['#title'] = $filter ? _webform_filter_xss($component['name']) : $component['name'];
  $form_item['#title_display'] = $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before';
  $form_item['#weight'] = $component['weight'];
  $form_item['#description'] = $filter ? _webform_filter_descriptions($component['extra']['description']) : $component['extra']['description'];
  return $form_item;
}

/**
 * Validation Callback for phone field
 */
function webform_validate_phone($element, $form_state) {
  require_once PHONE_MODULE_PATH . '/includes/phone.element.inc';
  $element['#value'] = $form_state['values']['submitted'][$element['#webform_component']['form_key']];
  $element['#title'] = $element['#webform_component']['name'];
  phone_element_validate($element, $form_state);
}

/**
 * Display the result of a submission for a component.
 * The output of this function will be displayed under the "Results" tab then
 * "Submissions". This should output the saved data in some reasonable manner.
 *
 * @param $component
 *   A Webform component array.
 * @param $value
 *   An array of information containing the submission result, directly
 *   correlating to the webform_submitted_data database table schema.
 * @param $format
 *   Either 'html' or 'text'. Defines the format that the content should be
 *   returned as. Make sure that returned content is run through check_plain()
 *   or other filtering functions when returning HTML.
 *
 * @return
 *   A renderable element containing at the very least these properties:
 *    - #title
 *    - #weight
 *    - #component
 *    - #format
 *    - #value
 *   Webform also uses #theme_wrappers to output the end result to the user,
 *   which will properly format the label and content for use within an e-mail
 *   (such as wrapping the text) or as HTML (ensuring consistent output).
 */
function _webform_display_phone($component, $value, $format = 'html') {
  $rendered_value = _webform_phone_render_value($component, $value);
  return array(
    '#title' => $component['name'],
    '#weight' => $component['weight'],
    '#theme' => 'webform_display_phonefield',
    '#theme_wrappers' => $format == 'html' ? array(
      'webform_element',
    ) : array(
      'webform_element_text',
    ),
    '#post_render' => array(
      'webform_element_wrapper',
    ),
    '#component' => $component,
    '#format' => $format,
    '#markup' => $rendered_value,
  );
}

/**
 * Format the output of data for this component.
 */
function theme_webform_display_phonefield($variables) {
  $markup = array();
  $element = $variables['element'];
  $rendered_output = drupal_render($element['#markup']);
  if ($element['#format'] == 'html') {
    $markup['#markup'] = $rendered_output;
  }
  else {
    $markup['#markup'] = check_plain($rendered_output);
  }
  return drupal_render($markup);
}

/**
 * Calculate and returns statistics about results for this component.
 * This takes into account all submissions to this webform. The output of this
 * function will be displayed under the "Results" tab then "Analysis".
 *
 * @param $component
 *   An array of information describing the component, directly correlating to
 *   the webform_component database schema.
 * @param $sids
 *   An optional array of submission IDs (sid). If supplied, the analysis will
 *   be limited to these sids.
 * @param $single
 *   Boolean flag determining if the details about a single component are being
 *   shown. May be used to provided detailed information about a single
 *   component's analysis, such as showing "Other" options within a select list.
 *
 * @return
 *   An array of data rows, each containing a statistic for this component's
 *   submissions.
 */
function _webform_analysis_phone($component, $sids = array(), $single = FALSE) {

  // Generate the list of options and questions.
  $query = db_select('webform_submitted_data', 'wsd', array(
    'fetch' => PDO::FETCH_ASSOC,
  ))
    ->fields('wsd', array(
    'data',
  ))
    ->condition('nid', $component['nid'])
    ->condition('cid', $component['cid']);
  if (count($sids)) {
    $query
      ->condition('sid', $sids, 'IN');
  }
  $non_blanks = 0;
  $submissions = 0;
  $result = $query
    ->execute();
  foreach ($result as $data) {
    if (drupal_strlen(trim($data['data'])) > 0) {
      $non_blanks++;
    }
    $submissions++;
  }
  $rows[0] = array(
    t('Left Blank'),
    $submissions - $non_blanks,
  );
  $rows[1] = array(
    t('User entered value'),
    $non_blanks,
  );
  return $rows;
}

/**
 * Return the result of a component value for display in a table.
 * The output of this function will be displayed under the "Results" tab then
 * "Table".
 *
 * @param $component
 *   A Webform component array.
 * @param $value
 *   An array of information containing the submission result, directly
 *   correlating to the webform_submitted_data database schema.
 *
 * @return
 *   Textual output formatted for human reading.
 */
function _webform_table_phone($component, $value) {
  $rendered_value = _webform_phone_render_value($component, $value);
  return strip_tags($rendered_value);
}

/**
 * Return the header for this component to be displayed in a CSV file.
 * The output of this function will be displayed under the "Results" tab then
 * "Download".
 *
 * @param $component
 *   A Webform component array.
 * @param $export_options
 *   An array of options that may configure export of this field.
 *
 * @return
 *   An array of data to be displayed in the first three rows of a CSV file, not
 *   including either prefixed or trailing commas.
 */
function _webform_csv_headers_phone($component, $export_options) {
  $header = array();
  $header[0] = '';
  $header[1] = '';
  $header[2] = $component['name'];
  return $header;
}

/**
 * Format the submitted data of a component for CSV downloading.
 * The output of this function will be displayed under the "Results" tab then
 * "Download".
 *
 * @param $component
 *   A Webform component array.
 * @param $export_options
 *   An array of options that may configure export of this field.
 * @param $value
 *   An array of information containing the submission result, directly
 *   correlating to the webform_submitted_data database schema.
 *
 * @return
 *   An array of items to be added to the CSV file. Each value within the array
 *   will be another column within the file. This function is called once for
 *   every row of data.
 */
function _webform_csv_data_phone($component, $export_options, $value) {
  $rendered_value = _webform_phone_render_value($component, $value);
  return strip_tags($rendered_value);
}

/**
 * Use the Phone module to generate the rendered output for this field's value
 */
function _webform_phone_render_value($component, $value) {
  require_once PHONE_MODULE_PATH . '/phone.module';

  //not really needed, since .modules are always in the namespace
  $entity_type = 'node';
  $entity = NULL;
  $field_settings = $component['extra']['field_settings'];
  $field_settings['comment_allowed_values_function'] = '';
  $field = array(
    'id' => uniqid(),
    //Prevent static caching @todo: change this so phone module can use the static cache if possible
    'settings' => $field_settings,
  );
  $instance = array(
    'widget' => array(
      'settings' => $component['extra']['widget_settings'],
    ),
  );
  $items = array(
    $value,
  );

  //has to be wrapped in array, Phone thinks there could be more than one value ("add another")
  $display = array(
    'module' => 'phone',
    'weight' => $component['weight'],
    'label' => str_replace(array(
      'before',
    ), array(
      'above',
    ), $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before'),
    'type' => 'phone_rfc3966',
    'settings' => $component['extra']['display_settings'],
  );
  $langcode = 'und';
  $output = phone_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display);
  $output = array_pop($output);
  return drupal_render($output);
}

/**
 * @defgroup form-builder-webform-phone-callbacks Callbacks for the Phone component
 * @{
 */

/**
 * Implements _form_builder_webform_form_builder_types_component().
 */

//function _form_builder_webform_form_builder_types_phone() {

//  //Icon is from the RRZE icon set (http://rrze-icon-set.berlios.de/) licensed under creative commons
//  drupal_add_css(drupal_get_path('module', 'webform_phone') . '/webform_phone.css');
//  $fields = array();
//  $fields['phone'] = array(
//    'title'      => t('Phone Number'),
//    'properties' => array(
//      'country',
//      'phone_country_code',
//      'phone_default_country_code',
//      'phone_int_max_length',
//      'ca_phone_separator',
//      'ca_phone_parentheses',
//    ),
//    'weight'     => -17,
//    //Doesn't make sense that modules get to weight themselves, why wouldn't everyone want to be first?
//  );
//  $defaults = _webform_defaults_phone();
//  $fields['phone']['default'] = _form_builder_webform_default('phone');
//  $fields['phone']['default']['#title'] = t('New Phone Number Field');
//  $fields['phone']['default']['#country'] = $defaults['extra']['country'];
//  $fields['phone']['default']['#phone_country_code'] = $defaults['extra']['phone_country_code'];
//  $fields['phone']['default']['#phone_default_country_code'] = $defaults['extra']['phone_default_country_code'];
//  $fields['phone']['default']['#phone_int_max_length'] = $defaults['extra']['phone_int_max_length'];
//  $fields['phone']['default']['#ca_phone_separator'] = $defaults['extra']['ca_phone_separator'];
//  $fields['phone']['default']['#ca_phone_parentheses'] = $defaults['extra']['ca_phone_parentheses'];
//  return $fields;

//}

/**
 * Implements _form_builder_webform_form_builder_map_component().
 */

//function _form_builder_webform_form_builder_map_phone() {

//  return array(
//    'form_builder_type' => 'phone',
//    'properties'        => array(
//      'size'                       => array(
//        'storage_parents' => array( 'extra', 'width' ),
//      ),
//      'country'                    => array(
//        'form_parents'    => array( 'extra', 'country' ),
//        'storage_parents' => array( 'extra', 'country' ),
//      ),
//      'phone_country_code'         => array(
//        'form_parents'    => array( 'extra', 'phone_country_code' ),
//        'storage_parents' => array( 'extra', 'phone_country_code' ),
//      ),
//      'phone_default_country_code' => array(
//        'form_parents'    => array( 'extra', 'phone_default_country_code' ),
//        'storage_parents' => array( 'extra', 'phone_default_country_code' ),
//      ),
//      'phone_int_max_length'       => array(
//        'form_parents'    => array( 'extra', 'phone_int_max_length' ),
//        'storage_parents' => array( 'extra', 'phone_int_max_length' ),
//      ),
//      'ca_phone_separator'         => array(
//        'form_parents'    => array( 'extra', 'ca_phone_separator' ),
//        'storage_parents' => array( 'extra', 'ca_phone_separator' ),
//      ),
//      'ca_phone_parentheses'       => array(
//        'form_parents'    => array( 'extra', 'ca_phone_parentheses' ),
//        'storage_parents' => array( 'extra', 'ca_phone_parentheses' ),
//      ),
//    ),
//  );

//}

/**
 * @} End of "defgroup form-builder-webform-phone-callbacks"
 */

Functions

Namesort descending Description
theme_webform_display_phonefield Format the output of data for this component.
webform_phone_webform_component_presave Modify a Webform component before it is saved to the database. Note that most of the time this hook is not necessary, because Webform will automatically add data to the component based on the component form. Using hook_form_alter() will be sufficient…
webform_validate_phone Validation Callback for phone field
_webform_analysis_phone Calculate and returns statistics about results for this component. This takes into account all submissions to this webform. The output of this function will be displayed under the "Results" tab then "Analysis".
_webform_csv_data_phone Format the submitted data of a component for CSV downloading. The output of this function will be displayed under the "Results" tab then "Download".
_webform_csv_headers_phone Return the header for this component to be displayed in a CSV file. The output of this function will be displayed under the "Results" tab then "Download".
_webform_defaults_phone Specify the default properties of a component.
_webform_display_phone Display the result of a submission for a component. The output of this function will be displayed under the "Results" tab then "Submissions". This should output the saved data in some reasonable manner.
_webform_edit_phone Generate the form for editing a component. Create a set of form elements to be displayed on the form for editing this component. Use care naming the form items, as this correlates directly to the database schema. The component "Name" and…
_webform_phone_render_value Use the Phone module to generate the rendered output for this field's value
_webform_render_phone Render a Webform component to be part of a form.
_webform_table_phone Return the result of a component value for display in a table. The output of this function will be displayed under the "Results" tab then "Table".
_webform_theme_phone Implements _webform_theme_component().

Constants

Namesort descending Description
PHONE_MODULE_PATH