uc_roles.rules.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. /**
  3. * @file
  4. * Rules hooks for uc_roles.module.
  5. */
  6. /**
  7. * Implements hook_rules_data_info().
  8. *
  9. * An entity is defined in order to get role expiration tokens in the email.
  10. */
  11. function uc_roles_rules_data_info() {
  12. // CA entity for a role expiration object.
  13. $entities['uc_roles_expiration'] = array(
  14. 'label' => t('Ubercart role expiration'),
  15. 'wrap' => TRUE,
  16. 'token type' => 'uc_role',
  17. 'property info' => array(
  18. 'reid' => array(
  19. 'type' => 'integer',
  20. 'label' => t('Role expiration ID'),
  21. 'description' => t('Primary key for role expirations.'),
  22. ),
  23. 'uid' => array(
  24. 'type' => 'integer',
  25. 'label' => t('User ID'),
  26. 'description' => t('The user account ID.'),
  27. ),
  28. 'user' => array(
  29. 'type' => 'user',
  30. 'label' => t('User'),
  31. 'description' => t('The user account that has the role.'),
  32. 'getter callback' => 'uc_roles_get_expiration_properties',
  33. 'setter callback' => 'uc_roles_set_expiration_properties',
  34. ),
  35. 'rid' => array(
  36. 'type' => 'integer',
  37. 'label' => t('Role ID'),
  38. 'description' => t('The granted role.'),
  39. ),
  40. 'expiration' => array(
  41. 'type' => 'date',
  42. 'label' => t('Expiration time'),
  43. 'description' => t('The time the role will be removed from the user.'),
  44. ),
  45. 'notified' => array(
  46. 'type' => 'boolean',
  47. 'label' => t('Notified'),
  48. 'description' => t('Indicates the user has been warned that the role will be removed soon.'),
  49. ),
  50. ),
  51. );
  52. return $entities;
  53. }
  54. /**
  55. * Callback for getting role expiration properties.
  56. *
  57. * @see entity_metadata_node_entity_info_alter()
  58. */
  59. function uc_roles_get_expiration_properties($expiration, array $options, $name, $entity_type) {
  60. switch ($name) {
  61. case 'user':
  62. return $expiration->uid;
  63. }
  64. }
  65. /**
  66. * Callback for setting role expiration properties.
  67. *
  68. * @see entity_metadata_node_entity_info_alter()
  69. */
  70. function uc_roles_set_expiration_properties($expiration, $name, $value) {
  71. if ($name == 'user') {
  72. $expiration->uid = $value;
  73. }
  74. }
  75. /**
  76. * Implements hook_rules_action_info().
  77. */
  78. function uc_roles_rules_action_info() {
  79. // Renew a role expiration.
  80. $actions['uc_roles_order_renew'] = array(
  81. 'label' => t('Renew the roles on an order.'),
  82. 'group' => t('Renewal'),
  83. 'base' => 'uc_roles_action_order_renew',
  84. 'parameter' => array(
  85. 'order' => array(
  86. 'type' => 'uc_order',
  87. 'label' => t('Order'),
  88. ),
  89. 'message' => array(
  90. 'type' => 'boolean',
  91. 'label' => t('Display messages to alert users of any new or updated roles.'),
  92. ),
  93. ),
  94. );
  95. $email_args = array(
  96. 'expiration' => array(
  97. 'type' => 'uc_roles_expiration',
  98. 'label' => t('Role expiration'),
  99. ),
  100. 'from' => array(
  101. 'type' => 'text',
  102. 'label' => t('Sender'),
  103. ),
  104. 'addresses' => array(
  105. 'type' => 'text',
  106. 'label' => t('Recipients'),
  107. ),
  108. 'subject' => array(
  109. 'type' => 'text',
  110. 'label' => t('Subject'),
  111. ),
  112. 'message' => array(
  113. 'type' => 'text',
  114. 'label' => t('Message'),
  115. ),
  116. 'format' => array(
  117. 'type' => 'text',
  118. 'label' => t('Message format'),
  119. 'options list' => 'uc_roles_message_formats',
  120. ),
  121. );
  122. // Send an email to an order with a role expiration
  123. $actions['uc_roles_order_email'] = array(
  124. 'label' => t('Send an order email regarding roles.'),
  125. 'group' => t('Notification'),
  126. 'base' => 'uc_roles_action_order_email',
  127. 'parameter' => array(
  128. 'order' => array(
  129. 'type' => 'uc_order',
  130. 'label' => t('Order'),
  131. ),
  132. ) + $email_args,
  133. );
  134. // Send an email to a user with a role expiration
  135. $actions['uc_roles_user_email'] = array(
  136. 'label' => t('Send a user an email regarding roles.'),
  137. 'group' => t('Notification'),
  138. 'base' => 'uc_roles_action_user_email',
  139. 'parameter' => array(
  140. 'account' => array(
  141. 'type' => 'user',
  142. 'label' => t('User'),
  143. ),
  144. ) + $email_args,
  145. );
  146. return $actions;
  147. }
  148. /**
  149. * Options list callback for message formats.
  150. */
  151. function uc_roles_message_formats() {
  152. global $user;
  153. $options = array();
  154. $formats = filter_formats($user);
  155. foreach ($formats as $format) {
  156. $options[$format->format] = $format->name;
  157. }
  158. return $options;
  159. }
  160. /**
  161. * Implements hook_rules_event_info().
  162. */
  163. function uc_roles_rules_event_info() {
  164. $order = array(
  165. 'type' => 'uc_order',
  166. 'label' => t('Order'),
  167. );
  168. $account = array(
  169. 'type' => 'user',
  170. 'label' => t('User'),
  171. );
  172. $expiration = array(
  173. 'type' => 'uc_roles_expiration',
  174. 'label' => t('Role expiration'),
  175. );
  176. $events['uc_roles_notify_grant'] = array(
  177. 'label' => t('E-mail for granted roles'),
  178. 'group' => t('Notification'),
  179. 'variables' => array(
  180. 'order' => $order,
  181. 'expiration' => $expiration,
  182. ),
  183. );
  184. $events['uc_roles_notify_revoke'] = array(
  185. 'label' => t('E-mail for revoked roles'),
  186. 'group' => t('Notification'),
  187. 'variables' => array(
  188. 'account' => $account,
  189. 'expiration' => $expiration,
  190. ),
  191. );
  192. $events['uc_roles_notify_renew'] = array(
  193. 'label' => t('E-mail for renewed roles'),
  194. 'group' => t('Notification'),
  195. 'variables' => array(
  196. 'order' => $order,
  197. 'expiration' => $expiration,
  198. ),
  199. );
  200. $events['uc_roles_notify_reminder'] = array(
  201. 'label' => t('E-mail for role expiration reminders'),
  202. 'group' => t('Notification'),
  203. 'variables' => array(
  204. 'account' => $account,
  205. 'expiration' => $expiration,
  206. ),
  207. );
  208. return $events;
  209. }
  210. /**
  211. * Send an email with order and role replacement tokens.
  212. *
  213. * The recipients, subject, and message fields take order token replacements.
  214. *
  215. * @see uc_roles_action_order_email_form()
  216. */
  217. function uc_roles_action_order_email($order, $role_expiration, $from, $addresses, $subject, $message, $format) {
  218. $settings = array(
  219. 'from' => $from,
  220. 'addresses' => $addresses,
  221. 'subject' => $subject,
  222. 'message' => $message,
  223. 'format' => $format,
  224. );
  225. // Token replacements for the subject and body
  226. $settings['replacements'] = array(
  227. 'uc_order' => $order,
  228. 'uc_role' => $role_expiration,
  229. );
  230. // Replace tokens and parse recipients.
  231. $recipients = array();
  232. $addresses = token_replace($settings['addresses'], $settings['replacements']);
  233. foreach (explode("\n", $addresses) as $address) {
  234. $address = trim($address);
  235. // Remove blank lines
  236. if (!empty($address)) {
  237. $recipients[] = $address;
  238. }
  239. }
  240. // Send to each recipient.
  241. foreach ($recipients as $email) {
  242. $sent = drupal_mail('uc_order', 'action-mail', $email, uc_store_mail_recipient_language($email), $settings, $settings['from']);
  243. if (!$sent['result']) {
  244. watchdog('uc_roles', 'Attempt to e-mail @email concerning order @order_id failed.', array('@email' => $email, '@order_id' => $order->order_id), WATCHDOG_ERROR);
  245. }
  246. }
  247. }
  248. /**
  249. * Send an email with order and role replacement tokens.
  250. *
  251. * The recipients, subject, and message fields take order token replacements.
  252. *
  253. * @see uc_roles_action_user_email_form()
  254. */
  255. function uc_roles_action_user_email($account, $role_expiration, $from, $addresses, $subject, $message, $format) {
  256. $settings = array(
  257. 'from' => $from,
  258. 'addresses' => $addresses,
  259. 'subject' => $subject,
  260. 'message' => $message,
  261. 'format' => $format,
  262. );
  263. // Token replacements for the subject and body
  264. $settings['replacements'] = array(
  265. 'user' => $account,
  266. 'uc_role' => $role_expiration,
  267. );
  268. // Replace tokens and parse recipients.
  269. $recipients = array();
  270. $addresses = token_replace($settings['addresses'], $settings['replacements']);
  271. foreach (explode("\n", $addresses) as $address) {
  272. $address = trim($address);
  273. // Remove blank lines
  274. if (!empty($address)) {
  275. $recipients[] = $address;
  276. }
  277. }
  278. // Send to each recipient.
  279. foreach ($recipients as $email) {
  280. $sent = drupal_mail('uc_order', 'action-mail', $email, uc_store_mail_recipient_language($email), $settings, $settings['from']);
  281. if (!$sent['result']) {
  282. watchdog('uc_roles', 'Attempt to e-mail @email concerning role notification failed.', array('@email' => $email), WATCHDOG_ERROR);
  283. }
  284. }
  285. }
  286. /**
  287. * Renews an order's product roles.
  288. *
  289. * This function updates expiration time on all roles found on all products
  290. * on a given order. First, the order user is loaded, then the order's products
  291. * are scanned for role product features. If any are found, the expiration time
  292. * of the role is set using the feature settings to determine the new length of
  293. * time the new expiration will last. An order comment is saved, and the user
  294. * is notified in Drupal, as well as through the email address associated with
  295. * the order.
  296. *
  297. * @param $order
  298. * An Ubercart order object.
  299. * @param $message
  300. * If TRUE, messages will be displayed to the user about the renewal.
  301. */
  302. function uc_roles_action_order_renew($order, $message) {
  303. // Load the order's user and exit if not available.
  304. if (!($account = user_load($order->uid))) {
  305. return;
  306. }
  307. // Loop through all the products on the order.
  308. foreach ($order->products as $product) {
  309. // Look for any role promotion features assigned to the product.
  310. $roles = db_query("SELECT * FROM {uc_roles_products} WHERE nid = :nid", array(':nid' => $product->nid));
  311. foreach ($roles as $role) {
  312. // Product model matches, or was 'any'.
  313. if (!empty($role->model) && $role->model != $product->model) {
  314. continue;
  315. }
  316. $existing_role = db_query("SELECT * FROM {uc_roles_expirations} WHERE uid = :uid AND rid = :rid", array(':uid' => $account->uid, ':rid' => $role->rid))->fetchObject();
  317. // Determine the expiration timestamp for the role.
  318. $expiration = _uc_roles_product_get_expiration($role, $product->qty, isset($existing_role->expiration) ? $existing_role->expiration : NULL);
  319. // Leave an order comment.
  320. if (isset($existing_role->expiration)) {
  321. $op = 'renew';
  322. $comment = t('Customer user role %role renewed.', array('%role' => _uc_roles_get_name($role->rid)));
  323. // Renew the user's role.
  324. uc_roles_renew($account, $role->rid, $expiration, !$message);
  325. }
  326. else {
  327. $op = 'grant';
  328. $comment = t('Customer granted user role %role.', array('%role' => _uc_roles_get_name($role->rid)));
  329. // Grant the role to the user.
  330. uc_roles_grant($account, $role->rid, $expiration, TRUE, !$message);
  331. }
  332. // Get the new expiration (if applicable)
  333. $new_expiration = db_query("SELECT * FROM {uc_roles_expirations} WHERE uid = :uid AND rid = :rid", array(':uid' => $account->uid, ':rid' => $role->rid))->fetchObject();
  334. if (!$new_expiration) {
  335. $new_expiration = new stdClass();
  336. $new_expiration->uid = $account->uid;
  337. $new_expiration->rid = $role->rid;
  338. $new_expiration->expiration = NULL;
  339. }
  340. uc_order_comment_save($order->order_id, $account->uid, $comment);
  341. // Trigger role email.
  342. rules_invoke_event('uc_roles_notify_' . $op, $order, $new_expiration);
  343. }
  344. }
  345. }