simplenews.install 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the simplenews module
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function simplenews_schema() {
  10. $schema['simplenews_category'] = array(
  11. 'description' => 'Simplenews newsletter categories.',
  12. 'fields' => array(
  13. 'tid' => array(
  14. 'description' => '{taxonomy_term_data}.tid used as newsletter category.',
  15. 'type' => 'int',
  16. 'not null' => TRUE,
  17. 'default' => 0,
  18. ),
  19. 'format' => array(
  20. 'type' => 'varchar',
  21. 'length' => 8,
  22. 'not null' => TRUE,
  23. 'default' => '',
  24. 'description' => 'Format of the newsletter email (plain, html).',
  25. ),
  26. 'priority' => array(
  27. 'type' => 'int',
  28. 'unsigned' => TRUE,
  29. 'not null' => TRUE,
  30. 'default' => 0,
  31. 'size' => 'tiny',
  32. 'description' => 'Email priority according to RFC 2156 and RFC 5231 (0 = none; 1 = highest; 2 = high; 3 = normal; 4 = low; 5 = lowest).',
  33. ),
  34. 'receipt' => array(
  35. 'type' => 'int',
  36. 'unsigned' => TRUE,
  37. 'not null' => TRUE,
  38. 'default' => 0,
  39. 'size' => 'tiny',
  40. 'description' => 'Boolean indicating request for email receipt confirmation according to RFC 2822.',
  41. ),
  42. 'from_name' => array(
  43. 'type' => 'varchar',
  44. 'length' => 64,
  45. 'not null' => TRUE,
  46. 'default' => '',
  47. 'description' => 'Sender name for newsletter emails.',
  48. ),
  49. 'email_subject' => array(
  50. 'type' => 'varchar',
  51. 'length' => 255,
  52. 'not null' => TRUE,
  53. 'default' => '',
  54. 'description' => 'Subject of newsletter email. May contain tokens.',
  55. ),
  56. 'from_address' => array(
  57. 'type' => 'varchar',
  58. 'length' => 64,
  59. 'not null' => TRUE,
  60. 'default' => '',
  61. 'description' => 'Sender address for newsletter emails',
  62. ),
  63. 'hyperlinks' => array(
  64. 'type' => 'int',
  65. 'size' => 'tiny',
  66. 'not null' => TRUE,
  67. 'default' => 0,
  68. 'description' => 'Flag indicating type of hyperlink conversion (1 = hyperlinks are in-line; 0 = hyperlinks are placed at email bottom).',
  69. ),
  70. 'new_account' => array(
  71. 'type' => 'varchar',
  72. 'length' => 12,
  73. 'not null' => TRUE,
  74. 'default' => '',
  75. 'description' => 'How to treat subscription at account creation (none = None; on = Default on; off = Default off; silent = Invisible subscription).',
  76. ),
  77. 'opt_inout' => array(
  78. 'type' => 'varchar',
  79. 'length' => 12,
  80. 'not null' => TRUE,
  81. 'default' => '',
  82. 'description' => 'How to treat subscription confirmation (hidden = Newsletter is hidden from the user; single = Single opt-in; double = Double opt-in).',
  83. ),
  84. 'block' => array(
  85. 'description' => 'For this category a subscription block is available.',
  86. 'type' => 'int',
  87. 'size' => 'tiny',
  88. 'not null' => TRUE,
  89. 'default' => 0,
  90. ),
  91. ),
  92. 'primary key' => array('tid'),
  93. );
  94. $schema['simplenews_newsletter'] = array(
  95. 'description' => 'Simplenews newsletter data.',
  96. 'fields' => array(
  97. 'nid' => array(
  98. 'description' => '{node} that is used as newsletter.',
  99. 'type' => 'int',
  100. 'not null' => TRUE,
  101. 'default' => 0,
  102. ),
  103. 'tid' => array(
  104. 'description' => 'The newsletter category {simplenews_category}.tid this newsletter belongs to.',
  105. 'type' => 'int',
  106. 'not null' => TRUE,
  107. 'default' => 0,
  108. ),
  109. 'status' => array(
  110. 'description' => 'sent status of the newsletter issue (0 = not sent; 1 = pending; 2 = sent, 3 = send on publish).',
  111. 'type' => 'int',
  112. 'size' => 'tiny',
  113. 'not null' => TRUE,
  114. 'default' => 0,
  115. ),
  116. 'sent_subscriber_count' => array(
  117. 'description' => 'The count of subscribers to the newsletter when it was sent.',
  118. 'type' => 'int',
  119. 'not null' => TRUE,
  120. 'default' => 0,
  121. ),
  122. ),
  123. 'primary key' => array('nid'),
  124. 'foreign keys' => array(
  125. 'nid' => array(
  126. 'table' => 'node',
  127. 'columns' => array('nid' => 'nid'),
  128. ),
  129. ),
  130. );
  131. $schema['simplenews_subscriber'] = array(
  132. 'description' => 'Subscribers to {simplenews_category}. Many-to-many relation via {simplenews_subscription}',
  133. 'fields' => array(
  134. 'snid' => array(
  135. 'description' => 'Primary key: Unique subscriber ID.',
  136. 'type' => 'serial',
  137. 'not null' => TRUE,
  138. ),
  139. 'activated' => array(
  140. 'description' => 'Boolean indicating the status of the subscription.',
  141. 'type' => 'int',
  142. 'size' => 'tiny',
  143. 'not null' => TRUE,
  144. 'default' => 0,
  145. ),
  146. 'mail' => array(
  147. 'description' => "The subscriber's email address.",
  148. 'type' => 'varchar',
  149. 'length' => 64,
  150. 'not null' => TRUE,
  151. 'default' => '',
  152. ),
  153. 'uid' => array(
  154. 'description' => 'The {users}.uid that has the same email address.',
  155. 'type' => 'int',
  156. 'not null' => TRUE,
  157. 'default' => 0,
  158. ),
  159. 'language' => array(
  160. 'type' => 'varchar',
  161. 'length' => 12,
  162. 'not null' => TRUE,
  163. 'default' => '',
  164. 'description' => 'Subscriber preferred language.',
  165. ),
  166. 'timestamp' => array(
  167. 'description' => 'UNIX timestamp of when the user is (un)subscribed.',
  168. 'type' => 'int',
  169. 'unsigned' => TRUE,
  170. 'not null' => TRUE,
  171. 'default' => 0,
  172. ),
  173. 'changes' => array(
  174. 'description' => 'Contains the requested subscription changes',
  175. 'type' => 'text',
  176. 'serialize' => TRUE,
  177. ),
  178. ),
  179. 'primary key' => array('snid'),
  180. 'indexes' => array(
  181. 'mail' => array('mail'),
  182. 'uid' => array('uid'),
  183. ),
  184. 'foreign keys' => array(
  185. 'uid' => array(
  186. 'table' => 'users',
  187. 'columns' => array('uid' => 'uid'),
  188. ),
  189. ),
  190. );
  191. $schema['simplenews_subscription'] = array(
  192. 'description' => 'Newsletter subscription data. Which subscriber is subscribed to which mailing list.',
  193. 'fields' => array(
  194. 'snid' => array(
  195. 'description' => 'The {simplenews_subscriber}.snid who is subscribed.',
  196. 'type' => 'int',
  197. 'not null' => TRUE,
  198. 'default' => 0,
  199. ),
  200. 'tid' => array(
  201. 'description' => 'The category ({simplenews_category}.tid) the subscriber is subscribed to.',
  202. 'type' => 'int',
  203. 'not null' => TRUE,
  204. 'default' => 0,
  205. ),
  206. 'status' => array(
  207. 'description' => 'A flag indicating whether the user is subscribed (1) or unsubscribed (0).',
  208. 'type' => 'int',
  209. 'size' => 'tiny',
  210. 'not null' => TRUE,
  211. 'default' => 1,
  212. ),
  213. 'timestamp' => array(
  214. 'description' => 'UNIX timestamp of when the user is (un)subscribed.',
  215. 'type' => 'int',
  216. 'unsigned' => TRUE,
  217. 'not null' => TRUE,
  218. 'default' => 0,
  219. ),
  220. 'source' => array(
  221. 'description' => 'The source via which the user is (un)subscription.',
  222. 'type' => 'varchar',
  223. 'length' => 24,
  224. 'not null' => TRUE,
  225. 'default' => '',
  226. ),
  227. ),
  228. 'primary key' => array('snid', 'tid'),
  229. 'foreign keys' => array(
  230. 'snid' => array(
  231. 'table' => 'simplenews_subscriber',
  232. 'columns' => array('snid' => 'snid'),
  233. ),
  234. 'tid' => array(
  235. 'table' => 'simplenews_category',
  236. 'columns' => array('tid' => 'tid'),
  237. )
  238. ),
  239. // @todo add foreign keys to other tables too?
  240. );
  241. $schema['simplenews_mail_spool'] = array(
  242. 'description' => 'Spool for temporary storage of newsletter emails.',
  243. 'fields' => array(
  244. 'msid' => array(
  245. 'description' => 'The primary identifier for a mail spool record.',
  246. 'type' => 'serial',
  247. 'unsigned' => TRUE,
  248. 'not null' => TRUE,
  249. ),
  250. 'mail' => array(
  251. 'description' => 'The formatted email address of mail message recipient.',
  252. 'type' => 'varchar',
  253. 'length' => 255,
  254. 'not null' => TRUE,
  255. 'default' => '',
  256. ),
  257. 'nid' => array(
  258. 'description' => 'The {node}.nid of this newsletter.',
  259. 'type' => 'int',
  260. 'not null' => TRUE,
  261. 'default' => 0,
  262. ),
  263. 'tid' => array(
  264. 'description' => 'The {simplenews_category}.tid this newsletter belongs to.',
  265. 'type' => 'int',
  266. 'not null' => TRUE,
  267. 'default' => 0,
  268. ),
  269. 'status' => array(
  270. 'description' => 'The sent status of the email (0 = hold, 1 = pending, 2 = done).',
  271. 'type' => 'int',
  272. 'unsigned' => TRUE,
  273. 'not null' => TRUE,
  274. 'default' => 0,
  275. 'size' => 'tiny',
  276. ),
  277. 'error' => array(
  278. 'description' => 'A boolean indicating whether an error occured while sending the email.',
  279. 'type' => 'int',
  280. 'size' => 'tiny',
  281. 'not null' => TRUE,
  282. 'default' => 0,
  283. ),
  284. 'timestamp' => array(
  285. 'description' => 'The time status was set or changed.',
  286. 'type' => 'int',
  287. 'unsigned' => TRUE,
  288. 'not null' => TRUE,
  289. 'default' => 0,
  290. ),
  291. 'data' => array(
  292. 'type' => 'text',
  293. 'not null' => FALSE,
  294. 'size' => 'big',
  295. 'serialize' => TRUE,
  296. 'description' => 'A serialized array of name value pairs that are related to the email address.',
  297. ),
  298. 'snid' => array(
  299. 'description' => 'Foreign key for subscriber table ({simplenews_subscriptions}.snid)',
  300. 'type' => 'int',
  301. 'not null' => TRUE,
  302. 'default' => 0,
  303. ),
  304. ),
  305. 'primary key' => array('msid'),
  306. 'indexes' => array(
  307. 'tid' => array('tid'),
  308. 'status' => array('status'),
  309. 'snid_tid' => array('snid', 'tid'),
  310. ),
  311. 'foreign keys' => array(
  312. 'nid' => array(
  313. 'table' => 'node',
  314. 'columns' => array('nid' => 'nid'),
  315. ),
  316. 'tid' => array(
  317. 'table' => 'simplenews_category',
  318. 'columns' => array('tid'),
  319. ),
  320. 'snid_tid' => array(
  321. 'table' => 'simplenews_subscription',
  322. 'columns' => array(
  323. 'snid' => 'snid',
  324. 'tid' => 'tid',
  325. ),
  326. ),
  327. ),
  328. );
  329. return $schema;
  330. }
  331. /**
  332. * Implements hook_install().
  333. */
  334. function simplenews_install() {
  335. _simplenews_init_simplenews_vocabulary();
  336. _simplenews_init_simplenews_category();
  337. // add nodetype with newsletter vocabulary
  338. _simplenews_install_nodetype();
  339. }
  340. /**
  341. * Implements hook_uninstall().
  342. */
  343. function simplenews_uninstall() {
  344. db_query("DELETE FROM {variable} WHERE name LIKE 'simplenews_%%'");
  345. }
  346. /**
  347. * Create simplenews node type.
  348. */
  349. function _simplenews_install_nodetype() {
  350. // Create a newsletter type if needed.
  351. $type = node_type_get_type('simplenews');
  352. if (!$type) {
  353. $type = node_type_set_defaults(array(
  354. 'type' => 'simplenews',
  355. 'name' => t('Simplenews newsletter'),
  356. 'base' => 'node_content',
  357. 'description' => t('A newsletter issue to be sent to subscribed email addresses.'),
  358. 'locked' => 0,
  359. 'custom' => 1,
  360. 'modified' => 1,
  361. ));
  362. node_type_save($type);
  363. node_add_body_field($type);
  364. }
  365. if (!field_info_instance('node', 'field_simplenews_term', $type->type)) {
  366. simplenews_add_term_field($type);
  367. }
  368. variable_set('simplenews_content_type_' . $type->type, TRUE);
  369. }
  370. /**
  371. * Create simplenews vocabulary and initial term.
  372. */
  373. function _simplenews_init_simplenews_vocabulary() {
  374. // Create the simplenews vocabulary. If it exists, set simplenews_vid variable.
  375. $vocabularies = taxonomy_vocabulary_load_multiple(array(), array('machine_name' => 'newsletter'));
  376. $vocabulary = reset($vocabularies);
  377. if (!$vocabulary) {
  378. $vocabulary = new stdClass();
  379. $vocabulary->name = t('Newsletter');
  380. $vocabulary->machine_name = 'newsletter';
  381. $vocabulary->description = t('Simplenews newsletter categories.');
  382. $vocabulary->weight = '0';
  383. $vocabulary->hierarchy = '0';
  384. $vocabulary->module = 'simplenews';
  385. }
  386. taxonomy_vocabulary_save($vocabulary);
  387. variable_set('simplenews_vid', $vocabulary->vid);
  388. // Create a newsletter taxonomy term if none exists.
  389. $tree = taxonomy_get_tree($vocabulary->vid);
  390. if (count($tree) == 0) {
  391. $term = new stdClass();
  392. $term->name = t('@site_name newsletter', array('@site_name' => variable_get('site_name', 'Drupal')));
  393. $term->description = t('@site_name newsletter categories.', array('@site_name' => variable_get('site_name', 'Drupal')));
  394. $term->vid = $vocabulary->vid;
  395. taxonomy_term_save($term);
  396. }
  397. }
  398. /**
  399. * Create initial simplenews categories.
  400. */
  401. function _simplenews_init_simplenews_category() {
  402. if ($tree = taxonomy_get_tree(variable_get('simplenews_vid', ''))) {
  403. $categories = simplenews_categories_load_multiple();
  404. $first = TRUE;
  405. foreach ($tree as $term) {
  406. // Create a newsletter category for each newsletter taxonomy term.
  407. if (!isset($categories[$term->tid])) {
  408. $category = new stdClass();
  409. $category->tid = $term->tid;
  410. // @todo use a function for category default values
  411. $category->from_name = variable_get('site_name', 'Drupal');
  412. $category->email_subject ='[[simplenews-category:name]] [node:title]';
  413. $category->from_address = variable_get('site_mail', ini_get('sendmail_from'));
  414. $category->format = 'plain';
  415. $category->priority = SIMPLENEWS_PRIORITY_NONE;
  416. $category->receipt = 0;
  417. $category->hyperlinks = 1;
  418. $category->new_account = 'none';
  419. $category->opt_inout = 'double';
  420. $category->block = 0;
  421. if ($first) {
  422. $category->block = 1;
  423. $first = FALSE;
  424. }
  425. simplenews_category_save($category);
  426. }
  427. }
  428. }
  429. }
  430. /**
  431. * Helper function to get all activated simplenews blocks
  432. *
  433. * @return Keyed array of simplenews blocks.
  434. */
  435. function _simplenews_get_blocks() {
  436. $query = db_select('block', 'b');
  437. $result = $query
  438. ->fields('b', array('delta'))
  439. ->condition('b.status', 1)
  440. ->condition('b.module', 'simplenews')
  441. ->execute();
  442. return $result->fetchAllAssoc('delta');
  443. }
  444. /**
  445. * Implements hook_update_last_removed().
  446. */
  447. function simplenews_update_last_removed() {
  448. // Support upgrades from 6.x-1.x and 6.x-2.x.
  449. return 6101;
  450. }
  451. /**
  452. * Implements hook_update_dependencies().
  453. */
  454. function simplenews_update_dependencies() {
  455. // Make sure that the taxonomy upgrade is run first.
  456. $dependencies['simplenews'][7000] = array(
  457. 'taxonomy' => 7010,
  458. );
  459. return $dependencies;
  460. }
  461. /**
  462. * Helper function to convert tokens in variables to D7 format.
  463. */
  464. function _simplenews_convert_tokens_in_variable($variables) {
  465. if (!is_array($variables)) {
  466. $variables = array($variables);
  467. }
  468. $old = array('[site-name]', '[user-mail]', '[site-url]/user', '[site-url]', '[simplenews-subscribe-url]', '[simplenews-unsubscribe-url]', '[simplenews-newsletter-url]', '[simplenews-newsletters-name]', '[simplenews-newsletters-url]', '[simplenews-receiver-mail]');
  469. $new = array('[site:name]', '[user:mail]', '[site:login-url]', '[site:url]', '[simplenews-subscriber:subscribe-url]', '[simplenews-subscriber:unsubscribe-url]', '[simplenews-newsletter:url]', '[simplenews-list:name]', '[simplenews-list:url]', '[simplenews-subscriber:mail]');
  470. foreach ($variables as $variable) {
  471. if ($text = variable_get($variable, FALSE)) {
  472. $text = str_replace($old, $new, $text);
  473. variable_set($variable, $text);
  474. }
  475. }
  476. }
  477. /**
  478. * Create table {simplenews_category} to replace taxonomy terms.
  479. * Migrate Newsletter taxonomy data to Newsletter categories.
  480. *
  481. * Rename table simplenews_subscriptions to simplenews_subscriber.
  482. * Rename table simplenews_newsletters to simplenews_newsletter.
  483. * Drop fields {simplenews_newsletter}.s_format, .priority and .receipt.
  484. *
  485. * Rename table simplenews_snid_tid to simplenews_subscription.
  486. *
  487. * Delete deprecated simplenews variables.
  488. */
  489. function simplenews_update_7000() {
  490. // Convert tokens in variables to D7 format.
  491. $variables = array('simplenews_confirm_subscribe_subject', 'simplenews_confirm_subscribe_unsubscribed', 'simplenews_confirm_subscribe_subscribed', 'simplenews_confirm_unsubscribe_subscribed', 'simplenews_confirm_unsubscribe_unsubscribed');
  492. _simplenews_convert_tokens_in_variable($variables);
  493. // Create table 'simplenews_category'.
  494. $schema['simplenews_category'] = array(
  495. 'description' => 'Simplenews newsletter categories.',
  496. 'fields' => array(
  497. 'tid' => array(
  498. 'description' => '{taxonomy_term_data}.tid used as newsletter category.',
  499. 'type' => 'int',
  500. 'not null' => TRUE,
  501. 'default' => 0,
  502. ),
  503. 'format' => array(
  504. 'description' => 'Format of the newsletter email (plain, html).',
  505. 'type' => 'varchar',
  506. 'length' => 8,
  507. 'not null' => TRUE,
  508. 'default' => '',
  509. ),
  510. 'priority' => array(
  511. 'description' => 'Email priority according to RFC 2156 and RFC 5231 (0 = none; 1 = highest; 2 = high; 3 = normal; 4 = low; 5 = lowest).',
  512. 'type' => 'int',
  513. 'unsigned' => TRUE,
  514. 'not null' => TRUE,
  515. 'default' => 0,
  516. 'size' => 'tiny',
  517. ),
  518. 'receipt' => array(
  519. 'description' => 'Boolean indicating request for email receipt confirmation according to RFC 2822.',
  520. 'type' => 'int',
  521. 'unsigned' => TRUE,
  522. 'not null' => TRUE,
  523. 'default' => 0,
  524. 'size' => 'tiny',
  525. ),
  526. 'from_name' => array(
  527. 'description' => 'Sender name for newsletter emails.',
  528. 'type' => 'varchar',
  529. 'length' => 64,
  530. 'not null' => TRUE,
  531. 'default' => '',
  532. ),
  533. 'email_subject' => array(
  534. 'description' => 'Subject of newsletter email. May contain tokens.',
  535. 'type' => 'varchar',
  536. 'length' => 255,
  537. 'not null' => TRUE,
  538. 'default' => '',
  539. ),
  540. 'from_address' => array(
  541. 'description' => 'Sender address for newsletter emails',
  542. 'type' => 'varchar',
  543. 'length' => 64,
  544. 'not null' => TRUE,
  545. 'default' => '',
  546. ),
  547. 'hyperlinks' => array(
  548. 'description' => 'Flag indicating type of hyperlink conversion (1 = hyperlinks are in-line; 0 = hyperlinks are placed at email bottom).',
  549. 'type' => 'int',
  550. 'size' => 'tiny',
  551. 'not null' => TRUE,
  552. 'default' => 0,
  553. ),
  554. 'new_account' => array(
  555. 'description' => 'How to treat subscription at account creation (none = None; on = Default on; off = Default off; silent = Invisible subscription).',
  556. 'type' => 'varchar',
  557. 'length' => 12,
  558. 'not null' => TRUE,
  559. 'default' => '',
  560. ),
  561. 'opt_inout' => array(
  562. 'description' => 'How to treat subscription confirmation (hidden = Newsletter is hidden from the user; single = Single opt-in; double = Double opt-in).',
  563. 'type' => 'varchar',
  564. 'length' => 12,
  565. 'not null' => TRUE,
  566. 'default' => '',
  567. ),
  568. 'block' => array(
  569. 'description' => 'For this category a subscription block is available.',
  570. 'type' => 'int',
  571. 'size' => 'tiny',
  572. 'not null' => TRUE,
  573. 'default' => 0,
  574. ),
  575. ),
  576. 'primary key' => array('tid'),
  577. );
  578. db_create_table('simplenews_category', $schema['simplenews_category']);
  579. // Migrate Newsletter taxonomy data to Newsletter categories.
  580. // Query the database directly, to avoid triggering our own hooks.
  581. $tids = db_query('SELECT tid FROM {taxonomy_term_data} where vid = :vid', array(':vid' => variable_get('simplenews_vid', '')))->fetchCol();
  582. // @todo Check if simplenews blocks are still active after core update.
  583. // If not, there is no purpose in migrating the block status ('block' => 0).
  584. $blocks = _simplenews_get_blocks();
  585. foreach ($tids as $tid) {
  586. _simplenews_convert_tokens_in_variable('simplenews_email_subject_' . $tid);
  587. db_insert('simplenews_category')
  588. ->fields(array(
  589. 'tid' => $tid,
  590. 'format' => 'plain',
  591. 'priority' => '0',
  592. 'receipt' => '0',
  593. 'from_name' => variable_get('simplenews_from_name_' . $tid, variable_get('simplenews_from_name', variable_get('site_name', 'Drupal'))),
  594. 'email_subject' => variable_get('simplenews_email_subject_' . $tid, '[[simplenews-newsletters-name]] [title-raw]'),
  595. 'from_address' => variable_get('simplenews_from_address_' . $tid, variable_get('simplenews_from_address', variable_get('site_mail', ini_get('sendmail_from')))),
  596. 'hyperlinks' => variable_get('simplenews_hyperlinks_' . $tid, 1),
  597. 'new_account' => variable_get('simplenews_new_account_' . $tid, 'none'),
  598. 'opt_inout' => variable_get('simplenews_opt_inout_' . $tid, 'double'),
  599. 'block' => isset($blocks[$tid]) ? 1 : 0,
  600. ))
  601. ->execute();
  602. }
  603. // Change table simplenews_subscriptions to simplenews_subscriber.
  604. db_rename_table('simplenews_subscriptions', 'simplenews_subscriber');
  605. // Change table simplenews_newsletters to simplenews_newsletter.
  606. // Drop fields: s_format, priority, receipt (moved to simplenews_category).
  607. db_rename_table('simplenews_newsletters', 'simplenews_newsletter');
  608. db_change_field('simplenews_newsletter', 'tid', 'tid', array(
  609. 'description' => 'The newsletter category {simplenews_category}.tid this newsletter belongs to.',
  610. 'type' => 'int',
  611. 'not null' => TRUE,
  612. 'default' => 0,
  613. ));
  614. db_drop_field('simplenews_newsletter', 's_format');
  615. db_drop_field('simplenews_newsletter', 'priority');
  616. db_drop_field('simplenews_newsletter', 'receipt');
  617. // Change table simplenews_snid_tid to simplenews_subscription.
  618. // Change field {simplenews_subscription}.tid description
  619. db_drop_primary_key('simplenews_snid_tid');
  620. db_rename_table('simplenews_snid_tid', 'simplenews_subscription');
  621. // Add {simplenews_mail_spool}.data to store subscriber data.
  622. db_add_field('simplenews_mail_spool', 'data', array(
  623. 'type' => 'text',
  624. 'not null' => FALSE,
  625. 'size' => 'big',
  626. 'serialize' => TRUE,
  627. 'description' => 'A serialized array of name value pairs that are related to the email address.',
  628. ));
  629. // Rename field {simplenews_mail_spool}.s_status to "status".
  630. db_change_field('simplenews_newsletter', 's_status', 'status', array(
  631. 'description' => 'sent status of the newsletter issue (0 = not sent; 1 = pending; 2 = sent). ',
  632. 'type' => 'int',
  633. 'size' => 'tiny',
  634. 'not null' => TRUE,
  635. 'default' => 0,
  636. ));
  637. // Delete deprecated variables.
  638. foreach ($tids as $tid) {
  639. variable_del('simplenews_from_name_' . $tid);
  640. variable_del('simplenews_email_subject_' . $tid);
  641. variable_del('simplenews_from_address_' . $tid);
  642. variable_del('simplenews_hyperlinks_' . $tid);
  643. variable_del('simplenews_new_account_' . $tid);
  644. variable_del('simplenews_opt_inout_' . $tid);
  645. }
  646. // @todo Add return text about checking of Newsletter Category settings.
  647. // @todo Add return text about Block checkboxes
  648. }
  649. /**
  650. * Create key snid_tid on simplenews_mail_spool table.
  651. */
  652. function simplenews_update_7001() {
  653. // Add the {simplenews_mail_spool}.snid field if it doesn't exist yet (added
  654. // in 6.x-2.x).
  655. if (!db_field_exists('simplenews_mail_spool', 'snid')) {
  656. db_add_field('simplenews_mail_spool', 'snid', array(
  657. 'description' => 'Foreign key for subscriber table ({simplenews_subscriptions}.snid)',
  658. 'type' => 'int',
  659. 'not null' => TRUE,
  660. 'default' => 0,
  661. ));
  662. }
  663. if (!db_index_exists('simplenews_mail_spool', 'snid_tid')) {
  664. db_add_index('simplenews_mail_spool', 'snid_tid', array('snid', 'tid'));
  665. }
  666. }
  667. /**
  668. * Drop support for node revisioning.
  669. */
  670. function simplenews_update_7002() {
  671. if (db_field_exists('simplenews_newsletter', 'vid')) {
  672. db_drop_field('simplenews_newsletter', 'vid');
  673. }
  674. if (db_field_exists('simplenews_mail_spool', 'vid')) {
  675. db_drop_field('simplenews_mail_spool', 'vid');
  676. }
  677. }
  678. /**
  679. * [simplenews-newsletter] tokens have been removed in favor of [node] tokens.
  680. */
  681. function simplenews_update_7003() {
  682. drupal_set_message(t('The [simplenews-newsletter] tokens have been removed in favor of [node] tokens. Existing newsletters might need to be updated accordingly.'), 'warning');
  683. }
  684. /**
  685. * Add the status field to {simplenews_subscription}.
  686. */
  687. function simplenews_update_7004() {
  688. if (!db_field_exists('simplenews_subscription', 'status')) {
  689. db_add_field('simplenews_subscription', 'status', array(
  690. 'description' => 'A flag indicating whether the user is subscribed (1) or unsubscribed (0).',
  691. 'type' => 'int',
  692. 'size' => 'small',
  693. 'not null' => TRUE,
  694. 'default' => 1
  695. )
  696. );
  697. }
  698. }
  699. /**
  700. * Add support for combined confirmation mails.
  701. */
  702. function simplenews_update_7005() {
  703. db_add_field('simplenews_subscriber', 'changes', array(
  704. 'description' => 'Contains the requested subscription changes',
  705. 'type' => 'text',
  706. 'serialize' => TRUE,
  707. ));
  708. // To keep existing installations consistent, disable combined confirmation
  709. // mails.
  710. variable_set('simplenews_use_combined', 'never');
  711. }