simplenews_rules.rules.inc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * @file
  4. * Rules hooks for the Simplenews newsletter module.
  5. *
  6. * @addtogroup rules
  7. * @{
  8. */
  9. /**
  10. * Implements hook_rules_action_info().
  11. */
  12. function simplenews_rules_rules_action_info() {
  13. return array(
  14. 'simplenews_rules_action_send' => array(
  15. 'label' => t('Send newsletter'),
  16. 'group' => t('Simplenews'),
  17. 'parameter' => array(
  18. 'node' => array(
  19. 'type' => 'node',
  20. 'label' => t('The newsletter node to be sent.'),
  21. 'description' => t('The newsletter node that should be sent.'),
  22. )
  23. )
  24. ),
  25. 'simplenews_rules_action_subscribe' => array(
  26. 'label' => t('Subscribe an e-mail adress to a newsletter'),
  27. 'group' => t('Simplenews'),
  28. 'named parameter' => TRUE,
  29. 'parameter' => array(
  30. 'mail' => array(
  31. 'type' => 'text',
  32. 'label' => t('E-mail'),
  33. 'description' => t('The e-mail address that should be subscribed.'),
  34. ),
  35. 'tid' => array(
  36. 'type' => 'integer',
  37. 'label' => t('Simplenews category'),
  38. 'descrption' => t('For which newsletter category the subscription should happen.'),
  39. 'options list' => 'simplenews_category_list',
  40. ),
  41. 'confirmation' => array(
  42. 'type' => 'integer',
  43. 'label' => t('Confirmation required'),
  44. 'description' => t('Select if a confirmation is required. Default uses the default setting from the chosen newsletter category.'),
  45. 'options list' => 'simplenews_rules_confirmation_list',
  46. 'default value' => SIMPLENEWS_RULES_CONFIRMATION_DEFAULT,
  47. ),
  48. 'source' => array(
  49. 'type' => 'string',
  50. 'label' => t('Source'),
  51. 'description' => t('A string to identify the source of this subscription'),
  52. 'optional' => TRUE,
  53. ),
  54. 'source' => array(
  55. 'type' => 'text',
  56. 'label' => t('Source'),
  57. 'description' => t('A string to identify the source of this subscription'),
  58. 'optional' => TRUE,
  59. 'default value' => 'rules',
  60. ),
  61. 'language' => array(
  62. 'type' => 'token',
  63. 'label' => t('Language'),
  64. 'description' => t('If specified, the language to use for the subscription. Defaults to the default language.'),
  65. 'options list' => 'entity_metadata_language_list',
  66. 'optional' => TRUE,
  67. 'default value' => LANGUAGE_NONE,
  68. ),
  69. ),
  70. ),
  71. 'simplenews_rules_action_unsubscribe' => array(
  72. 'label' => t('Unsubscribe an e-mail adress from a newsletter'),
  73. 'group' => t('Simplenews'), 'named parameter' => TRUE,
  74. 'parameter' => array(
  75. 'mail' => array(
  76. 'type' => 'text',
  77. 'label' => t('E-mail'),
  78. 'description' => t('The e-mail address that should be unsubscribed.'),
  79. ),
  80. 'tid' => array(
  81. 'type' => 'integer',
  82. 'label' => t('Simplenews category'),
  83. 'descrption' => t('For which newsletter category the unsubscription should happen.'),
  84. 'options list' => 'simplenews_category_list',
  85. ),
  86. 'confirmation' => array(
  87. 'type' => 'integer',
  88. 'label' => t('Confirmation required'),
  89. 'description' => t('Select if a confirmation is required. Default uses the default setting from the chosen newsletter category.'),
  90. 'options list' => 'simplenews_rules_confirmation_list',
  91. 'default value' => SIMPLENEWS_RULES_CONFIRMATION_DEFAULT,
  92. ),
  93. 'source' => array(
  94. 'type' => 'text',
  95. 'label' => t('Source'),
  96. 'description' => t('A string to identify the source of this subscription'),
  97. 'optional' => TRUE,
  98. 'default value' => 'rules',
  99. ),
  100. 'language' => array(
  101. 'type' => 'token',
  102. 'label' => t('Language'),
  103. 'description' => t('If specified, the language to use for the subscription. Defaults to the default language.'),
  104. 'options list' => 'entity_metadata_language_list',
  105. 'optional' => TRUE,
  106. 'default value' => LANGUAGE_NONE,
  107. ),
  108. ),
  109. ),
  110. );
  111. }
  112. /**
  113. * Implements hook_event_info().
  114. */
  115. function simplenews_rules_rules_event_info() {
  116. return array(
  117. 'simplenews_rules_event_subscribe' => array(
  118. 'label' => t('A user has been subscribed'),
  119. 'group' => t('Simplenews'),
  120. 'variables' => array(
  121. 'mail' => array(
  122. 'type' => 'text',
  123. 'label' => t('E-Mail'),
  124. 'description' => t('The e-mail address that has been subscribed.'),
  125. ),
  126. 'tid' => array(
  127. 'type' => 'integer',
  128. 'label' => t('Simplenews category'),
  129. 'descrption' => t('The newsletter category of the subscription.'),
  130. 'options list' => 'simplenews_category_list',
  131. ),
  132. ),
  133. ),
  134. 'simplenews_rules_event_unsubscribe' => array(
  135. 'label' => t('A user has been unsubscribed'),
  136. 'group' => t('Simplenews'),
  137. 'variables' => array(
  138. 'mail' => array(
  139. 'type' => 'text',
  140. 'label' => t('E-mail'),
  141. 'description' => t('The e-mail address that has been subscribed.'),
  142. ),
  143. 'tid' => array(
  144. 'type' => 'integer',
  145. 'label' => t('Simplenews category'),
  146. 'descrption' => t('The newsletter category of the subscription.'),
  147. 'options list' => 'simplenews_category_list',
  148. ),
  149. ),
  150. ),
  151. );
  152. }
  153. /**
  154. * Action implementation, send a newsletter node.
  155. */
  156. function simplenews_rules_action_send($node) {
  157. $newsletter = simplenews_newsletter_load($node->nid);
  158. if ($newsletter && ($newsletter->status != SIMPLENEWS_STATUS_SEND_PENDING || $newsletter->status != SIMPLENEWS_STATUS_SEND_PENDING)) {
  159. module_load_include('inc', 'simplenews', 'includes/simplenews.mail');
  160. simplenews_add_node_to_spool($node);
  161. }
  162. }
  163. /**
  164. * Action Implementation: Subscribe an e-mail adress to a Simplenews newsletter.
  165. */
  166. function simplenews_rules_action_subscribe($args, $settings) {
  167. if ($args['language'] == LANGUAGE_NONE) {
  168. $args['language'] = NULL;
  169. }
  170. $confirmation = simplenews_rules_map_confirmation($args);
  171. // Pass the call forward.
  172. simplenews_subscribe_user($args['mail'], $args['tid'], $confirmation, $args['source'], $args['language']);
  173. }
  174. /**
  175. * Action Implementation: Unsubscribe an e-mail adress to a Simplenews newsletter.
  176. */
  177. function simplenews_rules_action_unsubscribe($args, $settings) {
  178. if ($args['language'] == LANGUAGE_NONE) {
  179. $args['language'] = NULL;
  180. }
  181. $confirmation = simplenews_rules_map_confirmation($args);
  182. // Pass the call forward.
  183. simplenews_unsubscribe_user($args['mail'], $args['tid'], $confirmation, $args['source'], $args['language']);
  184. }
  185. /**
  186. * Map args to the confrmation argument for subscribing/unsubscribing.
  187. */
  188. function simplenews_rules_map_confirmation($args) {
  189. switch ($args['confirmation']) {
  190. case SIMPLENEWS_RULES_CONFIRMATION_YES:
  191. $confirmation = TRUE;
  192. break;
  193. case SIMPLENEWS_RULES_CONFIRMATION_NO:
  194. $confirmation = FALSE;
  195. break;
  196. case SIMPLENEWS_RULES_CONFIRMATION_DEFAULT:
  197. $account = simplenews_load_user_by_mail($args['mail']);
  198. $confirmation = simplenews_require_double_opt_in($args['tid'], $account);
  199. break;
  200. }
  201. return $confirmation;
  202. }
  203. /**
  204. * @}
  205. */