Projet

Général

Profil

Paste
Télécharger (28,6 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / webform / webform.install @ 76df55b7

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
      'data' => array(array('data', 64)),
373
    ),
374
  );
375

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

    
411
  return $schema;
412
}
413

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
794
  // Determine each submission with which a file is associated.
795
  if (!empty($files)) {
796
    foreach ($files as $fid => $file) {
797
      $file = (object) $file;
798
      $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);
799
      foreach ($sids as $sid => $row) {
800
        // We use a db_merge() instead of file_usage_add() to prevent problems
801
        // in the event this update was run twice. No file provided by Webform
802
        // should ever be in use more than once at this point.
803
        db_merge('file_usage')
804
          ->key(array(
805
            'fid' => $file->fid,
806
            'type' => 'submission',
807
            'module' => 'webform',
808
            'id' => $sid,
809
          ))
810
          ->fields(array(
811
            'count' => 1,
812
          ))
813
          ->execute();
814
      }
815

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

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

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

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

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

    
856
/**
857
 * Add index on {webform_submitted_data}.data.
858
 */
859
function webform_update_7322() {
860
  db_add_index('webform_submitted_data', 'data', array(array('data', 64)));
861
}