uc_coupon_purchase.rules.inc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. /**
  3. * @file
  4. * Rules integration for uc_coupon_purchase
  5. */
  6. /**
  7. * Implements hook_rules_event_info().
  8. */
  9. function uc_coupon_purchase_rules_event_info() {
  10. $events = array();
  11. $events['uc_coupon_purchase'] = array(
  12. 'label' => t('Customer purchases a coupon'),
  13. 'group' => t('Coupon'),
  14. 'variables' => array(
  15. 'order' => array(
  16. 'type' => 'uc_order',
  17. 'label' => t('Order'),
  18. ),
  19. 'coupon' => array(
  20. 'type' => 'uc_coupon',
  21. 'label' => t('Coupon'),
  22. ),
  23. 'product' => array(
  24. 'type' => 'uc_order_product',
  25. 'label' => t('The purchased product of which the coupon is a feature.'),
  26. ),
  27. ),
  28. );
  29. return $events;
  30. }
  31. /**
  32. * Implements hook_rules_action_info().
  33. */
  34. function uc_coupon_purchase_rules_action_info() {
  35. $actions = array();
  36. $actions['uc_coupon_purchase_email'] = array(
  37. 'label' => t('Send an order email regarding coupon purchase.'),
  38. 'group' => t('Coupon'),
  39. 'parameter' => array(
  40. 'order' => array(
  41. 'type' => 'uc_order',
  42. 'label' => t('Order'),
  43. 'optional' => TRUE,
  44. ),
  45. 'coupon' => array(
  46. 'type' => 'uc_coupon',
  47. 'label' => t('Coupon'),
  48. ),
  49. 'from' => array(
  50. 'type' => 'text',
  51. 'label' => t('Sender'),
  52. ),
  53. 'addresses' => array(
  54. 'type' => 'text',
  55. 'label' => t('Recipients'),
  56. 'description' => t('Enter the email addresses to receive the notifications, one on each line. You may use order tokens for dynamic email addresses.'),
  57. ),
  58. 'subject' => array(
  59. 'type' => 'text',
  60. 'label' => t('Subject'),
  61. ),
  62. 'message' => array(
  63. 'type' => 'text',
  64. 'label' => t('Message'),
  65. ),
  66. 'format' => array(
  67. 'type' => 'text',
  68. 'label' => t('Message format'),
  69. 'options list' => 'uc_coupon_purchase_message_formats',
  70. ),
  71. ),
  72. );
  73. $actions['uc_coupon_purchase_assign_action'] = array(
  74. 'label' => t('Assign a coupon to a user'),
  75. 'group' => t('Coupon'),
  76. 'provides' => array(
  77. 'coupon' => array(
  78. 'type' => 'uc_coupon',
  79. 'label' => t('Coupon'),
  80. ),
  81. ),
  82. 'named parameter' => TRUE,
  83. 'parameter' => array(
  84. 'cid' => array(
  85. 'type' => 'integer',
  86. 'label' => t('Coupon'),
  87. 'description' => t('Select the base coupon to be assigned.'),
  88. 'options list' => 'uc_coupon_purchase_cid_options',
  89. ),
  90. 'account' => array(
  91. 'type' => 'user',
  92. 'label' => t('Account'),
  93. 'description' => t('Select the user to whom the coupon should be assigned.'),
  94. ),
  95. 'restrict' => array(
  96. 'type' => 'boolean',
  97. 'label' => t('Restrict coupon usage to assigned users.'),
  98. 'description' => t('When selected, the assignee will be added to the list of authorized
  99. users for this coupon. Only authorized users will be able to use the coupon.'),
  100. 'default value' => FALSE,
  101. 'allow null' => TRUE,
  102. 'restriction' => 'input',
  103. 'optional' => TRUE,
  104. ),
  105. 'sole' => array(
  106. 'type' => 'boolean',
  107. 'label' => t('Make this user the sole owner.'),
  108. 'description' => t('When selected, all other assigned users will be removed.
  109. If you check "%r" above, this user will become the only person authorized to use this coupon.', array(
  110. '%r' => t('Restrict coupon usage to assigned users.')
  111. )),
  112. 'default value' => FALSE,
  113. 'allow null' => TRUE,
  114. 'restriction' => 'input',
  115. 'optional' => TRUE,
  116. ),
  117. 'clone' => array(
  118. 'type' => 'boolean',
  119. 'label' => t('Clone the base coupon before assigning.'),
  120. 'description' => t('When checked, the base coupon will be cloned and a copy assigned to the specified user,
  121. exactly as if the user had purchased the coupon. If unchecked, the base coupon will be assigned to the
  122. user directly'),
  123. 'default value' => FALSE,
  124. 'allow null' => TRUE,
  125. 'restriction' => 'input',
  126. 'optional' => TRUE,
  127. ),
  128. 'quantity' => array(
  129. 'type' => 'integer',
  130. 'label' => t('Quantity.'),
  131. 'description' => t('The number of copies of the base coupon to assign. If the base coupon is a bulk coupon,
  132. this will be multiplied by the base coupon\'s bulk number. Only meaningful if you select "%c" above.', array(
  133. '%c' => t('Clone the base coupon before assigning.'),
  134. )),
  135. 'default value' => 1,
  136. 'allow null' => TRUE,
  137. 'restriction' => 'input',
  138. 'optional' => TRUE,
  139. ),
  140. ),
  141. );
  142. return $actions;
  143. }
  144. /**
  145. * Rules form_alter callback for the uc_coupon_purchase_assign_action.
  146. */
  147. function uc_coupon_purchase_assign_action_form_alter(&$form, &$form_state) {
  148. if (isset($form['parameter']['cid']['settings']['cid'])) {
  149. // Add a set of radio buttons to limit the choice of coupons by status.
  150. $form['parameter']['cid']['settings']['cid']['#prefix'] = '<div id="uc-coupon-purchase-assign-cid">';
  151. $form['parameter']['cid']['settings']['cid']['#suffix'] = '</div>';
  152. //$cid_type = empty($form_state['values']['parameter']['cid']['settings']['cid_type']) ?
  153. // 0 : $form_state['values']['parameter']['cid']['settings']['cid_type'];
  154. //$form['parameter']['cid']['settings']['cid']['#options'] = uc_coupon_purchase_cid_options($cid_type);
  155. $form['parameter']['cid']['settings']['cid_type'] = array(
  156. '#type' => 'radios',
  157. '#title' => t('Choose from'),
  158. '#options' => array(
  159. 0 => t('All coupons'),
  160. 1 => t('Inactive coupons'),
  161. 2 => t('Active coupons'),
  162. ),
  163. '#default_value' => 0,
  164. '#ajax' => array(
  165. 'callback' => 'uc_coupon_purchase_assign_action_form_ajax',
  166. 'wrapper' => 'uc-coupon-purchase-assign-cid',
  167. ),
  168. );
  169. }
  170. elseif (isset($form['parameter']['cid']['settings']['cid:select'])) {
  171. $form['parameter']['cid']['#description'] = t('Select the base coupon to be assigned. Note, you must choose the
  172. ID of the coupon (i.e. "%selector")', array('%selector' => 'coupon:cid'));
  173. }
  174. // Hide the "quantity" ofield when not applicable.
  175. $form['parameter']['quantity']['#states'] = array(
  176. 'invisible' => array(
  177. 'input[name="parameter[clone][settings][clone]"]' => array('checked' => FALSE),
  178. )
  179. );
  180. }
  181. /**
  182. * Ajax callback for the uc_coupon_purchase_assign action.
  183. */
  184. function uc_coupon_purchase_assign_action_form_ajax($form, $form_state) {
  185. return $form['parameter']['cid']['settings']['cid'];
  186. }
  187. /**
  188. * Options list callback for message formats.
  189. */
  190. function uc_coupon_purchase_message_formats() {
  191. global $user;
  192. $options = array();
  193. $formats = filter_formats($user);
  194. foreach ($formats as $format) {
  195. $options[$format->format] = $format->name;
  196. }
  197. return $options;
  198. }
  199. /**
  200. * Action callback to send an e-mail regarding coupon purchase.
  201. */
  202. function uc_coupon_purchase_email($order, $coupon, $from, $addresses, $subject, $message, $format) {
  203. $settings = array(
  204. 'from' => $from,
  205. 'subject' => $subject,
  206. 'message' => $message,
  207. 'format' => $format,
  208. 'replacements' => array(), // Replacements are handled directly by Rules.
  209. );
  210. // Split up our recipient e-mail addresses.
  211. $recipients = array();
  212. foreach (explode("\n", $addresses) as $address) {
  213. $recipients[] = trim($address);
  214. }
  215. // Send the e-mails.
  216. foreach ($recipients as $email) {
  217. $sent = drupal_mail('uc_order', 'action-mail', $email, uc_store_mail_recipient_language($email), $settings, empty($settings['from']) ? uc_store_email_from() : $settings['from']);
  218. if (!$sent['result']) {
  219. watchdog('uc_order', 'Attempt to e-mail @email concerning order @order_id failed.', array('@email' => $email, '@order_id' => $order->order_id), WATCHDOG_ERROR);
  220. }
  221. }
  222. }
  223. /**
  224. * Action callback to assign a coupon to a user.
  225. */
  226. function uc_coupon_purchase_assign_action($params) {
  227. $coupon = uc_coupon_load($params['cid']);
  228. if ($coupon->cid) {
  229. if (!empty($params['clone'])) {
  230. $coupon = uc_coupon_purchase_create($coupon, $params['quantity'], $params['account']->uid);
  231. }
  232. elseif (!db_query('SELECT cpuid FROM {uc_coupon_purchase_users} where uid = :uid AND cid = :cid',
  233. array(':uid' => $params['account']->uid, ':cid' => $coupon->cid))->fetchField(0)) {
  234. $record = array('uid' => $params['account']->uid, 'cid' => $coupon->cid);
  235. drupal_write_record('uc_coupon_purchase_users', $record);
  236. }
  237. if (!empty($params['sole'])) {
  238. db_delete('uc_coupon_purchase_users')
  239. ->condition('cid', $coupon->cid)
  240. ->condition('uid', $params['account']->uid, '!=')
  241. ->execute();
  242. }
  243. if (!empty($params['restrict'])) {
  244. if (!empty($params['sole'])) {
  245. $coupon->data['users'] = array();
  246. }
  247. $coupon->data['users'][$params['account']->uid] = $params['account']->uid;
  248. uc_coupon_save($coupon);
  249. }
  250. return array('coupon' => $coupon);
  251. }
  252. else {
  253. return array('coupon' => NULL);
  254. }
  255. }
  256. /**
  257. * Options list callback for list of coupons to assign.
  258. */
  259. function uc_coupon_purchase_cid_options($action) {
  260. if (empty($action->settings['cid_type'])) {
  261. return db_query('SELECT cid, name FROM {uc_coupons}')->fetchAllKeyed();
  262. }
  263. else {
  264. $status = $action->settings['cid_type'] - 1;
  265. return db_query('SELECT cid, name FROM {uc_coupons} where status = :status', array(':status' => $status))->fetchAllKeyed();
  266. }
  267. }