You are here

webform_select_opts.module in Webform Select options 7.2

Same filename and directory in other branches
  1. 7.3 webform_select_opts.module
  2. 7 webform_select_opts.module

Change textfield webform component to select options from nodetype fields.

File

webform_select_opts.module
View source
<?php

/**
 * @file
 * Change textfield webform component to select options from nodetype fields.
 */

/**
 * Implements hook_help().
 */
function webform_select_opts_help($path, $arg) {
  switch ($path) {
    case 'admin/config/content/webform/wfso':
      $output = t('First step: enable content type here. Fields on enabled content types are avalable in Second step: Set Field setting on tab "Select opts fields settings".');
      return $output;
    case 'admin/config/content/webform/wfso/fields_setting':
      $output = t('Settings to source for webform component data. Option VALUE is sending as data of the user choose by webform, Option TEXT is a Label of this component. "Option TEXT" and "Option VALUE" field must contain one (1) value - don\'t use field with multiple values or term like tags.');
      return $output;
  }
}

/**
 * Implements hook_menu().
 */
function webform_select_opts_menu() {
  $items['admin/config/content/webform/wfso'] = array(
    'title' => 'Webform Select options settings',
    'description' => 'Settings for webform Select options source',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'node_types_wfso_avalable_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/config/content/webform/wfso/webform_select_opts_settings'] = array(
    'title' => 'Webform Select options settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'node_types_wfso_avalable_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 0,
  );
  $items['admin/config/content/webform/wfso/webform_select_opts_settings'] = array(
    'title' => 'Webform Select options settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'node_types_wfso_avalable_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => 0,
  );
  $items['admin/config/content/webform/wfso/fields_setting'] = array(
    'title' => 'Select opts fields settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'node_types_wfso_fields_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 1,
  );
  return $items;
}

/**
 *  Settings form for the fields of avalable node types
 */
function node_types_wfso_fields_settings() {
  $form = array();
  $node_types = node_type_get_types();
  $fields_ct = field_info_instances('node');
  $form['#tree'] = TRUE;
  foreach ($node_types as $n_type) {
    if (variable_get('webform_select_options_for_' . $n_type->type, 0)) {
      $options = array();
      foreach ($fields_ct[$n_type->type] as $fields) {
        if ($fields['label'] == 'Body') {
          continue;
        }
        else {
          $options[$fields['field_name']] = $fields['label'];
        }
      }
      $variable_value = variable_get('webform_select_options_fields_' . $n_type->type, '');
      $fields_names = unserialize($variable_value);
      $form['webform_select_options_ntypes'][$n_type->type] = array(
        '#type' => 'fieldset',
        '#title' => t('@ntype\'s Fields settings', array(
          '@ntype' => $n_type->name,
        )),
        '#description' => t('Fields of node type @type to construct select options in webform.', array(
          '@type' => drupal_strtoupper($n_type->name),
        )),
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
        '#weight' => -1,
      );
      $form['webform_select_options_ntypes'][$n_type->type]['node_type'] = array(
        '#type' => 'hidden',
        '#value' => $n_type->type,
      );
      $form['webform_select_options_ntypes'][$n_type->type]['select_value'] = array(
        '#type' => 'select',
        '#title' => t('Option VALUE'),
        '#description' => t('The value of this field will send by form!'),
        '#options' => $options,
        '#default_value' => $fields_names[0],
      );
      $form['webform_select_options_ntypes'][$n_type->type]['select_value_is_email'] = array(
        '#type' => 'checkbox',
        '#title' => t('Field VALUE is e-mail address'),
        '#default_value' => $fields_names[2],
      );
      $form['webform_select_options_ntypes'][$n_type->type]['select_text'] = array(
        '#type' => 'select',
        '#attributes' => array(
          'disabled' => 'disabled',
        ),
        '#title' => t('Option TEXT'),
        '#options' => array(
          'node_title' => t('Node title'),
        ),
        '#default_value' => 'node_title',
      );
    }
  }
  $form['#submit'][] = 'webform_select_options_fields_settings_submit';
  return system_settings_form($form);
}
function webform_select_options_fields_settings_submit($form, &$form_state) {
  foreach ($form_state['input']['webform_select_options_ntypes'] as $n_type => $val) {
    $variable_value = serialize(array(
      $val['select_value'],
      $val['select_text'],
      $val['select_value_is_email'],
    ));
    variable_set('webform_select_options_fields_' . $n_type, $variable_value);
  }
}

/**
 * Settings form for avalable node types to create Search&Location block for webforms with its fields
 */
function node_types_wfso_avalable_settings() {
  $form = array();
  $node_types = node_type_get_types();
  $form['#tree'] = TRUE;
  $form['webform_select_options'] = array(
    '#type' => 'fieldset',
    '#title' => t('Webform select options source'),
    '#description' => t('Enable content type for webform component source.'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#weight' => -1,
  );
  foreach ($node_types as $n_type) {
    $form['webform_select_options'][$n_type->type] = array(
      '#type' => 'checkbox',
      '#title' => $n_type->name,
      '#default_value' => variable_get('webform_select_options_for_' . $n_type->type, 0),
    );
  }
  $form['#submit'][] = 'webform_select_options_node_types_submit';
  return system_settings_form($form);
}
function webform_select_options_node_types_submit($form, &$form_state) {
  foreach ($form_state['input']['webform_select_options'] as $n_type => $val) {
    if ($val) {
      variable_set('webform_select_options_for_' . $n_type, $val);
    }
    else {
      variable_del('webform_select_options_for_' . $n_type);
      variable_del('webform_select_options_fields_' . $n_type);
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter();
 */
function webform_select_opts_form_webform_client_form_alter(&$form, &$form_state, $form_id) {
  if (arg(0) == 'node' && is_numeric(arg(1)) && strstr($form_id, 'webform_client_form_')) {
    foreach ($form['submitted'] as $input) {
      if ($input['#webform_component']['type'] == ('textfield' || 'email')) {
        $nid = $input['#webform_component']['nid'];
        $cid = $input['#webform_component']['cid'];
        $n_type = db_select('webform_select_opts_component', 's')
          ->fields('s', array(
          'data',
        ))
          ->condition('nid', $nid, '=')
          ->condition('cid', $cid, '=')
          ->execute()
          ->fetchField();
        if ($n_type) {
          $var = variable_get('webform_select_options_fields_' . $n_type, '');
          $field_val = unserialize($var);
          if ($var) {
            if ($field_val[0] == 'node_title') {
              $query = db_select('node', 'n');
              $query
                ->fields('n', array(
                'nid',
                'title',
              ));
              $query
                ->condition('n.type', $n_type, '=');
              $query
                ->orderBy('title');
              $records = $query
                ->execute();
              foreach ($records as $record) {
                $options[$record->title] = $record->title;
              }
            }
            else {
              $query = db_select('node', 'n');
              $query
                ->join('field_data_' . $field_val[0], 'm', 'n.nid = m.entity_id');
              $query
                ->fields('n', array(
                'nid',
                'title',
              ));
              $query
                ->fields('m', array(
                $field_val[0] . '_value',
              ));
              $query
                ->condition('n.type', $n_type, '=');
              $query
                ->orderBy('title');
              $records = $query
                ->execute();
              $options = array();
              foreach ($records as $record) {
                $record_arr = (array) $record;
                if ($record_arr[$field_val[0] . '_value'] != FALSE) {

                  //if field VALUE is email
                  if ($field_val[2]) {
                    $options[$record_arr['nid']] = $record_arr['title'];
                  }
                  else {
                    $options[$record_arr[$field_val[0] . '_value']] = $record_arr['title'];
                  }
                }
              }
            }
            $form['submitted'][$input['#webform_component']['form_key']]['#type'] = 'select';
            $form['submitted'][$input['#webform_component']['form_key']]['#options'] = $options;
          }
        }
      }
    }
    $form['#validate'][] = 'wfso_client_form_validation';
  }
}
function wfso_client_form_validation(&$form, &$form_state) {
  foreach ($form_state['webform']['component_tree']['children'] as $k => $component) {
    if ($component['type'] == 'textfield') {
      $n_type = db_select('webform_select_opts_component', 's')
        ->fields('s', array(
        'data',
      ))
        ->condition('nid', $component['nid'])
        ->condition('cid', $component['cid'])
        ->execute()
        ->fetchField();
      if ($n_type) {
        $field_val = unserialize(variable_get('webform_select_options_fields_' . $n_type, ''));
      }
      if (isset($field_val) && $field_val[2]) {
        $value_email = db_select('field_data_' . $field_val[0], 'e')
          ->fields('e', array(
          $field_val[0] . '_value',
        ))
          ->condition('entity_id', $form_state['values']['submitted'][$component['form_key']])
          ->execute()
          ->fetchField();
        $form_state['values']['submitted'][$component['form_key']] = $value_email;
      }
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * webform_component_edit_form
 */
function webform_select_opts_form_webform_component_edit_form_alter(&$form, &$form_state, $form_id) {
  if ($form['type']['#value'] == 'textfield') {
    $node_types = node_type_get_types();
    $options = array(
      0 => t('None'),
    );
    foreach ($node_types as $n_type) {
      if (variable_get('webform_select_options_for_' . $n_type->type, 0)) {
        $options[$n_type->type] = $n_type->name;
      }
    }
    $n_type = db_select('webform_select_opts_component', 's')
      ->fields('s', array(
      'data',
    ))
      ->condition('nid', $form['nid']['#value'])
      ->condition('cid', $form['cid']['#value'])
      ->execute()
      ->fetchField();
    $form['webform_select_opts'] = array(
      '#type' => 'fieldset',
      '#title' => t('Select options'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#weight' => 49,
    );
    $form['webform_select_opts']['wfso_value'] = array(
      '#type' => 'select',
      '#title' => t('Avalable content types'),
      '#options' => $options,
      '#default_value' => $n_type ? $n_type : 0,
    );
    $form['#submit'][] = 'wfso_component_save';
  }
}
function wfso_component_save($form, &$form_state) {
  db_merge('webform_select_opts_component')
    ->key(array(
    'nid' => $form_state['values']['nid'],
    'cid' => $form_state['values']['cid'],
  ))
    ->fields(array(
    'data' => $form_state['values']['webform_select_opts']['wfso_value'],
  ))
    ->execute();
}