simplenews.install 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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' => 128,
  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. 'created' => array(
  179. 'description' => 'UNIX timestamp of when the subscription record was added.',
  180. 'type' => 'int',
  181. 'unsigned' => TRUE,
  182. 'not null' => TRUE,
  183. 'default' => 0,
  184. ),
  185. ),
  186. 'primary key' => array('snid'),
  187. 'indexes' => array(
  188. 'mail' => array('mail'),
  189. 'uid' => array('uid'),
  190. ),
  191. 'foreign keys' => array(
  192. 'uid' => array(
  193. 'table' => 'users',
  194. 'columns' => array('uid' => 'uid'),
  195. ),
  196. ),
  197. );
  198. $schema['simplenews_subscription'] = array(
  199. 'description' => 'Newsletter subscription data. Which subscriber is subscribed to which mailing list.',
  200. 'fields' => array(
  201. 'snid' => array(
  202. 'description' => 'The {simplenews_subscriber}.snid who is subscribed.',
  203. 'type' => 'int',
  204. 'not null' => TRUE,
  205. 'default' => 0,
  206. ),
  207. 'tid' => array(
  208. 'description' => 'The category ({simplenews_category}.tid) the subscriber is subscribed to.',
  209. 'type' => 'int',
  210. 'not null' => TRUE,
  211. 'default' => 0,
  212. ),
  213. 'status' => array(
  214. 'description' => 'A flag indicating whether the user is subscribed (1) or unsubscribed (0).',
  215. 'type' => 'int',
  216. 'size' => 'tiny',
  217. 'not null' => TRUE,
  218. 'default' => 1,
  219. ),
  220. 'timestamp' => array(
  221. 'description' => 'UNIX timestamp of when the user is (un)subscribed.',
  222. 'type' => 'int',
  223. 'unsigned' => TRUE,
  224. 'not null' => TRUE,
  225. 'default' => 0,
  226. ),
  227. 'source' => array(
  228. 'description' => 'The source via which the user is (un)subscription.',
  229. 'type' => 'varchar',
  230. 'length' => 24,
  231. 'not null' => TRUE,
  232. 'default' => '',
  233. ),
  234. ),
  235. 'primary key' => array('snid', 'tid'),
  236. 'foreign keys' => array(
  237. 'snid' => array(
  238. 'table' => 'simplenews_subscriber',
  239. 'columns' => array('snid' => 'snid'),
  240. ),
  241. 'tid' => array(
  242. 'table' => 'simplenews_category',
  243. 'columns' => array('tid' => 'tid'),
  244. )
  245. ),
  246. // @todo add foreign keys to other tables too?
  247. );
  248. $schema['simplenews_mail_spool'] = array(
  249. 'description' => 'Spool for temporary storage of newsletter emails.',
  250. 'fields' => array(
  251. 'msid' => array(
  252. 'description' => 'The primary identifier for a mail spool record.',
  253. 'type' => 'serial',
  254. 'unsigned' => TRUE,
  255. 'not null' => TRUE,
  256. ),
  257. 'mail' => array(
  258. 'description' => 'The formatted email address of mail message recipient.',
  259. 'type' => 'varchar',
  260. 'length' => 255,
  261. 'not null' => TRUE,
  262. 'default' => '',
  263. ),
  264. 'nid' => array(
  265. 'description' => 'The {node}.nid of this newsletter.',
  266. 'type' => 'int',
  267. 'not null' => TRUE,
  268. 'default' => 0,
  269. ),
  270. 'tid' => array(
  271. 'description' => 'The {simplenews_category}.tid this newsletter belongs to.',
  272. 'type' => 'int',
  273. 'not null' => TRUE,
  274. 'default' => 0,
  275. ),
  276. 'status' => array(
  277. 'description' => 'The sent status of the email (0 = hold, 1 = pending, 2 = done).',
  278. 'type' => 'int',
  279. 'unsigned' => TRUE,
  280. 'not null' => TRUE,
  281. 'default' => 0,
  282. 'size' => 'tiny',
  283. ),
  284. 'error' => array(
  285. 'description' => 'A boolean indicating whether an error occured while sending the email.',
  286. 'type' => 'int',
  287. 'size' => 'tiny',
  288. 'not null' => TRUE,
  289. 'default' => 0,
  290. ),
  291. 'timestamp' => array(
  292. 'description' => 'The time status was set or changed.',
  293. 'type' => 'int',
  294. 'unsigned' => TRUE,
  295. 'not null' => TRUE,
  296. 'default' => 0,
  297. ),
  298. 'data' => array(
  299. 'type' => 'text',
  300. 'not null' => FALSE,
  301. 'size' => 'big',
  302. 'serialize' => TRUE,
  303. 'description' => 'A serialized array of name value pairs that are related to the email address.',
  304. ),
  305. 'snid' => array(
  306. 'description' => 'Foreign key for subscriber table ({simplenews_subscriptions}.snid)',
  307. 'type' => 'int',
  308. 'not null' => TRUE,
  309. 'default' => 0,
  310. ),
  311. ),
  312. 'primary key' => array('msid'),
  313. 'indexes' => array(
  314. 'tid' => array('tid'),
  315. 'status' => array('status'),
  316. 'snid_tid' => array('snid', 'tid'),
  317. ),
  318. 'foreign keys' => array(
  319. 'nid' => array(
  320. 'table' => 'node',
  321. 'columns' => array('nid' => 'nid'),
  322. ),
  323. 'tid' => array(
  324. 'table' => 'simplenews_category',
  325. 'columns' => array('tid'),
  326. ),
  327. 'snid_tid' => array(
  328. 'table' => 'simplenews_subscription',
  329. 'columns' => array(
  330. 'snid' => 'snid',
  331. 'tid' => 'tid',
  332. ),
  333. ),
  334. ),
  335. );
  336. return $schema;
  337. }
  338. /**
  339. * Implements hook_install().
  340. */
  341. function simplenews_install() {
  342. _simplenews_init_simplenews_vocabulary();
  343. _simplenews_init_simplenews_category();
  344. // add nodetype with newsletter vocabulary
  345. _simplenews_install_nodetype();
  346. }
  347. /**
  348. * Implements hook_uninstall().
  349. */
  350. function simplenews_uninstall() {
  351. db_query("DELETE FROM {variable} WHERE name LIKE 'simplenews_%%'");
  352. }
  353. /**
  354. * Create simplenews node type.
  355. */
  356. function _simplenews_install_nodetype() {
  357. // Create a newsletter type if needed.
  358. $type = node_type_get_type('simplenews');
  359. if (!$type) {
  360. $type = node_type_set_defaults(array(
  361. 'type' => 'simplenews',
  362. 'name' => t('Simplenews newsletter'),
  363. 'base' => 'node_content',
  364. 'description' => t('A newsletter issue to be sent to subscribed email addresses.'),
  365. 'locked' => 0,
  366. 'custom' => 1,
  367. 'modified' => 1,
  368. ));
  369. node_type_save($type);
  370. node_add_body_field($type);
  371. }
  372. if (!field_info_instance('node', 'field_simplenews_term', $type->type)) {
  373. simplenews_add_term_field($type);
  374. }
  375. variable_set('simplenews_content_type_' . $type->type, TRUE);
  376. }
  377. /**
  378. * Create simplenews vocabulary and initial term.
  379. */
  380. function _simplenews_init_simplenews_vocabulary() {
  381. // Create the simplenews vocabulary. If it exists, set simplenews_vid variable.
  382. $vocabularies = taxonomy_vocabulary_load_multiple(array(), array('machine_name' => 'newsletter'));
  383. $vocabulary = reset($vocabularies);
  384. if (!$vocabulary) {
  385. $vocabulary = new stdClass();
  386. $vocabulary->name = t('Newsletter');
  387. $vocabulary->machine_name = 'newsletter';
  388. $vocabulary->description = t('Simplenews newsletter categories.');
  389. $vocabulary->weight = '0';
  390. $vocabulary->hierarchy = '0';
  391. $vocabulary->module = 'simplenews';
  392. }
  393. taxonomy_vocabulary_save($vocabulary);
  394. variable_set('simplenews_vid', $vocabulary->vid);
  395. // Create a newsletter taxonomy term if none exists.
  396. $tree = taxonomy_get_tree($vocabulary->vid);
  397. if (count($tree) == 0) {
  398. $term = new stdClass();
  399. $term->name = t('@site_name newsletter', array('@site_name' => variable_get('site_name', 'Drupal')));
  400. $term->description = t('@site_name newsletter categories.', array('@site_name' => variable_get('site_name', 'Drupal')));
  401. $term->vid = $vocabulary->vid;
  402. taxonomy_term_save($term);
  403. }
  404. }
  405. /**
  406. * Create initial simplenews categories.
  407. */
  408. function _simplenews_init_simplenews_category() {
  409. if ($tree = taxonomy_get_tree(variable_get('simplenews_vid', ''))) {
  410. $categories = simplenews_categories_load_multiple();
  411. $first = TRUE;
  412. foreach ($tree as $term) {
  413. // Create a newsletter category for each newsletter taxonomy term.
  414. if (!isset($categories[$term->tid])) {
  415. $category = new stdClass();
  416. $category->tid = $term->tid;
  417. // @todo use a function for category default values
  418. $category->from_name = variable_get('site_name', 'Drupal');
  419. $category->email_subject ='[[simplenews-category:name]] [node:title]';
  420. $category->from_address = variable_get('site_mail', ini_get('sendmail_from'));
  421. $category->format = 'plain';
  422. $category->priority = SIMPLENEWS_PRIORITY_NONE;
  423. $category->receipt = 0;
  424. $category->hyperlinks = 1;
  425. $category->new_account = 'none';
  426. $category->opt_inout = 'double';
  427. $category->block = 0;
  428. if ($first) {
  429. $category->block = 1;
  430. $first = FALSE;
  431. }
  432. simplenews_category_save($category);
  433. }
  434. }
  435. }
  436. }
  437. /**
  438. * Helper function to get all activated simplenews blocks
  439. *
  440. * @return Keyed array of simplenews blocks.
  441. */
  442. function _simplenews_get_blocks() {
  443. $query = db_select('block', 'b');
  444. $result = $query
  445. ->fields('b', array('delta'))
  446. ->condition('b.status', 1)
  447. ->condition('b.module', 'simplenews')
  448. ->execute();
  449. return $result->fetchAllAssoc('delta');
  450. }
  451. /**
  452. * Implements hook_update_last_removed().
  453. */
  454. function simplenews_update_last_removed() {
  455. // Support upgrades from 6.x-1.x and 6.x-2.x.
  456. return 6101;
  457. }
  458. /**
  459. * Implements hook_update_dependencies().
  460. */
  461. function simplenews_update_dependencies() {
  462. // Make sure that the taxonomy upgrade is run first.
  463. $dependencies['simplenews'][7000] = array(
  464. 'taxonomy' => 7010,
  465. );
  466. return $dependencies;
  467. }
  468. /**
  469. * Helper function to convert tokens in variables to D7 format.
  470. */
  471. function _simplenews_convert_tokens_in_variable($variables) {
  472. if (!is_array($variables)) {
  473. $variables = array($variables);
  474. }
  475. $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]');
  476. $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]');
  477. foreach ($variables as $variable) {
  478. if ($text = variable_get($variable, FALSE)) {
  479. $text = str_replace($old, $new, $text);
  480. variable_set($variable, $text);
  481. }
  482. }
  483. }
  484. /**
  485. * Create table {simplenews_category} to replace taxonomy terms.
  486. * Migrate Newsletter taxonomy data to Newsletter categories.
  487. *
  488. * Rename table simplenews_subscriptions to simplenews_subscriber.
  489. * Rename table simplenews_newsletters to simplenews_newsletter.
  490. * Drop fields {simplenews_newsletter}.s_format, .priority and .receipt.
  491. *
  492. * Rename table simplenews_snid_tid to simplenews_subscription.
  493. *
  494. * Delete deprecated simplenews variables.
  495. */
  496. function simplenews_update_7000() {
  497. // Convert tokens in variables to D7 format.
  498. $variables = array('simplenews_confirm_subscribe_subject', 'simplenews_confirm_subscribe_unsubscribed', 'simplenews_confirm_subscribe_subscribed', 'simplenews_confirm_unsubscribe_subscribed', 'simplenews_confirm_unsubscribe_unsubscribed');
  499. _simplenews_convert_tokens_in_variable($variables);
  500. // Create table 'simplenews_category'.
  501. $schema['simplenews_category'] = array(
  502. 'description' => 'Simplenews newsletter categories.',
  503. 'fields' => array(
  504. 'tid' => array(
  505. 'description' => '{taxonomy_term_data}.tid used as newsletter category.',
  506. 'type' => 'int',
  507. 'not null' => TRUE,
  508. 'default' => 0,
  509. ),
  510. 'format' => array(
  511. 'description' => 'Format of the newsletter email (plain, html).',
  512. 'type' => 'varchar',
  513. 'length' => 8,
  514. 'not null' => TRUE,
  515. 'default' => '',
  516. ),
  517. 'priority' => array(
  518. 'description' => 'Email priority according to RFC 2156 and RFC 5231 (0 = none; 1 = highest; 2 = high; 3 = normal; 4 = low; 5 = lowest).',
  519. 'type' => 'int',
  520. 'unsigned' => TRUE,
  521. 'not null' => TRUE,
  522. 'default' => 0,
  523. 'size' => 'tiny',
  524. ),
  525. 'receipt' => array(
  526. 'description' => 'Boolean indicating request for email receipt confirmation according to RFC 2822.',
  527. 'type' => 'int',
  528. 'unsigned' => TRUE,
  529. 'not null' => TRUE,
  530. 'default' => 0,
  531. 'size' => 'tiny',
  532. ),
  533. 'from_name' => array(
  534. 'description' => 'Sender name for newsletter emails.',
  535. 'type' => 'varchar',
  536. 'length' => 128,
  537. 'not null' => TRUE,
  538. 'default' => '',
  539. ),
  540. 'email_subject' => array(
  541. 'description' => 'Subject of newsletter email. May contain tokens.',
  542. 'type' => 'varchar',
  543. 'length' => 255,
  544. 'not null' => TRUE,
  545. 'default' => '',
  546. ),
  547. 'from_address' => array(
  548. 'description' => 'Sender address for newsletter emails',
  549. 'type' => 'varchar',
  550. 'length' => 64,
  551. 'not null' => TRUE,
  552. 'default' => '',
  553. ),
  554. 'hyperlinks' => array(
  555. 'description' => 'Flag indicating type of hyperlink conversion (1 = hyperlinks are in-line; 0 = hyperlinks are placed at email bottom).',
  556. 'type' => 'int',
  557. 'size' => 'tiny',
  558. 'not null' => TRUE,
  559. 'default' => 0,
  560. ),
  561. 'new_account' => array(
  562. 'description' => 'How to treat subscription at account creation (none = None; on = Default on; off = Default off; silent = Invisible subscription).',
  563. 'type' => 'varchar',
  564. 'length' => 12,
  565. 'not null' => TRUE,
  566. 'default' => '',
  567. ),
  568. 'opt_inout' => array(
  569. 'description' => 'How to treat subscription confirmation (hidden = Newsletter is hidden from the user; single = Single opt-in; double = Double opt-in).',
  570. 'type' => 'varchar',
  571. 'length' => 12,
  572. 'not null' => TRUE,
  573. 'default' => '',
  574. ),
  575. 'block' => array(
  576. 'description' => 'For this category a subscription block is available.',
  577. 'type' => 'int',
  578. 'size' => 'tiny',
  579. 'not null' => TRUE,
  580. 'default' => 0,
  581. ),
  582. ),
  583. 'primary key' => array('tid'),
  584. );
  585. db_create_table('simplenews_category', $schema['simplenews_category']);
  586. // Migrate Newsletter taxonomy data to Newsletter categories.
  587. // Query the database directly, to avoid triggering our own hooks.
  588. $tids = db_query('SELECT tid FROM {taxonomy_term_data} where vid = :vid', array(':vid' => variable_get('simplenews_vid', '')))->fetchCol();
  589. // @todo Check if simplenews blocks are still active after core update.
  590. // If not, there is no purpose in migrating the block status ('block' => 0).
  591. $blocks = _simplenews_get_blocks();
  592. foreach ($tids as $tid) {
  593. _simplenews_convert_tokens_in_variable('simplenews_email_subject_' . $tid);
  594. // check string lengths: variables can be an arbitrary length but the new table has limits
  595. $from_name = variable_get('simplenews_from_name_' . $tid, variable_get('simplenews_from_name', variable_get('site_name', 'Drupal')));
  596. $email_subject = variable_get('simplenews_email_subject_' . $tid, '[[simplenews-newsletters-name]] [title-raw]');
  597. $from_address = variable_get('simplenews_from_address_' . $tid, variable_get('simplenews_from_address', variable_get('site_mail', ini_get('sendmail_from'))));
  598. if (strlen($from_name) > 128) {
  599. drupal_set_message(t('The from_name field for simplenews list @tid (@value) was too long and has been truncated to 128 characters.',
  600. array('@tid' => $tid, '@value' => $from_name)), 'warning');
  601. $from_name = substr($from_name, 0, 128);
  602. }
  603. if (strlen($email_subject) > 255) {
  604. drupal_set_message(t('The email_subject field for simplenews list @tid (@value) was too long and has been truncated to 255 characters.',
  605. array('@tid' => $tid, '@value' => $email_subject)), 'warning');
  606. $from_name = substr($email_subject, 0, 255);
  607. }
  608. if (strlen($from_address) > 64) {
  609. drupal_set_message(t('The from_address field for simplenews list @tid (@value) was too long and has been truncated to 64 characters.',
  610. array('@tid' => $tid, '@value' => $from_address)), 'warning');
  611. $from_name = substr($from_address, 0, 64);
  612. }
  613. db_insert('simplenews_category')
  614. ->fields(array(
  615. 'tid' => $tid,
  616. 'format' => 'plain',
  617. 'priority' => '0',
  618. 'receipt' => '0',
  619. 'from_name' => $from_name,
  620. 'email_subject' => $email_subject,
  621. 'from_address' => $from_address,
  622. 'hyperlinks' => variable_get('simplenews_hyperlinks_' . $tid, 1),
  623. 'new_account' => variable_get('simplenews_new_account_' . $tid, 'none'),
  624. 'opt_inout' => variable_get('simplenews_opt_inout_' . $tid, 'double'),
  625. 'block' => isset($blocks[$tid]) ? 1 : 0,
  626. ))
  627. ->execute();
  628. }
  629. // Change table simplenews_subscriptions to simplenews_subscriber.
  630. db_rename_table('simplenews_subscriptions', 'simplenews_subscriber');
  631. // Change table simplenews_newsletters to simplenews_newsletter.
  632. // Drop fields: s_format, priority, receipt (moved to simplenews_category).
  633. db_rename_table('simplenews_newsletters', 'simplenews_newsletter');
  634. db_change_field('simplenews_newsletter', 'tid', 'tid', array(
  635. 'description' => 'The newsletter category {simplenews_category}.tid this newsletter belongs to.',
  636. 'type' => 'int',
  637. 'not null' => TRUE,
  638. 'default' => 0,
  639. ));
  640. db_drop_field('simplenews_newsletter', 's_format');
  641. db_drop_field('simplenews_newsletter', 'priority');
  642. db_drop_field('simplenews_newsletter', 'receipt');
  643. // Change table simplenews_snid_tid to simplenews_subscription.
  644. // Change field {simplenews_subscription}.tid description
  645. db_drop_primary_key('simplenews_snid_tid');
  646. db_rename_table('simplenews_snid_tid', 'simplenews_subscription');
  647. // Add {simplenews_mail_spool}.data to store subscriber data.
  648. db_add_field('simplenews_mail_spool', 'data', array(
  649. 'type' => 'text',
  650. 'not null' => FALSE,
  651. 'size' => 'big',
  652. 'serialize' => TRUE,
  653. 'description' => 'A serialized array of name value pairs that are related to the email address.',
  654. ));
  655. // Rename field {simplenews_mail_spool}.s_status to "status".
  656. db_change_field('simplenews_newsletter', 's_status', 'status', array(
  657. 'description' => 'sent status of the newsletter issue (0 = not sent; 1 = pending; 2 = sent). ',
  658. 'type' => 'int',
  659. 'size' => 'tiny',
  660. 'not null' => TRUE,
  661. 'default' => 0,
  662. ));
  663. // Delete deprecated variables.
  664. foreach ($tids as $tid) {
  665. variable_del('simplenews_from_name_' . $tid);
  666. variable_del('simplenews_email_subject_' . $tid);
  667. variable_del('simplenews_from_address_' . $tid);
  668. variable_del('simplenews_hyperlinks_' . $tid);
  669. variable_del('simplenews_new_account_' . $tid);
  670. variable_del('simplenews_opt_inout_' . $tid);
  671. }
  672. // @todo Add return text about checking of Newsletter Category settings.
  673. // @todo Add return text about Block checkboxes
  674. // Convert old content type settings.
  675. module_load_include('module', 'node');
  676. module_load_include('module', 'simplenews');
  677. // Update the machine name of the simplenews vocabulary.
  678. if ($vid = variable_get('simplenews_vid', '')) {
  679. db_update('taxonomy_vocabulary')
  680. ->fields(array(
  681. 'machine_name' => 'newsletter',
  682. ))
  683. ->condition('vid', $vid)
  684. ->execute();
  685. $field_name = 'taxonomy_vocabulary_' . $vid;
  686. variable_set('simplenews_category_field', $field_name);
  687. $field = field_info_field($field_name);
  688. $field['settings']['allowed_values'][0]['vocabulary'] = 'newsletter';
  689. field_update_field($field);
  690. }
  691. variable_del('simplenews_vid');
  692. $content_types = variable_get('simplenews_content_types');
  693. if (!empty($content_types)) {
  694. foreach ($content_types as $simplenews_content_type) {
  695. variable_set('simplenews_content_type_' . $simplenews_content_type, TRUE);
  696. }
  697. }
  698. variable_del('simplenews_content_types');
  699. }
  700. /**
  701. * Create key snid_tid on simplenews_mail_spool table.
  702. */
  703. function simplenews_update_7001() {
  704. // Add the {simplenews_mail_spool}.snid field if it doesn't exist yet (added
  705. // in 6.x-2.x).
  706. if (!db_field_exists('simplenews_mail_spool', 'snid')) {
  707. db_add_field('simplenews_mail_spool', 'snid', array(
  708. 'description' => 'Foreign key for subscriber table ({simplenews_subscriptions}.snid)',
  709. 'type' => 'int',
  710. 'not null' => TRUE,
  711. 'default' => 0,
  712. ));
  713. }
  714. if (!db_index_exists('simplenews_mail_spool', 'snid_tid')) {
  715. db_add_index('simplenews_mail_spool', 'snid_tid', array('snid', 'tid'));
  716. }
  717. }
  718. /**
  719. * Drop support for node revisioning.
  720. */
  721. function simplenews_update_7002() {
  722. if (db_field_exists('simplenews_newsletter', 'vid')) {
  723. db_drop_field('simplenews_newsletter', 'vid');
  724. }
  725. if (db_field_exists('simplenews_mail_spool', 'vid')) {
  726. db_drop_field('simplenews_mail_spool', 'vid');
  727. }
  728. }
  729. /**
  730. * [simplenews-newsletter] tokens have been removed in favor of [node] tokens.
  731. */
  732. function simplenews_update_7003() {
  733. 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');
  734. }
  735. /**
  736. * Add the status field to {simplenews_subscription}.
  737. */
  738. function simplenews_update_7004() {
  739. if (!db_field_exists('simplenews_subscription', 'status')) {
  740. db_add_field('simplenews_subscription', 'status', array(
  741. 'description' => 'A flag indicating whether the user is subscribed (1) or unsubscribed (0).',
  742. 'type' => 'int',
  743. 'size' => 'small',
  744. 'not null' => TRUE,
  745. 'default' => 1
  746. )
  747. );
  748. }
  749. }
  750. /**
  751. * Add support for combined confirmation mails.
  752. */
  753. function simplenews_update_7005() {
  754. db_add_field('simplenews_subscriber', 'changes', array(
  755. 'description' => 'Contains the requested subscription changes',
  756. 'type' => 'text',
  757. 'serialize' => TRUE,
  758. ));
  759. // To keep existing installations consistent, disable combined confirmation
  760. // mails.
  761. variable_set('simplenews_use_combined', 'never');
  762. }
  763. /**
  764. * Add support for subscriber create date
  765. */
  766. function simplenews_update_7006() {
  767. db_add_field('simplenews_subscriber', 'created', array(
  768. 'description' => 'UNIX timestamp of when the subscription record was added.',
  769. 'type' => 'int',
  770. 'unsigned' => TRUE,
  771. 'not null' => TRUE,
  772. 'default' => 0,
  773. ));
  774. }
  775. /**
  776. * Allow longer from_name field values.
  777. */
  778. function simplenews_update_7007() {
  779. db_change_field('simplenews_category', 'from_name', 'from_name', array(
  780. 'type' => 'varchar',
  781. 'length' => 128,
  782. 'not null' => TRUE,
  783. 'default' => '',
  784. 'description' => 'Sender name for newsletter emails.',
  785. ));
  786. }
  787. /**
  788. * Add the timestamp and source columns to {simplenews_subscription} if missing.
  789. */
  790. function simplenews_update_7008() {
  791. if (!db_field_exists('simplenews_subscription', 'timestamp')) {
  792. db_add_field('simplenews_subscription', 'timestamp', array(
  793. 'description' => 'UNIX timestamp of when the user is (un)subscribed.',
  794. 'type' => 'int',
  795. 'unsigned' => TRUE,
  796. 'not null' => TRUE,
  797. 'default' => 0,
  798. ));
  799. }
  800. if (!db_field_exists('simplenews_subscription', 'source')) {
  801. db_add_field('simplenews_subscription', 'source', array(
  802. 'description' => 'The source via which the user is (un)subscription.',
  803. 'type' => 'varchar',
  804. 'length' => 24,
  805. 'not null' => TRUE,
  806. 'default' => '',
  807. ));
  808. }
  809. }
  810. /**
  811. * Add the sent_subsriber_countcolumn to {simplenews_newsletter} if missing.
  812. */
  813. function simplenews_update_7009() {
  814. if (!db_field_exists('simplenews_newsletter', 'sent_subscriber_count')) {
  815. db_add_field('simplenews_newsletter', 'sent_subscriber_count', array(
  816. 'description' => 'The count of subscribers to the newsletter when it was sent.',
  817. 'type' => 'int',
  818. 'not null' => TRUE,
  819. 'default' => 0,
  820. ));
  821. }
  822. }
  823. /**
  824. * Update empty sent subscriber count column to current subscriber count.
  825. */
  826. function simplenews_update_7010() {
  827. // Assume that already sent newsletters that have a sent subscriber count of
  828. // 0 have been sent to all subscribers. Do a update query per newsletter
  829. // category, to avoid having to re-execute the subquery on every row.
  830. $tids = db_query('SELECT tid FROM {simplenews_category}')->fetchCol();
  831. foreach ($tids as $tid) {
  832. $count = db_query('SELECT COUNT(*) FROM {simplenews_subscription} ss WHERE status = 1 AND tid = :tid', array(':tid' => $tid))->fetchField();
  833. db_update('simplenews_newsletter')
  834. ->fields(array(
  835. 'sent_subscriber_count' => $count,
  836. ))
  837. // 2 equals SIMPLENEWS_STATUS_SEND_READY.
  838. ->condition('status', 2)
  839. ->condition('sent_subscriber_count', 0)
  840. ->condition('tid', $tid)
  841. ->execute();
  842. }
  843. }