[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/webform/includes/ -> webform.submissions.inc (source)

   1  <?php
   2  
   3  /**
   4   * @file
   5   * This file is loaded when handling submissions, either submitting new,
   6   * editing, or viewing. It also contains all CRUD functions for submissions.
   7   *
   8   * @author Nathan Haug <nate@lullabot.com>
   9   */
  10  
  11  /**
  12   * Given an array of submitted values, flatten it into data for a submission.
  13   *
  14   * @param $node
  15   *   The node object containing the current webform.
  16   * @param $submitted
  17   *   The submitted user values from the webform.
  18   * @return
  19   *   An array suitable for use in the 'data' property of a $submission object.
  20   */
  21  function webform_submission_data($node, $submitted) {
  22    $data = array();
  23  
  24    foreach ($submitted as $cid => $values) {
  25      // Don't save pagebreaks as submitted data.
  26      if ($node->webform['components'][$cid]['type'] == 'pagebreak') {
  27        continue;
  28      }
  29  
  30      if (is_array($values)) {
  31        $data[$cid]['value'] = $values;
  32      }
  33      else {
  34        $data[$cid]['value'][0] = $values;
  35      }
  36    }
  37  
  38    return $data;
  39  }
  40  
  41  /**
  42   * Update a webform submission entry in the database.
  43   *
  44   * @param $node
  45   *   The node object containing the current webform.
  46   * @param $submission
  47   *   The webform submission object to be saved into the database.
  48   * @return
  49   *   The existing submission SID.
  50   */
  51  function webform_submission_update($node, $submission) {
  52    // Allow other modules to modify the submission before saving.
  53    foreach (module_implements('webform_submission_presave') as $module) {
  54      $function = $module . '_webform_submission_presave';
  55      $function($node, $submission);
  56    }
  57  
  58    // Update the main submission info.
  59    drupal_write_record('webform_submissions', $submission, 'sid');
  60  
  61    // If is draft, only delete data for components submitted, to
  62    // preserve any data from form pages not visited in this submission.
  63    if ($submission->is_draft) {
  64      $submitted_cids = array_keys($submission->data);
  65      if ($submitted_cids) {
  66        $placeholders = db_placeholders($submitted_cids);
  67        $arguments = array_merge(array($submission->sid), $submitted_cids);
  68        db_query("DELETE FROM {webform_submitted_data} WHERE sid = %d AND cid IN (" . $placeholders . ")", $arguments);
  69      }
  70    }
  71    else {
  72      db_query("DELETE FROM {webform_submitted_data} WHERE sid = %d", $submission->sid);
  73    }
  74  
  75    // Then re-add submission data to the database.
  76    $submission->is_new = FALSE;
  77    webform_submission_insert($node, $submission);
  78  
  79    module_invoke_all('webform_submission_update', $node, $submission);
  80  
  81    return $submission->sid;
  82  }
  83  
  84  /**
  85   * Insert a webform submission entry in the database.
  86   *
  87   * @param $node
  88   *   The node object containing the current webform.
  89   * @param $submission
  90   *   The webform submission object to be saved into the database.
  91   * @return
  92   *   The new submission SID.
  93   */
  94  function webform_submission_insert($node, $submission) {
  95    // The submission ID may already be set if being called as an update.
  96    if (!isset($submission->sid) && (!isset($submission->is_new) || $submission->is_new == FALSE)) {
  97      // Allow other modules to modify the submission before saving.
  98      foreach (module_implements('webform_submission_presave') as $module) {
  99        $function = $module . '_webform_submission_presave';
 100        $function($node, $submission);
 101      }
 102  
 103      $submission->nid = $node->webform['nid'];
 104      drupal_write_record('webform_submissions', $submission);
 105      $is_new = TRUE;
 106    }
 107  
 108    foreach ($submission->data as $cid => $values) {
 109      foreach ($values['value'] as $delta => $value) {
 110        $data = array(
 111          'nid' => $node->webform['nid'],
 112          'sid' => $submission->sid,
 113          'cid' => $cid,
 114          'no' => $delta,
 115          'data' => is_null($value) ? '' : $value,
 116        );
 117        drupal_write_record('webform_submitted_data', $data);
 118      }
 119    }
 120  
 121    // Invoke the insert hook after saving all the data.
 122    if (isset($is_new)) {
 123      module_invoke_all('webform_submission_insert', $node, $submission);
 124    }
 125  
 126    return $submission->sid;
 127  }
 128  
 129  /**
 130   * Delete a single submission.
 131   *
 132   * @param $nid
 133   *   ID of node for which this webform was submitted.
 134   * @param $sid
 135   *   ID of submission to be deleted (from webform_submitted_data).
 136   */
 137  function webform_submission_delete($node, $submission) {
 138    // Iterate through all components and let each do cleanup if necessary.
 139    foreach ($node->webform['components'] as $cid => $component) {
 140      if (isset($submission->data[$cid])) {
 141        webform_component_invoke($component['type'], 'delete', $component, $submission->data[$cid]['value']);
 142      }
 143    }
 144  
 145    // Delete any anonymous session information.
 146    if (isset($_SESSION['webform_submission'][$submission->sid])) {
 147      unset($_SESSION['webform_submission'][$submission->sid]);
 148    }
 149  
 150    db_query('DELETE FROM {webform_submitted_data} WHERE nid = %d AND sid = %d', $node->nid, $submission->sid);
 151    db_query('DELETE FROM {webform_submissions} WHERE nid = %d AND sid = %d', $node->nid, $submission->sid);
 152  
 153    module_invoke_all('webform_submission_delete', $node, $submission);
 154  }
 155  
 156  /**
 157   * Send related e-mails related to a submission.
 158   *
 159   * This function is usually invoked when a submission is completed, but may be
 160   * called any time e-mails should be redelivered.
 161   *
 162   * @param $node
 163   *   The node object containing the current webform.
 164   * @param $submission
 165   *   The webform submission object to be used in sending e-mails.
 166   * @param $emails
 167   *   (optional) An array of specific e-mail settings to be used. If omitted, all
 168   *   e-mails in $node->webform['emails'] will be sent.
 169   */
 170  function webform_submission_send_mail($node, $submission, $emails = NULL) {
 171    global $user;
 172  
 173    // Get the list of e-mails we'll be sending.
 174    $emails = isset($emails) ? $emails : $node->webform['emails'];
 175  
 176    // Create a themed message for mailing.
 177    $send_count = 0;
 178    foreach ($emails as $eid => $email) {
 179      // Set the HTML property based on availablity of MIME Mail.
 180      $email['html'] = ($email['html'] && webform_email_html_capable());
 181  
 182      // Pass through the theme layer if using the default template.
 183      if ($email['template'] == 'default') {
 184        $email['message'] = theme(array('webform_mail_' . $node->nid, 'webform_mail', 'webform_mail_message'), $node, $submission, $email);
 185      }
 186      else {
 187        $email['message'] = $email['template'];
 188      }
 189  
 190      // Replace tokens in the message.
 191      $email['message'] = _webform_filter_values($email['message'], $node, $submission, $email, FALSE, TRUE);
 192  
 193      // Build the e-mail headers.
 194      $email['headers'] = theme(array('webform_mail_headers_' . $node->nid, 'webform_mail_headers'), $node, $submission, $email);
 195  
 196      // Assemble the From string.
 197      if (isset($email['headers']['From'])) {
 198        // If a header From is already set, don't override it.
 199        $email['from'] = $email['headers']['From'];
 200        unset($email['headers']['From']);
 201      }
 202      else {
 203        $email['from'] = webform_format_email_address($email['from_address'], $email['from_name'], $node, $submission);
 204      }
 205  
 206      // Update the subject if set in the themed headers.
 207      if (isset($email['headers']['Subject'])) {
 208        $email['subject'] = $email['headers']['Subject'];
 209        unset($email['headers']['Subject']);
 210      }
 211      else {
 212        $email['subject'] = webform_format_email_subject($email['subject'], $node, $submission);
 213      }
 214  
 215      // Update the to e-mail if set in the themed headers.
 216      if (isset($email['headers']['To'])) {
 217        $email['email'] = $email['headers']['To'];
 218        unset($email['headers']['To']);
 219      }
 220  
 221      // Generate the list of addresses that this e-mail will be sent to.
 222      $addresses = array_filter(explode(',', $email['email']));
 223      $addresses_final = array();
 224      foreach ($addresses as $address) {
 225        $address = trim($address);
 226  
 227        // After filtering e-mail addresses with component values, a single value
 228        // might contain multiple addresses (such as from checkboxes or selects).
 229        $address = webform_format_email_address($address, NULL, $node, $submission, TRUE, FALSE, 'short');
 230  
 231        if (is_array($address)) {
 232          foreach ($address as $new_address) {
 233            $new_address = trim($new_address);
 234            if (valid_email_address($new_address)) {
 235              $addresses_final[] = $new_address;
 236            }
 237          }
 238        }
 239        elseif (valid_email_address($address)) {
 240          $addresses_final[] = $address;
 241        }
 242      }
 243  
 244      // Mail the webform results.
 245      foreach ($addresses_final as $address) {
 246        // Verify that this submission is not attempting to send any spam hacks.
 247        if (_webform_submission_spam_check($address, $email['subject'], $email['from'], $email['headers'])) {
 248          watchdog('webform', 'Possible spam attempt from @remote_addr' . "<br />\n" . nl2br(htmlentities($email['message'])), array('@remote_add' => ip_address()));
 249          drupal_set_message(t('Illegal information. Data not submitted.'), 'error');
 250          return FALSE;
 251        }
 252  
 253        $language = $user->uid ? user_preferred_language($user) : language_default();
 254        $mail_params = array(
 255          'message' => $email['message'],
 256          'subject' => $email['subject'],
 257          'headers' => $email['headers'],
 258          'node' => $node,
 259          'submission' => $submission,
 260          'email' => $email,
 261        );
 262  
 263        if (webform_email_html_capable()) {
 264          // Load attachments for the e-mail.
 265          $attachments = array();
 266          if ($email['attachments']) {
 267            webform_component_include('file');
 268            foreach ($node->webform['components'] as $component) {
 269              if (webform_component_feature($component['type'], 'attachment') && !empty($submission->data[$component['cid']]['value'][0])) {
 270                if (webform_component_implements($component['type'], 'attachments')) {
 271                  $files = webform_component_invoke($component['type'], 'attachments', $component, $submission->data[$component['cid']]['value']);
 272                  if ($files) {
 273                    $attachments = array_merge($attachments, $files);
 274                  }
 275                }
 276              }
 277            }
 278          }
 279  
 280          // Enable drupal_mail_alter() to alter attachments.
 281          $mail_params['attachments'] = $attachments;
 282  
 283          // Prepare (but don't send) the e-mail normally.
 284          $message = drupal_mail('webform', 'submission', $address, $language, $mail_params, $email['from'], FALSE);
 285  
 286          // Send the e-mail via MIME mail.
 287          $message = mimemail($message['from'], $message['to'], $message['subject'], $message['body'], !$email['html'], $message['headers'], $email['html'] ? NULL : $message['body'], $message['params']['attachments'], 'webform');
 288          // Support boolean (older) or array-based return values from MIME Mail.
 289          if ((is_array($message) && $message['result']) || (!is_array($message) && $message)) {
 290            $send_count++;
 291          }
 292        }
 293        else {
 294          // Normal Drupal mailer.
 295          $message = drupal_mail('webform', 'submission', $address, $language, $mail_params, $email['from']);
 296          if ($message['result']) {
 297            $send_count++;
 298          }
 299        }
 300      }
 301    }
 302  
 303    return $send_count;
 304  }
 305  
 306  /**
 307   * Confirm form to delete a single form submission.
 308   *
 309   * @param $form_state
 310   *   The current form state.
 311   * @param $node
 312   *   The node for which this webform was submitted.
 313   * @param $submission
 314   *   The submission to be deleted (from webform_submitted_data).
 315   */
 316  function webform_submission_delete_form($form_state, $node, $submission) {
 317    webform_set_breadcrumb($node, $submission);
 318  
 319    // Set the correct page title.
 320    drupal_set_title(webform_submission_title($node, $submission));
 321  
 322    // Keep the NID and SID in the same location as the webform_client_form().
 323    // This helps mollom identify the same fields when deleting a submission.
 324    $form['#tree'] = TRUE;
 325    $form['details']['nid'] = array(
 326      '#type' => 'value',
 327      '#value' => $node->nid,
 328    );
 329    $form['details']['sid'] = array(
 330      '#type' => 'value',
 331      '#value' => $submission->sid,
 332    );
 333  
 334    $question = t('Are you sure you want to delete this submission?');
 335  
 336    if (isset($_GET['destination'])) {
 337      $destination = $_GET['destination'];
 338    }
 339    elseif (webform_results_access($node)) {
 340      $destination = 'node/' . $node->nid . '/webform-results';
 341    }
 342    else {
 343      $destination = 'node/' . $node->nid . '/submissions';
 344    }
 345  
 346    return confirm_form($form, NULL, $destination, $question, t('Delete'), t('Cancel'));
 347  }
 348  
 349  function webform_submission_delete_form_submit($form, &$form_state) {
 350    $node = node_load($form_state['values']['details']['nid']);
 351    $submission = webform_get_submission($form_state['values']['details']['nid'], $form_state['values']['details']['sid']);
 352    webform_submission_delete($node, $submission);
 353    drupal_set_message(t('Submission deleted.'));
 354  
 355    $form_state['redirect'] = 'node/' . $node->nid . '/webform-results';
 356  }
 357  
 358  /**
 359   * Menu title callback; Return the submission number as a title.
 360   */
 361  function webform_submission_title($node, $submission) {
 362    return t('Submission #@sid', array('@sid' => $submission->sid));
 363  }
 364  
 365  /**
 366   * Menu callback; Present a Webform submission page for display or editing.
 367   */
 368  function webform_submission_page($node, $submission, $format) {
 369    global $user;
 370  
 371    // Add admin CSS.
 372    drupal_add_css(drupal_get_path('module', 'webform') .'/css/webform-admin.css', 'module', 'all', FALSE);
 373  
 374    // Render the admin UI breadcrumb.
 375    webform_set_breadcrumb($node, $submission);
 376  
 377    // Set the correct page title.
 378    drupal_set_title(webform_submission_title($node, $submission));
 379  
 380    if ($format == 'form') {
 381      $output = drupal_get_form('webform_client_form_' . $node->nid, $node, $submission);
 382    }
 383    else {
 384      $output = webform_submission_render($node, $submission, NULL, $format);
 385    }
 386  
 387    // Determine the mode in which we're displaying this submission.
 388    $mode = ($format != 'form') ? 'display' : 'form';
 389    if (strpos(request_uri(), 'print/') !== FALSE) {
 390      $mode = 'print';
 391    }
 392    if (strpos(request_uri(), 'printpdf/') !== FALSE) {
 393      $mode = 'pdf';
 394    }
 395  
 396    // Add navigation for administrators.
 397    if (webform_results_access($node)) {
 398      $navigation = theme('webform_submission_navigation', $node, $submission, $mode);
 399      $information = theme('webform_submission_information', $node, $submission, $mode);
 400    }
 401    else {
 402      $navigation = NULL;
 403      $information = NULL;
 404    }
 405  
 406    // Actions may be shown to all users.
 407    $actions = theme('links', module_invoke_all('webform_submission_actions', $node, $submission), array('class' => 'links inline webform-submission-actions'));
 408  
 409    // Disable the page cache for anonymous users viewing or editing submissions.
 410    if (!$user->uid) {
 411      webform_disable_page_cache();
 412    }
 413  
 414    return theme('webform_submission_page', $node, $submission, $output, $navigation, $information, $actions, $mode);
 415  }
 416  
 417  /**
 418   * Form to resend specific e-mails associated with a submission.
 419   */
 420  function webform_submission_resend($form_state, $node, $submission) {
 421    // Render the admin UI breadcrumb.
 422    webform_set_breadcrumb($node, $submission);
 423  
 424    $form['#tree'] = TRUE;
 425    $form['#node'] = $node;
 426    $form['#submission'] = $submission;
 427  
 428    foreach ($node->webform['emails'] as $eid => $email) {
 429      $email_addresses = array_filter(explode(',', check_plain($email['email'])));
 430      foreach ($email_addresses as $key => $email_address) {
 431        $email_addresses[$key] = webform_format_email_address($email_address, NULL, $node, $submission, FALSE);
 432      }
 433      $valid_email = !empty($email_addresses[0]) && valid_email_address($email_addresses[0]);
 434      $form['resend'][$eid] = array(
 435        '#type' => 'checkbox',
 436        '#default_value' => $valid_email ? TRUE : FALSE,
 437        '#disabled' => $valid_email ? FALSE : TRUE,
 438      );
 439      $form['emails'][$eid]['email'] = array(
 440        '#value' => implode('<br />', $email_addresses),
 441      );
 442      if (!$valid_email) {
 443        $form['emails'][$eid]['email']['#value'] .= ' (' . t('empty') . ')';
 444      }
 445      $form['emails'][$eid]['subject'] = array(
 446        '#value' => check_plain(webform_format_email_subject($email['subject'], $node, $submission)),
 447      );
 448  
 449      $form['actions'] = array('#type' => 'markup');
 450      $form['actions']['resend'] = array(
 451        '#type' => 'submit',
 452        '#value' => t('Resend e-mails'),
 453      );
 454      $form['actions']['cancel'] = array(
 455        '#type' => 'markup',
 456        '#value' => l(t('Cancel'), isset($_GET['destination']) ? $_GET['destination'] : 'node/' . $node->nid . '/submission/' . $submission->sid),
 457      );
 458    }
 459    return $form;
 460  }
 461  
 462  /**
 463   * Validate handler for webform_submission_resend().
 464   */
 465  function webform_submission_resend_validate($form, &$form_state) {
 466    if (count(array_filter($form_state['values']['resend'])) == 0) {
 467      form_set_error('emails', t('You must select at least one email address to resend submission.'));
 468    }
 469  }
 470  
 471  /**
 472   * Submit handler for webform_submission_resend().
 473   */
 474  function webform_submission_resend_submit($form, &$form_state) {
 475    $node = $form['#node'];
 476    $submission = $form['#submission'];
 477  
 478    $emails = array();
 479    foreach ($form_state['values']['resend'] as $eid => $checked) {
 480      if ($checked) {
 481        $emails[] = $form['#node']->webform['emails'][$eid];
 482      }
 483    }
 484    $sent_count = webform_submission_send_mail($node, $submission, $emails);
 485    if ($sent_count) {
 486      drupal_set_message(format_plural($sent_count,
 487        'Successfully re-sent submission #@sid to 1 recipient.',
 488        'Successfully re-sent submission #@sid to @count recipients.',
 489        array('@sid' => $submission->sid)
 490      ));
 491    }
 492    else {
 493      drupal_set_message(t('No e-mails were able to be sent due to a server error.'), 'error');
 494    }
 495  }
 496  
 497  /**
 498   * Theme the node components form. Use a table to organize the components.
 499   *
 500   * @param $form
 501   *   The form array.
 502   * @return
 503   *   Formatted HTML form, ready for display.
 504   */
 505  function theme_webform_submission_resend($form) {
 506    $header = array('', t('E-mail to'), t('Subject'));
 507    $rows = array();
 508    if (!empty($form['emails'])) {
 509      foreach (element_children($form['emails']) as $eid) {
 510        // Add each component to a table row.
 511        $rows[] = array(
 512          drupal_render($form['resend'][$eid]),
 513          drupal_render($form['emails'][$eid]['email']),
 514          drupal_render($form['emails'][$eid]['subject']),
 515        );
 516      }
 517    }
 518    else {
 519      $rows[] = array(array('data' => t('This webform is currently not setup to send emails.'), 'colspan' => 3));
 520    }
 521    $output = '';
 522    $output .= theme('table', $header, $rows, array('id' => 'webform-emails'));
 523    $output .= drupal_render($form);
 524    return $output;
 525  }
 526  
 527  /**
 528   * Print a Webform submission for display on a page or in an e-mail.
 529   */
 530  function webform_submission_render($node, $submission, $email, $format) {
 531    $component_tree = array();
 532    $renderable = array();
 533    $page_count = 1;
 534    $excluded_components = isset($email) ? $email['excluded_components'] : array();
 535  
 536    // Meta data that may be useful for modules implementing
 537    // hook_webform_submission_render_alter().
 538    $renderable['#node'] = $node;
 539    $renderable['#submission'] = $submission;
 540    $renderable['#email'] = $email;
 541    $renderable['#format'] = $format;
 542  
 543    // Set the theme function for submissions.
 544    $renderable['#theme'] = array('webform_submission_' . $node->nid, 'webform_submission');
 545  
 546    // Remove excluded components.
 547    $components = $node->webform['components'];
 548    foreach ($excluded_components as $cid) {
 549      unset($components[$cid]);
 550    }
 551  
 552    _webform_components_tree_build($components, $component_tree, 0, $page_count);
 553  
 554    // Make sure at least one field is available
 555    if (isset($component_tree['children'])) {
 556      // Recursively add components to the form.
 557      foreach ($component_tree['children'] as $cid => $component) {
 558        if (_webform_client_form_rule_check($node, $component, $component['page_num'], NULL, $submission)) {
 559          _webform_client_form_add_component($node, $component, NULL, $renderable, $renderable, NULL, $submission, $format);
 560        }
 561      }
 562    }
 563  
 564    drupal_alter('webform_submission_render', $renderable);
 565    return drupal_render($renderable);
 566  }
 567  
 568  /**
 569   * Return all the submissions for a particular node.
 570   *
 571   * @param $filters
 572   *   An array of filters to apply to this query. Usually in the format
 573   *   array('nid' => $nid, 'uid' => $uid). A single integer may also be passed
 574   *   in, which will be equivalent to specifying a $nid filter.
 575   * @param $header
 576   *   If the results of this fetch will be used in a sortable
 577   *   table, pass the array header of the table.
 578   * @param $pager_count
 579   *   Optional. The number of submissions to include in the results.
 580   */
 581  function webform_get_submissions($filters = array(), $header = NULL, $pager_count = 0) {
 582    $submissions = array();
 583  
 584    if (!is_array($filters)) {
 585      $filters = array('nid' => $filters);
 586    }
 587  
 588    // UID filters need to be against a specific table.
 589    if (isset($filters['uid'])) {
 590      $filters['u.uid'] = $filters['uid'];
 591      unset($filters['uid']);
 592    }
 593  
 594    // No need to find SIDs if it was given to us.
 595    if (isset($filters['sid'])) {
 596      if(is_array($filters['sid'])){
 597        $sids = $filters['sid'];
 598      }
 599      else{
 600        $sids = array($filters['sid']);
 601      }
 602    }
 603    // Build the list of SIDs that need to be retrieved.
 604    else {
 605      $arguments = array_values($filters);
 606      $where = array();
 607      foreach ($filters as $column => $value) {
 608        $where[] = $column . ' = ' . (is_numeric($value) ? '%d' : "'%s'");
 609      }
 610  
 611      if (isset($filters['u.uid']) && $filters['u.uid'] === 0) {
 612        if (!empty($_SESSION['webform_submission'])) {
 613          $anonymous_sids = array_keys($_SESSION['webform_submission']);
 614          $where[] = 'sid IN (' . db_placeholders($anonymous_sids) . ')';
 615          $arguments = array_merge($arguments, $anonymous_sids);
 616        }
 617        else {
 618          $where[] = 'sid = 0';
 619        }
 620      }
 621  
 622      $where_clause = implode(' AND ', $where);
 623      $pager_query = 'SELECT sid FROM {webform_submissions} s LEFT JOIN {users} u ON u.uid = s.uid WHERE ' . $where_clause;
 624  
 625      if (is_array($header)) {
 626        $pager_query .= tablesort_sql($header);
 627      }
 628      else {
 629        $pager_query .= ' ORDER BY sid ASC';
 630      }
 631  
 632      if ($pager_count) {
 633        $result = pager_query($pager_query, $pager_count, 0, NULL, $arguments);
 634      }
 635      else {
 636        $result = db_query($pager_query, $arguments);
 637      }
 638  
 639      $sids = array();
 640      while ($row = db_fetch_object($result)) {
 641        $sids[] = $row->sid;
 642        $submissions[$row->sid] = FALSE;
 643      }
 644    }
 645  
 646    // If there are no submissions being retrieved, return an empty array.
 647    if (empty($sids)) {
 648      return $submissions;
 649    }
 650  
 651    // Query the required submission data.
 652    $query = 'SELECT s.*, sd.cid, sd.no, sd.data, u.name, u.mail, u.status ' .
 653             'FROM {webform_submitted_data} sd ' .
 654             'LEFT JOIN {webform_submissions} s ON s.sid = sd.sid ' .
 655             'LEFT JOIN {users} u ON u.uid = s.uid ' .
 656             'WHERE sd.sid IN (' . db_placeholders($sids) . ') ' .
 657             // By adding the NID to this query we allow MySQL to use the primary
 658             // key in webform_submitted_data for sorting (nid_sid_cid_no).
 659             (isset($filters['nid']) ? 'AND sd.nid = %d ' : '') .
 660             'ORDER BY sd.sid ASC, sd.cid ASC, sd.no ASC';
 661  
 662    $args = $sids;
 663    if (isset($filters['nid'])) {
 664      $args[] = $filters['nid'];
 665    }
 666  
 667    $result = db_query($query, $args);
 668  
 669    // Convert the queried rows into submissions.
 670    $previous = array();
 671    while ($row = db_fetch_object($result)) {
 672      if ($row->sid != $previous) {
 673        $submissions[$row->sid] = new stdClass();
 674        $submissions[$row->sid]->sid = $row->sid;
 675        $submissions[$row->sid]->nid = $row->nid;
 676        $submissions[$row->sid]->submitted = $row->submitted;
 677        $submissions[$row->sid]->remote_addr = $row->remote_addr;
 678        $submissions[$row->sid]->uid = $row->uid;
 679        $submissions[$row->sid]->name = $row->name;
 680        $submissions[$row->sid]->is_draft = $row->is_draft;
 681        $submissions[$row->sid]->data = array();
 682      }
 683      // CID may be NULL if this submission does not actually contain any data.
 684      if ($row->cid) {
 685        $submissions[$row->sid]->data[$row->cid]['value'][$row->no] = $row->data;
 686      }
 687      $previous = $row->sid;
 688    }
 689  
 690    foreach (module_implements('webform_submission_load') as $module) {
 691      $function = $module . '_webform_submission_load';
 692      $function($submissions);
 693    }
 694  
 695    return $submissions;
 696  }
 697  
 698  /**
 699   * Return a count of the total number of submissions for a node.
 700   *
 701   * @param $nid
 702   *   The node ID for which submissions are being fetched.
 703   * @param $uid
 704   *   Optional; the user ID to filter the submissions by.
 705   * @return
 706   *   An integer value of the number of submissions.
 707   */
 708  function webform_get_submission_count($nid, $uid = NULL, $reset = FALSE) {
 709    static $counts;
 710  
 711    if (!isset($counts[$nid][$uid]) || $reset) {
 712      $query = 'SELECT count(*) FROM {webform_submissions} WHERE nid = %d AND is_draft = 0';
 713      $arguments = array($nid);
 714      if ($uid !== NULL) {
 715        $query .= ' AND uid = %d';
 716        $arguments[] = $uid;
 717      }
 718      if ($uid === 0) {
 719        $submissions = isset($_SESSION['webform_submission']) ? $_SESSION['webform_submission'] : array();
 720        $query .= count($submissions) ? ' AND sid IN (' . db_placeholders($submissions) . ')' : ' AND sid = 0';
 721        $arguments = array_merge($arguments, array_keys($submissions));
 722      }
 723  
 724      $counts[$nid][$uid] = db_result(db_query($query, $arguments));
 725    }
 726    return $counts[$nid][$uid];
 727  }
 728  
 729  /**
 730   * Fetch a specified submission for a webform node.
 731   */
 732  function webform_get_submission($nid, $sid, $reset = FALSE) {
 733    static $submissions = array();
 734  
 735    if ($reset) {
 736      $submissions = array();
 737      if (!isset($sid)) {
 738        return;
 739      }
 740    }
 741  
 742    // Load the submission if needed.
 743    if (!isset($submissions[$sid])) {
 744      $new_submissions = webform_get_submissions(array('nid' => $nid, 'sid' => $sid));
 745      $submissions[$sid] = isset($new_submissions[$sid]) ? $new_submissions[$sid] : FALSE;
 746    }
 747  
 748    return $submissions[$sid];
 749  }
 750  
 751  function _webform_submission_spam_check($to, $subject, $from, $headers = array()) {
 752    $headers = implode('\n', (array)$headers);
 753    // Check if they are attempting to spam using a bcc or content type hack.
 754    if (preg_match('/(b?cc\s?:)|(content\-type:)/i', $to . "\n" . $subject . "\n" . $from . "\n" . $headers)) {
 755      return TRUE; // Possible spam attempt.
 756    }
 757    return FALSE; // Not spam.
 758  }
 759  
 760  /**
 761   * Check if the current user has exceeded the limit on this form.
 762   *
 763   * @param $node
 764   *   The webform node to be checked.
 765   * @return
 766   *   Boolean TRUE if the user has exceeded their limit. FALSE otherwise.
 767   */
 768  function _webform_submission_user_limit_check($node) {
 769    global $user;
 770  
 771    // Check if submission limiting is enabled.
 772    if ($node->webform['submit_limit'] == '-1') {
 773      return FALSE; // No check enabled.
 774    }
 775  
 776    // Retrieve submission data for this IP address or username from the database.
 777    $query = 'SELECT count(*) ' .
 778             'FROM {webform_submissions} ' .
 779             "WHERE (( 0 = %d AND remote_addr = '%s') OR (uid > 0 AND uid = %d)) " .
 780             'AND submitted > %d AND nid = %d AND is_draft = 0';
 781  
 782    // Fetch all the entries from the database within the submit interval with this username and IP.
 783    $num_submissions_database = db_result(db_query($query, $user->uid, ip_address(), $user->uid, ($node->webform['submit_interval'] != -1) ? (time() - $node->webform['submit_interval']) : $node->webform['submit_interval'], $node->nid));
 784  
 785    // Double check the submission history from the users machine using cookies.
 786    $num_submissions_cookie = 0;
 787    if ($user->uid == 0 && variable_get('webform_use_cookies', 0)) {
 788      $cookie_name = 'webform-' . $node->nid;
 789  
 790      if (isset($_COOKIE[$cookie_name]) && is_array($_COOKIE[$cookie_name])) {
 791        foreach ($_COOKIE[$cookie_name] as $key => $timestamp) {
 792          if ($node->webform['submit_interval'] != -1 && $timestamp <= time() - $node->webform['submit_interval']) {
 793            // Remove the cookie if past the required time interval.
 794            $params = session_get_cookie_params();
 795            setcookie($cookie_name . '[' . $key . ']', '', 0, $params['path'], $params['domain'], $params['secure']);
 796          }
 797        }
 798        // Count the number of submissions recorded in cookies.
 799        $num_submissions_cookie = count($_COOKIE[$cookie_name]);
 800      }
 801      else {
 802        $num_submissions_cookie = 0;
 803      }
 804    }
 805  
 806    if ($num_submissions_database >= $node->webform['submit_limit'] || $num_submissions_cookie >= $node->webform['submit_limit']) {
 807      // Limit exceeded.
 808      return TRUE;
 809    }
 810  
 811    // Limit not exceeded.
 812    return FALSE;
 813  }
 814  
 815  /**
 816   * Check if the total number of submissions has exceeded the limit on this form.
 817   *
 818   * @param $node
 819   *   The webform node to be checked.
 820   * @return
 821   *   Boolean TRUE if the form has exceeded it's limit. FALSE otherwise.
 822   */
 823  function _webform_submission_total_limit_check($node) {
 824  
 825    // Check if submission limiting is enabled.
 826    if ($node->webform['total_submit_limit'] == '-1') {
 827      return FALSE; // No check enabled.
 828    }
 829  
 830    // Retrieve submission data from the database.
 831    $query = 'SELECT count(*) ' .
 832             'FROM {webform_submissions} ' .
 833             'WHERE submitted > %d AND nid = %d AND is_draft = 0';
 834  
 835    // Fetch all the entries from the database within the submit interval.
 836    $num_submissions_database = db_result(db_query($query, ($node->webform['total_submit_interval'] != -1) ? (time() - $node->webform['total_submit_interval']) : $node->webform['total_submit_interval'], $node->nid));
 837  
 838    if ($num_submissions_database >= $node->webform['total_submit_limit']) {
 839      // Limit exceeded.
 840      return TRUE;
 841    }
 842  
 843    // Limit not exceeded.
 844    return FALSE;
 845  }
 846  
 847  /**
 848   * Preprocess function for webform-submission.tpl.php.
 849   */
 850  function template_preprocess_webform_submission(&$vars) {
 851    $vars['node'] = $vars['renderable']['#node'];
 852    $vars['submission'] = $vars['renderable']['#submission'];
 853    $vars['email'] = $vars['renderable']['#email'];
 854    $vars['format'] = $vars['renderable']['#format'];
 855  }
 856  
 857  /**
 858   * Preprocess function for webform-submission-navigation.tpl.php.
 859   */
 860  function template_preprocess_webform_submission_navigation(&$vars) {
 861    $start_path = ($vars['mode'] == 'print') ? 'print/' : 'node/';
 862    $vars['previous'] = db_result(db_query('SELECT MAX(sid) FROM {webform_submissions} WHERE nid = %d AND sid < %d', array($vars['node']->nid, $vars['submission']->sid)));
 863    $vars['next'] = db_result(db_query('SELECT MIN(sid) FROM {webform_submissions} WHERE nid = %d AND sid > %d', array($vars['node']->nid, $vars['submission']->sid)));
 864    $vars['previous_url'] = $start_path . $vars['node']->nid . '/submission/' . $vars['previous'] . ($vars['mode'] == 'form' ? '/edit' : '');
 865    $vars['next_url'] = $start_path . $vars['node']->nid . '/submission/' . $vars['next'] . ($vars['mode'] == 'form' ? '/edit' : '');
 866  }
 867  
 868  /**
 869   * Preprocess function for webform-submission-navigation.tpl.php.
 870   */
 871  function template_preprocess_webform_submission_information(&$vars) {
 872    $vars['account'] = user_load($vars['submission']->uid);
 873  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7