You are here

function _fillpdf_get_tokens_from_components in FillPDF 6

1 call to _fillpdf_get_tokens_from_components()
fillpdf_token_values in ./webform_tokens.inc

File

./webform_tokens.inc, line 52

Code

function _fillpdf_get_tokens_from_components($submission) {
  $tokens = array();
  foreach ($submission->data as $cid => $component) {

    //    $tokens["webform-val-{$component['form_key']}"] = $submission->data[$component['cid']]['value'][0];
    // First, unserialize everything so we can work with them as arrays
    switch ($component['type']) {
      case 'fieldset':
      case 'pagebreak':
        break;
      default:
        $fullvalue = false;

        // For components with selectable options (including grids), make an array of options in the form array(safe_key => "Some readable option", ...)
        $options = false;
        if (is_string($component['extra'])) {
          $component['extra'] = unserialize($component['extra']);

          // Selects use "items"
          if (is_string($component['extra']['items'])) {
            $component['extra']['options'] = $component['extra']['items'];
          }

          // Grids use "options"
          if (is_string($component['extra']['options'])) {
            foreach (preg_split('/[\\r\\n]+/', $component['extra']['options']) as $_) {
              if (strpos($_, '|') !== false) {
                $option = explode('|', $_, 2);
                $options["{$option[0]}"] = $option[1];
              }
              else {

                // Make sure the keys are strings
                $options["{$_}"] = $_;
              }
            }
          }
        }
        if ($options) {
          $component['extra']['options'] = $options;
          unset($options);
        }
        else {
          $component['extra']['options'] = false;
        }
    }
    if ($component['value']) {
      switch ($component['type']) {
        case 'date':

          // Create ISO 8601 date
          if ($component['value'][0]) {
            $value = substr(format_date(strtotime($component['value'][0]), 'small'), 0, 10);
          }
          else {
            $value = '';
          }
          break;
        case 'select':
        case 'grid':

          // Make webform-fullval token
          $fullvalue = array();
          foreach ($component['value'] as $value) {
            if ($component['extra']['options'][$value]) {
              $fullvalue[] = $component['extra']['options'][$value];
            }
            else {
              $fullvalue[] = $value;
            }
          }
          $fullvalue = implode(', ', $fullvalue);

        // Don't break: every field gets a webform-val token
        default:

          // Usually there is only one value, so implode just removes the array
          // Otherwise, separate the values with a comma
          // BUG: There should probably be better handling for multiple values
          $value = implode(', ', $component['value']);
      }
    }
    else {
      $value = '';
    }
    $tokens['webform-val-' . $component['form_key']] = $value;
    if ($fullvalue) {
      $tokens['webform-fullval-' . $component['form_key']] = $fullvalue;
    }
  }
  return $tokens;
}