Project

General

Profile

Paste
Download (28.4 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / webform / webform.install @ 4cfd8be6

1
<?php
2

    
3
/**
4
 * @file
5
 *   Webform module install/schema hooks.
6
 */
7

    
8
/**
9
 * Implements hook_schema().
10
 */
11
function webform_schema() {
12
  $schema = array();
13

    
14
  $schema['webform'] = array(
15
    'description' => 'Table for storing additional properties for webform nodes.',
16
    'fields' => array(
17
      'nid' => array(
18
        'description' => 'The node identifier of a webform.',
19
        'type' => 'int',
20
        'unsigned' => TRUE,
21
        'not null' => TRUE,
22
      ),
23
      'confirmation' => array(
24
        'description' => 'The confirmation message or URL displayed to the user after submitting a form.',
25
        'type' => 'text',
26
        'not null' => TRUE,
27
      ),
28
      'confirmation_format' => array(
29
        'description' => 'The {filter_format}.format of the confirmation message.',
30
        'type' => 'varchar',
31
        'length' => 255,
32
        'not null' => FALSE,
33
      ),
34
      'redirect_url' => array(
35
        'description' => 'The URL a user is redirected to after submitting a form.',
36
        'type' => 'varchar',
37
        'length' => 255,
38
        'default' => '<confirmation>',
39
      ),
40
      'status' => array(
41
        'description' => 'Boolean value of a webform for open (1) or closed (0).',
42
        'type' => 'int',
43
        'size' => 'tiny',
44
        'not null' => TRUE,
45
        'default' => 1,
46
      ),
47
      'block' => array(
48
         'description' => 'Boolean value for whether this form be available as a block.',
49
         'type' => 'int',
50
         'size' => 'tiny',
51
         'not null' => TRUE,
52
         'default' => 0,
53
      ),
54
      'teaser' => array(
55
        'description' => 'Boolean value for whether the entire form should be displayed on the teaser.',
56
        'type' => 'int',
57
        'size' => 'tiny',
58
        'not null' => TRUE,
59
        'default' => 0,
60
      ),
61
      'allow_draft' => array(
62
         'description' => 'Boolean value for whether submissions to this form be saved as a draft.',
63
         'type' => 'int',
64
         'size' => 'tiny',
65
         'not null' => TRUE,
66
         'default' => 0,
67
      ),
68
      'auto_save' => array(
69
         'description' => 'Boolean value for whether submissions to this form should be auto-saved between pages.',
70
         'type' => 'int',
71
         'size' => 'tiny',
72
         'not null' => TRUE,
73
         'default' => 0,
74
      ),
75
      'submit_notice' => array(
76
        'description' => 'Boolean value for whether to show or hide the previous submissions notification.',
77
        'type' => 'int',
78
        'size' => 'tiny',
79
        'not null' => TRUE,
80
        'default' => 1,
81
      ),
82
      'submit_text' => array(
83
        'description' => 'The title of the submit button on the form.',
84
        'type' => 'varchar',
85
        'length' => 255,
86
      ),
87
      'submit_limit' => array(
88
        'description' => 'The number of submissions a single user is allowed to submit within an interval. -1 is unlimited.',
89
        'type' => 'int',
90
        'size' => 'tiny',
91
        'not null' => TRUE,
92
        'default' => -1,
93
      ),
94
      'submit_interval' => array(
95
        'description' => 'The amount of time in seconds that must pass before a user can submit another submission within the set limit.',
96
        'type' => 'int',
97
        'not null' => TRUE,
98
        'default' => -1,
99
      ),
100
      'total_submit_limit' => array(
101
        'description' => 'The total number of submissions allowed within an interval. -1 is unlimited.',
102
        'type' => 'int',
103
        'not null' => TRUE,
104
        'default' => -1,
105
      ),
106
      'total_submit_interval' => array(
107
        'description' => 'The amount of time in seconds that must pass before another submission can be submitted within the set limit.',
108
        'type' => 'int',
109
        'not null' => TRUE,
110
        'default' => -1,
111
      ),
112
    ),
113
    'primary key' => array('nid'),
114
  );
115

    
116
  $schema['webform_component'] = array(
117
    'description' => 'Stores information about components for webform nodes.',
118
    'fields' => array(
119
      'nid' => array(
120
        'description' => 'The node identifier of a webform.',
121
        'type' => 'int',
122
        'unsigned' => TRUE,
123
        'not null' => TRUE,
124
        'default' => 0,
125
      ),
126
      'cid' => array(
127
        'description' => 'The identifier for this component within this node, starts at 0 for each node.',
128
        'type' => 'int',
129
        'size' => 'small',
130
        'unsigned' => TRUE,
131
        'not null' => TRUE,
132
        'default' => 0,
133
      ),
134
      'pid' => array(
135
        'description' => 'If this component has a parent fieldset, the cid of that component.',
136
        'type' => 'int',
137
        'size' => 'small',
138
        'unsigned' => TRUE,
139
        'not null' => TRUE,
140
        'default' => 0,
141
      ),
142
      'form_key' => array(
143
        'description' => 'When the form is displayed and processed, this key can be used to reference the results.',
144
        'type' => 'varchar',
145
        'length' => 128,
146
      ),
147
      'name' => array(
148
        'description' => 'The label for this component.',
149
        'type' => 'varchar',
150
        'length' => 255,
151
      ),
152
      'type' => array(
153
        'description' => 'The field type of this component (textfield, select, hidden, etc.).',
154
        'type' => 'varchar',
155
        'length' => 16,
156
      ),
157
      'value' => array(
158
        'description' => 'The default value of the component when displayed to the end-user.',
159
        'type' => 'text',
160
        'not null' => TRUE,
161
      ),
162
      'extra' => array(
163
        'description' => 'Additional information unique to the display or processing of this component.',
164
        'type' => 'text',
165
        'not null' => TRUE,
166
      ),
167
      'mandatory' => array(
168
        'description' => 'Boolean flag for if this component is required.',
169
        'type' => 'int',
170
        'size' => 'tiny',
171
        'not null' => TRUE,
172
        'default' => 0,
173
      ),
174
      'weight' => array(
175
        'description' => 'Determines the position of this component in the form.',
176
        'type' => 'int',
177
        'size' => 'small',
178
        'not null' => TRUE,
179
        'default' => 0,
180
      ),
181
    ),
182
    'primary key' => array('nid', 'cid'),
183
  );
184

    
185
  $schema['webform_emails'] = array(
186
    'description' => 'Holds information regarding e-mails that should be sent upon submitting a webform',
187
    'fields' => array(
188
      'nid' => array(
189
        'description' => 'The node identifier of a webform.',
190
        'type' => 'int',
191
        'unsigned' => TRUE,
192
        'not null' => TRUE,
193
        'default' => 0,
194
      ),
195
      'eid' => array(
196
        'description' => 'The e-mail identifier for this row\'s settings.',
197
        'type' => 'int',
198
        'unsigned' => TRUE,
199
        'size' => 'small',
200
        'not null' => TRUE,
201
        'default' => 0,
202
      ),
203
      'email' => array(
204
        'description' => 'The e-mail address that will be sent to upon submission. This may be an e-mail address, the special key "default" or a numeric value. If a numeric value is used, the value of a component will be substituted on submission.',
205
        'type' => 'text',
206
        'not null' => FALSE,
207
      ),
208
      'subject' => array(
209
        'description' => 'The e-mail subject that will be used. This may be a string, the special key "default" or a numeric value. If a numeric value is used, the value of a component will be substituted on submission.',
210
        'type' => 'varchar',
211
        'length' => '255',
212
        'not null' => FALSE,
213
      ),
214
      'from_name' => array(
215
        'description' => 'The e-mail "from" name that will be used. This may be a string, the special key "default" or a numeric value. If a numeric value is used, the value of a component will be substituted on submission.',
216
        'type' => 'varchar',
217
        'length' => '255',
218
        'not null' => FALSE,
219
      ),
220
      'from_address' => array(
221
        'description' => 'The e-mail "from" e-mail address that will be used. This may be a string, the special key "default" or a numeric value. If a numeric value is used, the value of a component will be substituted on submission.',
222
        'type' => 'varchar',
223
        'length' => '255',
224
        'not null' => FALSE,
225
      ),
226
      'template' => array(
227
        'description' => 'A template that will be used for the sent e-mail. This may be a string or the special key "default", which will use the template provided by the theming layer.',
228
        'type' => 'text',
229
        'not null' => FALSE,
230
      ),
231
      'excluded_components' => array(
232
        'description' => 'A list of components that will not be included in the %email_values token. A list of CIDs separated by commas.',
233
        'type' => 'text',
234
        'not null' => TRUE,
235
      ),
236
      'html' => array(
237
        'description' => 'Determines if the e-mail will be sent in an HTML format. Requires Mime Mail module.',
238
        'type' => 'int',
239
        'unsigned' => TRUE,
240
        'size' => 'tiny',
241
        'not null' => TRUE,
242
        'default' => 0,
243
      ),
244
      'attachments' => array(
245
        'description' => 'Determines if the e-mail will include file attachments. Requires Mime Mail module.',
246
        'type' => 'int',
247
        'unsigned' => TRUE,
248
        'size' => 'tiny',
249
        'not null' => TRUE,
250
        'default' => 0,
251
      ),
252
    ),
253
    'primary key' => array('nid', 'eid'),
254
  );
255

    
256
  $schema['webform_roles'] = array(
257
    'description' => 'Holds access information regarding which roles are allowed to submit which webform nodes. Does not prevent access to the webform node entirely, use the {node_access} table for that purpose.',
258
    'fields' => array(
259
      'nid' => array(
260
        'description' => 'The node identifier of a webform.',
261
        'type' => 'int',
262
        'unsigned' => TRUE,
263
        'not null' => TRUE,
264
        'default' => 0,
265
      ),
266
      'rid' => array(
267
        'description' => 'The role identifier.',
268
        'type' => 'int',
269
        'unsigned' => TRUE,
270
        'not null' => TRUE,
271
        'default' => 0,
272
      ),
273
    ),
274
    'primary key' => array('nid', 'rid'),
275
  );
276

    
277
  $schema['webform_submissions'] = array(
278
    'description' => 'Holds general information about submissions outside of field values.',
279
    'fields' => array(
280
      'sid' => array(
281
        'description' => 'The unique identifier for this submission.',
282
        'type' => 'serial',
283
        'unsigned' => TRUE,
284
        'not null' => TRUE,
285
      ),
286
      'nid' => array(
287
        'description' => 'The node identifier of a webform.',
288
        'type' => 'int',
289
        'unsigned' => TRUE,
290
        'not null' => TRUE,
291
        'default' => 0,
292
      ),
293
      'uid' => array(
294
        'description' => 'The id of the user that completed this submission.',
295
        'type' => 'int',
296
        'unsigned' => TRUE,
297
        'not null' => TRUE,
298
        'default' => 0,
299
      ),
300
      'is_draft' => array(
301
         'description' => 'Is this a draft of the submission?',
302
         'type' => 'int',
303
         'size' => 'tiny',
304
         'not null' => TRUE,
305
         'default' => 0,
306
      ),
307
      'submitted' => array(
308
        'description' => 'Timestamp of when the form was submitted.',
309
        'type' => 'int',
310
        'not null' => TRUE,
311
        'default' => 0,
312
      ),
313
      'remote_addr' => array(
314
        'description' => 'The IP address of the user that submitted the form.',
315
        'type' => 'varchar',
316
        'length' => 128,
317
      ),
318
    ),
319
    'primary key' => array('sid'),
320
    'unique keys' => array(
321
      'sid_nid' => array('sid', 'nid'),
322
    ),
323
    'indexes' => array(
324
      'nid_uid_sid' => array('nid', 'uid', 'sid'),
325
      'nid_sid' => array('nid', 'sid'),
326
    ),
327
  );
328

    
329
  $schema['webform_submitted_data'] = array(
330
    'description' => 'Stores all submitted field data for webform submissions.',
331
    'fields' => array(
332
      'nid' => array(
333
        'description' => 'The node identifier of a webform.',
334
        'type' => 'int',
335
        'unsigned' => TRUE,
336
        'not null' => TRUE,
337
        'default' => 0,
338
      ),
339
      'sid' => array(
340
        'description' => 'The unique identifier for this submission.',
341
        'type' => 'int',
342
        'unsigned' => TRUE,
343
        'not null' => TRUE,
344
        'default' => 0,
345
      ),
346
      'cid' => array(
347
        'description' => 'The identifier for this component within this node, starts at 0 for each node.',
348
        'type' => 'int',
349
        'size' => 'small',
350
        'unsigned' => TRUE,
351
        'not null' => TRUE,
352
        'default' => 0,
353
      ),
354
      'no' => array(
355
        'description' => 'Usually this value is 0, but if a field has multiple values (such as a time or date), it may require multiple rows in the database.',
356
        'type' => 'varchar',
357
        'length' => 128,
358
        'not null' => TRUE,
359
        'default' => '0',
360
      ),
361
      'data' => array(
362
        'description' => 'The submitted value of this field, may be serialized for some components.',
363
        'type' => 'text',
364
        'size' => 'medium',
365
        'not null' => TRUE,
366
      ),
367
    ),
368
    'primary key' => array('nid', 'sid', 'cid', 'no'),
369
    'indexes' => array(
370
      'nid' => array('nid'),
371
      'sid_nid' => array('sid', 'nid'),
372
    ),
373
  );
374

    
375
  $schema['webform_last_download'] = array(
376
   'description' => 'Stores last submission number per user download.',
377
    'fields' => array(
378
      'nid' => array(
379
        'description' => 'The node identifier of a webform.',
380
        'type' => 'int',
381
        'unsigned' => TRUE,
382
        'not null' => TRUE,
383
        'default' => 0,
384
      ),
385
      'uid' => array(
386
       'description' => 'The user identifier.',
387
        'type' => 'int',
388
        'unsigned' => TRUE,
389
        'not null' => TRUE,
390
        'default' => 0,
391
      ),
392
     'sid' => array(
393
        'description' => 'The last downloaded submission number.',
394
        'type' => 'int',
395
        'unsigned' => TRUE,
396
        'not null' => TRUE,
397
        'default' => 0,
398
      ),
399
     'requested' => array(
400
        'description' => 'Timestamp of last download request.',
401
        'type' => 'int',
402
        'unsigned' => TRUE,
403
        'not null' => TRUE,
404
        'default' => 0,
405
      ),
406
    ),
407
    'primary key' => array('nid', 'uid'),
408
  );
409

    
410
  return $schema;
411
}
412

    
413
/**
414
 * Implements hook_install().
415
 */
416
function webform_install() {
417
  module_load_include('inc', 'node', 'content_types');
418
  db_update('system')
419
    ->condition('name', 'webform')
420
    ->condition('type', 'module')
421
    ->fields(array('weight' => -1))
422
    ->execute();
423

    
424
  // Optionally create the default webform type.
425
  if (variable_get('webform_install_create_content_type', TRUE)) {
426
    $webform_type = array(
427
      'type' => 'webform',
428
      'name' => st('Webform'),
429
      'base' => 'node_content',
430
      'description' => st('Create a new form or questionnaire accessible to users. Submission results and statistics are recorded and accessible to privileged users.'),
431
      'custom' => TRUE,
432
      'modified' => TRUE,
433
      'locked' => FALSE,
434
    );
435
    $webform_type = node_type_set_defaults($webform_type);
436
    node_type_save($webform_type);
437
    if (variable_get('webform_install_add_body_field', TRUE)) {
438
      node_add_body_field($webform_type);
439
    }
440
  }
441
}
442

    
443
/**
444
 * Implements hook_uninstall().
445
 */
446
function webform_uninstall() {
447
  // Unset webform variables.
448
  variable_del('webform_node_types');
449
  variable_del('webform_node_types_primary');
450
  variable_del('webform_disabled_components');
451
  variable_del('webform_use_cookies');
452
  variable_del('webform_default_from_address');
453
  variable_del('webform_default_from_name');
454
  variable_del('webform_default_subject');
455
  variable_del('webform_default_format');
456
  variable_del('webform_format_override');
457
  variable_del('webform_csv_delimiter');
458
  variable_del('webform_allowed_tags');
459
  variable_del('webform_blocks');
460
  variable_del('webform_search_index');
461
  variable_del('webform_email_address_format');
462
  variable_del('webform_export_format');
463
  variable_del('webform_submission_access_control');
464
  variable_del('webform_update_batch_size');
465

    
466
  $component_list = array();
467
  $path = drupal_get_path('module', 'webform') . '/components';
468
  $files = file_scan_directory($path, '/^.*\.inc$/');
469
  foreach ($files as $filename => $file) {
470
    variable_del('webform_enable_' . $file->name, 1);
471
  }
472

    
473
  // Delete uploaded files.
474
  $filepath = file_build_uri('webform');
475
  file_unmanaged_delete_recursive($filepath);
476
}
477

    
478
/**
479
 * Set the minimum upgrade version.
480
 *
481
 * Currently you cannot upgrade from 2.x in Drupal 6 to 3.x in Drupal 7. However
482
 * there are no database changes between the 3.x versions, so no update is
483
 * needed at all to move from 3.x in Drupal 6 to Drupal 7.
484
 */
485
function webform_update_last_removed() {
486
  return 6313;
487
}
488

    
489
/**
490
 * Allow the confirmation format column to have a NULL value.
491
 */
492
function webform_update_7301() {
493
  // These changes are modeled after user_update_7010().
494
  db_change_field('webform', 'confirmation_format', 'confirmation_format', array(
495
    'description' => 'The {filter_format}.format of the confirmation message.',
496
    'type' => 'int',
497
    'unsigned' => TRUE,
498
    'not null' => FALSE,
499
  ));
500
  db_update('webform')
501
    ->fields(array('confirmation_format' => NULL))
502
    ->condition('confirmation', '')
503
    ->condition('confirmation_format', 0)
504
    ->execute();
505
  $existing_formats = db_query("SELECT format FROM {filter_format}")->fetchCol();
506
  $default_format = variable_get('filter_default_format', 1);
507

    
508
  // Since Webform may be updated separately from Drupal core, not all format
509
  // names may be numbers when running this update.
510
  $numeric_formats = array();
511
  foreach ($existing_formats as $format_name) {
512
    if (is_numeric($format_name)) {
513
      $numeric_formats[] = (int) $format_name;
514
    }
515
  }
516

    
517
  $query = db_update('webform')
518
    ->fields(array('confirmation_format' => $default_format))
519
    ->isNotNull('confirmation_format');
520

    
521
  if (!empty($numeric_formats)) {
522
    $query->condition('confirmation_format', $numeric_formats, 'NOT IN');
523
  }
524

    
525
  $query->execute();
526
}
527

    
528
/**
529
 * Add columns for e-mail HTML and attachment settings.
530
 */
531
function webform_update_7302() {
532
  if (!db_field_exists('webform_emails', 'html')) {
533
    db_add_field('webform_emails', 'html', array('type' => 'int', 'size' => 'tiny', 'unsigned' => TRUE, 'default' => 0, 'not null' => TRUE));
534
    db_add_field('webform_emails', 'attachments', array('type' => 'int', 'size' => 'tiny', 'unsigned' => TRUE, 'default' => 0, 'not null' => TRUE));
535
  }
536
}
537

    
538
/**
539
 * Set the default for the "submit_notice" column to 1.
540
 */
541
function webform_update_7303() {
542
  db_change_field('webform', 'submit_notice', 'submit_notice', array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 1));
543
}
544

    
545
/**
546
 * Add field for block feature and redirection setting.
547
 */
548
function webform_update_7304() {
549
  if (!db_field_exists('webform', 'block')) {
550
    db_add_field('webform', 'block', array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 0));
551
    db_change_field('webform', 'redirect_url', 'redirect_url', array('type' => 'varchar', 'length' => 255, 'default' => '<confirmation>'));
552
    db_update('webform')
553
      ->fields(array('redirect_url' => 'confirmation'))
554
      ->condition('redirect_url', '')
555
      ->execute();
556
  }
557
}
558

    
559
/**
560
 * Set additional_validate and additional_submit columns to allow NULL.
561
 */
562
function webform_update_7305() {
563
  if (db_field_exists('webform', 'additional_validate')) {
564
    db_change_field('webform', 'additional_validate', 'additional_validate', array('type' => 'text', 'not null' => FALSE));
565
    db_change_field('webform', 'additional_submit', 'additional_submit', array('type' => 'text', 'not null' => FALSE));
566
  }
567
}
568

    
569
/**
570
 * Add column for webform status (open or closed).
571
 */
572
function webform_update_7306() {
573
  if (!db_field_exists('webform', 'status')) {
574
    db_add_field('webform', 'status', array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 1));
575
  }
576
}
577

    
578
/**
579
 * Update the confirmation_format column for default text format changes.
580
 */
581
function webform_update_7307() {
582
  // Update removed and moved to webform_update_7301().
583
  // See http://drupal.org/node/976102.
584
}
585

    
586
/**
587
 * Update the confirmation_format column to allow it to store strings.
588
 */
589
function webform_update_7308() {
590
  db_change_field('webform', 'confirmation_format', 'confirmation_format', array(
591
    'description' => 'The {filter_format}.format of the confirmation message.',
592
    'type' => 'varchar',
593
    'length' => 255,
594
    'not null' => FALSE,
595
  ));
596
}
597

    
598
/**
599
 * Add the ability to auto-save as draft between pages.
600
 */
601
function webform_update_7309() {
602
  if (!db_field_exists('webform', 'auto_save')) {
603
    db_add_field('webform', 'auto_save', array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 0));
604
  }
605
}
606

    
607
/**
608
 * Remove orphaned and unnecessary rows in the webform table.
609
 */
610
function webform_update_7310() {
611
  $result = db_query("SELECT nid FROM {webform} WHERE
612
    nid NOT IN
613
    (SELECT DISTINCT(w1.nid) FROM {webform} w1 INNER JOIN {webform_component} wc ON w1.nid = wc.nid)
614
    AND nid NOT IN
615
    (SELECT w2.nid FROM {webform} w2 INNER JOIN {node} n ON w2.nid = n.nid WHERE n.type = 'webform')"
616
  );
617
  $empty_nids = array();
618
  foreach ($result as $row) {
619
    $empty_nids[] = $row->nid;
620
  }
621
  if (!empty($empty_nids)) {
622
    db_delete('webform')->condition('nid', $empty_nids, 'IN')->execute();
623
  }
624
}
625

    
626
/**
627
 * Add an index for nid_uid_sid to webform_submissions.
628
 */
629
function webform_update_7311() {
630
  if (!db_index_exists('webform_submissions', 'nid_uid_sid')) {
631
    db_add_index('webform_submissions', 'nid_uid_sid', array('nid', 'uid', 'sid'));
632
  }
633
}
634

    
635
/**
636
 * Remove unused Webform variables.
637
 */
638
function webform_update_7312() {
639
  variable_del('node_types');
640
  variable_del('components');
641
}
642

    
643
/**
644
 * Convert the Date component start and end year options to start and end date.
645
 */
646
function webform_update_7313() {
647
  $result = db_select('webform_component', 'wc', array('fetch' => PDO::FETCH_ASSOC))
648
    ->fields('wc')
649
    ->condition('type', 'date')
650
    ->execute();
651
  foreach ($result as $component) {
652
    $component['extra'] = unserialize($component['extra']);
653
    if (!isset($component['extra']['start_date']) && !isset($component['end_date'])) {
654
      foreach (array('year_start' => 'start_date', 'year_end' => 'end_date') as $key => $replacement) {
655
        $value = isset($component['extra'][$key]) ? trim($component['extra'][$key]) : '';
656
        // Relative years.
657
        if (preg_match('/[-+][ ]*[0-9]+/', $value)) {
658
          $component['extra'][$replacement] = ($value == 1) ? ($value . ' year') : ($value . ' years');
659
        }
660
        // Absolute years.
661
        elseif (is_numeric($value)) {
662
          $component['extra'][$replacement] = 'Dec 31 ' . $value;
663
        }
664
        unset($component['extra'][$key]);
665
      }
666
      $component['extra'] = serialize($component['extra']);
667
      drupal_write_record('webform_component', $component, array('nid', 'cid'));
668
    }
669
  }
670
}
671

    
672
/**
673
 * Add webform_last_download table to store last downloaded sid per user.
674
 */
675
function webform_update_7314() {
676
  // Safety check to prevent recreating the webform_last_download table.
677
  if (db_table_exists('webform_last_download')) {
678
    return;
679
  }
680

    
681
  $schema['webform_last_download'] = array(
682
    'description' => 'Stores last submission number per user download.',
683
    'fields' => array(
684
      'nid' => array(
685
        'description' => 'The node identifier of a webform.',
686
        'type' => 'int',
687
        'unsigned' => TRUE,
688
        'not null' => TRUE,
689
        'default' => 0,
690
      ),
691
      'uid' => array(
692
        'description' => 'The user identifier.',
693
        'type' => 'int',
694
        'unsigned' => TRUE,
695
        'not null' => TRUE,
696
        'default' => 0,
697
      ),
698
      'sid' => array(
699
        'description' => 'The last downloaded submission number.',
700
        'type' => 'int',
701
        'unsigned' => TRUE,
702
        'not null' => TRUE,
703
        'default' => 0,
704
      ),
705
    ),
706
    'primary key' => array('nid', 'uid'),
707
  );
708
  db_create_table('webform_last_download', $schema['webform_last_download']);
709
}
710

    
711
/**
712
 * Add column for timestamp of last requested CSV download.
713
 */
714
function webform_update_7315() {
715
  if (!db_field_exists('webform_last_download', 'requested')) {
716
    db_add_field('webform_last_download', 'requested', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0,));
717
  }
718
}
719

    
720
/**
721
 * Add additional columns for total submission limit.
722
 */
723
function webform_update_7316() {
724
  if (!db_field_exists('webform', 'total_submit_limit')) {
725
    db_add_field('webform', 'total_submit_limit', array('type' => 'int', 'not null' => TRUE, 'default' => -1));
726
  }
727

    
728
  if (!db_field_exists('webform', 'total_submit_interval')) {
729
    db_add_field('webform', 'total_submit_interval', array('type' => 'int', 'not null' => TRUE, 'default' => -1));
730
  }
731
}
732

    
733
/**
734
 * Add an index for 'nid_sid' to webform_submissions.
735
 */
736
function webform_update_7317() {
737
  // Even though we already have an index 'nid_uid_sid', adding the index for
738
  // 'nid_sid' saves us a tablesort on the node/x/webform-results page.
739
  if (!db_index_exists('webform_submissions', 'nid_sid')) {
740
    db_add_index('webform_submissions', 'nid_sid', array('nid', 'sid'));
741
  }
742
}
743

    
744
/**
745
 * Upgrade file components to support the new AJAX-upload element.
746
 */
747
function webform_update_7318() {
748
  $result = db_select('webform_component', 'wc', array('fetch' => PDO::FETCH_ASSOC))
749
    ->fields('wc')
750
    ->condition('type', 'file')
751
    ->execute();
752
  foreach ($result as $component) {
753
    $component['extra'] = unserialize($component['extra']);
754
    if (!isset($component['extra']['directory'])) {
755
      $component['extra']['directory'] = $component['extra']['savelocation'];
756
      $component['extra']['scheme'] = file_default_scheme();
757
      $component['extra']['filtering']['size'] = $component['extra']['filtering']['size'] . ' KB';
758
      unset($component['extra']['savelocation']);
759
      $component['extra'] = serialize($component['extra']);
760
      drupal_write_record('webform_component', $component, array('nid', 'cid'));
761
    }
762
  }
763

    
764
  return t('File components updated to support AJAX uploading.');
765
}
766

    
767
/**
768
 * Add file usage entries for all files uploaded through Webform.
769
 */
770
function webform_update_7319(&$sandbox) {
771
  if (!isset($sandbox['progress'])) {
772
    // Initialize batch update information.
773
    $sandbox['progress'] = 0;
774
    $sandbox['last_fid_processed'] = -1;
775
    $sandbox['max'] = db_select('file_managed')
776
      ->condition('uri', '%' . db_like('://webform/') . '%', 'LIKE')
777
      ->countQuery()
778
      ->execute()
779
      ->fetchField();
780
  }
781

    
782
  // Process all files attached to a given revision during the same batch.
783
  $limit = variable_get('webform_update_batch_size', 100);
784
  $files = db_select('file_managed', 'f')
785
    ->fields('f')
786
    ->condition('uri', '%' . db_like('://webform/') . '%', 'LIKE')
787
    ->condition('fid', $sandbox['last_fid_processed'], '>')
788
    ->orderBy('fid', 'ASC')
789
    ->range(0, $limit)
790
    ->execute()
791
    ->fetchAllAssoc('fid', PDO::FETCH_ASSOC);
792

    
793
  // Determine each submission with which a file is associated.
794
  if (!empty($files)) {
795
    foreach ($files as $fid => $file) {
796
      $file = (object) $file;
797
      $sids = db_query('SELECT wsd.sid FROM {webform_component} wc INNER JOIN {webform_submitted_data} wsd ON wc.nid = wsd.nid AND wc.type = :file WHERE data = :fid', array(':file' => 'file', ':fid' => $file->fid))->fetchAllAssoc('sid', PDO::FETCH_ASSOC);
798
      foreach ($sids as $sid => $row) {
799
        // We use a db_merge() instead of file_usage_add() to prevent problems
800
        // in the event this update was run twice. No file provided by Webform
801
        // should ever be in use more than once at this point.
802
        db_merge('file_usage')
803
          ->key(array(
804
            'fid' => $file->fid,
805
            'type' => 'submission',
806
            'module' => 'webform',
807
            'id' => $sid,
808
          ))
809
          ->fields(array(
810
            'count' => 1,
811
          ))
812
          ->execute();
813
      }
814

    
815
      // Update our progress information for the batch update.
816
      $sandbox['progress']++;
817
      $sandbox['last_fid_processed'] = $file->fid;
818
    }
819
  }
820

    
821
  // If less than limit was processed, the update process is finished.
822
  if (count($files) < $limit || $sandbox['progress'] == $sandbox['max']) {
823
    $finished = TRUE;
824
  }
825

    
826
  // If there's no max value then there's nothing to update and we're finished.
827
  if (empty($sandbox['max']) || isset($finished)) {
828
    return t('Webform file entries created in the file_usage table.');
829
  }
830
  else {
831
    // Indicate our current progress to the batch update system.
832
    $sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
833
  }
834
}
835

    
836
/**
837
 * Mark files uploaded through Webform that report active usage permanent.
838
 */
839
function webform_update_7320() {
840
  db_query("UPDATE {file_managed} SET status = 1 WHERE fid IN (SELECT fid FROM {file_usage} WHERE module = :module_name)", array(':module_name' => 'webform'));
841
}
842

    
843
/**
844
 * Remove files left over from deleted submissions. Such files are now deleted
845
 * automatically.
846
 */
847
function webform_update_7321() {
848
  module_load_include('inc', 'webform', 'components/file');
849
  $fids = db_query("SELECT fid FROM {file_usage} WHERE module = 'webform' AND type = 'submission' AND NOT id IN(SELECT sid FROM {webform_submissions})")->fetchCol();
850
  foreach ($fids as $fid) {
851
    _webform_delete_file(NULL, array($fid));
852
  }
853
}