You are here

function signup_webform_signup_data_alter in Signup 6.2

Implementation of hook_signup_data_alter().

File

modules/signup_webform/signup_webform.module, line 154
signup_webform.module

Code

function signup_webform_signup_data_alter(&$signup, $form_values) {

  // Act on each pane that is a webform pane.
  foreach (_signup_webform_pane_ids($signup->form_data) as $pane_id) {

    // Load the webform API.
    // Do this inside the loop so signups that don't use webform don't load this.
    module_load_include('inc', 'webform', 'webform_submissions');

    // Get the real nid from the prefixed pane ID and thence the node.
    $webform_nid = substr($pane_id, 8);
    $webform_node = node_load($webform_nid);

    /*
    So... some missing documentation!
    webform_submission_insert($node, $submitted)
    expects this for $submitted, where keys are cids:
    *   Array
    *  (
    *      [1] => Array // Single checkbox TRUE/FALSE
    *          (
    *              [1] => 1
    *          )
    *
    *      [2] => Abbey Road // Textfield
    *      [3] => Array // Multiple select: checkboxes
    *          (
    *              [John] => John
    *              [Paul] => Paul
    *          )
    *
    *  )
    *
    */
    $component_lookup = _signup_webform_translate_cid_form_keys($webform_node);
    $submitted = array();
    $webform_data = $signup->form_data[$pane_id];
    foreach ($webform_data as $form_key => $component_data) {
      $cid = $component_lookup[$form_key];

      // Remove 0 values for unselected checkbox items
      if (is_array($component_data)) {
        $component_data = array_filter($component_data);
      }
      $submitted[$cid] = $component_data;
    }
    if (isset($signup->sid)) {

      // existing signup: update the webform submission with the new data.
      $submission_sid = db_result(db_query('SELECT submission_sid FROM {signup_webform_submission} WHERE signup_sid = %d AND webform_nid = %d', $signup->sid, $webform_nid));
      webform_submission_update($webform_node, $submission_sid, $submitted);

      // We store the sid so hook_signup_form_data_display() has something to work on.
      $signup->form_data[$pane_id] = array(
        'sid' => $submission_sid,
      );
    }
    else {

      // new signup: insert webform submission.
      $sid = webform_submission_insert($webform_node, $submitted);

      // Replace our data with just the submission ID; this is retrieved
      // by signup_webform_signup_insert() once the signup is saved and has
      // an ID.
      $signup->form_data[$pane_id] = array(
        'sid' => $sid,
      );
    }
  }
}