You are here

webform_html_textarea.module in Webform HTML Textarea 7

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

Webform module textarea component.

File

webform_html_textarea.module
View source
<?php

/**
 * @file
 * Webform module textarea component.
 */

/**
 * Implements hook_webform_component_info().
 */
function webform_html_textarea_webform_component_info() {
  return array(
    'html_textarea' => array(
      'label' => t('HTML Textarea'),
      'description' => t('A large text area that allows for multiple lines of input in HMTL. Supoorts wysiwyg'),
      'features' => array(
        'spam_analysis' => TRUE,
      ),
    ),
  );
}

/**
 * Implements _webform_defaults_component().
 */
function _webform_defaults_html_textarea() {
  return array(
    'name' => '',
    'form_key' => NULL,
    'pid' => 0,
    'weight' => 0,
    'value' => '',
    'mandatory' => 0,
    'extra' => array(
      'cols' => '',
      'rows' => '',
      'title_display' => 0,
      'resizable' => 1,
      'disabled' => 0,
      'description' => '',
      'attributes' => array(),
      'format' => filter_default_format(),
      'show_format_fieldset' => 1,
      'show_tips' => 1,
      'show_link' => 1,
      'private' => FALSE,
    ),
  );
}

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

/**
 * Implements _webform_edit_component().
 */
function _webform_edit_html_textarea($component) {
  $form = array();
  $form['#tree'] = TRUE;
  $form['value'] = array(
    '#type' => 'text_format',
    '#base_type' => 'textarea',
    '#title' => t('Default value'),
    '#default_value' => check_markup($component['value'], $component['extra']['format']),
    '#description' => t('The default value of the field.') . theme('webform_token_help'),
    '#cols' => 60,
    '#rows' => 5,
    '#weight' => 0,
    '#format' => $component['extra']['format'],
  );
  $form['display']['cols'] = array(
    '#type' => 'textfield',
    '#title' => t('Width'),
    '#default_value' => $component['extra']['cols'],
    '#description' => t('Width of the textarea.') . ' ' . t('Leaving blank will use the default size.'),
    '#size' => 5,
    '#maxlength' => 10,
    '#parents' => array(
      'extra',
      'cols',
    ),
  );
  $form['display']['rows'] = array(
    '#type' => 'textfield',
    '#title' => t('Height'),
    '#default_value' => $component['extra']['rows'],
    '#description' => t('Height of the textarea.') . ' ' . t('Leaving blank will use the default size.'),
    '#size' => 5,
    '#maxlength' => 10,
    '#parents' => array(
      'extra',
      'rows',
    ),
  );
  $form['display']['resizable'] = array(
    '#type' => 'checkbox',
    '#title' => t('Resizable'),
    '#description' => t('Make this field resizable by the user.'),
    '#weight' => 2,
    '#default_value' => $component['extra']['resizable'],
    '#parents' => array(
      'extra',
      'resizable',
    ),
  );
  $form['display']['disabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('Disabled'),
    '#return_value' => 1,
    '#description' => t('Make this field non-editable. Useful for setting an unchangeable default value.'),
    '#weight' => 11,
    '#default_value' => $component['extra']['disabled'],
    '#parents' => array(
      'extra',
      'disabled',
    ),
  );
  $form['display']['show_format_fieldset'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show format selection'),
    '#description' => t('Show format selection to the user. @notice', array(
      '@notice' => t('Note: The user won\'t be able to change the selected format, he\'ll just be able to see it.'),
    )),
    '#weight' => 10,
    '#default_value' => $component['extra']['show_format_fieldset'],
    '#parents' => array(
      'extra',
      'show_format_fieldset',
    ),
  );
  $form['display']['show_tips'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show filter tips'),
    '#description' => t('Show the filter tips to the user.'),
    '#weight' => 11,
    '#default_value' => $component['extra']['show_tips'],
    '#parents' => array(
      'extra',
      'show_tips',
    ),
    '#states' => array(
      'visible' => array(
        ':input[name="extra[show_format_fieldset]"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form['display']['show_link'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show "More information about text formats" link'),
    '#description' => t('Show the "More information about text formats" link to the user.'),
    '#weight' => 11,
    '#default_value' => $component['extra']['show_link'],
    '#parents' => array(
      'extra',
      'show_link',
    ),
    '#states' => array(
      'visible' => array(
        ':input[name="extra[show_format_fieldset]"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  return $form;
}

/**
 * Implements hook_webform_component_presave().
 */
function webform_html_textarea_webform_component_presave(&$component) {
  if ($component['type'] == 'html_textarea') {

    // Components saved via UI will have value in an array keyed by value and format
    if (isset($component['value']) && is_array($component['value'])) {
      $component['extra']['format'] = $component['value']['format'];
      $value = $component['value']['value'];
    }
    else {
      $value = isset($component['value']) ? $component['value'] : '';
    }
    $format = isset($component['extra']['format']) ? $component['extra']['format'] : filter_default_format();
    $component['value'] = check_markup($value, $format);
  }
}

/**
 * Implements _webform_render_component().
 */
function _webform_render_html_textarea($component, $value = NULL, $filter = TRUE) {
  $element = array(
    '#weight' => $component['weight'],
    '#webform_component' => $component,
    '#type' => 'text_format',
    '#base_type' => 'textarea',
    '#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
    '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
    '#default_value' => $filter ? _webform_filter_values($component['value']) : $component['value'],
    '#required' => $component['mandatory'],
    '#weight' => $component['weight'],
    '#description' => $filter ? _webform_filter_descriptions($component['extra']['description']) : $component['extra']['description'],
    '#rows' => !empty($component['extra']['rows']) ? $component['extra']['rows'] : 5,
    '#cols' => !empty($component['extra']['cols']) ? $component['extra']['cols'] : 60,
    '#attributes' => $component['extra']['attributes'],
    '#resizable' => (bool) $component['extra']['resizable'],
    // MUST be FALSE to disable.
    '#format' => $component['extra']['format'],
    '#after_build' => array(
      '_webform_html_textarea_after_build',
    ),
  );
  if ($component['extra']['disabled']) {
    $element['#attributes']['readonly'] = 'readonly';
  }
  if (isset($value)) {
    $element['#default_value'] = check_markup($value[0], $component['extra']['format']);
  }
  return $element;
}
function _webform_html_textarea_after_build($element) {

  //wysiwyg needs the select box to function

  //we will hide the select box and remove all

  //filter formats except the one selected for

  //this component
  if ($element['#type'] == 'text_format') {
    $format = $element['#webform_component']['extra']['format'];
    if (!isset($element['format']['format']['#options'][$format])) {
      $format = filter_default_format();
    }
    $element['format']['format']['#options'] = array(
      $format => $element['format']['format']['#options'][$format],
    );
    $element['format']['format']['#attributes']['disabled'] = 'disabled';
    if (!$element['#webform_component']['extra']['show_format_fieldset']) {
      $element['format']['#attributes']['style'] = 'display:none;';
    }
    $element['format']['format']['#description'] = t('This text format will be used to filter your html. You can not change this.');
    if (!$element['#webform_component']['extra']['show_tips']) {
      unset($element['format']['guidelines']);
    }
    if (!$element['#webform_component']['extra']['show_link']) {
      unset($element['format']['help']);
    }
  }
  return $element;
}

/**
 * Implements _webform_display_component().
 */
function _webform_display_html_textarea($component, $value, $format = 'html') {
  return array(
    '#title' => $component['name'],
    '#weight' => $component['weight'],
    '#theme' => 'webform_display_html_textarea',
    '#theme_wrappers' => $format == 'html' ? array(
      'webform_element',
    ) : array(
      'webform_element_text',
    ),
    '#format' => $format,
    '#value' => isset($value[0]) ? check_markup($value[0], $component['extra']['format']) : '',
    '#webform_component' => $component,
  );
}

/**
 * Implements _webform_submit_component().
 */
function _webform_submit_html_textarea($component, $value) {
  $val = isset($value['value']) ? check_markup($value['value'], $component['extra']['format']) : '';
  return array(
    $val,
  );
}

/**
 * Format the output of data for this component.
 */
function theme_webform_display_html_textarea($variables) {
  $element = $variables['element'];
  $output = check_markup($element['#value'], $element['#webform_component']['extra']['format']);
  $output = $element['#format'] == 'html' ? '<div class="webform-html-textarea">' . $output . '</div>' : $output;
  return $output !== '' ? $output : ' ';
}

/**
 * Implements _webform_analysis_component().
 */
function _webform_analysis_html_textarea($component, $sids = array()) {
  $query = db_select('webform_submitted_data', 'wsd', array(
    'fetch' => PDO::FETCH_ASSOC,
  ))
    ->fields('wsd', array(
    'no',
    'data',
  ))
    ->condition('nid', $component['nid'])
    ->condition('cid', $component['cid']);
  if (count($sids)) {
    $query
      ->condition('sid', $sids, 'IN');
  }
  $nonblanks = 0;
  $submissions = 0;
  $wordcount = 0;
  $stripped_wordcount = 0;
  $result = $query
    ->execute();
  foreach ($result as $data) {
    if (drupal_strlen(trim($data['data'])) > 0) {
      $nonblanks++;
      $wordcount += str_word_count(trim($data['data']));
      $stripped_wordcount += str_word_count(strip_tags($data['data']));
    }
    $submissions++;
  }
  $rows[0] = array(
    t('Left Blank'),
    $submissions - $nonblanks,
  );
  $rows[1] = array(
    t('User entered value'),
    $nonblanks,
  );
  $rows[2] = array(
    t('Average submission length in words (ex blanks)'),
    $nonblanks != 0 ? number_format($wordcount / $nonblanks, 2) : '0',
  );
  $rows[3] = array(
    t('Average submission length in words (ex blanks, without HTML tags)'),
    $nonblanks != 0 ? number_format($stripped_wordcount / $nonblanks, 2) : '0',
  );
  return $rows;
}

/**
 * Implements _webform_table_component().
 */
function _webform_table_html_textarea($component, $value) {
  return empty($value[0]) ? '' : check_markup($value[0], $component['extra']['format']);
}

/**
 * Implements _webform_csv_headers_component().
 */
function _webform_csv_headers_html_textarea($component, $export_options) {
  $header = array();
  $header[0] = '';
  $header[1] = '';
  $header[2] = $component['name'];
  return $header;
}

/**
 * Implements _webform_csv_data_component().
 */
function _webform_csv_data_html_textarea($component, $export_options, $value) {
  return empty($value[0]) ? '' : check_markup($value[0], $component['extra']['format']);
}