View source
<?php
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;
}
function webform_select_image_theme() {
$theme = array(
'webform_element_select_image' => array(
'render element' => 'element',
),
);
return $theme;
}
function _webform_select_image_edit_validate($element, &$form_state) {
module_load_include('inc', 'webform_select_image', 'components/select_image');
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']);
$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++;
}
}
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);
}
}
}
$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;
}
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'];
}
}
}
}
function _webform_select_managed_file_validate($element, &$form_state, $form) {
if ($form_state['triggering_element']['#name'] == $element['remove_button']['#name']) {
$form_state['images_to_delete'][] = $element['#file']->fid;
}
}
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.'));
}
}
function theme_webform_element_select_image($variables) {
$variables['element'] += array(
'#title_display' => 'before',
);
$element = $variables['element'];
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'];
}
$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 (!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 .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
break;
}
if (!empty($element['#description'])) {
$output .= ' <div class="description">' . $element['#description'] . "</div>\n";
}
$output .= "</div>\n";
return $output;
}