simplenews_rules.rules.inc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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'),
  74. 'named parameter' => TRUE,
  75. 'parameter' => array(
  76. 'mail' => array(
  77. 'type' => 'text',
  78. 'label' => t('E-mail'),
  79. 'description' => t('The e-mail address that should be unsubscribed.'),
  80. ),
  81. 'tid' => array(
  82. 'type' => 'integer',
  83. 'label' => t('Simplenews category'),
  84. 'description' => t('For which newsletter category the unsubscription should happen.'),
  85. 'options list' => 'simplenews_category_list',
  86. ),
  87. 'confirmation' => array(
  88. 'type' => 'integer',
  89. 'label' => t('Confirmation required'),
  90. 'description' => t('Select if a confirmation is required. Default uses the default setting from the chosen newsletter category.'),
  91. 'options list' => 'simplenews_rules_confirmation_list',
  92. 'default value' => SIMPLENEWS_RULES_CONFIRMATION_DEFAULT,
  93. ),
  94. 'source' => array(
  95. 'type' => 'text',
  96. 'label' => t('Source'),
  97. 'description' => t('A string to identify the source of this subscription'),
  98. 'optional' => TRUE,
  99. 'default value' => 'rules',
  100. ),
  101. 'language' => array(
  102. 'type' => 'token',
  103. 'label' => t('Language'),
  104. 'description' => t('If specified, the language to use for the subscription. Defaults to the default language.'),
  105. 'options list' => 'entity_metadata_language_list',
  106. 'optional' => TRUE,
  107. 'default value' => LANGUAGE_NONE,
  108. ),
  109. ),
  110. ),
  111. 'simplenews_rules_action_subscription_delete' => array(
  112. 'label' => t('Delete an e-mail address from a newsletter'),
  113. 'group' => t('Simplenews'),
  114. 'named parameter' => TRUE,
  115. 'parameter' => array(
  116. 'mail' => array(
  117. 'type' => 'text',
  118. 'label' => t('E-mail'),
  119. 'description' => t('The e-mail address that should be permanently deleted from the newsletter. Note, use the unsubscribe action to temporarily unsubscribe an email address.'),
  120. ),
  121. 'tid' => array(
  122. 'type' => 'integer',
  123. 'label' => t('Simplenews category'),
  124. 'description' => t('For which newsletter category the delete should happen.'),
  125. 'options list' => 'simplenews_category_list',
  126. ),
  127. ),
  128. ),
  129. 'simplenews_rules_action_subscriber_delete' => array(
  130. 'label' => t('Delete an e-mail address from all newsletters'),
  131. 'group' => t('Simplenews'),
  132. 'named parameter' => TRUE,
  133. 'parameter' => array(
  134. 'mail' => array(
  135. 'type' => 'text',
  136. 'label' => t('E-mail'),
  137. 'description' => t('The e-mail address that should be permanently deleted from all newsletters. Note, use the unsubscribe action to temporarily unsubscribe an email address.'),
  138. ),
  139. ),
  140. ),
  141. );
  142. }
  143. /**
  144. * Implements hook_event_info().
  145. */
  146. function simplenews_rules_rules_event_info() {
  147. return array(
  148. 'simplenews_rules_event_subscribe' => array(
  149. 'label' => t('A user has been subscribed'),
  150. 'group' => t('Simplenews'),
  151. 'variables' => array(
  152. 'mail' => array(
  153. 'type' => 'text',
  154. 'label' => t('E-Mail'),
  155. 'description' => t('The e-mail address that has been subscribed.'),
  156. ),
  157. 'tid' => array(
  158. 'type' => 'integer',
  159. 'label' => t('Simplenews category'),
  160. 'descrption' => t('The newsletter category of the subscription.'),
  161. 'options list' => 'simplenews_category_list',
  162. ),
  163. ),
  164. ),
  165. 'simplenews_rules_event_unsubscribe' => array(
  166. 'label' => t('A user has been unsubscribed'),
  167. 'group' => t('Simplenews'),
  168. 'variables' => array(
  169. 'mail' => array(
  170. 'type' => 'text',
  171. 'label' => t('E-mail'),
  172. 'description' => t('The e-mail address that has been subscribed.'),
  173. ),
  174. 'tid' => array(
  175. 'type' => 'integer',
  176. 'label' => t('Simplenews category'),
  177. 'descrption' => t('The newsletter category of the subscription.'),
  178. 'options list' => 'simplenews_category_list',
  179. ),
  180. ),
  181. ),
  182. );
  183. }
  184. /**
  185. * Action implementation, send a newsletter node.
  186. */
  187. function simplenews_rules_action_send($node) {
  188. $newsletter = simplenews_newsletter_load($node->nid);
  189. if ($newsletter && ($newsletter->status != SIMPLENEWS_STATUS_SEND_PENDING || $newsletter->status != SIMPLENEWS_STATUS_SEND_PENDING)) {
  190. module_load_include('inc', 'simplenews', 'includes/simplenews.mail');
  191. simplenews_add_node_to_spool($node);
  192. }
  193. }
  194. /**
  195. * Action Implementation: Subscribe an e-mail adress to a Simplenews newsletter.
  196. */
  197. function simplenews_rules_action_subscribe($args, $settings) {
  198. if ($args['language'] == LANGUAGE_NONE) {
  199. $args['language'] = NULL;
  200. }
  201. $confirmation = simplenews_rules_map_confirmation($args);
  202. // Pass the call forward.
  203. simplenews_subscribe_user($args['mail'], $args['tid'], $confirmation, $args['source'], $args['language']);
  204. }
  205. /**
  206. * Action Implementation: Unsubscribe an e-mail adress to a Simplenews newsletter.
  207. */
  208. function simplenews_rules_action_unsubscribe($args, $settings) {
  209. if ($args['language'] == LANGUAGE_NONE) {
  210. $args['language'] = NULL;
  211. }
  212. $confirmation = simplenews_rules_map_confirmation($args);
  213. // Pass the call forward.
  214. simplenews_unsubscribe_user($args['mail'], $args['tid'], $confirmation, $args['source'], $args['language']);
  215. }
  216. /**
  217. * Action Implementation: Delete an email address from a specific Simplenews newsletter
  218. */
  219. function simplenews_rules_action_subscription_delete($args, $settings) {
  220. if (!empty($args['mail']) && !empty($args['tid'])){
  221. simplenews_subscription_delete(array('mail' => $args['mail'], 'tid' => $args['tid']));
  222. }
  223. }
  224. /**
  225. * Action Implementation: Delete an email address from all Simplenews newsletters
  226. */
  227. function simplenews_rules_action_subscriber_delete($args, $settings) {
  228. $subscriber = simplenews_subscriber_load_by_mail($args['mail']);
  229. if ($subscriber) {
  230. simplenews_subscriber_delete($subscriber);
  231. }
  232. }
  233. /**
  234. * Map args to the confrmation argument for subscribing/unsubscribing.
  235. */
  236. function simplenews_rules_map_confirmation($args) {
  237. switch ($args['confirmation']) {
  238. case SIMPLENEWS_RULES_CONFIRMATION_YES:
  239. $confirmation = TRUE;
  240. break;
  241. case SIMPLENEWS_RULES_CONFIRMATION_NO:
  242. $confirmation = FALSE;
  243. break;
  244. case SIMPLENEWS_RULES_CONFIRMATION_DEFAULT:
  245. $account = simplenews_load_user_by_mail($args['mail']);
  246. $confirmation = simplenews_require_double_opt_in($args['tid'], $account);
  247. break;
  248. }
  249. return $confirmation;
  250. }
  251. /**
  252. * @}
  253. */