Projet

Général

Profil

Paste
Télécharger (39,4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / webform / includes / webform.submissions.inc @ 76bdcd04

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5 01f36513 Assos Assos
 * Submission handling functions.
6
 *
7 85ad3d82 Assos Assos
 * This file is loaded when handling submissions, either submitting new,
8
 * editing, or viewing. It also contains all CRUD functions for submissions.
9
 *
10
 * @author Nathan Haug <nate@lullabot.com>
11
 */
12
13
/**
14
 * Given an array of submitted values, flatten it into data for a submission.
15
 *
16
 * @param $node
17
 *   The node object containing the current webform.
18
 * @param $submitted
19
 *   The submitted user values from the webform.
20 8c72e82a Assos Assos
 *
21
 * @return array
22 85ad3d82 Assos Assos
 *   An array suitable for use in the 'data' property of a $submission object.
23
 */
24
function webform_submission_data($node, $submitted) {
25
  $data = array();
26
27
  foreach ($submitted as $cid => $values) {
28 a45e4bc1 Assos Assos
    // Don't save pseudo-fields or pagebreaks as submitted data.
29
    if (!isset($node->webform['components'][$cid]) || $node->webform['components'][$cid]['type'] == 'pagebreak') {
30 85ad3d82 Assos Assos
      continue;
31
    }
32
33
    if (is_array($values)) {
34 a45e4bc1 Assos Assos
      $data[$cid] = $values;
35 85ad3d82 Assos Assos
    }
36
    else {
37 a45e4bc1 Assos Assos
      $data[$cid][0] = $values;
38 85ad3d82 Assos Assos
    }
39
  }
40
41
  return $data;
42
}
43
44 a45e4bc1 Assos Assos
/**
45
 * Given set of $form_state values, prepare a psuedo-submission.
46
 *
47
 * @param object $node
48
 *   The webform node object.
49
 * @param object $account
50
 *   The user account that is creating this submission.
51
 * @param array $form_state
52 feca1e4a Assos Assos
 *   The form_state containing the values for the submission.
53 a45e4bc1 Assos Assos
 * @param object $prototype
54
 *   An existing submission that is being previewed, if any.
55 feca1e4a Assos Assos
 *
56 a45e4bc1 Assos Assos
 * @return object
57
 *   A new submission object, possibly for preview
58
 */
59 01f36513 Assos Assos
function webform_submission_create($node, $account, array $form_state, $is_preview = FALSE, $prototype = NULL) {
60 a45e4bc1 Assos Assos
  $data = webform_submission_data($node, $form_state['values']['submitted']);
61 8c72e82a Assos Assos
  if (is_object($prototype)) {
62 a45e4bc1 Assos Assos
    $submission = clone $prototype;
63
    $submission->preview = $is_preview;
64
    $submission->data = $data;
65
  }
66
  else {
67
    $submission = (object) array(
68
      'nid' => $node->nid,
69
      'uid' => $account->uid,
70
      'sid' => NULL,
71
      'submitted' => REQUEST_TIME,
72
      'completed' => 0,
73
      'modified' => REQUEST_TIME,
74
      'remote_addr' => webform_ip_address($node),
75
      'is_draft' => TRUE,
76
      'highest_valid_page' => 0,
77
      'preview' => $is_preview,
78
      'serial' => NULL,
79
      'data' => $data,
80
    );
81 feca1e4a Assos Assos
    drupal_alter('webform_submission_create', $submission, $node, $account, $form_state);
82 a45e4bc1 Assos Assos
  }
83
  return $submission;
84
}
85
86 85ad3d82 Assos Assos
/**
87
 * Update a webform submission entry in the database.
88
 *
89
 * @param $node
90
 *   The node object containing the current webform.
91
 * @param $submission
92
 *   The webform submission object to be saved into the database.
93 feca1e4a Assos Assos
 *
94 01f36513 Assos Assos
 * @return int
95 85ad3d82 Assos Assos
 *   The existing submission SID.
96
 */
97
function webform_submission_update($node, $submission) {
98
  // Allow other modules to modify the submission before saving.
99
  foreach (module_implements('webform_submission_presave') as $module) {
100
    $function = $module . '_webform_submission_presave';
101
    $function($node, $submission);
102
  }
103
104 a45e4bc1 Assos Assos
  $submission->completed = empty($submission->completed) && !$submission->is_draft ? REQUEST_TIME : $submission->completed;
105
  $submission->modified = REQUEST_TIME;
106
107 85ad3d82 Assos Assos
  // Update the main submission info.
108
  drupal_write_record('webform_submissions', $submission, 'sid');
109
110
  // If is draft, only delete data for components submitted, to
111
  // preserve any data from form pages not visited in this submission.
112
  if ($submission->is_draft) {
113
    $submitted_cids = array_keys($submission->data);
114
    if ($submitted_cids) {
115
      db_delete('webform_submitted_data')
116
        ->condition('sid', $submission->sid)
117
        ->condition('cid', $submitted_cids, 'IN')
118
        ->execute();
119
    }
120
  }
121
  else {
122
    db_delete('webform_submitted_data')
123
      ->condition('sid', $submission->sid)
124
      ->execute();
125
  }
126
127
  // Then re-add submission data to the database.
128
  $submission->is_new = FALSE;
129
  webform_submission_insert($node, $submission);
130
131
  module_invoke_all('webform_submission_update', $node, $submission);
132
133
  return $submission->sid;
134
}
135
136
/**
137
 * Insert a webform submission entry in the database.
138
 *
139
 * @param $node
140
 *   The node object containing the current webform.
141
 * @param $submission
142
 *   The webform submission object to be saved into the database.
143 feca1e4a Assos Assos
 *
144 01f36513 Assos Assos
 * @return int
145 85ad3d82 Assos Assos
 *   The new submission SID.
146
 */
147
function webform_submission_insert($node, $submission) {
148
  // The submission ID may already be set if being called as an update.
149
  if (!isset($submission->sid) && (!isset($submission->is_new) || $submission->is_new == FALSE)) {
150
    // Allow other modules to modify the submission before saving.
151
    foreach (module_implements('webform_submission_presave') as $module) {
152
      $function = $module . '_webform_submission_presave';
153
      $function($node, $submission);
154
    }
155
    $submission->nid = $node->webform['nid'];
156 a45e4bc1 Assos Assos
    if (empty($submission->serial)) {
157
      $submission->serial = _webform_submission_serial_next_value($node->nid);
158
    }
159
    $submission->completed = empty($submission->completed) && !$submission->is_draft ? REQUEST_TIME : $submission->completed;
160 85ad3d82 Assos Assos
    drupal_write_record('webform_submissions', $submission);
161
    $is_new = TRUE;
162
  }
163
164
  foreach ($submission->data as $cid => $values) {
165 a45e4bc1 Assos Assos
    foreach ($values as $delta => $value) {
166 85ad3d82 Assos Assos
      $data = array(
167
        'nid' => $node->webform['nid'],
168
        'sid' => $submission->sid,
169
        'cid' => $cid,
170
        'no' => $delta,
171
        'data' => is_null($value) ? '' : $value,
172
      );
173
      drupal_write_record('webform_submitted_data', $data);
174
    }
175
  }
176
177
  // Invoke the insert hook after saving all the data.
178
  if (isset($is_new)) {
179
    module_invoke_all('webform_submission_insert', $node, $submission);
180
  }
181
182
  return $submission->sid;
183
}
184
185
/**
186
 * Delete a single submission.
187
 *
188 3753f249 Assos Assos
 * @param $node
189
 *   The node object containing the current webform.
190
 * @param $submission
191
 *   The webform submission object to be deleted from the database.
192 85ad3d82 Assos Assos
 */
193
function webform_submission_delete($node, $submission) {
194
  // Iterate through all components and let each do cleanup if necessary.
195
  foreach ($node->webform['components'] as $cid => $component) {
196
    if (isset($submission->data[$cid])) {
197 a45e4bc1 Assos Assos
      webform_component_invoke($component['type'], 'delete', $component, $submission->data[$cid]);
198 85ad3d82 Assos Assos
    }
199
  }
200
201
  // Delete any anonymous session information.
202
  if (isset($_SESSION['webform_submission'][$submission->sid])) {
203
    unset($_SESSION['webform_submission'][$submission->sid]);
204
  }
205
206
  db_delete('webform_submitted_data')
207
    ->condition('nid', $node->nid)
208
    ->condition('sid', $submission->sid)
209
    ->execute();
210
  db_delete('webform_submissions')
211
    ->condition('nid', $node->nid)
212
    ->condition('sid', $submission->sid)
213
    ->execute();
214
215
  module_invoke_all('webform_submission_delete', $node, $submission);
216
}
217
218
/**
219
 * Send related e-mails related to a submission.
220
 *
221
 * This function is usually invoked when a submission is completed, but may be
222
 * called any time e-mails should be redelivered.
223
 *
224
 * @param $node
225
 *   The node object containing the current webform.
226
 * @param $submission
227
 *   The webform submission object to be used in sending e-mails.
228
 * @param $emails
229
 *   (optional) An array of specific e-mail settings to be used. If omitted, all
230
 *   e-mails in $node->webform['emails'] will be sent.
231 8c72e82a Assos Assos
 *
232
 * @return int
233
 *   Number of mail sent.
234 85ad3d82 Assos Assos
 */
235
function webform_submission_send_mail($node, $submission, $emails = NULL) {
236
  // Get the list of e-mails we'll be sending.
237
  $emails = isset($emails) ? $emails : $node->webform['emails'];
238
239
  // Create a themed message for mailing.
240
  $send_count = 0;
241
  foreach ($emails as $eid => $email) {
242 76bdcd04 Assos Assos
    $mail = _webform_submission_prepare_mail($node, $submission, $email);
243
    if (!$mail) {
244 a45e4bc1 Assos Assos
      continue;
245 feca1e4a Assos Assos
    }
246 a45e4bc1 Assos Assos
247 76bdcd04 Assos Assos
    $addresses_final = $mail['addresses_final'];
248
    $send_increment = $mail['send_increment'];
249
    $language = $mail['language'];
250
    $mail_params = $mail['mail_params'];
251 85ad3d82 Assos Assos
252 76bdcd04 Assos Assos
    // Mail the webform results.
253
    foreach ($addresses_final as $address) {
254
      $message = drupal_mail('webform', 'submission', $address, $language, $mail_params, $email['from']);
255
      if ($message['result']) {
256
        $send_count += $send_increment;
257
      }
258 85ad3d82 Assos Assos
    }
259 76bdcd04 Assos Assos
  }
260 85ad3d82 Assos Assos
261 76bdcd04 Assos Assos
  return $send_count;
262
}
263 85ad3d82 Assos Assos
264 76bdcd04 Assos Assos
/**
265
 * Prepare a submission email for use by webform_submission_send_mail()
266
 *
267
 * @param $node
268
 *   The node object containing the current webform.
269
 * @param $submission
270
 *   The webform submission object to be used in sending e-mails.
271
 * @param $email
272
 *   The e-mail settings to be used. This will have some of its values adjusted.
273
 *
274
 * @return array|null
275
 *   An array of the information about the email needed by drupal_mail().
276
 */
277
function _webform_submission_prepare_mail($node, $submission, &$email) {
278
  global $user;
279 85ad3d82 Assos Assos
280 76bdcd04 Assos Assos
  // Don't process disabled emails.
281
  if (!$email['status']) {
282
    return;
283
  }
284 a45e4bc1 Assos Assos
285 76bdcd04 Assos Assos
  // Set the HTML property based on availablity of MIME Mail.
286
  $email['html'] = ($email['html'] && webform_variable_get('webform_email_html_capable'));
287 85ad3d82 Assos Assos
288 76bdcd04 Assos Assos
  // Pass through the theme layer if using the default template.
289
  if ($email['template'] == 'default') {
290
    $email['message'] = theme(array('webform_mail_' . $node->nid, 'webform_mail', 'webform_mail_message'), array('node' => $node, 'submission' => $submission, 'email' => $email));
291
  }
292
  else {
293
    $email['message'] = $email['template'];
294
  }
295 85ad3d82 Assos Assos
296 76bdcd04 Assos Assos
  // Replace tokens in the message.
297
  $email['message'] = webform_replace_tokens($email['message'], $node, $submission, $email, (boolean) $email['html']);
298 85ad3d82 Assos Assos
299 76bdcd04 Assos Assos
  // Build the e-mail headers.
300
  $email['headers'] = theme(array('webform_mail_headers_' . $node->nid, 'webform_mail_headers'), array('node' => $node, 'submission' => $submission, 'email' => $email));
301 a45e4bc1 Assos Assos
302 76bdcd04 Assos Assos
  // Assemble the From string.
303
  if (isset($email['headers']['From'])) {
304
    // If a header From is already set, don't override it.
305
    $email['from'] = $email['headers']['From'];
306
    unset($email['headers']['From']);
307
  }
308
  else {
309
    // Format the From address.
310
    $mapping = isset($email['extra']['from_address_mapping']) ? $email['extra']['from_address_mapping'] : NULL;
311
    $email['from'] = webform_format_email_address($email['from_address'], $email['from_name'], $node, $submission, TRUE, TRUE, NULL, $mapping);
312
  }
313
314
  // If "Use Reply-To header" is set in Webform settings and the Reply-To
315
  // header is not already set, set Reply-To to the From and set From address
316
  // to webform_default_from_name and webform_default_from_address.
317
  if (webform_variable_get('webform_email_replyto') &&
318
      empty($email['headers']['Reply-To']) &&
319
      ($default_from_name = webform_variable_get('webform_default_from_name')) &&
320
      ($default_from_address = webform_variable_get('webform_default_from_address')) &&
321
      $email['from'] !== $default_from_name) {
322
    $email['headers']['Reply-To'] = $email['from'];
323
    $email['from'] = $default_from_address;
324
    if (webform_variable_get('webform_email_address_format') == 'long') {
325
      $email_parts = webform_parse_email_address($email['headers']['Reply-To']);
326
      $from_name = t('!name via !site_name',
327
                      array(
328
                        '!name' => strlen($email_parts['name']) ? $email_parts['name'] : $email_parts['address'],
329
                        '!site_name' => $default_from_name,
330
                      ));
331
      $from_name = implode(' ', array_map('mime_header_encode', explode(' ', $from_name)));
332
      $email['from'] = '"' . $from_name . '" <' . $email['from'] . '>';
333
    }
334
  }
335
336
  // Update the subject if set in the themed headers.
337
  if (isset($email['headers']['Subject'])) {
338
    $email['subject'] = $email['headers']['Subject'];
339
    unset($email['headers']['Subject']);
340
  }
341
  else {
342
    $email['subject'] = webform_format_email_subject($email['subject'], $node, $submission);
343
  }
344 85ad3d82 Assos Assos
345 76bdcd04 Assos Assos
  // Update the to e-mail if set in the themed headers.
346
  if (isset($email['headers']['To'])) {
347
    $email['email'] = $email['headers']['To'];
348
    unset($email['headers']['To']);
349
    $addresses = array_filter(explode(',', $email['email']));
350
  }
351
  else {
352
    // Format the To address(es).
353
    $mapping = isset($email['extra']['email_mapping']) ? $email['extra']['email_mapping'] : NULL;
354
    $addresses = webform_format_email_address($email['email'], NULL, $node, $submission, TRUE, FALSE, NULL, $mapping);
355
    $email['email'] = implode(',', $addresses);
356
  }
357 a45e4bc1 Assos Assos
358 76bdcd04 Assos Assos
  // Generate the list of addresses that this e-mail will be sent to.
359
  $addresses_final = array_filter($addresses, 'webform_valid_email_address');
360
361
  if (!$addresses_final) {
362
    return;
363
  }
364
365
  // Verify that this submission is not attempting to send any spam hacks.
366
  foreach ($addresses_final as $address) {
367
    if (_webform_submission_spam_check($address, $email['subject'], $email['from'], $email['headers'])) {
368
      watchdog('webform', 'Possible spam attempt from @remote !message',
369
        array('@remote' => ip_address(), '!message' => "<br />\n" . nl2br(htmlentities($email['message']))));
370
      drupal_set_message(t('Illegal information. Data not submitted.'), 'error');
371
      return;
372 a45e4bc1 Assos Assos
    }
373 76bdcd04 Assos Assos
  }
374 a45e4bc1 Assos Assos
375 76bdcd04 Assos Assos
  // Consolidate addressees into one message if permitted by configuration.
376
  $send_increment = 1;
377
  if (!webform_variable_get('webform_email_address_individual')) {
378
    $send_increment = count($addresses_final);
379
    $addresses_final = array(implode(', ', $addresses_final));
380
  }
381 85ad3d82 Assos Assos
382 76bdcd04 Assos Assos
  // Prepare the variables used by drupal_mail().
383
  $language = $user->uid ? user_preferred_language($user) : language_default();
384
  $mail_params = array(
385
    'message' => $email['message'],
386
    'subject' => $email['subject'],
387
    'headers' => $email['headers'],
388
    'node' => $node,
389
    'submission' => $submission,
390
    'email' => $email,
391
  );
392 85ad3d82 Assos Assos
393 76bdcd04 Assos Assos
  if (webform_variable_get('webform_email_html_capable')) {
394
    // Load attachments for the e-mail.
395
    $attachments = array();
396
    if ($email['attachments']) {
397
      webform_component_include('file');
398
      foreach ($node->webform['components'] as $component) {
399
        if (webform_component_feature($component['type'], 'attachment') && !empty($submission->data[$component['cid']][0])) {
400
          if (webform_component_implements($component['type'], 'attachments')) {
401
            $files = webform_component_invoke($component['type'], 'attachments', $component, $submission->data[$component['cid']]);
402
            if ($files) {
403
              $attachments = array_merge($attachments, $files);
404 85ad3d82 Assos Assos
            }
405
          }
406
        }
407
      }
408 76bdcd04 Assos Assos
    }
409 85ad3d82 Assos Assos
410 76bdcd04 Assos Assos
    // Add the attachments to the mail parameters.
411
    $mail_params['attachments'] = $attachments;
412
413
    // Set all other properties for HTML e-mail handling.
414
    $mail_params['plain'] = !$email['html'];
415
    $mail_params['plaintext'] = $email['html'] ? NULL : $email['message'];
416
    $mail_params['headers'] = $email['headers'];
417
    if ($email['html']) {
418
      // MIME Mail requires this header or it will filter all text.
419
      $mail_params['headers']['Content-Type'] = 'text/html; charset=UTF-8';
420 85ad3d82 Assos Assos
    }
421
  }
422
423 76bdcd04 Assos Assos
  return array(
424
    'addresses_final' => $addresses_final,
425
    'send_increment' => $send_increment,
426
    'language' => $language,
427
    'mail_params' => $mail_params,
428
  );
429 85ad3d82 Assos Assos
}
430
431
/**
432
 * Confirm form to delete a single form submission.
433
 *
434
 * @param $form
435
 *   The new form array.
436
 * @param $form_state
437
 *   The current form state.
438
 * @param $node
439
 *   The node for which this webform was submitted.
440
 * @param $submission
441
 *   The submission to be deleted (from webform_submitted_data).
442
 */
443
function webform_submission_delete_form($form, $form_state, $node, $submission) {
444
  webform_set_breadcrumb($node, $submission);
445
446
  // Set the correct page title.
447
  drupal_set_title(webform_submission_title($node, $submission));
448
449
  // Keep the NID and SID in the same location as the webform_client_form().
450
  $form['#tree'] = TRUE;
451
  $form['details']['nid'] = array(
452
    '#type' => 'value',
453
    '#value' => $node->nid,
454
  );
455
  $form['details']['sid'] = array(
456
    '#type' => 'value',
457
    '#value' => $submission->sid,
458
  );
459
460
  $question = t('Are you sure you want to delete this submission?');
461
462 a45e4bc1 Assos Assos
  return confirm_form($form, NULL, "node/{$node->nid}/submission/{$submission->sid}", $question, t('Delete'), t('Cancel'));
463 85ad3d82 Assos Assos
}
464
465 feca1e4a Assos Assos
/**
466 76bdcd04 Assos Assos
 * Form submit handler.
467 feca1e4a Assos Assos
 */
468 85ad3d82 Assos Assos
function webform_submission_delete_form_submit($form, &$form_state) {
469
  $node = node_load($form_state['values']['details']['nid']);
470
  $submission = webform_get_submission($form_state['values']['details']['nid'], $form_state['values']['details']['sid']);
471
  webform_submission_delete($node, $submission);
472
  drupal_set_message(t('Submission deleted.'));
473
474 8c72e82a Assos Assos
  // If no destination query was supplied in the URL (for example, Edit tab),
475
  // redirect to the most-privledged destination.
476 a45e4bc1 Assos Assos
  $form_state['redirect'] = 'node/' . $node->nid .
477
                            (webform_results_access($node) ? '/webform-results' : '/submissions');
478 85ad3d82 Assos Assos
}
479
480
/**
481
 * Menu title callback; Return the submission number as a title.
482
 */
483
function webform_submission_title($node, $submission) {
484 a45e4bc1 Assos Assos
  return t('Submission #@serial', array('@serial' => $submission->serial));
485 85ad3d82 Assos Assos
}
486
487
/**
488
 * Menu callback; Present a Webform submission page for display or editing.
489
 */
490
function webform_submission_page($node, $submission, $format) {
491
  global $user;
492
493
  // Render the admin UI breadcrumb.
494
  webform_set_breadcrumb($node, $submission);
495
496
  // Set the correct page title.
497
  drupal_set_title(webform_submission_title($node, $submission));
498
499
  if ($format == 'form') {
500
    $output = drupal_get_form('webform_client_form_' . $node->nid, $node, $submission);
501
  }
502
  else {
503 a45e4bc1 Assos Assos
    $renderable = webform_submission_render($node, $submission, NULL, $format);
504
    $renderable['#attached']['css'][] = drupal_get_path('module', 'webform') . '/css/webform.css';
505
    $output = drupal_render($renderable);
506 85ad3d82 Assos Assos
  }
507
508
  // Determine the mode in which we're displaying this submission.
509
  $mode = ($format != 'form') ? 'display' : 'form';
510
  if (strpos(request_uri(), 'print/') !== FALSE) {
511
    $mode = 'print';
512
  }
513
  if (strpos(request_uri(), 'printpdf/') !== FALSE) {
514
    $mode = 'pdf';
515
  }
516
517
  // Add navigation for administrators.
518
  if (webform_results_access($node)) {
519
    $navigation = theme('webform_submission_navigation', array('node' => $node, 'submission' => $submission, 'mode' => $mode));
520
    $information = theme('webform_submission_information', array('node' => $node, 'submission' => $submission, 'mode' => $mode));
521
  }
522
  else {
523
    $navigation = NULL;
524
    $information = NULL;
525
  }
526
527
  // Actions may be shown to all users.
528
  $actions = theme('links', array('links' => module_invoke_all('webform_submission_actions', $node, $submission), 'attributes' => array('class' => array('links', 'inline', 'webform-submission-actions'))));
529
530
  // Disable the page cache for anonymous users viewing or editing submissions.
531
  if (!$user->uid) {
532
    webform_disable_page_cache();
533
  }
534
535
  $page = array(
536
    '#theme' => 'webform_submission_page',
537
    '#node' => $node,
538
    '#mode' => $mode,
539
    '#submission' => $submission,
540
    '#submission_content' => $output,
541
    '#submission_navigation' => $navigation,
542
    '#submission_information' => $information,
543
    '#submission_actions' => $actions,
544
  );
545
  $page['#attached']['library'][] = array('webform', 'admin');
546
  return $page;
547
}
548
549
/**
550
 * Form to resend specific e-mails associated with a submission.
551
 */
552
function webform_submission_resend($form, $form_state, $node, $submission) {
553
  // Render the admin UI breadcrumb.
554
  webform_set_breadcrumb($node, $submission);
555
556
  $form['#tree'] = TRUE;
557
  $form['#node'] = $node;
558
  $form['#submission'] = $submission;
559
560
  foreach ($node->webform['emails'] as $eid => $email) {
561 a45e4bc1 Assos Assos
562 feca1e4a Assos Assos
    $mapping = isset($email['extra']['email_mapping']) ? $email['extra']['email_mapping'] : NULL;
563 a45e4bc1 Assos Assos
    $addresses = webform_format_email_address($email['email'], NULL, $node, $submission, FALSE, FALSE, NULL, $mapping);
564
    $addresses_valid = array_map('webform_valid_email_address', $addresses);
565
    $valid_email = count($addresses) == array_sum($addresses_valid);
566 e9984459 Assos Assos
567 85ad3d82 Assos Assos
    $form['resend'][$eid] = array(
568
      '#type' => 'checkbox',
569 a45e4bc1 Assos Assos
      '#default_value' => $valid_email && $email['status'],
570
      '#disabled' => !$valid_email,
571 85ad3d82 Assos Assos
    );
572
    $form['emails'][$eid]['email'] = array(
573 a45e4bc1 Assos Assos
      '#markup' => nl2br(check_plain(implode("\n", $addresses))),
574 85ad3d82 Assos Assos
    );
575
    if (!$valid_email) {
576 a45e4bc1 Assos Assos
      $form['emails'][$eid]['email']['#markup'] .= ' (' . t('empty or invalid') . ')';
577 85ad3d82 Assos Assos
    }
578
    $form['emails'][$eid]['subject'] = array(
579
      '#markup' => check_plain(webform_format_email_subject($email['subject'], $node, $submission)),
580
    );
581
582
    $form['actions'] = array('#type' => 'actions');
583 ca0757b9 Assos Assos
    $form['actions']['submit'] = array(
584 85ad3d82 Assos Assos
      '#type' => 'submit',
585
      '#value' => t('Resend e-mails'),
586
    );
587
    $form['actions']['cancel'] = array(
588
      '#type' => 'markup',
589
      '#markup' => l(t('Cancel'), isset($_GET['destination']) ? $_GET['destination'] : 'node/' . $node->nid . '/submission/' . $submission->sid),
590
    );
591
  }
592
  return $form;
593
}
594
595
/**
596
 * Validate handler for webform_submission_resend().
597
 */
598
function webform_submission_resend_validate($form, &$form_state) {
599
  if (count(array_filter($form_state['values']['resend'])) == 0) {
600
    form_set_error('emails', t('You must select at least one email address to resend submission.'));
601
  }
602
}
603
604
/**
605
 * Submit handler for webform_submission_resend().
606
 */
607
function webform_submission_resend_submit($form, &$form_state) {
608
  $node = $form['#node'];
609
  $submission = $form['#submission'];
610
611
  $emails = array();
612
  foreach ($form_state['values']['resend'] as $eid => $checked) {
613
    if ($checked) {
614
      $emails[] = $form['#node']->webform['emails'][$eid];
615
    }
616
  }
617
  $sent_count = webform_submission_send_mail($node, $submission, $emails);
618
  if ($sent_count) {
619
    drupal_set_message(format_plural($sent_count,
620
      'Successfully re-sent submission #@sid to 1 recipient.',
621
      'Successfully re-sent submission #@sid to @count recipients.',
622
      array('@sid' => $submission->sid)
623
    ));
624
  }
625
  else {
626
    drupal_set_message(t('No e-mails were able to be sent due to a server error.'), 'error');
627
  }
628
}
629
630
/**
631
 * Theme the node components form. Use a table to organize the components.
632
 *
633 8c72e82a Assos Assos
 * @param array $variables
634
 *   Array with key "form" containing the form array.
635
 *
636
 * @return string
637 85ad3d82 Assos Assos
 *   Formatted HTML form, ready for display.
638 8c72e82a Assos Assos
 *
639
 * @throws Exception
640 85ad3d82 Assos Assos
 */
641 01f36513 Assos Assos
function theme_webform_submission_resend(array $variables) {
642 85ad3d82 Assos Assos
  $form = $variables['form'];
643
644 a45e4bc1 Assos Assos
  $header = array(t('Send'), t('E-mail to'), t('Subject'));
645 85ad3d82 Assos Assos
  $rows = array();
646
  if (!empty($form['emails'])) {
647
    foreach (element_children($form['emails']) as $eid) {
648
      // Add each component to a table row.
649
      $rows[] = array(
650
        drupal_render($form['resend'][$eid]),
651
        drupal_render($form['emails'][$eid]['email']),
652
        drupal_render($form['emails'][$eid]['subject']),
653
      );
654
    }
655
  }
656
  else {
657
    $rows[] = array(array('data' => t('This webform is currently not setup to send emails.'), 'colspan' => 3));
658
  }
659
  $output = '';
660 a45e4bc1 Assos Assos
  $output .= theme('table', array('header' => $header, 'rows' => $rows, 'sticky' => FALSE, 'attributes' => array('id' => 'webform-emails')));
661 85ad3d82 Assos Assos
  $output .= drupal_render_children($form);
662
  return $output;
663
}
664
665
/**
666
 * Print a Webform submission for display on a page or in an e-mail.
667
 */
668 a45e4bc1 Assos Assos
function webform_submission_render($node, $submission, $email, $format, $excluded_components = NULL) {
669 85ad3d82 Assos Assos
  $component_tree = array();
670
  $renderable = array();
671
  $page_count = 1;
672
673
  // Meta data that may be useful for modules implementing
674
  // hook_webform_submission_render_alter().
675
  $renderable['#node'] = $node;
676
  $renderable['#submission'] = $submission;
677
  $renderable['#email'] = $email;
678
  $renderable['#format'] = $format;
679
680
  // Set the theme function for submissions.
681
  $renderable['#theme'] = array('webform_submission_' . $node->nid, 'webform_submission');
682
683
  $components = $node->webform['components'];
684 e9984459 Assos Assos
685 a45e4bc1 Assos Assos
  // Remove excluded components.
686
  if (is_array($excluded_components)) {
687
    foreach ($excluded_components as $cid) {
688
      unset($components[$cid]);
689
    }
690 e9984459 Assos Assos
    if (!empty($email['exclude_empty'])) {
691 a45e4bc1 Assos Assos
      foreach ($submission->data as $cid => $data) {
692 ba09eb79 Assos Assos
        // Caution. Grids store their data in an array index by question key.
693 e9984459 Assos Assos
        if (implode($data) == '') {
694
          unset($components[$cid]);
695 a45e4bc1 Assos Assos
        }
696
      }
697
    }
698 85ad3d82 Assos Assos
  }
699
700 a45e4bc1 Assos Assos
  module_load_include('inc', 'webform', 'includes/webform.components');
701 85ad3d82 Assos Assos
  _webform_components_tree_build($components, $component_tree, 0, $page_count);
702
703 feca1e4a Assos Assos
  // Make sure at least one field is available.
704 85ad3d82 Assos Assos
  if (isset($component_tree['children'])) {
705
    // Recursively add components to the form.
706 a45e4bc1 Assos Assos
    $sorter = webform_get_conditional_sorter($node);
707
    $input_values = $sorter->executeConditionals($submission->data);
708 85ad3d82 Assos Assos
    foreach ($component_tree['children'] as $cid => $component) {
709 a45e4bc1 Assos Assos
      if ($sorter->componentVisibility($cid, $component['page_num']) == webformConditionals::componentShown) {
710
        _webform_client_form_add_component($node, $component, NULL, $renderable, $renderable, $input_values, $format);
711 85ad3d82 Assos Assos
      }
712
    }
713
  }
714
715
  drupal_alter('webform_submission_render', $renderable);
716 a45e4bc1 Assos Assos
  return $renderable;
717 85ad3d82 Assos Assos
}
718
719
/**
720
 * Return all the submissions for a particular node.
721
 *
722
 * @param $filters
723
 *   An array of filters to apply to this query. Usually in the format
724
 *   array('nid' => $nid, 'uid' => $uid). A single integer may also be passed
725
 *   in, which will be equivalent to specifying a $nid filter.
726
 * @param $header
727
 *   If the results of this fetch will be used in a sortable
728
 *   table, pass the array header of the table.
729
 * @param $pager_count
730
 *   Optional. The number of submissions to include in the results.
731 8c72e82a Assos Assos
 *
732
 * @return array
733
 *   Array of submission data for a particular node.
734 85ad3d82 Assos Assos
 */
735
function webform_get_submissions($filters = array(), $header = NULL, $pager_count = 0) {
736 a45e4bc1 Assos Assos
  return webform_get_submissions_load(webform_get_submissions_query($filters, $header, $pager_count));
737
}
738 85ad3d82 Assos Assos
739 a45e4bc1 Assos Assos
/**
740
 * Returns an unexecuted webform_submissions query on for the arguments.
741
 *
742
 * This is an internal routine and not intended for use by other modules.
743
 *
744
 * @param $filters
745
 *   An array of filters to apply to this query. Usually in the format
746
 *   array('nid' => $nid, 'uid' => $uid). A single integer may also be passed
747
 *   in, which will be equivalent to specifying a $nid filter. 'sid' may also
748
 *   be included, either as a single sid or an array of sid's.
749
 * @param $header
750
 *   If the results of this fetch will be used in a sortable
751
 *   table, pass the array header of the table.
752
 * @param $pager_count
753
 *   Optional. The number of submissions to include in the results.
754 8c72e82a Assos Assos
 *
755
 * @return QueryExtendableInterface|SelectQueryInterface
756 01f36513 Assos Assos
 *   The query object.
757 a45e4bc1 Assos Assos
 */
758
function webform_get_submissions_query($filters = array(), $header = NULL, $pager_count = 0) {
759 85ad3d82 Assos Assos
  if (!is_array($filters)) {
760 a45e4bc1 Assos Assos
    $filters = array('ws.nid' => $filters);
761
  }
762
763
  // Qualify all filters with a table alias. ws.* is assumed, except for u.uid.
764
  foreach ($filters as $column => $value) {
765
    if (strpos($column, '.') === FALSE) {
766
      $filters[($column == 'uid' ? 'u.' : 'ws.') . $column] = $value;
767
      unset($filters[$column]);
768
    }
769 85ad3d82 Assos Assos
  }
770
771 a45e4bc1 Assos Assos
  // If the sid is specified, but there are none, force the query to fail
772 feca1e4a Assos Assos
  // rather than query on an empty array.
773 a45e4bc1 Assos Assos
  if (isset($filters['ws.sid']) && empty($filters['ws.sid'])) {
774
    $filters['ws.sid'] = 0;
775 85ad3d82 Assos Assos
  }
776
777 a45e4bc1 Assos Assos
  // Build the list of submissions and load their basic information.
778
  $pager_query = db_select('webform_submissions', 'ws')
779
    // Ensure only one row per submission is returned. Could be more than one if
780
    // sorting on a column that uses multiple rows for its data.
781
    ->distinct()
782
    ->addTag('webform_get_submissions_sids')
783
    ->fields('ws');
784
785
  // Add each filter.
786
  foreach ($filters as $column => $value) {
787
    $pager_query->condition($column, $value);
788 85ad3d82 Assos Assos
  }
789
790 a45e4bc1 Assos Assos
  // Join to the users table to include user name in results.
791
  $pager_query->leftJoin('users', 'u', 'u.uid = ws.uid');
792
  $pager_query->fields('u', array('name'));
793
  if (isset($filters['u.uid']) && $filters['u.uid'] === 0) {
794
    if (!empty($_SESSION['webform_submission'])) {
795
      $anonymous_sids = array_keys($_SESSION['webform_submission']);
796
      $pager_query->condition('ws.sid', $anonymous_sids, 'IN');
797 85ad3d82 Assos Assos
    }
798 a45e4bc1 Assos Assos
    else {
799
      $pager_query->condition('ws.sid', 0);
800
    }
801
  }
802 85ad3d82 Assos Assos
803 a45e4bc1 Assos Assos
  if (is_array($header)) {
804
    $metadata_columns = array();
805
    foreach ($header as $header_item) {
806
      $metadata_columns[] = $header_item['data'];
807
    }
808
    $sort = drupal_get_query_parameters();
809
    // Sort by submitted data column if order is set but not in
810
    // $metadata_columns.
811
    if (isset($sort['order']) && !in_array($sort['order'], $metadata_columns, TRUE)) {
812
      // Default if sort is unset or invalid.
813
      if (!isset($sort['sort']) || !in_array($sort['sort'], array('asc', 'desc'), TRUE)) {
814
        $sort['sort'] = '';
815 85ad3d82 Assos Assos
      }
816 a45e4bc1 Assos Assos
      $pager_query->leftJoin('webform_component', 'wc', 'ws.nid = wc.nid AND wc.name = :form_key', array('form_key' => $sort['order']));
817
      $pager_query->leftJoin('webform_submitted_data', 'wsd', 'wc.nid = wsd.nid AND ws.sid = wsd.sid AND wc.cid = wsd.cid');
818
      $pager_query->orderBy('wsd.data', $sort['sort']);
819
      $pager_query->orderBy('ws.sid', 'ASC');
820 85ad3d82 Assos Assos
    }
821 a45e4bc1 Assos Assos
    // Sort by metadata column.
822
    else {
823
      // Extending the query instantiates a new query object.
824 85ad3d82 Assos Assos
      $pager_query = $pager_query->extend('TableSort');
825
      $pager_query->orderByHeader($header);
826
    }
827 a45e4bc1 Assos Assos
  }
828
  else {
829
    $pager_query->orderBy('ws.sid', 'ASC');
830
  }
831 85ad3d82 Assos Assos
832 a45e4bc1 Assos Assos
  if ($pager_count) {
833
    // Extending the query instantiates a new query object.
834
    $pager_query = $pager_query->extend('PagerDefault');
835
    $pager_query->limit($pager_count);
836
  }
837
  return $pager_query;
838
}
839
840
/**
841
 * Retrieve and load the submissions for the specified submissions query.
842
 *
843
 * This is an internal routine and not intended for use by other modules.
844
 *
845
 * @params object $pager_query
846
 *   A select or extended select query containing the needed fields:
847
 *     webform_submissions: all fields
848
 *     user: name
849 feca1e4a Assos Assos
 *
850 a45e4bc1 Assos Assos
 * @return array
851
 *   An array of loaded webform submissions.
852
 */
853
function webform_get_submissions_load($pager_query) {
854
  // If the "$pager_query" is actually an unextended select query, then instead
855 feca1e4a Assos Assos
  // of querying the webform_submissions_data table with a potentially huge
856
  // array of sids in an IN clause, use the select query directly as this will
857 a45e4bc1 Assos Assos
  // be much faster. Extended queries don't work in join clauses. The query
858
  // is assumed to include the sid.
859
  if ($pager_query instanceof SelectQuery) {
860
    $submissions_query = clone $pager_query;
861
  }
862 85ad3d82 Assos Assos
863 feca1e4a Assos Assos
  // Extract any filter on node id to use in an optimization below.
864 a45e4bc1 Assos Assos
  foreach ($pager_query->conditions() as $index => $condition) {
865
    if ($index !== '#conjunction' && $condition['operator'] === '=' && ($condition['field'] === 'nid' || $condition['field'] === 'ws.nid')) {
866
      $nid = $condition['value'];
867
      break;
868 85ad3d82 Assos Assos
    }
869
  }
870
871 a45e4bc1 Assos Assos
  $result = $pager_query->execute();
872
  $submissions = $result->fetchAllAssoc('sid');
873
874 85ad3d82 Assos Assos
  // If there are no submissions being retrieved, return an empty array.
875 a45e4bc1 Assos Assos
  if (!$submissions) {
876 85ad3d82 Assos Assos
    return $submissions;
877
  }
878
879 a45e4bc1 Assos Assos
  foreach ($submissions as $sid => $submission) {
880
    $submissions[$sid]->preview = FALSE;
881
    $submissions[$sid]->data = array();
882
  }
883
884 85ad3d82 Assos Assos
  // Query the required submission data.
885
  $query = db_select('webform_submitted_data', 'sd');
886
  $query
887 a45e4bc1 Assos Assos
    ->addTag('webform_get_submissions_data')
888
    ->fields('sd', array('sid', 'cid', 'no', 'data'))
889 85ad3d82 Assos Assos
    ->orderBy('sd.sid', 'ASC')
890
    ->orderBy('sd.cid', 'ASC')
891
    ->orderBy('sd.no', 'ASC');
892
893 a45e4bc1 Assos Assos
  if (isset($submissions_query)) {
894
    // If available, prefer joining on the subquery as it is much faster than an
895
    // IN clause on a large array. A subquery with the IN operator doesn't work
896
    // when the subquery has a LIMIT clause, requiring an inner join instead.
897
    $query->innerJoin($submissions_query, 'ws2', 'sd.sid = ws2.sid');
898
  }
899
  else {
900 feca1e4a Assos Assos
    $query->condition('sd.sid', array_keys($submissions), 'IN');
901 a45e4bc1 Assos Assos
  }
902
903 85ad3d82 Assos Assos
  // By adding the NID to this query we allow MySQL to use the primary key on
904
  // in webform_submitted_data for sorting (nid_sid_cid_no).
905 a45e4bc1 Assos Assos
  if (isset($nid)) {
906
    $query->condition('sd.nid', $nid);
907 85ad3d82 Assos Assos
  }
908
909
  $result = $query->execute();
910
911 a45e4bc1 Assos Assos
  // Convert the queried rows into submission data.
912 85ad3d82 Assos Assos
  foreach ($result as $row) {
913 a45e4bc1 Assos Assos
    $submissions[$row->sid]->data[$row->cid][$row->no] = $row->data;
914 85ad3d82 Assos Assos
  }
915
916
  foreach (module_implements('webform_submission_load') as $module) {
917
    $function = $module . '_webform_submission_load';
918
    $function($submissions);
919
  }
920
921
  return $submissions;
922
}
923
924
/**
925
 * Return a count of the total number of submissions for a node.
926
 *
927
 * @param $nid
928
 *   The node ID for which submissions are being fetched.
929
 * @param $uid
930
 *   Optional; the user ID to filter the submissions by.
931 a45e4bc1 Assos Assos
 * @param $is_draft
932
 *   Optional; NULL for all, truthy for drafts only, falsy for completed only.
933
 *   The default is completed submissions only.
934 feca1e4a Assos Assos
 *
935 01f36513 Assos Assos
 * @return int
936
 *   The number of submissions.
937 85ad3d82 Assos Assos
 */
938 a45e4bc1 Assos Assos
function webform_get_submission_count($nid, $uid = NULL, $is_draft = 0) {
939
  $counts = &drupal_static(__FUNCTION__);
940 85ad3d82 Assos Assos
941 a45e4bc1 Assos Assos
  if (!isset($counts[$nid][$uid])) {
942 85ad3d82 Assos Assos
    $query = db_select('webform_submissions', 'ws')
943
      ->addTag('webform_get_submission_count')
944 a45e4bc1 Assos Assos
      ->condition('ws.nid', $nid);
945 85ad3d82 Assos Assos
    if ($uid !== NULL) {
946
      $query->condition('ws.uid', $uid);
947
    }
948
    if ($uid === 0) {
949
      $submissions = isset($_SESSION['webform_submission']) ? $_SESSION['webform_submission'] : NULL;
950
      if ($submissions) {
951
        $query->condition('ws.sid', $submissions, 'IN');
952
      }
953
      else {
954
        // Intentionally never match anything if the anonymous user has no
955
        // submissions.
956
        $query->condition('ws.sid', 0);
957
      }
958
    }
959 a45e4bc1 Assos Assos
    if (isset($is_draft)) {
960
      $query->condition('ws.is_draft', $is_draft);
961
    }
962 85ad3d82 Assos Assos
963
    $counts[$nid][$uid] = $query->countQuery()->execute()->fetchField();
964
  }
965
  return $counts[$nid][$uid];
966
}
967
968
/**
969
 * Fetch a specified submission for a webform node.
970
 */
971 a45e4bc1 Assos Assos
function webform_get_submission($nid, $sid) {
972
  $submissions = &drupal_static(__FUNCTION__, array());
973 85ad3d82 Assos Assos
974
  // Load the submission if needed.
975
  if (!isset($submissions[$sid])) {
976
    $new_submissions = webform_get_submissions(array('nid' => $nid, 'sid' => $sid));
977
    $submissions[$sid] = isset($new_submissions[$sid]) ? $new_submissions[$sid] : FALSE;
978
  }
979
980
  return $submissions[$sid];
981
}
982
983 feca1e4a Assos Assos
/**
984 76bdcd04 Assos Assos
 * Verify that an email is not attempting to send any spam.
985 feca1e4a Assos Assos
 */
986 85ad3d82 Assos Assos
function _webform_submission_spam_check($to, $subject, $from, $headers = array()) {
987 feca1e4a Assos Assos
  $headers = implode('\n', (array) $headers);
988 85ad3d82 Assos Assos
  // Check if they are attempting to spam using a bcc or content type hack.
989
  if (preg_match('/(b?cc\s?:)|(content\-type:)/i', $to . "\n" . $subject . "\n" . $from . "\n" . $headers)) {
990 feca1e4a Assos Assos
    // Possible spam attempt.
991
    return TRUE;
992 85ad3d82 Assos Assos
  }
993 feca1e4a Assos Assos
  // Not spam.
994
  return FALSE;
995 85ad3d82 Assos Assos
}
996
997
/**
998
 * Check if the current user has exceeded the limit on this form.
999
 *
1000
 * @param $node
1001
 *   The webform node to be checked.
1002 a45e4bc1 Assos Assos
 * @param $account
1003
 *   Optional parameter. Specify the account you want to check the limit
1004
 *   against.
1005
 *
1006 8c72e82a Assos Assos
 * @return bool
1007 85ad3d82 Assos Assos
 *   Boolean TRUE if the user has exceeded their limit. FALSE otherwise.
1008
 */
1009 a45e4bc1 Assos Assos
function webform_submission_user_limit_check($node, $account = NULL) {
1010 85ad3d82 Assos Assos
  global $user;
1011 a45e4bc1 Assos Assos
  $tracking_mode = webform_variable_get('webform_tracking_mode');
1012
1013
  if (!isset($account)) {
1014
    $account = $user;
1015
  }
1016
1017
  // We can only check access for anonymous users through their cookies.
1018
  if ($user->uid !== 0 && $account->uid === 0) {
1019
    watchdog('webform', 'Unable to check anonymous user submission limit when logged in as user @uid.', array('@uid' => $user->uid), WATCHDOG_WARNING);
1020
    return FALSE;
1021
  }
1022 85ad3d82 Assos Assos
1023
  // Check if submission limiting is enabled.
1024
  if ($node->webform['submit_limit'] == '-1') {
1025 feca1e4a Assos Assos
    // No check enabled.
1026
    return FALSE;
1027 85ad3d82 Assos Assos
  }
1028
1029 a45e4bc1 Assos Assos
  // Fetch all the entries from the database within the submit interval with
1030
  // this username and IP.
1031
  $num_submissions_database = 0;
1032
  if (!$node->webform['confidential'] &&
1033
      ($account->uid !== 0 || $tracking_mode === 'ip_address' || $tracking_mode === 'strict')) {
1034
    $query = db_select('webform_submissions')
1035
      ->addTag('webform_submission_user_limit_check')
1036
      ->condition('nid', $node->nid)
1037
      ->condition('is_draft', 0);
1038
1039
    if ($node->webform['submit_interval'] != -1) {
1040
      $query->condition('submitted', REQUEST_TIME - $node->webform['submit_interval'], '>');
1041
    }
1042 85ad3d82 Assos Assos
1043 a45e4bc1 Assos Assos
    if ($account->uid) {
1044
      $query->condition('uid', $account->uid);
1045
    }
1046
    else {
1047
      $query->condition('remote_addr', ip_address());
1048
    }
1049
    $num_submissions_database = $query->countQuery()->execute()->fetchField();
1050 85ad3d82 Assos Assos
  }
1051
1052
  // Double check the submission history from the users machine using cookies.
1053
  $num_submissions_cookie = 0;
1054 a45e4bc1 Assos Assos
  if ($account->uid === 0 && ($tracking_mode === 'cookie' || $tracking_mode === 'strict')) {
1055 85ad3d82 Assos Assos
    $cookie_name = 'webform-' . $node->nid;
1056
1057
    if (isset($_COOKIE[$cookie_name]) && is_array($_COOKIE[$cookie_name])) {
1058
      foreach ($_COOKIE[$cookie_name] as $key => $timestamp) {
1059
        if ($node->webform['submit_interval'] != -1 && $timestamp <= REQUEST_TIME - $node->webform['submit_interval']) {
1060
          // Remove the cookie if past the required time interval.
1061
          $params = session_get_cookie_params();
1062
          setcookie($cookie_name . '[' . $key . ']', '', 0, $params['path'], $params['domain'], $params['secure'], $params['httponly']);
1063
        }
1064
      }
1065
      // Count the number of submissions recorded in cookies.
1066
      $num_submissions_cookie = count($_COOKIE[$cookie_name]);
1067
    }
1068
  }
1069
1070
  if ($num_submissions_database >= $node->webform['submit_limit'] || $num_submissions_cookie >= $node->webform['submit_limit']) {
1071
    // Limit exceeded.
1072
    return TRUE;
1073
  }
1074
1075
  // Limit not exceeded.
1076
  return FALSE;
1077
}
1078
1079
/**
1080
 * Check if the total number of submissions has exceeded the limit on this form.
1081
 *
1082
 * @param $node
1083
 *   The webform node to be checked.
1084 8c72e82a Assos Assos
 *
1085
 * @return bool
1086 85ad3d82 Assos Assos
 *   Boolean TRUE if the form has exceeded it's limit. FALSE otherwise.
1087
 */
1088 a45e4bc1 Assos Assos
function webform_submission_total_limit_check($node) {
1089 85ad3d82 Assos Assos
1090
  // Check if submission limiting is enabled.
1091
  if ($node->webform['total_submit_limit'] == '-1') {
1092 feca1e4a Assos Assos
    // No check enabled.
1093
    return FALSE;
1094 85ad3d82 Assos Assos
  }
1095
1096
  // Retrieve submission data from the database.
1097
  $query = db_select('webform_submissions')
1098 3753f249 Assos Assos
    ->addTag('webform_submission_total_limit_check')
1099 85ad3d82 Assos Assos
    ->condition('nid', $node->nid)
1100
    ->condition('is_draft', 0);
1101
1102
  if ($node->webform['total_submit_interval'] != -1) {
1103
    $query->condition('submitted', REQUEST_TIME - $node->webform['total_submit_interval'], '>');
1104
  }
1105
1106
  // Fetch all the entries from the database within the submit interval.
1107
  $num_submissions_database = $query->countQuery()->execute()->fetchField();
1108
1109
  if ($num_submissions_database >= $node->webform['total_submit_limit']) {
1110
    // Limit exceeded.
1111
    return TRUE;
1112
  }
1113
1114
  // Limit not exceeded.
1115
  return FALSE;
1116
}
1117
1118
/**
1119
 * Preprocess function for webform-submission.tpl.php.
1120
 */
1121
function template_preprocess_webform_submission(&$vars) {
1122
  $vars['node'] = $vars['renderable']['#node'];
1123
  $vars['submission'] = $vars['renderable']['#submission'];
1124
  $vars['email'] = $vars['renderable']['#email'];
1125
  $vars['format'] = $vars['renderable']['#format'];
1126
}
1127
1128
/**
1129
 * Preprocess function for webform-submission-navigation.tpl.php.
1130
 */
1131
function template_preprocess_webform_submission_navigation(&$vars) {
1132
  $start_path = ($vars['mode'] == 'print') ? 'print/' : 'node/';
1133
1134
  $previous_query = db_select('webform_submissions')
1135
    ->condition('nid', $vars['node']->nid)
1136
    ->condition('sid', $vars['submission']->sid, '<');
1137
  $previous_query->addExpression('MAX(sid)');
1138
1139
  $next_query = db_select('webform_submissions')
1140
    ->condition('nid', $vars['node']->nid)
1141
    ->condition('sid', $vars['submission']->sid, '>');
1142
  $next_query->addExpression('MIN(sid)');
1143
1144
  $vars['previous'] = $previous_query->execute()->fetchField();
1145
  $vars['next'] = $next_query->execute()->fetchField();
1146
  $vars['previous_url'] = $start_path . $vars['node']->nid . '/submission/' . $vars['previous'] . ($vars['mode'] == 'form' ? '/edit' : '');
1147
  $vars['next_url'] = $start_path . $vars['node']->nid . '/submission/' . $vars['next'] . ($vars['mode'] == 'form' ? '/edit' : '');
1148
}
1149
1150
/**
1151
 * Preprocess function for webform-submission-navigation.tpl.php.
1152
 */
1153
function template_preprocess_webform_submission_information(&$vars) {
1154
  $vars['account'] = user_load($vars['submission']->uid);
1155
  $vars['actions'] = theme('links', module_invoke_all('webform_submission_actions', $vars['node'], $vars['submission']));
1156
}