webform.install 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. <?php
  2. /**
  3. * @file
  4. * Webform module install/schema hooks.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function webform_schema() {
  10. $schema = array();
  11. $schema['webform'] = array(
  12. 'description' => 'Table for storing additional properties for webform nodes.',
  13. 'fields' => array(
  14. 'nid' => array(
  15. 'description' => 'The node identifier of a webform.',
  16. 'type' => 'int',
  17. 'unsigned' => TRUE,
  18. 'not null' => TRUE,
  19. ),
  20. 'confirmation' => array(
  21. 'description' => 'The confirmation message or URL displayed to the user after submitting a form.',
  22. 'type' => 'text',
  23. 'not null' => TRUE,
  24. ),
  25. 'confirmation_format' => array(
  26. 'description' => 'The {filter_format}.format of the confirmation message.',
  27. 'type' => 'varchar',
  28. 'length' => 255,
  29. 'not null' => FALSE,
  30. ),
  31. 'redirect_url' => array(
  32. 'description' => 'The URL a user is redirected to after submitting a form.',
  33. 'type' => 'varchar',
  34. 'length' => 255,
  35. 'default' => '<confirmation>',
  36. ),
  37. 'status' => array(
  38. 'description' => 'Boolean value of a webform for open (1) or closed (0).',
  39. 'type' => 'int',
  40. 'size' => 'tiny',
  41. 'not null' => TRUE,
  42. 'default' => 1,
  43. ),
  44. 'block' => array(
  45. 'description' => 'Boolean value for whether this form be available as a block.',
  46. 'type' => 'int',
  47. 'size' => 'tiny',
  48. 'not null' => TRUE,
  49. 'default' => 0,
  50. ),
  51. 'teaser' => array(
  52. 'description' => 'Boolean value for whether the entire form should be displayed on the teaser.',
  53. 'type' => 'int',
  54. 'size' => 'tiny',
  55. 'not null' => TRUE,
  56. 'default' => 0,
  57. ),
  58. 'allow_draft' => array(
  59. 'description' => 'Boolean value for whether submissions to this form be saved as a draft.',
  60. 'type' => 'int',
  61. 'size' => 'tiny',
  62. 'not null' => TRUE,
  63. 'default' => 0,
  64. ),
  65. 'auto_save' => array(
  66. 'description' => 'Boolean value for whether submissions to this form should be auto-saved between pages.',
  67. 'type' => 'int',
  68. 'size' => 'tiny',
  69. 'not null' => TRUE,
  70. 'default' => 0,
  71. ),
  72. 'submit_notice' => array(
  73. 'description' => 'Boolean value for whether to show or hide the previous submissions notification.',
  74. 'type' => 'int',
  75. 'size' => 'tiny',
  76. 'not null' => TRUE,
  77. 'default' => 1,
  78. ),
  79. 'submit_text' => array(
  80. 'description' => 'The title of the submit button on the form.',
  81. 'type' => 'varchar',
  82. 'length' => 255,
  83. ),
  84. 'submit_limit' => array(
  85. 'description' => 'The number of submissions a single user is allowed to submit within an interval. -1 is unlimited.',
  86. 'type' => 'int',
  87. 'size' => 'tiny',
  88. 'not null' => TRUE,
  89. 'default' => -1,
  90. ),
  91. 'submit_interval' => array(
  92. 'description' => 'The amount of time in seconds that must pass before a user can submit another submission within the set limit.',
  93. 'type' => 'int',
  94. 'not null' => TRUE,
  95. 'default' => -1,
  96. ),
  97. 'total_submit_limit' => array(
  98. 'description' => 'The total number of submissions allowed within an interval. -1 is unlimited.',
  99. 'type' => 'int',
  100. 'not null' => TRUE,
  101. 'default' => -1,
  102. ),
  103. 'total_submit_interval' => array(
  104. 'description' => 'The amount of time in seconds that must pass before another submission can be submitted within the set limit.',
  105. 'type' => 'int',
  106. 'not null' => TRUE,
  107. 'default' => -1,
  108. ),
  109. ),
  110. 'primary key' => array('nid'),
  111. );
  112. $schema['webform_component'] = array(
  113. 'description' => 'Stores information about components for webform nodes.',
  114. 'fields' => array(
  115. 'nid' => array(
  116. 'description' => 'The node identifier of a webform.',
  117. 'type' => 'int',
  118. 'unsigned' => TRUE,
  119. 'not null' => TRUE,
  120. 'default' => 0,
  121. ),
  122. 'cid' => array(
  123. 'description' => 'The identifier for this component within this node, starts at 0 for each node.',
  124. 'type' => 'int',
  125. 'size' => 'small',
  126. 'unsigned' => TRUE,
  127. 'not null' => TRUE,
  128. 'default' => 0,
  129. ),
  130. 'pid' => array(
  131. 'description' => 'If this component has a parent fieldset, the cid of that component.',
  132. 'type' => 'int',
  133. 'size' => 'small',
  134. 'unsigned' => TRUE,
  135. 'not null' => TRUE,
  136. 'default' => 0,
  137. ),
  138. 'form_key' => array(
  139. 'description' => 'When the form is displayed and processed, this key can be used to reference the results.',
  140. 'type' => 'varchar',
  141. 'length' => 128,
  142. ),
  143. 'name' => array(
  144. 'description' => 'The label for this component.',
  145. 'type' => 'varchar',
  146. 'length' => 255,
  147. ),
  148. 'type' => array(
  149. 'description' => 'The field type of this component (textfield, select, hidden, etc.).',
  150. 'type' => 'varchar',
  151. 'length' => 16,
  152. ),
  153. 'value' => array(
  154. 'description' => 'The default value of the component when displayed to the end-user.',
  155. 'type' => 'text',
  156. 'not null' => TRUE,
  157. ),
  158. 'extra' => array(
  159. 'description' => 'Additional information unique to the display or processing of this component.',
  160. 'type' => 'text',
  161. 'not null' => TRUE,
  162. ),
  163. 'mandatory' => array(
  164. 'description' => 'Boolean flag for if this component is required.',
  165. 'type' => 'int',
  166. 'size' => 'tiny',
  167. 'not null' => TRUE,
  168. 'default' => 0,
  169. ),
  170. 'weight' => array(
  171. 'description' => 'Determines the position of this component in the form.',
  172. 'type' => 'int',
  173. 'size' => 'small',
  174. 'not null' => TRUE,
  175. 'default' => 0,
  176. ),
  177. ),
  178. 'primary key' => array('nid', 'cid'),
  179. );
  180. $schema['webform_emails'] = array(
  181. 'description' => 'Holds information regarding e-mails that should be sent upon submitting a webform',
  182. 'fields' => array(
  183. 'nid' => array(
  184. 'description' => 'The node identifier of a webform.',
  185. 'type' => 'int',
  186. 'unsigned' => TRUE,
  187. 'not null' => TRUE,
  188. 'default' => 0,
  189. ),
  190. 'eid' => array(
  191. 'description' => 'The e-mail identifier for this row\'s settings.',
  192. 'type' => 'int',
  193. 'unsigned' => TRUE,
  194. 'size' => 'small',
  195. 'not null' => TRUE,
  196. 'default' => 0,
  197. ),
  198. 'email' => array(
  199. '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.',
  200. 'type' => 'text',
  201. 'not null' => FALSE,
  202. ),
  203. 'subject' => array(
  204. '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.',
  205. 'type' => 'varchar',
  206. 'length' => '255',
  207. 'not null' => FALSE,
  208. ),
  209. 'from_name' => array(
  210. '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.',
  211. 'type' => 'varchar',
  212. 'length' => '255',
  213. 'not null' => FALSE,
  214. ),
  215. 'from_address' => array(
  216. '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.',
  217. 'type' => 'varchar',
  218. 'length' => '255',
  219. 'not null' => FALSE,
  220. ),
  221. 'template' => array(
  222. '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.',
  223. 'type' => 'text',
  224. 'not null' => FALSE,
  225. ),
  226. 'excluded_components' => array(
  227. 'description' => 'A list of components that will not be included in the %email_values token. A list of CIDs separated by commas.',
  228. 'type' => 'text',
  229. 'not null' => TRUE,
  230. ),
  231. 'html' => array(
  232. 'description' => 'Determines if the e-mail will be sent in an HTML format. Requires Mime Mail module.',
  233. 'type' => 'int',
  234. 'unsigned' => TRUE,
  235. 'size' => 'tiny',
  236. 'not null' => TRUE,
  237. 'default' => 0,
  238. ),
  239. 'attachments' => array(
  240. 'description' => 'Determines if the e-mail will include file attachments. Requires Mime Mail module.',
  241. 'type' => 'int',
  242. 'unsigned' => TRUE,
  243. 'size' => 'tiny',
  244. 'not null' => TRUE,
  245. 'default' => 0,
  246. ),
  247. ),
  248. 'primary key' => array('nid', 'eid'),
  249. );
  250. $schema['webform_roles'] = array(
  251. '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.',
  252. 'fields' => array(
  253. 'nid' => array(
  254. 'description' => 'The node identifier of a webform.',
  255. 'type' => 'int',
  256. 'unsigned' => TRUE,
  257. 'not null' => TRUE,
  258. 'default' => 0,
  259. ),
  260. 'rid' => array(
  261. 'description' => 'The role identifier.',
  262. 'type' => 'int',
  263. 'unsigned' => TRUE,
  264. 'not null' => TRUE,
  265. 'default' => 0,
  266. ),
  267. ),
  268. 'primary key' => array('nid', 'rid'),
  269. );
  270. $schema['webform_submissions'] = array(
  271. 'description' => 'Holds general information about submissions outside of field values.',
  272. 'fields' => array(
  273. 'sid' => array(
  274. 'description' => 'The unique identifier for this submission.',
  275. 'type' => 'serial',
  276. 'unsigned' => TRUE,
  277. 'not null' => TRUE,
  278. ),
  279. 'nid' => array(
  280. 'description' => 'The node identifier of a webform.',
  281. 'type' => 'int',
  282. 'unsigned' => TRUE,
  283. 'not null' => TRUE,
  284. 'default' => 0,
  285. ),
  286. 'uid' => array(
  287. 'description' => 'The id of the user that completed this submission.',
  288. 'type' => 'int',
  289. 'unsigned' => TRUE,
  290. 'not null' => TRUE,
  291. 'default' => 0,
  292. ),
  293. 'is_draft' => array(
  294. 'description' => 'Is this a draft of the submission?',
  295. 'type' => 'int',
  296. 'size' => 'tiny',
  297. 'not null' => TRUE,
  298. 'default' => 0,
  299. ),
  300. 'submitted' => array(
  301. 'description' => 'Timestamp of when the form was submitted.',
  302. 'type' => 'int',
  303. 'not null' => TRUE,
  304. 'default' => 0,
  305. ),
  306. 'remote_addr' => array(
  307. 'description' => 'The IP address of the user that submitted the form.',
  308. 'type' => 'varchar',
  309. 'length' => 128,
  310. ),
  311. ),
  312. 'primary key' => array('sid'),
  313. 'unique keys' => array(
  314. 'sid_nid' => array('sid', 'nid'),
  315. ),
  316. 'indexes' => array(
  317. 'nid_uid_sid' => array('nid', 'uid', 'sid'),
  318. 'nid_sid' => array('nid', 'sid'),
  319. ),
  320. );
  321. $schema['webform_submitted_data'] = array(
  322. 'description' => 'Stores all submitted field data for webform submissions.',
  323. 'fields' => array(
  324. 'nid' => array(
  325. 'description' => 'The node identifier of a webform.',
  326. 'type' => 'int',
  327. 'unsigned' => TRUE,
  328. 'not null' => TRUE,
  329. 'default' => 0,
  330. ),
  331. 'sid' => array(
  332. 'description' => 'The unique identifier for this submission.',
  333. 'type' => 'int',
  334. 'unsigned' => TRUE,
  335. 'not null' => TRUE,
  336. 'default' => 0,
  337. ),
  338. 'cid' => array(
  339. 'description' => 'The identifier for this component within this node, starts at 0 for each node.',
  340. 'type' => 'int',
  341. 'size' => 'small',
  342. 'unsigned' => TRUE,
  343. 'not null' => TRUE,
  344. 'default' => 0,
  345. ),
  346. 'no' => array(
  347. '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.',
  348. 'type' => 'varchar',
  349. 'length' => 128,
  350. 'not null' => TRUE,
  351. 'default' => '0',
  352. ),
  353. 'data' => array(
  354. 'description' => 'The submitted value of this field, may be serialized for some components.',
  355. 'type' => 'text',
  356. 'size' => 'medium',
  357. 'not null' => TRUE,
  358. ),
  359. ),
  360. 'primary key' => array('nid', 'sid', 'cid', 'no'),
  361. 'indexes' => array(
  362. 'nid' => array('nid'),
  363. 'sid_nid' => array('sid', 'nid'),
  364. 'data' => array(array('data', 64)),
  365. ),
  366. );
  367. $schema['webform_last_download'] = array(
  368. 'description' => 'Stores last submission number per user download.',
  369. 'fields' => array(
  370. 'nid' => array(
  371. 'description' => 'The node identifier of a webform.',
  372. 'type' => 'int',
  373. 'unsigned' => TRUE,
  374. 'not null' => TRUE,
  375. 'default' => 0,
  376. ),
  377. 'uid' => array(
  378. 'description' => 'The user identifier.',
  379. 'type' => 'int',
  380. 'unsigned' => TRUE,
  381. 'not null' => TRUE,
  382. 'default' => 0,
  383. ),
  384. 'sid' => array(
  385. 'description' => 'The last downloaded submission number.',
  386. 'type' => 'int',
  387. 'unsigned' => TRUE,
  388. 'not null' => TRUE,
  389. 'default' => 0,
  390. ),
  391. 'requested' => array(
  392. 'description' => 'Timestamp of last download request.',
  393. 'type' => 'int',
  394. 'unsigned' => TRUE,
  395. 'not null' => TRUE,
  396. 'default' => 0,
  397. ),
  398. ),
  399. 'primary key' => array('nid', 'uid'),
  400. );
  401. return $schema;
  402. }
  403. /**
  404. * Implements hook_install().
  405. */
  406. function webform_install() {
  407. module_load_include('inc', 'node', 'content_types');
  408. db_update('system')
  409. ->condition('name', 'webform')
  410. ->condition('type', 'module')
  411. ->fields(array('weight' => -1))
  412. ->execute();
  413. // Optionally create the default webform type.
  414. if (variable_get('webform_install_create_content_type', TRUE)) {
  415. $webform_type = array(
  416. 'type' => 'webform',
  417. 'name' => st('Webform'),
  418. 'base' => 'node_content',
  419. 'description' => st('Create a new form or questionnaire accessible to users. Submission results and statistics are recorded and accessible to privileged users.'),
  420. 'custom' => TRUE,
  421. 'modified' => TRUE,
  422. 'locked' => FALSE,
  423. );
  424. $webform_type = node_type_set_defaults($webform_type);
  425. node_type_save($webform_type);
  426. if (variable_get('webform_install_add_body_field', TRUE)) {
  427. node_add_body_field($webform_type);
  428. }
  429. }
  430. }
  431. /**
  432. * Implements hook_uninstall().
  433. */
  434. function webform_uninstall() {
  435. // Unset webform variables.
  436. variable_del('webform_node_types');
  437. variable_del('webform_node_types_primary');
  438. variable_del('webform_disabled_components');
  439. variable_del('webform_use_cookies');
  440. variable_del('webform_default_from_address');
  441. variable_del('webform_default_from_name');
  442. variable_del('webform_default_subject');
  443. variable_del('webform_default_format');
  444. variable_del('webform_format_override');
  445. variable_del('webform_csv_delimiter');
  446. variable_del('webform_allowed_tags');
  447. variable_del('webform_blocks');
  448. variable_del('webform_search_index');
  449. variable_del('webform_email_address_format');
  450. variable_del('webform_export_format');
  451. variable_del('webform_submission_access_control');
  452. variable_del('webform_update_batch_size');
  453. $component_list = array();
  454. $path = drupal_get_path('module', 'webform') . '/components';
  455. $files = file_scan_directory($path, '/^.*\.inc$/');
  456. foreach ($files as $filename => $file) {
  457. variable_del('webform_enable_' . $file->name, 1);
  458. }
  459. // Delete uploaded files.
  460. $filepath = file_build_uri('webform');
  461. file_unmanaged_delete_recursive($filepath);
  462. }
  463. /**
  464. * Set the minimum upgrade version.
  465. *
  466. * Currently you cannot upgrade from 2.x in Drupal 6 to 3.x in Drupal 7. However
  467. * there are no database changes between the 3.x versions, so no update is
  468. * needed at all to move from 3.x in Drupal 6 to Drupal 7.
  469. */
  470. function webform_update_last_removed() {
  471. return 6313;
  472. }
  473. /**
  474. * Allow the confirmation format column to have a NULL value.
  475. */
  476. function webform_update_7301() {
  477. // These changes are modeled after user_update_7010().
  478. db_change_field('webform', 'confirmation_format', 'confirmation_format', array(
  479. 'description' => 'The {filter_format}.format of the confirmation message.',
  480. 'type' => 'int',
  481. 'unsigned' => TRUE,
  482. 'not null' => FALSE,
  483. ));
  484. db_update('webform')
  485. ->fields(array('confirmation_format' => NULL))
  486. ->condition('confirmation', '')
  487. ->condition('confirmation_format', 0)
  488. ->execute();
  489. $existing_formats = db_query("SELECT format FROM {filter_format}")->fetchCol();
  490. $default_format = variable_get('filter_default_format', 1);
  491. // Since Webform may be updated separately from Drupal core, not all format
  492. // names may be numbers when running this update.
  493. $numeric_formats = array();
  494. foreach ($existing_formats as $format_name) {
  495. if (is_numeric($format_name)) {
  496. $numeric_formats[] = (int) $format_name;
  497. }
  498. }
  499. $query = db_update('webform')
  500. ->fields(array('confirmation_format' => $default_format))
  501. ->isNotNull('confirmation_format');
  502. if (!empty($numeric_formats)) {
  503. $query->condition('confirmation_format', $numeric_formats, 'NOT IN');
  504. }
  505. $query->execute();
  506. }
  507. /**
  508. * Add columns for e-mail HTML and attachment settings.
  509. */
  510. function webform_update_7302() {
  511. if (!db_field_exists('webform_emails', 'html')) {
  512. db_add_field('webform_emails', 'html', array('type' => 'int', 'size' => 'tiny', 'unsigned' => TRUE, 'default' => 0, 'not null' => TRUE));
  513. db_add_field('webform_emails', 'attachments', array('type' => 'int', 'size' => 'tiny', 'unsigned' => TRUE, 'default' => 0, 'not null' => TRUE));
  514. }
  515. }
  516. /**
  517. * Set the default for the "submit_notice" column to 1.
  518. */
  519. function webform_update_7303() {
  520. db_change_field('webform', 'submit_notice', 'submit_notice', array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 1));
  521. }
  522. /**
  523. * Add field for block feature and redirection setting.
  524. */
  525. function webform_update_7304() {
  526. if (!db_field_exists('webform', 'block')) {
  527. db_add_field('webform', 'block', array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 0));
  528. db_change_field('webform', 'redirect_url', 'redirect_url', array('type' => 'varchar', 'length' => 255, 'default' => '<confirmation>'));
  529. db_update('webform')
  530. ->fields(array('redirect_url' => 'confirmation'))
  531. ->condition('redirect_url', '')
  532. ->execute();
  533. }
  534. }
  535. /**
  536. * Set additional_validate and additional_submit columns to allow NULL.
  537. */
  538. function webform_update_7305() {
  539. if (db_field_exists('webform', 'additional_validate')) {
  540. db_change_field('webform', 'additional_validate', 'additional_validate', array('type' => 'text', 'not null' => FALSE));
  541. db_change_field('webform', 'additional_submit', 'additional_submit', array('type' => 'text', 'not null' => FALSE));
  542. }
  543. }
  544. /**
  545. * Add column for webform status (open or closed).
  546. */
  547. function webform_update_7306() {
  548. if (!db_field_exists('webform', 'status')) {
  549. db_add_field('webform', 'status', array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 1));
  550. }
  551. }
  552. /**
  553. * Update the confirmation_format column for default text format changes.
  554. */
  555. function webform_update_7307() {
  556. // Update removed and moved to webform_update_7301().
  557. // See http://drupal.org/node/976102.
  558. }
  559. /**
  560. * Update the confirmation_format column to allow it to store strings.
  561. */
  562. function webform_update_7308() {
  563. db_change_field('webform', 'confirmation_format', 'confirmation_format', array(
  564. 'description' => 'The {filter_format}.format of the confirmation message.',
  565. 'type' => 'varchar',
  566. 'length' => 255,
  567. 'not null' => FALSE,
  568. ));
  569. }
  570. /**
  571. * Add the ability to auto-save as draft between pages.
  572. */
  573. function webform_update_7309() {
  574. if (!db_field_exists('webform', 'auto_save')) {
  575. db_add_field('webform', 'auto_save', array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 0));
  576. }
  577. }
  578. /**
  579. * Remove orphaned and unnecessary rows in the webform table.
  580. */
  581. function webform_update_7310() {
  582. $result = db_query("SELECT nid FROM {webform} WHERE
  583. nid NOT IN
  584. (SELECT DISTINCT(w1.nid) FROM {webform} w1 INNER JOIN {webform_component} wc ON w1.nid = wc.nid)
  585. AND nid NOT IN
  586. (SELECT w2.nid FROM {webform} w2 INNER JOIN {node} n ON w2.nid = n.nid WHERE n.type = 'webform')"
  587. );
  588. $empty_nids = array();
  589. foreach ($result as $row) {
  590. $empty_nids[] = $row->nid;
  591. }
  592. if (!empty($empty_nids)) {
  593. db_delete('webform')->condition('nid', $empty_nids, 'IN')->execute();
  594. }
  595. }
  596. /**
  597. * Add an index for nid_uid_sid to webform_submissions.
  598. */
  599. function webform_update_7311() {
  600. if (!db_index_exists('webform_submissions', 'nid_uid_sid')) {
  601. db_add_index('webform_submissions', 'nid_uid_sid', array('nid', 'uid', 'sid'));
  602. }
  603. }
  604. /**
  605. * Remove unused Webform variables.
  606. */
  607. function webform_update_7312() {
  608. variable_del('node_types');
  609. variable_del('components');
  610. }
  611. /**
  612. * Convert the Date component start and end year options to start and end date.
  613. */
  614. function webform_update_7313() {
  615. $result = db_select('webform_component', 'wc', array('fetch' => PDO::FETCH_ASSOC))
  616. ->fields('wc')
  617. ->condition('type', 'date')
  618. ->execute();
  619. foreach ($result as $component) {
  620. $component['extra'] = unserialize($component['extra']);
  621. if (!isset($component['extra']['start_date']) && !isset($component['end_date'])) {
  622. foreach (array('year_start' => 'start_date', 'year_end' => 'end_date') as $key => $replacement) {
  623. $value = isset($component['extra'][$key]) ? trim($component['extra'][$key]) : '';
  624. // Relative years.
  625. if (preg_match('/[-+][ ]*[0-9]+/', $value)) {
  626. $component['extra'][$replacement] = ($value == 1) ? ($value . ' year') : ($value . ' years');
  627. }
  628. // Absolute years.
  629. elseif (is_numeric($value)) {
  630. $component['extra'][$replacement] = 'Dec 31 ' . $value;
  631. }
  632. unset($component['extra'][$key]);
  633. }
  634. $component['extra'] = serialize($component['extra']);
  635. drupal_write_record('webform_component', $component, array('nid', 'cid'));
  636. }
  637. }
  638. }
  639. /**
  640. * Add webform_last_download table to store last downloaded sid per user.
  641. */
  642. function webform_update_7314() {
  643. // Safety check to prevent recreating the webform_last_download table.
  644. if (db_table_exists('webform_last_download')) {
  645. return;
  646. }
  647. $schema['webform_last_download'] = array(
  648. 'description' => 'Stores last submission number per user download.',
  649. 'fields' => array(
  650. 'nid' => array(
  651. 'description' => 'The node identifier of a webform.',
  652. 'type' => 'int',
  653. 'unsigned' => TRUE,
  654. 'not null' => TRUE,
  655. 'default' => 0,
  656. ),
  657. 'uid' => array(
  658. 'description' => 'The user identifier.',
  659. 'type' => 'int',
  660. 'unsigned' => TRUE,
  661. 'not null' => TRUE,
  662. 'default' => 0,
  663. ),
  664. 'sid' => array(
  665. 'description' => 'The last downloaded submission number.',
  666. 'type' => 'int',
  667. 'unsigned' => TRUE,
  668. 'not null' => TRUE,
  669. 'default' => 0,
  670. ),
  671. ),
  672. 'primary key' => array('nid', 'uid'),
  673. );
  674. db_create_table('webform_last_download', $schema['webform_last_download']);
  675. }
  676. /**
  677. * Add column for timestamp of last requested CSV download.
  678. */
  679. function webform_update_7315() {
  680. if (!db_field_exists('webform_last_download', 'requested')) {
  681. db_add_field('webform_last_download', 'requested', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0,));
  682. }
  683. }
  684. /**
  685. * Add additional columns for total submission limit.
  686. */
  687. function webform_update_7316() {
  688. if (!db_field_exists('webform', 'total_submit_limit')) {
  689. db_add_field('webform', 'total_submit_limit', array('type' => 'int', 'not null' => TRUE, 'default' => -1));
  690. }
  691. if (!db_field_exists('webform', 'total_submit_interval')) {
  692. db_add_field('webform', 'total_submit_interval', array('type' => 'int', 'not null' => TRUE, 'default' => -1));
  693. }
  694. }
  695. /**
  696. * Add an index for 'nid_sid' to webform_submissions.
  697. */
  698. function webform_update_7317() {
  699. // Even though we already have an index 'nid_uid_sid', adding the index for
  700. // 'nid_sid' saves us a tablesort on the node/x/webform-results page.
  701. if (!db_index_exists('webform_submissions', 'nid_sid')) {
  702. db_add_index('webform_submissions', 'nid_sid', array('nid', 'sid'));
  703. }
  704. }
  705. /**
  706. * Upgrade file components to support the new AJAX-upload element.
  707. */
  708. function webform_update_7318() {
  709. $result = db_select('webform_component', 'wc', array('fetch' => PDO::FETCH_ASSOC))
  710. ->fields('wc')
  711. ->condition('type', 'file')
  712. ->execute();
  713. foreach ($result as $component) {
  714. $component['extra'] = unserialize($component['extra']);
  715. if (!isset($component['extra']['directory'])) {
  716. $component['extra']['directory'] = $component['extra']['savelocation'];
  717. $component['extra']['scheme'] = file_default_scheme();
  718. $component['extra']['filtering']['size'] = $component['extra']['filtering']['size'] . ' KB';
  719. unset($component['extra']['savelocation']);
  720. $component['extra'] = serialize($component['extra']);
  721. drupal_write_record('webform_component', $component, array('nid', 'cid'));
  722. }
  723. }
  724. return t('File components updated to support AJAX uploading.');
  725. }
  726. /**
  727. * Add file usage entries for all files uploaded through Webform.
  728. */
  729. function webform_update_7319(&$sandbox) {
  730. if (!isset($sandbox['progress'])) {
  731. // Initialize batch update information.
  732. $sandbox['progress'] = 0;
  733. $sandbox['last_fid_processed'] = -1;
  734. $sandbox['max'] = db_select('file_managed')
  735. ->condition('uri', '%' . db_like('://webform/') . '%', 'LIKE')
  736. ->countQuery()
  737. ->execute()
  738. ->fetchField();
  739. }
  740. // Process all files attached to a given revision during the same batch.
  741. $limit = variable_get('webform_update_batch_size', 100);
  742. $files = db_select('file_managed', 'f')
  743. ->fields('f')
  744. ->condition('uri', '%' . db_like('://webform/') . '%', 'LIKE')
  745. ->condition('fid', $sandbox['last_fid_processed'], '>')
  746. ->orderBy('fid', 'ASC')
  747. ->range(0, $limit)
  748. ->execute()
  749. ->fetchAllAssoc('fid', PDO::FETCH_ASSOC);
  750. // Determine each submission with which a file is associated.
  751. if (!empty($files)) {
  752. foreach ($files as $fid => $file) {
  753. $file = (object) $file;
  754. $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);
  755. foreach ($sids as $sid => $row) {
  756. // We use a db_merge() instead of file_usage_add() to prevent problems
  757. // in the event this update was run twice. No file provided by Webform
  758. // should ever be in use more than once at this point.
  759. db_merge('file_usage')
  760. ->key(array(
  761. 'fid' => $file->fid,
  762. 'type' => 'submission',
  763. 'module' => 'webform',
  764. 'id' => $sid,
  765. ))
  766. ->fields(array(
  767. 'count' => 1,
  768. ))
  769. ->execute();
  770. }
  771. // Update our progress information for the batch update.
  772. $sandbox['progress']++;
  773. $sandbox['last_fid_processed'] = $file->fid;
  774. }
  775. }
  776. // If less than limit was processed, the update process is finished.
  777. if (count($files) < $limit || $sandbox['progress'] == $sandbox['max']) {
  778. $finished = TRUE;
  779. }
  780. // If there's no max value then there's nothing to update and we're finished.
  781. if (empty($sandbox['max']) || isset($finished)) {
  782. return t('Webform file entries created in the file_usage table.');
  783. }
  784. else {
  785. // Indicate our current progress to the batch update system.
  786. $sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
  787. }
  788. }
  789. /**
  790. * Mark files uploaded through Webform that report active usage permanent.
  791. */
  792. function webform_update_7320() {
  793. db_query("UPDATE {file_managed} SET status = 1 WHERE fid IN (SELECT fid FROM {file_usage} WHERE module = :module_name)", array(':module_name' => 'webform'));
  794. }
  795. /**
  796. * Remove files left over from deleted submissions. Such files are now deleted
  797. * automatically.
  798. */
  799. function webform_update_7321() {
  800. module_load_include('inc', 'webform', 'components/file');
  801. $fids = db_query("SELECT fid FROM {file_usage} WHERE module = 'webform' AND type = 'submission' AND NOT id IN(SELECT sid FROM {webform_submissions})")->fetchCol();
  802. foreach ($fids as $fid) {
  803. _webform_delete_file(NULL, array($fid));
  804. }
  805. }
  806. /**
  807. * Add index on {webform_submitted_data}.data.
  808. */
  809. function webform_update_7322() {
  810. db_add_index('webform_submitted_data', 'data', array(array('data', 64)));
  811. }