You are here

webform_structured_text.module in Webform Structured Text 7

Same filename and directory in other branches
  1. 6 webform_structured_text.module

Main module file for Webform Structured Text.

@author Shawn Sheridan <Shawn@SDSheridan.com>

File

webform_structured_text.module
View source
<?php

/**
 * @file
 * Main module file for Webform Structured Text.
 *
 * @author Shawn Sheridan <Shawn@SDSheridan.com>
 */

/**
 * Implements hook_help().
 */
function webform_structured_text_help($section = 'admin/help#webform_structured_text', $arg = NULL) {
  switch ($section) {
    case 'admin/help#webform_structured_text':

      // Return a line-break version of the module README.txt
      return nl2br(file_get_contents(drupal_get_path('module', 'webform_structured_text') . '/README.txt'));
  }
  return '';
}

/**
 * Implements hook_webform_component_info().
 */
function webform_structured_text_webform_component_info() {
  $components = array();
  $components['structured_text'] = array(
    'label' => t('Structured textfield'),
    'description' => t('A textfield type that uses an input mask, rendering the portions of the masked field as separate text fields.'),
    'features' => array(
      'csv' => TRUE,
      'default_value' => TRUE,
      'description' => TRUE,
      'email' => TRUE,
      'email_address' => FALSE,
      'email_name' => TRUE,
      'required' => TRUE,
      'title' => TRUE,
      'title_display' => TRUE,
      'title_inline' => TRUE,
      'conditional' => TRUE,
      'group' => FALSE,
      'spam_analysis' => FALSE,
      'attachment' => FALSE,
    ),
    'file' => 'structured_text.inc',
  );
  return $components;
}

/**
 * Implements hood_webform_validator_alter.  Note that webform_validation.validators.inc's
 * _webform_validation_check_false($var) function must be patched to filter out empty
 * strings as well as just the array elements that are FALSE for this to work.
 * @param array $validators The array of validators.
 */
function webform_structured_text_webform_validator_alter(&$validators) {
  foreach (array(
    'equal',
    'unique',
    'oneoftwo',
    'oneofseveral',
  ) as $validator) {
    $validators[$validator]['component_types'][] = 'structured_text';
  }
}

/**********************************
 * Translation-related functions.
 */

/**
* Implements hook_i18n_string_info()
*/
function webform_structured_text_i18n_string_info() {
  $groups['webform_structured_text'] = array(
    'title' => t('Webform Structured Text'),
    'description' => t('User-defined error messages and mask components for localisable webform structured text fields.'),
    'format' => FALSE,
    // This group doesn't have strings with format
    'list' => TRUE,
    // This group can list all strings
    'refresh callback' => 'webform_structured_text_i18n_string_refresh',
  );
  return $groups;
}

/**
 * Implements hook_i18n_string_list().
 */
function webform_structured_text_i18n_string_list($group) {
  if ($group == 'webform_structured_text' || $group == 'all') {
    module_load_include('inc', 'webform_structured_text', 'structured_text');
    $strings = array();
    $components = db_query("SELECT nid, cid, form_key, name, extra FROM webform_component WHERE type = 'structured_text'")
      ->fetchAll();
    foreach ($components as $component) {
      $component->extra = unserialize($component->extra);

      // First, deal with the markup elements of the mask.
      if (isset($component->extra['mask']) && !empty($component->extra['mask'])) {
        $mask = webform_structured_text_parse_mask($component->extra['mask']);
        foreach ($mask as $index => $part) {
          if ($part['type'] == 'markup') {
            $strings['webform_structured_text'][$component->nid][$component->cid]['mask'][$index] = $part['value'];
          }
        }
      }

      // Next, the error messages in the RegEx
      if (isset($component->extra['mask_regex']) && !empty($component->extra['mask_regex'])) {
        $regex = array_filter(explode("\n", $component->extra['mask_regex']));
        foreach ($regex as $line) {
          list($line_numb, $error_message, $pattern) = explode('|', $line, 3);
          $strings['webform_structured_text'][$component->nid][$component->cid]['regex'][$line_numb]['message'] = trim($error_message);
        }
      }
    }
    return $strings;
  }
}

/**
 * Custom translation function for portions of the webform structured text that need to be translated.
 * @param string $name The context name that will be appended to the textgroup and a colon, to be passed through i18n_string.
 * @param string $string The string to be translated.
 * @param string $langcode (Optional) the language code.
 * @param string $textgroup (Optional - defaults to 'webform_structured_text') the textgroup to use.
 * @return string The translated string.
 */
function _webform_structured_text_t($name, $string, $langcode = NULL, $textgroup = 'webform_structured_text') {
  return function_exists('i18n_string') ? i18n_string($textgroup . ':' . $name, $string, array(
    'langcode' => $langcode,
  )) : $string;
}

/**
 * Update translatable strings.
 * @param array $component A single component for which to update or remove translations.
 */
function webform_structured_text_i18n_update_strings($component = NULL, $op = 'update') {
  if (!function_exists('i18n_string_update')) {
    return;
  }
  $update_function = $op == 'update' ? 'i18n_string_update' : 'i18n_string_remove';
  if (empty($component)) {
    $components = db_query("SELECT nid, cid, form_key, name, extra FROM {webform_component} WHERE type = 'structured_text'")
      ->fetchAll();
  }
  else {
    $components = array(
      (object) $component,
    );
  }
  module_load_include('inc', 'webform_structured_text', 'structured_text');
  foreach ($components as $component) {
    if (!is_array($component->extra)) {
      $component->extra = unserialize($component->extra);
    }

    // First, deal with the markup elements of the mask.
    if (isset($component->extra['mask']) && !empty($component->extra['mask'])) {
      $mask = webform_structured_text_parse_mask($component->extra['mask']);
      foreach ($mask as $index => $part) {
        if ($part['type'] == 'markup') {
          $update_function("webform_structured_text:{$component->nid}:{$component->cid}:mask:{$index}", $part['value']);
        }
      }
    }

    // Next, the error messages in the RegEx
    if (isset($component->extra['mask_regex']) && !empty($component->extra['mask_regex'])) {
      $regex = array_filter(explode("\n", $component->extra['mask_regex']));
      foreach ($regex as $line) {
        list($line_numb, $error_message, $pattern) = explode('|', $line, 3);
        $update_function("webform_structured_text:{$component->nid}:{$component->cid}:regex:{$line_numb}:message", trim($error_message));
      }
    }
  }
}

/**
 * Implements hook_i18n_string_refresh().
 *
 * Refresh translations for all user-generated strings managed by webform_structured_text.
 * This will load all strings inputted via the webform_structured_text user interface and
 * register them (and their translations, if there are any) with the
 * i18n_strings system.
 */
function webform_structured_text_i18n_string_refresh() {
  webform_structured_text_i18n_update_strings();
  return TRUE;
}

Functions

Namesort descending Description
webform_structured_text_help Implements hook_help().
webform_structured_text_i18n_string_info Implements hook_i18n_string_info()
webform_structured_text_i18n_string_list Implements hook_i18n_string_list().
webform_structured_text_i18n_string_refresh Implements hook_i18n_string_refresh().
webform_structured_text_i18n_update_strings Update translatable strings.
webform_structured_text_webform_component_info Implements hook_webform_component_info().
webform_structured_text_webform_validator_alter Implements hood_webform_validator_alter. Note that webform_validation.validators.inc's _webform_validation_check_false($var) function must be patched to filter out empty strings as well as just the array elements that are FALSE for this to work.
_webform_structured_text_t Custom translation function for portions of the webform structured text that need to be translated.