You are here

webform_select_image.module in Webform Select Image 7

File

webform_select_image.module
View source
<?php

/**
 *  This module provides a select images webform component.
 *
 */

/**
 * Implements hook_webform_component_info().
 */
function webform_select_image_webform_component_info() {
  $components = array();
  $components['select_image'] = array(
    'label' => t('Select image'),
    'description' => t('Allow users to add a list of images displayed as checkboxes / radios'),
    'features' => array(
      'default_value' => FALSE,
      'required' => TRUE,
    ),
    'file' => 'components/select_image.inc',
  );
  return $components;
}

/**
 * Implements hook_theme().
 */
function webform_select_image_theme() {
  $theme = array(
    'webform_element_select_image' => array(
      'render element' => 'element',
    ),
  );
  return $theme;
}

// Fix webform ajax callabck undefined error / Put validate fuction in .module

/**
 * Set the appropriate webform values when using the options element module.
 */
function _webform_select_image_edit_validate($element, &$form_state) {
  module_load_include('inc', 'webform_select_image', 'components/select_image');

  // Save values
  if ($form_state['triggering_element']['#name'] == 'op') {
    $element_options = $form_state['values']['items'];
    $form_state['values']['extra']['layout'] = $form_state['values']['display']['layout'];
    $default_values = (array) $form_state['values']['value'];
    unset($form_state['values']['items']);
    unset($form_state['values']['value']);
    unset($form_state['values']['display']);

    // Images to save
    $count = 0;
    foreach ($element_options['images'] as $item) {
      if (!empty($item['src']['fid'])) {
        $file = file_load($item['src']['fid']);
        $file->status = FILE_STATUS_PERMANENT;
        file_save($file);
        file_usage_add($file, 'webform_select_image', 'node', $form_state['values']['nid']);
        $count++;
      }
    }

    // Image to delete
    if (!empty($form_state['images_to_delete'])) {
      foreach ($form_state['images_to_delete'] as $fid) {
        if ($file = file_load($fid)) {
          file_usage_delete($file, 'webform_select_image', 'node', $form_state['values']['nid']);
          file_delete($file);
        }
      }
    }

    // Convert array to text
    $form_state['values']['value'] = serialize($default_values);
    $form_state['values']['extra']['images'] = _webform_select_images_to_text($element_options['images'], 'custom');
    $form_state['values']['extra']['legends'] = _webform_select_legends_to_text($element_options['images'], 'custom');
    $form_state['values']['extra']['options'] = _webform_select_image_options_to_text($element_options['images'], 'custom');
    $form_state['values']['extra']['num_images'] = $count;
  }

  // Remove image
  if ($form_state['triggering_element']['#name'] == 'remove_image') {
    if (!empty($form_state['values']['items']['images'])) {
      $item = end($form_state['values']['items']['images']);
      if (!empty($item['src']['fid'])) {
        $form_state['images_to_delete'][] = $item['src']['fid'];
      }
    }
  }
}

/**
 * Callback validate for managed file element
 * @see _webform_edit_select_image()
 */
function _webform_select_managed_file_validate($element, &$form_state, $form) {

  // Remove images
  if ($form_state['triggering_element']['#name'] == $element['remove_button']['#name']) {
    $form_state['images_to_delete'][] = $element['#file']->fid;
  }
}

/**
 * Validate image legend
 */
function _webform_select_image_legend_validate($element, &$form_state) {
  $value = $element['#value'];
  $regex = "/[\\|,]/";
  if (!empty($value) && preg_match($regex, $value)) {
    form_error($element, t($element['#title'] . ' should not content special character.'));
  }
}

/**
 * Replacement for theme_webform_element().
 * FIXME: Find a another way to just add "webform-component-select-image" class to element
 */
function theme_webform_element_select_image($variables) {

  // Ensure defaults.
  $variables['element'] += array(
    '#title_display' => 'before',
  );
  $element = $variables['element'];

  // All elements using this for display only are given the "display" type.
  if (isset($element['#format']) && $element['#format'] == 'html') {
    $type = 'display';
  }
  else {
    $type = isset($element['#type']) && !in_array($element['#type'], array(
      'markup',
      'textfield',
      'webform_email',
      'webform_number',
    )) ? $element['#type'] : $element['#webform_component']['type'];
  }

  // Convert the parents array into a string, excluding the "submitted" wrapper.
  $nested_level = $element['#parents'][0] == 'submitted' ? 1 : 0;
  $parents = str_replace('_', '-', implode('--', array_slice($element['#parents'], $nested_level)));
  $wrapper_classes = array(
    'form-item',
    'webform-component',
    'webform-component-' . $type,
    'webform-component-select-image',
  );
  if ($element['#webform_component']['extra']['layout'] == 'horizontally') {
    $wrapper_classes[] = 'clearfix';
    drupal_add_css('#' . $element['#id'] . ' .form-type-radio {float:left;margin-left:20px;}', 'inline');
  }
  if (isset($element['#title_display']) && strcmp($element['#title_display'], 'inline') === 0) {
    $wrapper_classes[] = 'webform-container-inline';
  }
  $output = '<div class="' . implode(' ', $wrapper_classes) . '" id="webform-component-' . $parents . '">' . "\n";

  // If #title is not set, we don't display any label or required marker.
  if (!isset($element['#title'])) {
    $element['#title_display'] = 'none';
  }
  $prefix = isset($element['#field_prefix']) ? '<span class="field-prefix">' . _webform_filter_xss($element['#field_prefix']) . '</span> ' : '';
  $suffix = isset($element['#field_suffix']) ? ' <span class="field-suffix">' . _webform_filter_xss($element['#field_suffix']) . '</span>' : '';
  switch ($element['#title_display']) {
    case 'inline':
    case 'before':
    case 'invisible':
      $output .= ' ' . theme('form_element_label', $variables);
      $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
      break;
    case 'after':
      $output .= ' ' . $prefix . $element['#children'] . $suffix;
      $output .= ' ' . theme('form_element_label', $variables) . "\n";
      break;
    case 'none':
    case 'attribute':

      // Output no label and no required marker, only the children.
      $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
      break;
  }
  if (!empty($element['#description'])) {
    $output .= ' <div class="description">' . $element['#description'] . "</div>\n";
  }
  $output .= "</div>\n";
  return $output;
}

Functions

Namesort descending Description
theme_webform_element_select_image Replacement for theme_webform_element(). FIXME: Find a another way to just add "webform-component-select-image" class to element
webform_select_image_theme Implements hook_theme().
webform_select_image_webform_component_info Implements hook_webform_component_info().
_webform_select_image_edit_validate Set the appropriate webform values when using the options element module.
_webform_select_image_legend_validate Validate image legend
_webform_select_managed_file_validate Callback validate for managed file element