comparison sites/all/modules/webform/webform.install @ 0:ff03f76ab3fe

initial version
author danieleb <danielebarchiesi@me.com>
date Wed, 21 Aug 2013 18:51:11 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:ff03f76ab3fe
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 node_add_body_field($webform_type);
438 }
439 }
440
441 /**
442 * Implements hook_uninstall().
443 */
444 function webform_uninstall() {
445 // Unset webform variables.
446 variable_del('webform_node_types');
447 variable_del('webform_node_types_primary');
448 variable_del('webform_use_cookies');
449 variable_del('webform_default_from_address');
450 variable_del('webform_default_from_name');
451 variable_del('webform_default_subject');
452 variable_del('webform_default_format');
453 variable_del('webform_format_override');
454 variable_del('webform_csv_delimiter');
455 variable_del('webform_allowed_tags');
456 variable_del('webform_blocks');
457
458 $component_list = array();
459 $path = drupal_get_path('module', 'webform') . '/components';
460 $files = file_scan_directory($path, '/^.*\.inc$/');
461 foreach ($files as $filename => $file) {
462 variable_del('webform_enable_' . $file->name, 1);
463 }
464
465 // Delete uploaded files.
466 $filepath = file_build_uri('webform');
467 file_unmanaged_delete_recursive($filepath);
468 }
469
470 /**
471 * Set the minimum upgrade version.
472 *
473 * Currently you cannot upgrade from 2.x in Drupal 6 to 3.x in Drupal 7. However
474 * there are no database changes between the 3.x versions, so no update is
475 * needed at all to move from 3.x in Drupal 6 to Drupal 7.
476 */
477 function webform_update_last_removed() {
478 return 6313;
479 }
480
481 /**
482 * Allow the confirmation format column to have a NULL value.
483 */
484 function webform_update_7301() {
485 // These changes are modeled after user_update_7010().
486 db_change_field('webform', 'confirmation_format', 'confirmation_format', array(
487 'description' => 'The {filter_format}.format of the confirmation message.',
488 'type' => 'int',
489 'unsigned' => TRUE,
490 'not null' => FALSE,
491 ));
492 db_update('webform')
493 ->fields(array('confirmation_format' => NULL))
494 ->condition('confirmation', '')
495 ->condition('confirmation_format', 0)
496 ->execute();
497 $existing_formats = db_query("SELECT format FROM {filter_format}")->fetchCol();
498 $default_format = variable_get('filter_default_format', 1);
499
500 // Since Webform may be updated separately from Drupal core, not all format
501 // names may be numbers when running this update.
502 $numeric_formats = array();
503 foreach ($existing_formats as $format_name) {
504 if (is_numeric($format_name)) {
505 $numeric_formats[] = (int) $format_name;
506 }
507 }
508
509 $query = db_update('webform')
510 ->fields(array('confirmation_format' => $default_format))
511 ->isNotNull('confirmation_format');
512
513 if (!empty($numeric_formats)) {
514 $query->condition('confirmation_format', $numeric_formats, 'NOT IN');
515 }
516
517 $query->execute();
518 }
519
520 /**
521 * Add columns for e-mail HTML and attachment settings.
522 */
523 function webform_update_7302() {
524 if (!db_field_exists('webform_emails', 'html')) {
525 db_add_field('webform_emails', 'html', array('type' => 'int', 'size' => 'tiny', 'unsigned' => TRUE, 'default' => 0, 'not null' => TRUE));
526 db_add_field('webform_emails', 'attachments', array('type' => 'int', 'size' => 'tiny', 'unsigned' => TRUE, 'default' => 0, 'not null' => TRUE));
527 }
528 }
529
530 /**
531 * Set the default for the "submit_notice" column to 1.
532 */
533 function webform_update_7303() {
534 db_change_field('webform', 'submit_notice', 'submit_notice', array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 1));
535 }
536
537 /**
538 * Add field for block feature and redirection setting.
539 */
540 function webform_update_7304() {
541 if (!db_field_exists('webform', 'block')) {
542 db_add_field('webform', 'block', array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 0));
543 db_change_field('webform', 'redirect_url', 'redirect_url', array('type' => 'varchar', 'length' => 255, 'default' => '<confirmation>'));
544 db_update('webform')
545 ->fields(array('redirect_url' => 'confirmation'))
546 ->condition('redirect_url', '')
547 ->execute();
548 }
549 }
550
551 /**
552 * Set additional_validate and additional_submit columns to allow NULL.
553 */
554 function webform_update_7305() {
555 if (db_field_exists('webform', 'additional_validate')) {
556 db_change_field('webform', 'additional_validate', 'additional_validate', array('type' => 'text', 'not null' => FALSE));
557 db_change_field('webform', 'additional_submit', 'additional_submit', array('type' => 'text', 'not null' => FALSE));
558 }
559 }
560
561 /**
562 * Add column for webform status (open or closed).
563 */
564 function webform_update_7306() {
565 if (!db_field_exists('webform', 'status')) {
566 db_add_field('webform', 'status', array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 1));
567 }
568 }
569
570 /**
571 * Update the confirmation_format column for default text format changes.
572 */
573 function webform_update_7307() {
574 // Update removed and moved to webform_update_7301().
575 // See http://drupal.org/node/976102.
576 }
577
578 /**
579 * Update the confirmation_format column to allow it to store strings.
580 */
581 function webform_update_7308() {
582 db_change_field('webform', 'confirmation_format', 'confirmation_format', array(
583 'description' => 'The {filter_format}.format of the confirmation message.',
584 'type' => 'varchar',
585 'length' => 255,
586 'not null' => FALSE,
587 ));
588 }
589
590 /**
591 * Add the ability to auto-save as draft between pages.
592 */
593 function webform_update_7309() {
594 if (!db_field_exists('webform', 'auto_save')) {
595 db_add_field('webform', 'auto_save', array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 0));
596 }
597 }
598
599 /**
600 * Remove orphaned and unnecessary rows in the webform table.
601 */
602 function webform_update_7310() {
603 $result = db_query("SELECT nid FROM {webform} WHERE
604 nid NOT IN
605 (SELECT DISTINCT(w1.nid) FROM {webform} w1 INNER JOIN {webform_component} wc ON w1.nid = wc.nid)
606 AND nid NOT IN
607 (SELECT w2.nid FROM {webform} w2 INNER JOIN {node} n ON w2.nid = n.nid WHERE n.type = 'webform')"
608 );
609 $empty_nids = array();
610 foreach ($result as $row) {
611 $empty_nids[] = $row->nid;
612 }
613 if (!empty($empty_nids)) {
614 db_delete('webform')->condition('nid', $empty_nids, 'IN')->execute();
615 }
616 }
617
618 /**
619 * Add an index for nid_uid_sid to webform_submissions.
620 */
621 function webform_update_7311() {
622 if (!db_index_exists('webform_submissions', 'nid_uid_sid')) {
623 db_add_index('webform_submissions', 'nid_uid_sid', array('nid', 'uid', 'sid'));
624 }
625 }
626
627 /**
628 * Remove unused Webform variables.
629 */
630 function webform_update_7312() {
631 variable_del('node_types');
632 variable_del('components');
633 }
634
635 /**
636 * Convert the Date component start and end year options to start and end date.
637 */
638 function webform_update_7313() {
639 $result = db_select('webform_component', 'wc', array('fetch' => PDO::FETCH_ASSOC))
640 ->fields('wc')
641 ->condition('type', 'date')
642 ->execute();
643 foreach ($result as $component) {
644 $component['extra'] = unserialize($component['extra']);
645 if (!isset($component['extra']['start_date']) && !isset($component['end_date'])) {
646 foreach (array('year_start' => 'start_date', 'year_end' => 'end_date') as $key => $replacement) {
647 $value = isset($component['extra'][$key]) ? trim($component['extra'][$key]) : '';
648 // Relative years.
649 if (preg_match('/[-+][ ]*[0-9]+/', $value)) {
650 $component['extra'][$replacement] = ($value == 1) ? ($value . ' year') : ($value . ' years');
651 }
652 // Absolute years.
653 elseif (is_numeric($value)) {
654 $component['extra'][$replacement] = 'Dec 31 ' . $value;
655 }
656 unset($component['extra'][$key]);
657 }
658 $component['extra'] = serialize($component['extra']);
659 drupal_write_record('webform_component', $component, array('nid', 'cid'));
660 }
661 }
662 }
663
664 /**
665 * Add webform_last_download table to store last downloaded sid per user.
666 */
667 function webform_update_7314() {
668 // Safety check to prevent recreating the webform_last_download table.
669 if (db_table_exists('webform_last_download')) {
670 return;
671 }
672
673 $schema['webform_last_download'] = array(
674 'description' => 'Stores last submission number per user download.',
675 'fields' => array(
676 'nid' => array(
677 'description' => 'The node identifier of a webform.',
678 'type' => 'int',
679 'unsigned' => TRUE,
680 'not null' => TRUE,
681 'default' => 0,
682 ),
683 'uid' => array(
684 'description' => 'The user identifier.',
685 'type' => 'int',
686 'unsigned' => TRUE,
687 'not null' => TRUE,
688 'default' => 0,
689 ),
690 'sid' => array(
691 'description' => 'The last downloaded submission number.',
692 'type' => 'int',
693 'unsigned' => TRUE,
694 'not null' => TRUE,
695 'default' => 0,
696 ),
697 ),
698 'primary key' => array('nid', 'uid'),
699 );
700 db_create_table('webform_last_download', $schema['webform_last_download']);
701 }
702
703 /**
704 * Add column for timestamp of last requested CSV download.
705 */
706 function webform_update_7315() {
707 if (!db_field_exists('webform_last_download', 'requested')) {
708 db_add_field('webform_last_download', 'requested', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0,));
709 }
710 }
711
712 /**
713 * Add additional columns for total submission limit.
714 */
715 function webform_update_7316() {
716 if (!db_field_exists('webform', 'total_submit_limit')) {
717 db_add_field('webform', 'total_submit_limit', array('type' => 'int', 'not null' => TRUE, 'default' => -1));
718 }
719
720 if (!db_field_exists('webform', 'total_submit_interval')) {
721 db_add_field('webform', 'total_submit_interval', array('type' => 'int', 'not null' => TRUE, 'default' => -1));
722 }
723 }
724
725 /**
726 * Add an index for 'nid_sid' to webform_submissions.
727 */
728 function webform_update_7317() {
729 // Even though we already have an index 'nid_uid_sid', adding the index for
730 // 'nid_sid' saves us a tablesort on the node/x/webform-results page.
731 if (!db_index_exists('webform_submissions', 'nid_sid')) {
732 db_add_index('webform_submissions', 'nid_sid', array('nid', 'sid'));
733 }
734 }
735
736 /**
737 * Upgrade file components to support the new AJAX-upload element.
738 */
739 function webform_update_7318() {
740 $result = db_select('webform_component', 'wc', array('fetch' => PDO::FETCH_ASSOC))
741 ->fields('wc')
742 ->condition('type', 'file')
743 ->execute();
744 foreach ($result as $component) {
745 $component['extra'] = unserialize($component['extra']);
746 if (!isset($component['extra']['directory'])) {
747 $component['extra']['directory'] = $component['extra']['savelocation'];
748 $component['extra']['scheme'] = file_default_scheme();
749 $component['extra']['filtering']['size'] = $component['extra']['filtering']['size'] . ' KB';
750 unset($component['extra']['savelocation']);
751 $component['extra'] = serialize($component['extra']);
752 drupal_write_record('webform_component', $component, array('nid', 'cid'));
753 }
754 }
755
756 return t('File components updated to support AJAX uploading.');
757 }
758
759 /**
760 * Add file usage entries for all files uploaded through Webform.
761 */
762 function webform_update_7319(&$sandbox) {
763 if (!isset($sandbox['progress'])) {
764 // Initialize batch update information.
765 $sandbox['progress'] = 0;
766 $sandbox['last_fid_processed'] = -1;
767 $sandbox['max'] = db_select('file_managed')
768 ->condition('uri', '%' . db_like('://webform/') . '%', 'LIKE')
769 ->countQuery()
770 ->execute()
771 ->fetchField();
772 }
773
774 // Process all files attached to a given revision during the same batch.
775 $limit = variable_get('webform_update_batch_size', 100);
776 $files = db_select('file_managed', 'f')
777 ->fields('f')
778 ->condition('uri', '%' . db_like('://webform/') . '%', 'LIKE')
779 ->condition('fid', $sandbox['last_fid_processed'], '>')
780 ->orderBy('fid', 'ASC')
781 ->range(0, $limit)
782 ->execute()
783 ->fetchAllAssoc('fid', PDO::FETCH_ASSOC);
784
785 // Determine each submission with which a file is associated.
786 if (!empty($files)) {
787 foreach ($files as $fid => $file) {
788 $file = (object) $file;
789 $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);
790 foreach ($sids as $sid => $row) {
791 // We use a db_merge() instead of file_usage_add() to prevent problems
792 // in the event this update was run twice. No file provided by Webform
793 // should ever be in use more than once at this point.
794 db_merge('file_usage')
795 ->key(array(
796 'fid' => $file->fid,
797 'type' => 'submission',
798 'module' => 'webform',
799 'id' => $sid,
800 ))
801 ->fields(array(
802 'count' => 1,
803 ))
804 ->execute();
805 }
806
807 // Update our progress information for the batch update.
808 $sandbox['progress']++;
809 $sandbox['last_fid_processed'] = $file->fid;
810 }
811 }
812
813 // If less than limit was processed, the update process is finished.
814 if (count($files) < $limit || $sandbox['progress'] == $sandbox['max']) {
815 $finished = TRUE;
816 }
817
818 // If there's no max value then there's nothing to update and we're finished.
819 if (empty($sandbox['max']) || isset($finished)) {
820 return t('Webform file entries created in the file_usage table.');
821 }
822 else {
823 // Indicate our current progress to the batch update system.
824 $sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
825 }
826 }
827
828 /**
829 * Mark files uploaded through Webform that report active usage permanent.
830 */
831 function webform_update_7320() {
832 db_query("UPDATE {file_managed} SET status = 1 WHERE fid IN (SELECT fid FROM {file_usage} WHERE module = :module_name)", array(':module_name' => 'webform'));
833 }
834
835 /**
836 * Remove files left over from deleted submissions. Such files are now deleted
837 * automatically.
838 */
839 function webform_update_7321() {
840 module_load_include('inc', 'webform', 'components/file');
841 $fids = db_query("SELECT fid FROM {file_usage} WHERE module = 'webform' AND type = 'submission' AND NOT id IN(SELECT sid FROM {webform_submissions})")->fetchCol();
842 foreach ($fids as $fid) {
843 _webform_delete_file(NULL, array($fid));
844 }
845 }