simplenews_scheduler.install 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * @file
  4. * Install and uninstall functions for the Simplenews Scheduler module.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function simplenews_scheduler_schema() {
  10. $schema['simplenews_scheduler'] = array(
  11. 'description' => 'Scheduled newsletter data.',
  12. 'fields' => array(
  13. 'nid' => array(
  14. 'description' => 'The node id for a scheduled newsletter.',
  15. 'type' => 'int',
  16. 'not null' => TRUE,
  17. 'default' => 0,
  18. ),
  19. 'last_run' => array(
  20. 'description' => 'The timestamp the scheduled newsletter was last sent.',
  21. 'type' => 'int',
  22. 'not null' => TRUE,
  23. 'default' => 0,
  24. ),
  25. 'next_run' => array(
  26. 'description' => 'The future timestamp the next scheduled newsletter is due to be sent.',
  27. 'type' => 'int',
  28. 'not null' => TRUE,
  29. 'default' => 0,
  30. ),
  31. 'activated' => array(
  32. 'description' => 'Whether the schedule is active.',
  33. 'type' => 'int',
  34. 'size' => 'tiny',
  35. 'not null' => TRUE,
  36. 'default' => 0,
  37. ),
  38. 'send_interval' => array(
  39. 'description' => 'The interval at which to send, as a text string.',
  40. 'type' => 'varchar',
  41. 'length' => 10,
  42. ),
  43. 'interval_frequency' => array(
  44. 'description' => 'The number of intervals between newsletter transmission.',
  45. 'type' => 'int',
  46. 'default' => 1,
  47. 'not null' => TRUE,
  48. ),
  49. 'start_date' => array(
  50. 'description' => 'The timestamp at which to start sending editions.',
  51. 'type' => 'int',
  52. 'not null' => TRUE,
  53. ),
  54. 'stop_type' => array(
  55. 'description' => 'How to determine when to stop sending editions.',
  56. 'type' => 'int',
  57. 'not null' => TRUE,
  58. ),
  59. 'stop_date' => array(
  60. 'description' => 'The timestamp at which to stop sending editions.',
  61. 'type' => 'int',
  62. 'not null' => TRUE,
  63. 'default' => 1388447999,
  64. ),
  65. 'stop_edition' => array(
  66. 'description' => 'The edition count at which to stop sending editions.',
  67. 'type' => 'int',
  68. 'not null' => TRUE,
  69. 'default' => 0,
  70. ),
  71. 'php_eval' => array(
  72. 'description' => 'PHP code to evaluate to determine whether to send an edition.',
  73. 'type' => 'blob',
  74. 'not null' => TRUE,
  75. ),
  76. 'title' => array(
  77. 'description' => 'The title of new edition nodes.',
  78. 'type' => 'varchar',
  79. 'length' => 255,
  80. 'not null' => TRUE,
  81. 'default' => '',
  82. ),
  83. ),
  84. 'primary key' => array('nid'),
  85. );
  86. $schema['simplenews_scheduler_editions'] = array(
  87. 'description' => 'Stores data for each edition of a scheduled newsletter.',
  88. 'fields' => array(
  89. 'eid' => array(
  90. 'description' => 'The node id for the edition.',
  91. 'type' => 'int',
  92. 'not null' => TRUE,
  93. 'default' => 0,
  94. ),
  95. 'pid' => array(
  96. 'description' => 'The node id for the parent scheduled newsletter node.',
  97. 'type' => 'int',
  98. 'not null' => TRUE,
  99. 'default' => 0,
  100. ),
  101. 'date_issued' => array(
  102. 'description' => 'The timestamp on which this edition was sent.',
  103. 'type' => 'int',
  104. 'not null' => TRUE,
  105. 'default' => 0,
  106. ),
  107. ),
  108. 'primary key' => array('eid'),
  109. );
  110. return $schema;
  111. }
  112. /**
  113. * Implementation of hook_uninstall().
  114. */
  115. function simplenews_scheduler_uninstall() {
  116. drupal_uninstall_schema('simplenews_scheduler');
  117. drupal_uninstall_schema('simplenews_scheduler_editions');
  118. }
  119. /**
  120. * Implements hook_install().
  121. */
  122. function simplenews_scheduler_install() {
  123. // Update the module weight to run before simplenews.
  124. db_update('system')
  125. ->condition('name', 'simplenews_scheduler')
  126. ->fields(array(
  127. 'weight' => -1,
  128. ))
  129. ->execute();
  130. }
  131. /*
  132. * Implements hook_update_last_removed().
  133. */
  134. function simplenews_scheduler_update_last_removed() {
  135. return 6005;
  136. }
  137. /**
  138. * Add the title field to the scheduler table.
  139. */
  140. function simplenews_scheduler_update_7000() {
  141. if (!db_field_exists('simplenews_scheduler', 'title')) {
  142. $field = array(
  143. 'description' => 'The title of new edition nodes.',
  144. 'type' => 'varchar',
  145. 'length' => 255,
  146. 'not null' => TRUE,
  147. 'default' => '',
  148. 'initial' => '[node:title]',
  149. );
  150. db_add_field('simplenews_scheduler', 'title', $field);
  151. }
  152. }
  153. /**
  154. * Add the next_run field to the scheduler table and populate it.
  155. */
  156. function simplenews_scheduler_update_7001() {
  157. // Only act if the field doesn't exist yet: this accounts for the possibility
  158. // it's been added in a 62xx update.
  159. if (!db_field_exists('simplenews_scheduler', 'next_run')) {
  160. // Add the field.
  161. $field = array(
  162. 'description' => 'The future timestamp the next scheduled newsletter is due to be sent.',
  163. 'type' => 'int',
  164. 'not null' => TRUE,
  165. 'default' => 0,
  166. 'initial' => 0,
  167. );
  168. db_add_field('simplenews_scheduler', 'next_run', $field);
  169. // Populate the new field with each schedule's next run time.
  170. // Retrieve all records into an associative array keyed by nid.
  171. $schedules = db_query("SELECT * FROM {simplenews_scheduler}")->fetchAllAssoc('nid');
  172. foreach ($schedules as $nid => $schedule) {
  173. // Clear last_run to force the next_run calculation to work from the
  174. // start_date. This ensures that any error in previous edition dates due to
  175. // bugs with month length is ignored.
  176. // @see http://drupal.org/node/1364784
  177. $schedule->last_run = 0;
  178. // Get the next run time relative to the request time.
  179. $next_run = simplenews_scheduler_calculate_next_run_time($schedule, REQUEST_TIME);
  180. // Don't use drupal_write_record() in a hook_update_N().
  181. db_update('simplenews_scheduler')
  182. ->fields(array('next_run' => $next_run))
  183. ->condition('nid', $nid)
  184. ->execute();
  185. }
  186. }
  187. }
  188. /**
  189. * Update the module weight if it has not been customized.
  190. */
  191. function simplenews_scheduler_update_7002() {
  192. // Update the module weight to run before simplenews.
  193. db_update('system')
  194. ->condition('name', 'simplenews_scheduler')
  195. // Only change the existing value if it has not been customized.
  196. ->condition('weight', 0)
  197. ->fields(array(
  198. 'weight' => -1,
  199. ))
  200. ->execute();
  201. }