[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/webform/components/ -> select.inc (source)

   1  <?php
   2  // $Id: select.inc,v 1.39.2.22 2010/09/29 18:38:19 quicksketch Exp $
   3  
   4  /**
   5   * @file
   6   * Webform module multiple select component.
   7   */
   8  
   9  /**
  10   * Implementation of _webform_defaults_component().
  11   */
  12  function _webform_defaults_select() {
  13    return array(
  14      'name' => '',
  15      'form_key' => NULL,
  16      'email' => 1,
  17      'mandatory' => 0,
  18      'pid' => 0,
  19      'weight' => 0,
  20      'value' => '',
  21      'extra' => array(
  22        'items' => '',
  23        'multiple' => NULL,
  24        'aslist' => NULL,
  25        'optrand' => 0,
  26        'other_option' => NULL,
  27        'other_text' => t('Other...'),
  28        'title_display' => 0,
  29        'description' => '',
  30        'custom_keys' => FALSE,
  31        'options_source' => '',
  32      ),
  33    );
  34  }
  35  
  36  /**
  37   * Implementation of _webform_theme_component().
  38   */
  39  function _webform_theme_select() {
  40    return array(
  41      'webform_display_select' => array(
  42        'arguments' => array('element' => NULL),
  43      ),
  44    );
  45  }
  46  
  47  /**
  48   * Implementation of _webform_edit_component().
  49   */
  50  function _webform_edit_select($component) {
  51    $form = array();
  52  
  53    drupal_add_js(drupal_get_path('module', 'webform') . '/js/select-admin.js', 'module', 'header', FALSE, TRUE, FALSE);
  54    drupal_add_js(array('webform' => array('selectOptionsUrl' => url('webform/ajax/options/' . $component['nid']))), 'setting');
  55  
  56    $other = array();
  57    if ($info = _webform_select_options_info()) {
  58      $options = array('' => t('None'));
  59      foreach ($info as $name => $source) {
  60        $options[$name] = $source['title'];
  61      }
  62  
  63      $other['options_source'] = array(
  64        '#title' => t('Load a pre-built option list'),
  65        '#type' => 'select',
  66        '#options' => $options,
  67        '#default_value' => $component['extra']['options_source'],
  68        '#weight' => 1,
  69        '#description' => t('Use a pre-built list of options rather than entering options manually. Options will not be editable if using pre-built list.'),
  70        '#parents' => array('extra', 'options_source'),
  71        '#weight' => 5,
  72      );
  73    }
  74  
  75    if (module_exists('select_or_other')) {
  76      $other['other_option'] = array(
  77        '#type' => 'checkbox',
  78        '#title' => t('Allow "Other..." option'),
  79        '#default_value' => $component['extra']['other_option'],
  80        '#description' => t('Check this option if you want to allow users to enter an option not on the list.'),
  81        '#parents' => array('extra', 'other_option'),
  82        '#weight' => 2,
  83      );
  84      $other['other_text'] = array(
  85        '#type' => 'textfield',
  86        '#title' => t('Text for "Other..." option'),
  87        '#default_value' => $component['extra']['other_text'],
  88        '#description' => t('If allowing other options, enter text to be used for other-enabling option.'),
  89        '#parents' => array('extra', 'other_text'),
  90        '#weight' => 3,
  91      );
  92    }
  93  
  94    if (module_exists('options_element')) {
  95      $options = _webform_select_options($component, FALSE, FALSE);
  96  
  97      $form['items'] = array(
  98        '#type' => 'fieldset',
  99        '#title' => t('Options'),
 100        '#collapsible' => TRUE,
 101        '#attributes' => array('class' => 'webform-options-element'),
 102        '#element_validate' => array('_webform_edit_validate_options'),
 103        '#weight' => 2,
 104      );
 105  
 106      $form['items']['options'] = array(
 107        '#type' => 'options',
 108        '#limit' => 500,
 109        '#optgroups' => $component['extra']['aslist'],
 110        '#multiple' => $component['extra']['multiple'],
 111        '#multiple_toggle' => t('Multiple'),
 112        '#default_value' => $component['value'],
 113        '#options' => $options,
 114        '#key_type' => 'mixed',
 115        '#key_type_toggle' => t('Customize keys (Advanced)'),
 116        '#key_type_toggled' => $component['extra']['custom_keys'],
 117        '#disabled' => !empty($component['extra']['options_source']),
 118        '#weight' => 1,
 119      );
 120  
 121      $form['items']['options']['option_settings'] = $other;
 122    }
 123    else {
 124      $form['extra']['items'] = array(
 125        '#type' => 'textarea',
 126        '#title' => t('Options'),
 127        '#default_value' => $component['extra']['items'],
 128        '#description' => t('<strong>Key-value pairs MUST be specified as "safe_key|Some readable option"</strong>. Only alphanumeric characters and underscores are allowed as a key. One option per line. Option groups may be specified with &lt;Group Name&gt;. &lt;&gt; can be used to insert items at the root of the menu after specifying a group.') . theme('webform_token_help'),
 129        '#cols' => 60,
 130        '#rows' => 5,
 131        '#weight' => 0,
 132        '#required' => TRUE,
 133        '#wysiwyg' => FALSE,
 134        '#element_validate' => array('_webform_edit_validate_select'),
 135      );
 136  
 137      if (!empty($component['extra']['options_source'])) {
 138        $form['extra']['items']['#attributes'] = array('readonly' => 'readonly');
 139      }
 140  
 141      $form['extra'] = array_merge($form['extra'], $other);
 142      $form['value'] = array(
 143        '#type' => 'textfield',
 144        '#title' => t('Default value'),
 145        '#default_value' => $component['value'],
 146        '#description' => t('The default value of the field. For multiple selects use commas to separate multiple defaults.') . theme('webform_token_help'),
 147        '#size' => 60,
 148        '#maxlength' => 256,
 149        '#weight' => 0,
 150      );
 151      $form['extra']['multiple'] = array(
 152        '#type' => 'checkbox',
 153        '#title' => t('Multiple'),
 154        '#default_value' => $component['extra']['multiple'],
 155        '#description' => t('Check this option if the user should be allowed to choose multiple values.'),
 156        '#weight' => 0,
 157      );
 158    }
 159  
 160    $form['display']['aslist'] = array(
 161      '#type' => 'checkbox',
 162      '#title' => t('Listbox'),
 163      '#default_value' => $component['extra']['aslist'],
 164      '#description' => t('Check this option if you want the select component to be of listbox type instead of radio buttons or checkboxes.'),
 165      '#parents' => array('extra', 'aslist'),
 166    );
 167    $form['display']['optrand'] = array(
 168      '#type' => 'checkbox',
 169      '#title' => t('Randomize options'),
 170      '#default_value' => $component['extra']['optrand'],
 171      '#description' => t('Randomizes the order of the options when they are displayed in the form.'),
 172      '#parents' => array('extra', 'optrand'),
 173    );
 174  
 175    return $form;
 176  }
 177  
 178  /**
 179   * Element validation callback. Ensure keys are not duplicated.
 180   */
 181  function _webform_edit_validate_select($element, &$form_state) {
 182    // Check for duplicate key values to prevent unexpected data loss. Require
 183    // all options to include a safe_key.
 184    if (!empty($element['#value'])) {
 185      $lines = explode("\n", trim($element['#value']));
 186      $existing_keys = array();
 187      $duplicate_keys = array();
 188      $missing_keys = array();
 189      $long_keys = array();
 190      $group = '';
 191      foreach ($lines as $line) {
 192        $matches = array();
 193        $line = trim($line);
 194        if (preg_match('/^\<([^>]*)\>$/', $line, $matches)) {
 195          $group = $matches[1];
 196          $key = NULL; // No need to store group names.
 197        }
 198        elseif (preg_match('/^([^|]*)\|(.*)$/', $line, $matches)) {
 199          $key = $matches[1];
 200          if (strlen($key) > 128) {
 201            $long_keys[] = $key;
 202          }
 203        }
 204        else {
 205          $missing_keys[] = $line;
 206        }
 207  
 208        if (isset($key)) {
 209          if (isset($existing_keys[$group][$key])) {
 210            $duplicate_keys[$key] = $key;
 211          }
 212          else {
 213            $existing_keys[$group][$key] = $key;
 214          }
 215        }
 216      }
 217  
 218      if (!empty($missing_keys)) {
 219        form_error($element, t('Every option must have a key specified. Specify each option as "safe_key|Some readable option".'));
 220      }
 221  
 222      if (!empty($long_keys)) {
 223        form_error($element, t('Option keys must be less than 128 characters. The following keys exceed this limit:') . theme('item_list', $long_keys));
 224      }
 225  
 226      if (!empty($duplicate_keys)) {
 227        form_error($element, t('Options within the select list must be unique. The following keys have been used multiple times:') . theme('item_list', $duplicate_keys));
 228      }
 229  
 230    }
 231  
 232    return TRUE;
 233  }
 234  
 235  /**
 236   * Set the appropriate webform values when using the options element module.
 237   */
 238  function _webform_edit_validate_options($element, &$form_state) {
 239    $key = end($element['#parents']);
 240    $element_options = $form_state['values'][$key]['options'];
 241    unset($form_state['values'][$key]);
 242  
 243    $form_state['values']['extra'][$key] = form_options_to_text($element_options['options'], 'custom');
 244  
 245    // Options saved for select components.
 246    if ($key == 'items') {
 247      $form_state['values']['extra']['multiple'] = $element_options['multiple'];
 248      $form_state['values']['extra']['custom_keys'] = $element_options['custom_keys'];
 249      $form_state['values']['value'] = is_array($element_options['default_value']) ? implode(', ', $element_options['default_value']) : $element_options['default_value'];
 250    }
 251    // Options saved for grid components.
 252    else {
 253      $form_state['values']['extra']['custom_' . rtrim($key, 's') . '_keys'] = $element_options['custom_keys'];
 254    }
 255  }
 256  
 257  /**
 258   * Implementation of _webform_render_component().
 259   */
 260  function _webform_render_select($component, $value = NULL, $filter = TRUE) {
 261    $element = array(
 262      '#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
 263      '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : NULL,
 264      '#required' => $component['mandatory'],
 265      '#weight' => $component['weight'],
 266      '#description' => $filter ? _webform_filter_descriptions($component['extra']['description']) : $component['extra']['description'],
 267      '#pre_render' => array('webform_element_title_display'),
 268      '#webform_component' => $component,
 269    );
 270  
 271    // Convert the user-entered options list into an array.
 272    $default_value = $filter ? _webform_filter_values($component['value'], NULL, NULL, NULL, FALSE) : $component['value'];
 273    $options = _webform_select_options($component, !$component['extra']['aslist'], $filter);
 274  
 275    if ($component['extra']['optrand']) {
 276      _webform_shuffle_options($options);
 277    }
 278  
 279    if ($component['extra']['aslist'] && !$component['extra']['multiple'] && !($component['mandatory'] && $default_value !== '')) {
 280      $options = array('' => t('select...')) + $options;
 281    }
 282  
 283    // Set the component options.
 284    $element['#options'] = $options;
 285  
 286    // Set the default value.
 287    if (isset($value)) {
 288      if ($component['extra']['multiple']) {
 289        // Set the value as an array.
 290        $element['#default_value'] = array();
 291        foreach ((array) $value as $key => $option_value) {
 292          $element['#default_value'][] = $option_value;
 293        }
 294      }
 295      else {
 296        // Set the value as a single string.
 297        $element['#default_value'] = '';
 298        foreach ((array) $value as $option_value) {
 299          $element['#default_value'] = $option_value;
 300        }
 301      }
 302    }
 303    elseif ($default_value !== '') {
 304      // Convert default value to a list if necessary.
 305      if ($component['extra']['multiple']) {
 306        $varray = explode(',', $default_value);
 307        foreach ($varray as $key => $v) {
 308          $v = trim($v);
 309          if ($v !== '') {
 310            $element['#default_value'][] = $v;
 311          }
 312        }
 313      }
 314      else {
 315        $element['#default_value'] = $default_value;
 316      }
 317    }
 318    elseif ($component['extra']['multiple']) {
 319      $element['#default_value'] = array();
 320    }
 321  
 322    if ($component['extra']['other_option'] && module_exists('select_or_other')) {
 323      // Set display as a select list:
 324      $element['#type'] = 'select_or_other';
 325      $element['#other'] = !empty($component['extra']['other_text']) ? check_plain($component['extra']['other_text']) : t('Other...');
 326      $element['#other_unknown_defaults'] = 'other';
 327      $element['#other_delimiter'] = ', ';
 328      if ($component['extra']['multiple']) {
 329        $element['#multiple'] = TRUE;
 330        $element['#select_type'] = 'checkboxes';
 331      }
 332      else {
 333        $element['#multiple'] = FALSE;
 334        $element['#select_type'] = 'radios';
 335      }
 336      if ($component['extra']['aslist']) {
 337        $element['#select_type'] = 'select';
 338      }
 339    }
 340    elseif ($component['extra']['aslist']) {
 341      // Set display as a select list:
 342      $element['#type'] = 'select';
 343      if ($component['extra']['multiple']) {
 344        $element['#size'] = 4;
 345        $element['#multiple'] = TRUE;
 346      }
 347    }
 348    else {
 349      if ($component['extra']['multiple']) {
 350        // Set display as a checkbox set.
 351        $element['#type'] = 'checkboxes';
 352        // Drupal 6 hack to properly render on multipage forms.
 353        $element['#process'] = array('webform_expand_checkboxes', 'webform_expand_select_ids');
 354      }
 355      else {
 356        // Set display as a radio set.
 357        $element['#type'] = 'radios';
 358        $element['#process'] = array('expand_radios', 'webform_expand_select_ids');
 359      }
 360    }
 361  
 362    $element['#prefix'] = '<div class="webform-component webform-component-' . $element['#type'] . '" id="webform-component-' . $component['form_key'] . '">';
 363    $element['#suffix'] = '</div>';
 364  
 365    return $element;
 366  }
 367  
 368  /**
 369   * Drupal 6 hack that properly *renders* checkboxes in multistep forms. This is
 370   * different than the value hack needed in Drupal 5, which is no longer needed.
 371   */
 372  function webform_expand_checkboxes($element) {
 373    // Elements that have a value set are already in the form structure cause
 374    // them not to be written when the expand_checkboxes function is called.
 375    $default_value = array();
 376    foreach (element_children($element) as $key) {
 377      if (isset($element[$key]['#default_value'])) {
 378        $default_value[$key] = $element[$key]['#default_value'];
 379        unset($element[$key]);
 380      }
 381    }
 382  
 383    $element = expand_checkboxes($element);
 384  
 385    // Escape the values of checkboxes.
 386    foreach (element_children($element) as $key) {
 387      $element[$key]['#return_value'] = check_plain($element[$key]['#return_value']);
 388      $element[$key]['#name'] = $element['#name'] . '[' . $element[$key]['#return_value'] . ']';
 389      $element[$key]['#value_callback'] = 'webform_checkbox_value';
 390    }
 391  
 392    foreach ($default_value as $key => $val) {
 393      $element[$key]['#default_value'] = $val;
 394    }
 395    return $element;
 396  }
 397  
 398  /**
 399   * Helper function to determine the value for a checkbox form element.
 400   *
 401   * Drupal core cannot decern the difference between an unchecked checkbox and
 402   * a checked checkbox that has a value of '0'. This value callback will return
 403   * NULL instead of an integer 0 for checkboxes that are not checked.
 404   *
 405   * @see form_checkbox_value()
 406   */
 407  function webform_checkbox_value($form, $edit = FALSE) {
 408    if ($edit !== FALSE) {
 409      if (empty($form['#disabled'])) {
 410        return !empty($edit) ? $form['#return_value'] : NULL; // 0 in core.
 411      }
 412      else {
 413        return $form['#default_value'];
 414      }
 415    }
 416  }
 417  
 418  /**
 419   * FAPI process function to rename IDs attached to checkboxes and radios.
 420   */
 421  function webform_expand_select_ids($element) {
 422    $id = $element['#id'] = str_replace('_', '-', _webform_safe_name(strip_tags($element['#id'])));
 423    $delta = 0;
 424    foreach (element_children($element) as $key) {
 425      $delta++;
 426      // Convert the #id for each child to a safe name, regardless of key.
 427      $element[$key]['#id'] = $id . '-' . $delta;
 428  
 429      // Prevent scripts or CSS in the labels for each checkbox or radio.
 430      $element[$key]['#title'] = _webform_filter_xss($element[$key]['#title']);
 431    }
 432    return $element;
 433  }
 434  
 435  /**
 436   * Implementation of _webform_display_component().
 437   */
 438  function _webform_display_select($component, $value, $format = 'html') {
 439    return array(
 440      '#title' => $component['name'],
 441      '#weight' => $component['weight'],
 442      '#theme' => 'webform_display_select',
 443      '#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
 444      '#post_render' => array('webform_element_wrapper'),
 445      '#format' => $format,
 446      '#value' => (array) $value,
 447      '#webform_component' => $component,
 448    );
 449  }
 450  
 451  /**
 452   * Implementation of _webform_submit_component().
 453   *
 454   * Convert FAPI 0/1 values into something saveable.
 455   */
 456  function _webform_submit_select($component, $value) {
 457    // Build a list of all valid keys expected to be submitted.
 458    $options = _webform_select_options($component, TRUE);
 459  
 460    $return = NULL;
 461    if (is_array($value)) {
 462      $return = array();
 463      foreach ($value as $key => $option_value) {
 464        // Handle options that are specified options.
 465        if ($option_value !== '' && isset($options[$option_value])) {
 466          // Checkboxes submit a value of FALSE when unchecked. A checkbox with
 467          // a value of '0' is valid, so we can't use empty() here.
 468          if ($option_value === FALSE && !$component['extra']['aslist'] && $component['extra']['multiple']) {
 469            unset($value[$option_value]);
 470          }
 471          else {
 472            $return[] = $option_value;
 473          }
 474        }
 475        // Handle options that are added through the "other" field.
 476        elseif ($component['extra']['other_option'] && module_exists('select_or_other')) {
 477          $return[] = $option_value;
 478        }
 479      }
 480    }
 481    elseif (is_string($value)) {
 482      $return = $value;
 483    }
 484  
 485    return $return;
 486  }
 487  
 488  /**
 489   * Format the text output for this component.
 490   */
 491  function theme_webform_display_select($element) {
 492    $component = $element['#webform_component'];
 493  
 494    // Convert submitted 'safe' values to un-edited, original form.
 495    $options = _webform_select_options($component, TRUE);
 496  
 497    $items = array();
 498    if ($component['extra']['multiple']) {
 499      foreach ((array) $element['#value'] as $option_value) {
 500        if ($option_value !== '') {
 501          // Administer provided values.
 502          if (isset($options[$option_value])) {
 503            $items[] = $element['#format'] == 'html' ? _webform_filter_xss($options[$option_value]) : $options[$option_value];
 504          }
 505          // User-specified in the "other" field.
 506          else {
 507            $items[] = $element['#format'] == 'html' ? check_plain($option_value) : $option_value;
 508          }
 509        }
 510      }
 511    }
 512    else {
 513      if (isset($element['#value'][0]) && $element['#value'][0] !== '') {
 514        // Administer provided values.
 515        if (isset($options[$element['#value'][0]])) {
 516          $items[] = $element['#format'] == 'html' ? _webform_filter_xss($options[$element['#value'][0]]) : $options[$element['#value'][0]];
 517        }
 518        // User-specified in the "other" field.
 519        else {
 520          $items[] = $element['#format'] == 'html' ? check_plain($element['#value'][0]) : $element['#value'][0];
 521        }
 522      }
 523    }
 524  
 525    if ($element['#format'] == 'html') {
 526      $output = count($items) > 1 ? theme('item_list', $items) : (isset($items[0]) ? $items[0] : ' ');
 527    }
 528    else {
 529      if (count($items) > 1) {
 530        foreach ($items as $key => $item) {
 531          $items[$key] = ' - ' . $item;
 532        }
 533        $output = implode("\n", $items);
 534      }
 535      else {
 536        $output = isset($items[0]) ? $items[0] : ' ';
 537      }
 538    }
 539  
 540    return $output;
 541  }
 542  
 543  /**
 544   * Implementation of _webform_analysis_component().
 545   */
 546  function _webform_analysis_select($component, $sids = array(), $single = FALSE) {
 547    $options = _webform_select_options($component, TRUE);
 548    $show_other_results = $single;
 549  
 550    $sid_placeholders = count($sids) ? array_fill(0, count($sids), "'%s'") : array();
 551    $sid_filter = count($sids) ? " AND sid IN (" . implode(",", $sid_placeholders) . ")" : "";
 552  
 553    $option_operator = $show_other_results ? 'NOT IN' : 'IN';
 554    $placeholders = count($options) ? array_fill(0, count($options), "'%s'") : array();
 555    $query = 'SELECT data, count(data) as datacount ' .
 556      ' FROM {webform_submitted_data} ' .
 557      ' WHERE nid = %d ' .
 558      ' AND cid = %d ' .
 559      " AND data != ''" . $sid_filter .
 560      ($placeholders ? ' AND data ' . $option_operator . ' (' . implode(',', $placeholders) . ')' : '') .
 561      ' GROUP BY data ';
 562  
 563    $count_query = 'SELECT count(*) as datacount ' .
 564      ' FROM {webform_submitted_data} ' .
 565      ' WHERE nid = %d ' .
 566      ' AND cid = %d ' .
 567      " AND data != ''" . $sid_filter;
 568  
 569    $result = db_query($query, array_merge(array($component['nid'], $component['cid']), $sids, array_keys($options)));
 570    $rows = array();
 571    $normal_count = 0;
 572    while ($data = db_fetch_array($result)) {
 573      $display_option = $single ? $data['data'] : $options[$data['data']];
 574      $rows[$data['data']] = array(_webform_filter_xss($display_option), $data['datacount']);
 575      $normal_count += $data['datacount'];
 576    }
 577  
 578    if (!$show_other_results) {
 579      // Order the results according to the normal options array.
 580      $ordered_rows = array();
 581      foreach (array_intersect_key($options, $rows) as $key => $label) {
 582        $ordered_rows[] = $rows[$key];
 583      }
 584  
 585      // Add a row for any unknown or user-entered values.
 586      if ($component['extra']['other_option']) {
 587        $full_count = db_result(db_query($count_query, array_merge(array($component['nid'], $component['cid']), $sids)));
 588        $other_count = $full_count - $normal_count;
 589        $display_option = !empty($component['extra']['other_text']) ? check_plain($component['extra']['other_text']) : t('Other...');
 590        $other_text = $other_count ? $other_count . ' (' . l(t('view'), 'node/' . $component['nid'] . '/webform-results/analysis/' . $component['cid']) . ')' : $other_count;
 591        $ordered_rows[] = array($display_option, $other_text);
 592      }
 593  
 594      $rows = $ordered_rows;
 595    }
 596  
 597    return $rows;
 598  }
 599  
 600  /**
 601   * Implementation of _webform_table_component().
 602   */
 603  function _webform_table_select($component, $value) {
 604    // Convert submitted 'safe' values to un-edited, original form.
 605    $options = _webform_select_options($component, TRUE);
 606  
 607    $value = (array) $value;
 608    $items = array();
 609    // Set the value as a single string.
 610    foreach ($value as $option_value) {
 611      if ($option_value !== '') {
 612        if (isset($options[$option_value])) {
 613          $items[] = _webform_filter_xss($options[$option_value]);
 614        }
 615        else {
 616          $items[] = check_plain($option_value);
 617        }
 618      }
 619    }
 620  
 621    return implode('<br />', $items);
 622  }
 623  
 624  /**
 625   * Implementation of _webform_csv_headers_component().
 626   */
 627  function _webform_csv_headers_select($component, $export_options) {
 628    $headers = array(
 629      0 => array(),
 630      1 => array(),
 631      2 => array(),
 632    );
 633  
 634    if ($component['extra']['multiple'] && $export_options['select_format'] == 'separate') {
 635      $headers[0][] = '';
 636      $headers[1][] = $component['name'];
 637      $items = _webform_select_options($component, TRUE, FALSE);
 638      if ($component['extra']['other_option']) {
 639        $other_label = !empty($component['extra']['other_text']) ? check_plain($component['extra']['other_text']) : t('Other...');
 640        $items[$other_label] = $other_label;
 641      }
 642      $count = 0;
 643      foreach ($items as $key => $item) {
 644        // Empty column per sub-field in main header.
 645        if ($count != 0) {
 646          $headers[0][] = '';
 647          $headers[1][] = '';
 648        }
 649        $headers[2][] = $key;
 650        $count++;
 651      }
 652    }
 653    else {
 654      $headers[0][] = '';
 655      $headers[1][] = '';
 656      $headers[2][] = $component['name'];
 657    }
 658    return $headers;
 659  }
 660  
 661  /**
 662   * Implementation of _webform_csv_data_component().
 663   */
 664  function _webform_csv_data_select($component, $export_options, $value) {
 665    $options = _webform_select_options($component, TRUE, FALSE);
 666    $return = array();
 667  
 668    if ($component['extra']['multiple']) {
 669      foreach ($options as $key => $item) {
 670        $index = array_search($key, (array) $value);
 671        if ($index !== FALSE) {
 672          if ($export_options['select_format'] == 'separate') {
 673            $return[] = 'X';
 674          }
 675          else {
 676            $return[] = $export_options['select_keys'] ? $key : $item;
 677          }
 678          unset($value[$index]);
 679        }
 680        elseif ($export_options['select_format'] == 'separate') {
 681          $return[] = '';
 682        }
 683      }
 684  
 685      // Any remaining items in the $value array will be user-added options.
 686      if ($component['extra']['other_option']) {
 687        $return[] = count($value) ? implode(',', $value) : '';
 688      }
 689    }
 690    else {
 691      $key = $value[0];
 692      if ($export_options['select_keys']){
 693        $return = $key;
 694      }
 695      else {
 696        $return = isset($options[$key]) ? $options[$key] : $key;
 697      }
 698    }
 699  
 700    if ($component['extra']['multiple'] && $export_options['select_format'] == 'compact') {
 701      $return = implode(',', (array) $return);
 702    }
 703  
 704    return $return;
 705  }
 706  
 707  /**
 708   * Menu callback; Return a predefined list of select options as JSON.
 709   */
 710  function webform_select_options_ajax($source_name = '') {
 711    $info = _webform_select_options_info();
 712  
 713    $component['extra']['options_source'] = $source_name;
 714    if ($source_name && isset($info[$source_name])) {
 715      $options = _webform_select_options_to_text(_webform_select_options($component, !$component['extra']['aslist'], FALSE));
 716    }
 717    else {
 718      $options = '';
 719    }
 720  
 721    $return = array(
 722      'elementId' => module_exists('options_element') ? 'edit-items-options-options-field-widget' : 'edit-extra-items',
 723      'options' => $options,
 724    );
 725  
 726    drupal_json($return);
 727  }
 728  
 729  
 730  /**
 731   * Generate a list of options for a select list.
 732   */
 733  function _webform_select_options($component, $flat = FALSE, $filter = TRUE) {
 734    if ($component['extra']['options_source']) {
 735      $options = _webform_select_options_callback($component['extra']['options_source'], $component, $flat, $filter);
 736    }
 737    else {
 738      $options = _webform_select_options_from_text($component['extra']['items'], $flat, $filter);
 739    }
 740  
 741    return isset($options) ? $options : array();
 742  }
 743  
 744  /**
 745   * Load Webform select option info from 3rd party modules.
 746   */
 747  function _webform_select_options_info() {
 748    static $info;
 749    if (!isset($info)) {
 750      $info = array();
 751  
 752      foreach (module_implements('webform_select_options_info') as $module) {
 753        $additions = module_invoke($module, 'webform_select_options_info');
 754        foreach ($additions as $key => $addition) {
 755          $additions[$key]['module'] = $module;
 756        }
 757        $info = array_merge($info, $additions);
 758      }
 759      drupal_alter('webform_select_options_info', $info);
 760    }
 761    return $info;
 762  }
 763  
 764  /**
 765   * Execute a select option callback.
 766   *
 767   * @param $name
 768   *   The name of the options group.
 769   * @param $component
 770   *   The full Webform component.
 771   * @param $flat
 772   *   Whether the information returned should exclude any nested groups.
 773   * @param $filter
 774   *   Whether information returned should be sanitized. Defaults to TRUE.
 775   */
 776  function _webform_select_options_callback($name, $component, $flat = FALSE, $filter = TRUE) {
 777    $info = _webform_select_options_info();
 778  
 779    // Include any necessary files.
 780    if (isset($info[$name]['file'])) {
 781      $pathinfo = pathinfo($info[$name]['file']);
 782      $path = ($pathinfo['dirname'] ? $pathinfo['dirname'] . '/' : '') . basename($pathinfo['basename'], '.' . $pathinfo['extension']);
 783      module_load_include($pathinfo['extension'], $info[$name]['module'], $path);
 784    }
 785  
 786    // Execute the callback function.
 787    if (isset($info[$name]['options callback']) && function_exists($info[$name]['options callback'])) {
 788      $function = $info[$name]['options callback'];
 789  
 790      $options_args = array();
 791      if (isset($info[$name]['options arguments'])) {
 792        $arguments = $info[$name]['options arguments'];
 793      }
 794  
 795      return $function($component, $flat, $filter, $arguments);
 796    }
 797  }
 798  
 799  /**
 800   * Utility function to split user-entered values from new-line seperated
 801   * text into an array of options.
 802   *
 803   * @param $text
 804   *   Text to be converted into a select option array.
 805   * @param $flat
 806   *   Optional. If specified, return the option array and exclude any optgroups.
 807   * @param $filter
 808   *   Optional. Whether or not to filter returned values.
 809   */
 810  function _webform_select_options_from_text($text, $flat = FALSE, $filter = TRUE) {
 811    static $option_cache = array();
 812  
 813    // Keep each processed option block in an array indexed by the MD5 hash of
 814    // the option text and the value of the $flat variable.
 815    $md5 = md5($text);
 816  
 817    // Check if this option block has been previously processed.
 818    if (!isset($option_cache[$flat][$md5])) {
 819      $options = array();
 820      $rows = array_filter(explode("\n", trim($text)));
 821      $group = NULL;
 822      foreach ($rows as $option) {
 823        $option = trim($option);
 824        /**
 825         * If the Key of the option is within < >, treat as an optgroup
 826         *
 827         * <Group 1>
 828         *   creates an optgroup with the label "Group 1"
 829         *
 830         * <>
 831         *   Unsets the current group, allowing items to be inserted at the root element.
 832         */
 833        if (preg_match('/^\<([^>]*)\>$/', $option, $matches)) {
 834          if (empty($matches[1])) {
 835            unset($group);
 836          }
 837          elseif (!$flat) {
 838            $group = $filter ? _webform_filter_values($matches[1], NULL, NULL, NULL, FALSE) : $matches[1];
 839          }
 840        }
 841        elseif (preg_match('/^([^|]+)\|(.*)$/', $option, $matches)) {
 842          $key = $filter ? _webform_filter_values($matches[1], NULL, NULL, NULL, FALSE) : $matches[1];
 843          $value = $filter ? _webform_filter_values($matches[2], NULL, NULL, NULL, FALSE) : $matches[2];
 844          isset($group) ? $options[$group][$key] = $value : $options[$key] = $value;
 845        }
 846        else {
 847          $filtered_option = $filter ? _webform_filter_values($option, NULL, NULL, NULL, FALSE) : $option;
 848          isset($group) ? $options[$group][$filtered_option] = $filtered_option : $options[$filtered_option] = $filtered_option;
 849        }
 850      }
 851  
 852      $option_cache[$flat][$md5] = $options;
 853    }
 854  
 855    // Return our options from the option_cache array.
 856    return $option_cache[$flat][$md5];
 857  }
 858  
 859  
 860  /**
 861   * Convert an array of options into text.
 862   */
 863  function _webform_select_options_to_text($options) {
 864    $output = '';
 865    $previous_key = false;
 866  
 867    foreach ($options as $key => $value) {
 868      // Convert groups.
 869      if (is_array($value)) {
 870        $output .= '<' . $key . '>' . "\n";
 871        foreach ($value as $subkey => $subvalue) {
 872          $output .= $subkey . '|' . $subvalue . "\n";
 873        }
 874        $previous_key = $key;
 875      }
 876      // Typical key|value pairs.
 877      else {
 878        // Exit out of any groups.
 879        if (isset($options[$previous_key]) && is_array($options[$previous_key])) {
 880          $output .= "<>\n";
 881        }
 882        // Skip empty rows.
 883        if ($options[$key] !== '') {
 884          $output .= $key . '|' . $value . "\n";
 885        }
 886        $previous_key = $key;
 887      }
 888    }
 889  
 890    return $output;
 891  }
 892  
 893  /**
 894   * Utility function to shuffle an array while preserving key-value pairs.
 895   */
 896  function _webform_shuffle_options(&$array) {
 897    // First shuffle the array keys, then use them as the basis for ordering
 898    // the options.
 899    $aux = array();
 900    $keys = array_keys($array);
 901    shuffle($keys);
 902    foreach ($keys as $key) {
 903      $aux[$key] = $array[$key];
 904    }
 905    $array = $aux;
 906  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7