Newer
Older
Nate Lampton
committed
* Webform module multiple select component.
* Implements _webform_defaults_component().
*/
function _webform_defaults_select() {
return array(
'name' => '',
'form_key' => NULL,
'mandatory' => 0,
'pid' => 0,
'weight' => 0,
'value' => '',
'extra' => array(
'items' => '',
'multiple' => NULL,
'aslist' => NULL,
Nate Lampton
committed
'optrand' => 0,
Nate Lampton
committed
'other_option' => NULL,
'other_text' => t('Other...'),
'title_display' => 0,
'private' => FALSE,
Nate Lampton
committed
/**
* Implements _webform_theme_component().
Nate Lampton
committed
*/
function _webform_theme_select() {
return array(
'webform_display_select' => array(
'file' => 'components/select.inc',
Nate Lampton
committed
),
);
}
* Implements _webform_edit_component().
Nate Lampton
committed
function _webform_edit_select($component) {
$form = array(
'#attached' => array(
'js' => array(
drupal_get_path('module', 'webform') . '/js/select-admin.js' => array('preprocess' => FALSE),
array('data' => array('webform' => array('selectOptionsUrl' => url('webform/ajax/options/' . $component['nid']))), 'type' => 'setting'),
),
),
);
if ($info = _webform_select_options_info()) {
$options = array('' => t('None'));
foreach ($info as $name => $source) {
$options[$name] = $source['title'];
}
$other['options_source'] = array(
'#title' => t('Load a pre-built option list'),
'#type' => 'select',
'#options' => $options,
'#default_value' => $component['extra']['options_source'],
'#weight' => 1,
'#description' => t('Use a pre-built list of options rather than entering options manually. Options will not be editable if using pre-built list.'),
'#parents' => array('extra', 'options_source'),
'#weight' => 5,
);
}
if (module_exists('select_or_other')) {
$other['other_option'] = array(
'#type' => 'checkbox',
'#title' => t('Allow "Other..." option'),
'#default_value' => $component['extra']['other_option'],
'#description' => t('Check this option if you want to allow users to enter an option not on the list.'),
'#parents' => array('extra', 'other_option'),
);
$other['other_text'] = array(
'#type' => 'textfield',
'#title' => t('Text for "Other..." option'),
'#default_value' => $component['extra']['other_text'],
'#description' => t('If allowing other options, enter text to be used for other-enabling option.'),
'#parents' => array('extra', 'other_text'),
);
}
if (module_exists('options_element')) {
Nate Lampton
committed
$options = _webform_select_options($component, FALSE, FALSE);
$form['items'] = array(
'#type' => 'fieldset',
'#title' => t('Options'),
'#collapsible' => TRUE,
'#attributes' => array('class' => array('webform-options-element')),
'#element_validate' => array('_webform_edit_validate_options'),
);
$form['items']['options'] = array(
'#type' => 'options',
'#limit' => 500,
'#optgroups' => $component['extra']['aslist'],
'#multiple' => $component['extra']['multiple'],
'#multiple_toggle' => t('Multiple'),
'#default_value' => $component['value'],
'#options' => $options,
'#key_type' => 'mixed',
'#key_type_toggle' => t('Customize keys (Advanced)'),
'#key_type_toggled' => $component['extra']['custom_keys'],
'#default_value_pattern' => '^%.+\[.+\]$',
'#disabled' => !empty($component['extra']['options_source']),
'#weight' => 1,
);
$form['items']['options']['option_settings'] = $other;
}
else {
$form['extra']['items'] = array(
'#type' => 'textarea',
'#title' => t('Options'),
'#default_value' => $component['extra']['items'],
'#description' => t('<strong>Key-value pairs MUST be specified as "safe_key|Some readable option"</strong>. Use of only alphanumeric characters and underscores is recommended in keys. One option per line. Option groups may be specified with <Group Name>. <> can be used to insert items at the root of the menu after specifying a group.') . theme('webform_token_help'),
'#cols' => 60,
'#rows' => 5,
Nate Lampton
committed
'#weight' => 0,
'#element_validate' => array('_webform_edit_validate_select'),
);
Nate Lampton
committed
if (!empty($component['extra']['options_source'])) {
$form['extra']['items']['#attributes'] = array('readonly' => 'readonly');
}
$form['extra'] = array_merge($form['extra'], $other);
$form['value'] = array(
'#type' => 'textfield',
'#title' => t('Default value'),
'#default_value' => $component['value'],
'#description' => t('The default value of the field identified by its key. For multiple selects use commas to separate multiple defaults.') . theme('webform_token_help'),
'#maxlength' => 1024,
'#weight' => 0,
);
$form['extra']['multiple'] = array(
'#type' => 'checkbox',
'#title' => t('Multiple'),
'#default_value' => $component['extra']['multiple'],
'#description' => t('Check this option if the user should be allowed to choose multiple values.'),
);
}
$form['display']['aslist'] = array(
'#type' => 'checkbox',
'#title' => t('Listbox'),
Nate Lampton
committed
'#default_value' => $component['extra']['aslist'],
'#description' => t('Check this option if you want the select component to be of listbox type instead of radio buttons or checkboxes.'),
'#parents' => array('extra', 'aslist'),
Nate Lampton
committed
);
$form['display']['optrand'] = array(
'#type' => 'checkbox',
'#title' => t('Randomize options'),
'#default_value' => $component['extra']['optrand'],
'#description' => t('Randomizes the order of the options when they are displayed in the form.'),
'#parents' => array('extra', 'optrand'),
);
Nate Lampton
committed
Nate Lampton
committed
return $form;
/**
* Element validation callback. Ensure keys are not duplicated.
*/
function _webform_edit_validate_select($element, &$form_state) {
// Check for duplicate key values to prevent unexpected data loss. Require
// all options to include a safe_key.
if (!empty($element['#value'])) {
$lines = explode("\n", trim($element['#value']));
$existing_keys = array();
$duplicate_keys = array();
$missing_keys = array();
$long_keys = array();
$group = '';
foreach ($lines as $line) {
$matches = array();
$line = trim($line);
if (preg_match('/^\<([^>]*)\>$/', $line, $matches)) {
$group = $matches[1];
$key = NULL; // No need to store group names.
elseif (preg_match('/^([^|]*)\|(.*)$/', $line, $matches)) {
if (strlen($key) > 128) {
$long_keys[] = $key;
}
$missing_keys[] = $line;
}
if (isset($key)) {
if (isset($existing_keys[$group][$key])) {
$duplicate_keys[$key] = $key;
}
else {
$existing_keys[$group][$key] = $key;
}
}
}
if (!empty($missing_keys)) {
form_error($element, t('Every option must have a key specified. Specify each option as "safe_key|Some readable option".'));
}
if (!empty($long_keys)) {
form_error($element, t('Option keys must be less than 128 characters. The following keys exceed this limit:') . theme('item_list', $long_keys));
}
if (!empty($duplicate_keys)) {
form_error($element, t('Options within the select list must be unique. The following keys have been used multiple times:') . theme('item_list', array('items' => $duplicate_keys)));
/**
* Set the appropriate webform values when using the options element module.
*/
function _webform_edit_validate_options($element, &$form_state) {
$key = end($element['#parents']);
$element_options = $form_state['values'][$key]['options'];
unset($form_state['values'][$key]);
$form_state['values']['extra'][$key] = form_options_to_text($element_options['options'], 'custom');
// Options saved for select components.
if ($key == 'items') {
$form_state['values']['extra']['multiple'] = $element_options['multiple'];
$form_state['values']['extra']['custom_keys'] = $element_options['custom_keys'];
$form_state['values']['value'] = is_array($element_options['default_value']) ? implode(', ', $element_options['default_value']) : $element_options['default_value'];
}
// Options saved for grid components.
else {
$form_state['values']['extra']['custom_' . rtrim($key, 's') . '_keys'] = $element_options['custom_keys'];
}
}
* Implements _webform_render_component().
Nate Lampton
committed
function _webform_render_select($component, $value = NULL, $filter = TRUE) {
Nate Lampton
committed
$node = isset($component['nid']) ? node_load($component['nid']) : NULL;
Nate Lampton
committed
$element = array(
'#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
'#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
'#required' => $component['mandatory'],
'#weight' => $component['weight'],
Nate Lampton
committed
'#description' => $filter ? _webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
Nate Lampton
committed
'#theme_wrappers' => array('webform_element'),
'#pre_render' => array(), // Needed to disable double-wrapping of radios and checkboxes.
Nate Lampton
committed
'#translatable' => array('title', 'description', 'options'),
// Convert the user-entered options list into an array.
Nate Lampton
committed
$default_value = $filter ? _webform_filter_values($component['value'], $node, NULL, NULL, FALSE) : $component['value'];
Nate Lampton
committed
$options = _webform_select_options($component, !$component['extra']['aslist'], $filter);
Nate Lampton
committed
if ($component['extra']['optrand']) {
_webform_shuffle_options($options);
}
Nate Lampton
committed
// Add default options if using a select list with no default. This trigger's
// Drupal 7's adding of the option for us. See @form_process_select().
if ($component['extra']['aslist'] && !$component['extra']['multiple'] && $default_value === '') {
$element['#empty_value'] = '';
}
// Set the component options.
Nate Lampton
committed
$element['#options'] = $options;
// Set the default value.
Nate Lampton
committed
if (isset($value)) {
if ($component['extra']['multiple']) {
Nate Lampton
committed
// Set the value as an array.
Nate Lampton
committed
$element['#default_value'] = array();
Nate Lampton
committed
foreach ((array) $value as $key => $option_value) {
Nate Lampton
committed
$element['#default_value'][] = $option_value;
Nate Lampton
committed
}
}
else {
// Set the value as a single string.
Nate Lampton
committed
$element['#default_value'] = '';
Nate Lampton
committed
foreach ((array) $value as $option_value) {
Nate Lampton
committed
$element['#default_value'] = $option_value;
Nate Lampton
committed
}
}
}
Nate Lampton
committed
elseif ($default_value !== '') {
// Convert default value to a list if necessary.
if ($component['extra']['multiple']) {
Nate Lampton
committed
$varray = explode(',', $default_value);
foreach ($varray as $key => $v) {
Nate Lampton
committed
$v = trim($v);
if ($v !== '') {
$element['#default_value'][] = $v;
}
Nate Lampton
committed
}
Nate Lampton
committed
$element['#default_value'] = $default_value;
Nate Lampton
committed
}
elseif ($component['extra']['multiple']) {
Nate Lampton
committed
$element['#default_value'] = array();
}
Nate Lampton
committed
else {
$element['#default_value'] = FALSE;
}
if ($component['extra']['other_option'] && module_exists('select_or_other')) {
Nate Lampton
committed
// Set display as a select_or_other element:
Nate Lampton
committed
$element['#type'] = 'select_or_other';
$element['#other'] = !empty($component['extra']['other_text']) ? check_plain($component['extra']['other_text']) : t('Other...');
Nate Lampton
committed
$element['#other_unknown_defaults'] = 'other';
$element['#other_delimiter'] = ', ';
Nate Lampton
committed
// Merge in Webform's #process function for Select or other.
$element['#process'] = array_merge(element_info_property('select_or_other', '#process'), array('webform_expand_select_or_other'));
if ($component['extra']['multiple']) {
Nate Lampton
committed
$element['#multiple'] = TRUE;
$element['#select_type'] = 'checkboxes';
}
else {
$element['#multiple'] = FALSE;
$element['#select_type'] = 'radios';
}
if ($component['extra']['aslist']) {
Nate Lampton
committed
$element['#select_type'] = 'select';
}
}
elseif ($component['extra']['aslist']) {
// Set display as a select list:
Nate Lampton
committed
$element['#type'] = 'select';
if ($component['extra']['multiple']) {
Nate Lampton
committed
$element['#multiple'] = TRUE;
}
}
else {
if ($component['extra']['multiple']) {
// Set display as a checkbox set.
Nate Lampton
committed
$element['#type'] = 'checkboxes';
Nate Lampton
committed
$element['#theme_wrappers'] = array_merge(array('checkboxes'), $element['#theme_wrappers']);
Nate Lampton
committed
$element['#process'] = array_merge(element_info_property('checkboxes', '#process'), array('webform_expand_select_ids'));
// Entirely replace the normal expand checkboxes with our custom version.
// This helps render checkboxes in multipage forms.
$process_key = array_search('form_process_checkboxes', $element['#process']);
$element['#process'][$process_key] = 'webform_expand_checkboxes';
// Set display as a radio set.
Nate Lampton
committed
$element['#type'] = 'radios';
Nate Lampton
committed
$element['#theme_wrappers'] = array_merge(array('radios'), $element['#theme_wrappers']);
Nate Lampton
committed
$element['#process'] = array_merge(element_info_property('radios', '#process'), array('webform_expand_select_ids'));
Nate Lampton
committed
Nate Lampton
committed
return $element;
Nate Lampton
committed
/**
* Process function to ensure select_or_other elements validate properly.
*/
function webform_expand_select_or_other($element) {
// Disable validation for back-button and save draft.
$element['select']['#validated'] = TRUE;
$element['select']['#webform_validated'] = FALSE;
$element['other']['#validated'] = TRUE;
$element['other']['#webform_validated'] = FALSE;
// If the default value contains "select_or_other" (the key of the select
// element for the "other..." choice), discard it and set the "other" value.
if (is_array($element['#default_value']) && in_array('select_or_other', $element['#default_value'])) {
$key = array_search('select_or_other', $element['#default_value']);
unset($element['#default_value'][$key]);
$element['#default_value'] = array_values($element['#default_value']);
$element['other']['#default_value'] = implode(', ', $element['#default_value']);
}
// Sanitize the options in Select or other check boxes and radio buttons.
if ($element['#select_type'] == 'checkboxes' || $element['#select_type'] == 'radios') {
$element['select']['#process'] = array_merge(element_info_property($element['#select_type'], '#process'), array('webform_expand_select_ids'));
}
Nate Lampton
committed
return $element;
}
/**
* Drupal 6 hack that properly *renders* checkboxes in multistep forms. This is
* different than the value hack needed in Drupal 5, which is no longer needed.
*/
function webform_expand_checkboxes($element) {
// Elements that have a value set are already in the form structure cause
// them not to be written when the expand_checkboxes function is called.
$default_value = array();
foreach (element_children($element) as $key) {
if (isset($element[$key]['#default_value'])) {
$default_value[$key] = $element[$key]['#default_value'];
unset($element[$key]);
}
}
$element = form_process_checkboxes($element);
Nate Lampton
committed
// Escape the values of checkboxes.
foreach (element_children($element) as $key) {
$element[$key]['#return_value'] = check_plain($element[$key]['#return_value']);
$element[$key]['#name'] = $element['#name'] . '[' . $element[$key]['#return_value'] . ']';
}
foreach ($default_value as $key => $val) {
$element[$key]['#default_value'] = $val;
}
return $element;
}
Nate Lampton
committed
/**
* FAPI process function to rename IDs attached to checkboxes and radios.
*/
function webform_expand_select_ids($element) {
$id = $element['#id'] = str_replace('_', '-', _webform_safe_name(strip_tags($element['#id'])));
$delta = 0;
foreach (element_children($element) as $key) {
$delta++;
// Convert the #id for each child to a safe name, regardless of key.
$element[$key]['#id'] = $id . '-' . $delta;
// Prevent scripts or CSS in the labels for each checkbox or radio.
$element[$key]['#title'] = _webform_filter_xss($element[$key]['#title']);
}
return $element;
}
* Implements _webform_display_component().
Nate Lampton
committed
function _webform_display_select($component, $value, $format = 'html') {
return array(
'#title' => $component['name'],
'#weight' => $component['weight'],
'#theme' => 'webform_display_select',
'#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
'#format' => $format,
Nate Lampton
committed
'#options' => _webform_select_options($component, !$component['extra']['aslist']),
Nate Lampton
committed
'#value' => (array) $value,
Nate Lampton
committed
'#translatable' => array('title', 'options'),
Nate Lampton
committed
);
}
* Implements _webform_submit_component().
Nate Lampton
committed
* Convert FAPI 0/1 values into something saveable.
Nate Lampton
committed
function _webform_submit_select($component, $value) {
Nate Lampton
committed
// Build a list of all valid keys expected to be submitted.
Nate Lampton
committed
$options = _webform_select_options($component, TRUE);
$return = NULL;
Nate Lampton
committed
if (is_array($value)) {
$return = array();
Nate Lampton
committed
foreach ($value as $key => $option_value) {
Nate Lampton
committed
// Handle options that are specified options.
Nate Lampton
committed
if ($option_value !== '' && isset($options[$option_value])) {
Nate Lampton
committed
// Checkboxes submit an integer value of 0 when unchecked. A checkbox
// with a value of '0' is valid, so we can't use empty() here.
if ($option_value === 0 && !$component['extra']['aslist'] && $component['extra']['multiple']) {
Nate Lampton
committed
unset($value[$option_value]);
Nate Lampton
committed
}
else {
$return[] = $option_value;
Nate Lampton
committed
}
Nate Lampton
committed
// Handle options that are added through the "other" field. Specifically
// exclude the "select_or_other" value, which is added by the select list.
elseif ($component['extra']['other_option'] && module_exists('select_or_other') && $option_value != 'select_or_other') {
$return[] = $option_value;
Nate Lampton
committed
}
elseif (is_string($value)) {
$return = $value;
}
Nate Lampton
committed
return $return;
}
/**
Nate Lampton
committed
* Format the text output for this component.
*/
function theme_webform_display_select($variables) {
$element = $variables['element'];
$component = $element['#webform_component'];
Nate Lampton
committed
Nate Lampton
committed
// Flatten the list of options so we can get values easily. These options
// may be translated by hook_webform_display_component_alter().
$options = array();
foreach ($element['#options'] as $key => $value) {
if (is_array($value)) {
foreach ($value as $subkey => $subvalue) {
$options[$subkey] = $subvalue;
}
}
else {
$options[$key] = $value;
}
}
Nate Lampton
committed
$items = array();
if ($component['extra']['multiple']) {
Nate Lampton
committed
foreach ((array) $element['#value'] as $option_value) {
if ($option_value !== '') {
Nate Lampton
committed
// Administer provided values.
Nate Lampton
committed
if (isset($options[$option_value])) {
Nate Lampton
committed
$items[] = $element['#format'] == 'html' ? _webform_filter_xss($options[$option_value]) : $options[$option_value];
Nate Lampton
committed
// User-specified in the "other" field.
else {
Nate Lampton
committed
$items[] = $element['#format'] == 'html' ? check_plain($option_value) : $option_value;
Nate Lampton
committed
}
}
}
}
else {
Nate Lampton
committed
if (isset($element['#value'][0]) && $element['#value'][0] !== '') {
// Administer provided values.
if (isset($options[$element['#value'][0]])) {
Nate Lampton
committed
$items[] = $element['#format'] == 'html' ? _webform_filter_xss($options[$element['#value'][0]]) : $options[$element['#value'][0]];
Nate Lampton
committed
}
// User-specified in the "other" field.
else {
Nate Lampton
committed
$items[] = $element['#format'] == 'html' ? check_plain($element['#value'][0]) : $element['#value'][0];
Nate Lampton
committed
}
}
Nate Lampton
committed
if ($element['#format'] == 'html') {
$output = count($items) > 1 ? theme('item_list', array('items' => $items)) : (isset($items[0]) ? $items[0] : ' ');
Nate Lampton
committed
}
else {
if (count($items) > 1) {
foreach ($items as $key => $item) {
$items[$key] = ' - ' . $item;
}
$output = implode("\n", $items);
}
else {
$output = isset($items[0]) ? $items[0] : ' ';
}
}
return $output;
Nate Lampton
committed
}
* Implements _webform_analysis_component().
Nate Lampton
committed
function _webform_analysis_select($component, $sids = array(), $single = FALSE) {
Nate Lampton
committed
$options = _webform_select_options($component, TRUE);
Nate Lampton
committed
$show_other_results = $single;
$sid_placeholders = count($sids) ? array_fill(0, count($sids), "'%s'") : array();
$sid_filter = count($sids) ? " AND sid IN (" . implode(",", $sid_placeholders) . ")" : "";
Nate Lampton
committed
$option_operator = $show_other_results ? 'NOT IN' : 'IN';
$query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
->fields('wsd', array('data'))
->condition('nid', $component['nid'])
->condition('cid', $component['cid'])
->condition('data', '', '<>')
->condition('data', array_keys($options), $option_operator)
->groupBy('data');
$query->addExpression('COUNT(data)', 'datacount');
if (count($sids)) {
$query->condition('sid', $sids, 'IN');
}
$count_query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
->condition('nid', $component['nid'])
->condition('cid', $component['cid'])
->condition('data', '', '<>');
$count_query->addExpression('COUNT(*)', 'datacount');
if (count($sids)) {
$count_query->condition('sid', $sids, 'IN');
}
$result = $query->execute();
$rows = array();
Nate Lampton
committed
$normal_count = 0;
Nate Lampton
committed
$display_option = $single ? $data['data'] : $options[$data['data']];
Nate Lampton
committed
$rows[$data['data']] = array(_webform_filter_xss($display_option), $data['datacount']);
Nate Lampton
committed
$normal_count += $data['datacount'];
}
if (!$show_other_results) {
// Order the results according to the normal options array.
$ordered_rows = array();
foreach (array_intersect_key($options, $rows) as $key => $label) {
$ordered_rows[] = $rows[$key];
}
Nate Lampton
committed
// Add a row for any unknown or user-entered values.
if ($component['extra']['other_option']) {
$full_count = $count_query->execute()->fetchField();
Nate Lampton
committed
$other_count = $full_count - $normal_count;
$display_option = !empty($component['extra']['other_text']) ? check_plain($component['extra']['other_text']) : t('Other...');
Nate Lampton
committed
$other_text = $other_count ? $other_count . ' (' . l(t('view'), 'node/' . $component['nid'] . '/webform-results/analysis/' . $component['cid']) . ')' : $other_count;
$ordered_rows[] = array($display_option, $other_text);
Nate Lampton
committed
$rows = $ordered_rows;
Nate Lampton
committed
return $rows;
}
* Implements _webform_table_component().
Nate Lampton
committed
function _webform_table_select($component, $value) {
Nate Lampton
committed
// Convert submitted 'safe' values to un-edited, original form.
$options = _webform_select_options($component, TRUE);
$value = (array) $value;
$items = array();
// Set the value as a single string.
Nate Lampton
committed
foreach ($value as $option_value) {
if ($option_value !== '') {
if (isset($options[$option_value])) {
$items[] = _webform_filter_xss($options[$option_value]);
}
else {
$items[] = check_plain($option_value);
}
}
Nate Lampton
committed
return implode('<br />', $items);
}
* Implements _webform_csv_headers_component().
function _webform_csv_headers_select($component, $export_options) {
$headers = array(
0 => array(),
1 => array(),
2 => array(),
);
if ($component['extra']['multiple'] && $export_options['select_format'] == 'separate') {
$headers[0][] = '';
$headers[1][] = $component['name'];
Nate Lampton
committed
$items = _webform_select_options($component, TRUE, FALSE);
if ($component['extra']['other_option']) {
$other_label = !empty($component['extra']['other_text']) ? check_plain($component['extra']['other_text']) : t('Other...');
Nate Lampton
committed
$items[$other_label] = $other_label;
}
$count = 0;
foreach ($items as $key => $item) {
// Empty column per sub-field in main header.
if ($count != 0) {
$headers[0][] = '';
$headers[1][] = '';
}
Nate Lampton
committed
if ($export_options['select_keys']) {
$headers[2][] = $key;
}
else {
Nate Lampton
committed
$headers[2][] = $item;
}
}
}
else {
$headers[0][] = '';
$headers[1][] = '';
$headers[2][] = $component['name'];
}
}
* Implements _webform_csv_data_component().
function _webform_csv_data_select($component, $export_options, $value) {
Nate Lampton
committed
$options = _webform_select_options($component, TRUE, FALSE);
if ($component['extra']['multiple']) {
foreach ($options as $key => $item) {
Nate Lampton
committed
$index = array_search($key, (array) $value);
if ($index !== FALSE) {
Nate Lampton
committed
if ($export_options['select_format'] == 'separate') {
$return[] = 'X';
}
else {
$return[] = $export_options['select_keys'] ? $key : $item;
}
Nate Lampton
committed
unset($value[$index]);
}
elseif ($export_options['select_format'] == 'separate') {
}
}
Nate Lampton
committed
// Any remaining items in the $value array will be user-added options.
if ($component['extra']['other_option']) {
Nate Lampton
committed
$return[] = count($value) ? implode(',', $value) : '';
}
}
else {
Nate Lampton
committed
$key = $value[0];
if ($export_options['select_keys']) {
Nate Lampton
committed
$return = $key;
}
else {
$return = isset($options[$key]) ? $options[$key] : $key;
}
}
if ($component['extra']['multiple'] && $export_options['select_format'] == 'compact') {
$return = implode(',', (array) $return);
}
return $return;
}
/**
* Menu callback; Return a predefined list of select options as JSON.
*/
function webform_select_options_ajax($source_name = '') {
$info = _webform_select_options_info();
$component['extra']['options_source'] = $source_name;
if ($source_name && isset($info[$source_name])) {
Nate Lampton
committed
$options = _webform_select_options_to_text(_webform_select_options($component, !$component['extra']['aslist'], FALSE));
}
else {
$options = '';
}
$return = array(
'elementId' => module_exists('options_element') ? 'edit-items-options-options-field-widget' : 'edit-extra-items',
'options' => $options,
);
drupal_json_output($return);
}
/**
* Generate a list of options for a select list.
*/
Nate Lampton
committed
function _webform_select_options($component, $flat = FALSE, $filter = TRUE) {
if ($component['extra']['options_source']) {
Nate Lampton
committed
$options = _webform_select_options_callback($component['extra']['options_source'], $component, $flat, $filter);
Nate Lampton
committed
$options = _webform_select_options_from_text($component['extra']['items'], $flat, $filter);
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
}
return isset($options) ? $options : array();
}
/**
* Load Webform select option info from 3rd party modules.
*/
function _webform_select_options_info() {
static $info;
if (!isset($info)) {
$info = array();
foreach (module_implements('webform_select_options_info') as $module) {
$additions = module_invoke($module, 'webform_select_options_info');
foreach ($additions as $key => $addition) {
$additions[$key]['module'] = $module;
}
$info = array_merge($info, $additions);
}
drupal_alter('webform_select_options_info', $info);
}
return $info;
}
/**
* Execute a select option callback.
*
* @param $name
* The name of the options group.
* @param $component
* The full Webform component.
Nate Lampton
committed
* @param $flat
* Whether the information returned should exclude any nested groups.
* @param $filter
* Whether information returned should be sanitized. Defaults to TRUE.
*/
Nate Lampton
committed
function _webform_select_options_callback($name, $component, $flat = FALSE, $filter = TRUE) {
$info = _webform_select_options_info();
// Include any necessary files.
if (isset($info[$name]['file'])) {
$pathinfo = pathinfo($info[$name]['file']);
$path = ($pathinfo['dirname'] ? $pathinfo['dirname'] . '/' : '') . basename($pathinfo['basename'], '.' . $pathinfo['extension']);
module_load_include($pathinfo['extension'], $info[$name]['module'], $path);
}
// Execute the callback function.
Nate Lampton
committed
if (isset($info[$name]['options callback']) && function_exists($info[$name]['options callback'])) {
$function = $info[$name]['options callback'];
Nate Lampton
committed
if (isset($info[$name]['options arguments'])) {
$arguments = $info[$name]['options arguments'];
}
return $function($component, $flat, $filter, $arguments);
/**
* Utility function to split user-entered values from new-line seperated
* text into an array of options.
*
* @param $text
* Text to be converted into a select option array.
* @param $flat
* Optional. If specified, return the option array and exclude any optgroups.
Nate Lampton
committed
* @param $filter
* Optional. Whether or not to filter returned values.
function _webform_select_options_from_text($text, $flat = FALSE, $filter = TRUE) {
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
static $option_cache = array();
// Keep each processed option block in an array indexed by the MD5 hash of
// the option text and the value of the $flat variable.
$md5 = md5($text);
// Check if this option block has been previously processed.
if (!isset($option_cache[$flat][$md5])) {
$options = array();
$rows = array_filter(explode("\n", trim($text)));
$group = NULL;
foreach ($rows as $option) {
$option = trim($option);
/**
* If the Key of the option is within < >, treat as an optgroup
*
* <Group 1>
* creates an optgroup with the label "Group 1"
*
* <>
* Unsets the current group, allowing items to be inserted at the root element.
*/
if (preg_match('/^\<([^>]*)\>$/', $option, $matches)) {
if (empty($matches[1])) {
unset($group);
}
elseif (!$flat) {
$group = $filter ? _webform_filter_values($matches[1], NULL, NULL, NULL, FALSE) : $matches[1];
}
elseif (preg_match('/^([^|]+)\|(.*)$/', $option, $matches)) {
$key = $filter ? _webform_filter_values($matches[1], NULL, NULL, NULL, FALSE) : $matches[1];
$value = $filter ? _webform_filter_values($matches[2], NULL, NULL, NULL, FALSE) : $matches[2];
isset($group) ? $options[$group][$key] = $value : $options[$key] = $value;
}
else {
$filtered_option = $filter ? _webform_filter_values($option, NULL, NULL, NULL, FALSE) : $option;
isset($group) ? $options[$group][$filtered_option] = $filtered_option : $options[$filtered_option] = $filtered_option;
$option_cache[$flat][$md5] = $options;
// Return our options from the option_cache array.
return $option_cache[$flat][$md5];
}
Nate Lampton
committed
/**
* Convert an array of options into text.
*/
function _webform_select_options_to_text($options) {
$output = '';
$previous_key = FALSE;
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
foreach ($options as $key => $value) {
// Convert groups.
if (is_array($value)) {
$output .= '<' . $key . '>' . "\n";
foreach ($value as $subkey => $subvalue) {
$output .= $subkey . '|' . $subvalue . "\n";
}
$previous_key = $key;
}
// Typical key|value pairs.
else {
// Exit out of any groups.
if (isset($options[$previous_key]) && is_array($options[$previous_key])) {
$output .= "<>\n";
}
// Skip empty rows.
if ($options[$key] !== '') {
$output .= $key . '|' . $value . "\n";
}
$previous_key = $key;
}
}
return $output;
}
Nate Lampton
committed
/**
* Utility function to shuffle an array while preserving key-value pairs.
*/
function _webform_shuffle_options(&$array) {
// First shuffle the array keys, then use them as the basis for ordering
// the options.
$aux = array();
$keys = array_keys($array);
shuffle($keys);
foreach ($keys as $key) {
$aux[$key] = $array[$key];
}
$array = $aux;
}