diff --git a/payment/uc_payment/src/Event/PaymentEnteredEvent.php b/payment/uc_payment/src/Event/PaymentEnteredEvent.php new file mode 100644 index 0000000..bfddc34 --- /dev/null +++ b/payment/uc_payment/src/Event/PaymentEnteredEvent.php @@ -0,0 +1,43 @@ +order = $uc_order; + $this->account = $account; + } + +} diff --git a/payment/uc_payment/uc_payment.rules.events.yml b/payment/uc_payment/uc_payment.rules.events.yml new file mode 100644 index 0000000..ae800ac --- /dev/null +++ b/payment/uc_payment/uc_payment.rules.events.yml @@ -0,0 +1,10 @@ +uc_payment_entered: + label: 'A payment gets entered for an order' + category: 'Payment' + context: + order: + type: 'entity:uc_order' + label: 'Order' + account: + type: 'entity:user' + label: 'User' diff --git a/payment/uc_payment/uc_payment.rules.inc b/payment/uc_payment/uc_payment.rules.inc deleted file mode 100644 index 4bcaae3..0000000 --- a/payment/uc_payment/uc_payment.rules.inc +++ /dev/null @@ -1,100 +0,0 @@ - t('A payment gets entered for an order'), - 'group' => t('Payment'), - 'variables' => array( - 'order' => array( - 'type' => 'uc_order', - 'label' => t('Order'), - ), - 'account' => array( - 'type' => 'user', - 'label' => t('User'), - ), - ), - ); - - return $events; -} - -/** - * Implements hook_rules_condition_info(). - */ -function uc_payment_rules_condition_info() { - $conditions['uc_payment_condition_order_balance'] = array( - 'label' => t('Check the order balance'), - 'group' => t('Payment'), - 'base' => 'uc_payment_condition_order_balance', - 'parameter' => array( - 'order' => array( - 'type' => 'uc_order', - 'label' => t('Order'), - 'restriction' => 'selector', - ), - 'balance_comparison' => array( - 'type' => 'text', - 'label' => t('Operator'), - 'options list' => 'uc_payment_condition_balance_options', - 'restriction' => 'input', - ), - 'include_authorizations' => array( - 'type' => 'boolean', - 'label' => t('Include authorizations?'), - 'description' => t('Should "authorization only" credit card transactions be used in calculating the order balance?'), - 'restriction' => 'input', - 'optional' => TRUE, - 'default value' => FALSE, - ), - ), - ); - - return $conditions; -} - -/** - * Condition: Check the current order balance. - */ -function uc_payment_condition_order_balance($order, $balance_comparison, $include_authorizations) { - $balance = uc_payment_balance($order); - if ($include_authorizations) { - foreach ((array) $order->data->cc_txns['authorizations'] as $auth_id => $data) { - $balance -= $data['amount']; - } - } - - switch ($balance_comparison) { - case 'less': - return $balance < 0; - case 'less_equal': - return $balance <= 0.01; - case 'equal': - return $balance < 0.01 && $balance > -0.01; - case 'greater': - return $balance >= 0.01; - } -} - -/** - * Returns balance options. - */ -function uc_payment_condition_balance_options() { - $zero = array('!zero' => uc_currency_format(0)); - $options = array( - 'less' => t('Balance is less than !zero.', $zero), - 'less_equal' => t('Balance is less than or equal to !zero.', $zero), - 'equal' => t('Balance is equal to !zero.', $zero), - 'greater' => t('Balance is greater than !zero.', $zero), - ); - - return $options; -} diff --git a/shipping/uc_fulfillment/src/Event/ShipmentSaveEvent.php b/shipping/uc_fulfillment/src/Event/ShipmentSaveEvent.php new file mode 100644 index 0000000..e514a0f --- /dev/null +++ b/shipping/uc_fulfillment/src/Event/ShipmentSaveEvent.php @@ -0,0 +1,42 @@ +order = $uc_order; + $this->expiration = $expiration; + } + +} diff --git a/shipping/uc_fulfillment/src/Shipment.php b/shipping/uc_fulfillment/src/Shipment.php index 31c459b..89bc98f 100644 --- a/shipping/uc_fulfillment/src/Shipment.php +++ b/shipping/uc_fulfillment/src/Shipment.php @@ -2,6 +2,7 @@ namespace Drupal\uc_fulfillment; +use Drupal\uc_fulfillment\ShipmentSaveEvent; use Drupal\uc_order\Entity\Order; use Drupal\uc_store\Address; use Drupal\uc_store\AddressInterface; @@ -494,6 +495,9 @@ class Shipment implements ShipmentInterface { \Drupal::moduleHandler()->invokeAll('uc_shipment', array('save', $this)); $order = Order::load($this->order_id); // rules_invoke_event('uc_shipment_save', $order, $shipment); + $event = new ShipmentSaveEvent($order, $shipment); + $event_dispatcher = \Drupal::service('event_dispatcher'); + $event_dispatcher->dispatch(ShipmentSaveEvent::EVENT_NAME, $event); } /** diff --git a/shipping/uc_fulfillment/uc_fulfillment.rules.events.yml b/shipping/uc_fulfillment/uc_fulfillment.rules.events.yml new file mode 100644 index 0000000..eba2cf1 --- /dev/null +++ b/shipping/uc_fulfillment/uc_fulfillment.rules.events.yml @@ -0,0 +1,10 @@ +uc_shipment_save: + label: 'A shipment is saved' + category: 'Fulfillment' + context: + order: + type: 'entity:uc_order' + label: 'Order' + shipment: + type: 'uc_shipment' + label: 'Shipment' diff --git a/uc_cart/src/CartManager.php b/uc_cart/src/CartManager.php index 71ad2dd..e3ba08d 100644 --- a/uc_cart/src/CartManager.php +++ b/uc_cart/src/CartManager.php @@ -4,6 +4,7 @@ namespace Drupal\uc_cart; use Drupal\Core\Session\AccountProxyInterface; use Drupal\user\Entity\User; +use Drupal\uc_cart\Event\CheckoutCompleteEvent; use Drupal\uc_order\OrderInterface; use Symfony\Component\HttpFoundation\Session\SessionInterface; @@ -87,10 +88,15 @@ class CartManager implements CartManagerInterface { $order->save(); - // Invoke the checkout complete trigger and hook. + // Invoke the checkout complete hook. $account = $order->getOwner(); \Drupal::moduleHandler()->invokeAll('uc_checkout_complete', array($order, $account)); + + // Trigger the checkout complete event. // rules_invoke_event('uc_checkout_complete', $order); + $event = new CheckoutCompleteEvent($order); + $event_dispatcher = \Drupal::service('event_dispatcher'); + $event_dispatcher->dispatch(CheckoutCompleteEvent::EVENT_NAME, $event); } $type = $order->data->complete_sale; diff --git a/uc_cart/src/Controller/CheckoutController.php b/uc_cart/src/Controller/CheckoutController.php index e4e38dd..11199ff 100644 --- a/uc_cart/src/Controller/CheckoutController.php +++ b/uc_cart/src/Controller/CheckoutController.php @@ -8,6 +8,7 @@ use Drupal\Core\Url; use Drupal\uc_cart\CartInterface; use Drupal\uc_cart\CartManagerInterface; use Drupal\uc_cart\Plugin\CheckoutPaneManager; +use Drupal\uc_cart\Event\CheckoutStartEvent; use Drupal\uc_order\Entity\Order; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Session\SessionInterface; @@ -167,9 +168,14 @@ class CheckoutController extends ControllerBase implements ContainerInjectionInt return $this->redirect('uc_cart.cart'); } - // Trigger the "Customer starts checkout" hook and event. + // Invoke the customer starts checkout hook. $this->moduleHandler()->invokeAll('uc_cart_checkout_start', array($order)); + + // Trigger the checkout start event. // rules_invoke_event('uc_cart_checkout_start', $order); + $event = new CheckoutStartEvent($order); + $event_dispatcher = \Drupal::service('event_dispatcher'); + $event_dispatcher->dispatch(CheckoutStartEvent::EVENT_NAME, $event); return $this->formBuilder()->getForm('Drupal\uc_cart\Form\CheckoutForm', $order); } diff --git a/uc_cart/src/Event/CheckoutCompleteEvent.php b/uc_cart/src/Event/CheckoutCompleteEvent.php new file mode 100644 index 0000000..d16b534 --- /dev/null +++ b/uc_cart/src/Event/CheckoutCompleteEvent.php @@ -0,0 +1,32 @@ +order = $uc_order; + } + +} diff --git a/uc_cart/src/Event/CheckoutStartEvent.php b/uc_cart/src/Event/CheckoutStartEvent.php new file mode 100644 index 0000000..1ed3001 --- /dev/null +++ b/uc_cart/src/Event/CheckoutStartEvent.php @@ -0,0 +1,32 @@ +order = $uc_order; + } + +} diff --git a/uc_cart/uc_cart.rules.events.yml b/uc_cart/uc_cart.rules.events.yml new file mode 100644 index 0000000..25a925b --- /dev/null +++ b/uc_cart/uc_cart.rules.events.yml @@ -0,0 +1,15 @@ +uc_cart_checkout_start: + label: 'Customer starts checkout' + category: 'Cart' + context: + order: + type: 'entity:uc_order' + label: 'Order' + +uc_cart_checkout_complete: + label: 'Customer completes checkout' + category: 'Cart' + context: + order: + type: 'entity:uc_order' + label: 'Order' diff --git a/uc_cart/uc_cart.rules.inc b/uc_cart/uc_cart.rules.inc deleted file mode 100644 index 133d8a7..0000000 --- a/uc_cart/uc_cart.rules.inc +++ /dev/null @@ -1,36 +0,0 @@ - t('Customer completes checkout'), - 'group' => t('Cart'), - 'variables' => array( - 'order' => array( - 'type' => 'uc_order', - 'label' => t('Order'), - ), - ), - ); - - $events['uc_cart_checkout_start'] = array( - 'label' => t('Customer starts checkout'), - 'group' => t('Cart'), - 'variables' => array( - 'order' => array( - 'type' => 'uc_order', - 'label' => t('Order'), - ), - ), - ); - - return $events; -} diff --git a/uc_file/src/Event/NotifyGrantEvent.php b/uc_file/src/Event/NotifyGrantEvent.php new file mode 100644 index 0000000..a29e184 --- /dev/null +++ b/uc_file/src/Event/NotifyGrantEvent.php @@ -0,0 +1,42 @@ +order = $uc_order; + $this->expiration = $expiration; + } + +} diff --git a/uc_file/uc_file.rules.events.yml b/uc_file/uc_file.rules.events.yml new file mode 100644 index 0000000..4e855e3 --- /dev/null +++ b/uc_file/uc_file.rules.events.yml @@ -0,0 +1,10 @@ +uc_file_notify_grant: + label: 'E-mail for granted files' + category: 'File downloads' + context: + order: + type: 'entity:uc_order' + label: 'Order' + expiration: + type: 'array' + label: 'File expiration' diff --git a/uc_order/src/Entity/Order.php b/uc_order/src/Entity/Order.php index 4425ff8..e72be6a 100644 --- a/uc_order/src/Entity/Order.php +++ b/uc_order/src/Entity/Order.php @@ -7,6 +7,7 @@ use Drupal\Core\Entity\EntityChangedTrait; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Field\BaseFieldDefinition; +use Drupal\uc_order\Event\OrderStatusUpdateEvent; use Drupal\uc_order\OrderInterface; use Drupal\uc_store\Address; use Drupal\user\Entity\User; @@ -143,6 +144,9 @@ class Order extends ContentEntityBase implements OrderInterface { ]); // rules_invoke_event('uc_order_status_update', $this->original, $this); + $event = new OrderStatusUpdateEvent($this->original, $this); + $event_dispatcher = \Drupal::service('event_dispatcher'); + $event_dispatcher->dispatch(OrderStatusUpdateEvent::EVENT_NAME, $event); } } diff --git a/uc_order/src/Event/OrderStatusEmailUpdateEvent.php b/uc_order/src/Event/OrderStatusEmailUpdateEvent.php new file mode 100644 index 0000000..6e94037 --- /dev/null +++ b/uc_order/src/Event/OrderStatusEmailUpdateEvent.php @@ -0,0 +1,32 @@ +order = $order; + } + +} diff --git a/uc_order/src/Event/OrderStatusUpdateEvent.php b/uc_order/src/Event/OrderStatusUpdateEvent.php new file mode 100644 index 0000000..566674b --- /dev/null +++ b/uc_order/src/Event/OrderStatusUpdateEvent.php @@ -0,0 +1,42 @@ +originalOrder = $originalOrder; + $this->order = $order; + } + +} diff --git a/uc_order/src/Form/OrderUpdateForm.php b/uc_order/src/Form/OrderUpdateForm.php index 9404a6a..174d0bc 100644 --- a/uc_order/src/Form/OrderUpdateForm.php +++ b/uc_order/src/Form/OrderUpdateForm.php @@ -6,6 +6,7 @@ use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\uc_order\Entity\Order; use Drupal\uc_order\Entity\OrderStatus; +use Drupal\uc_order\Event\OrderStatusEmailUpdateEvent; use Drupal\uc_order\OrderInterface; /** @@ -103,10 +104,13 @@ class OrderUpdateForm extends FormBase { } // Let Rules send email if requested. - // if ($form_state->getValue('notify')) { - // $order = Order::load($form_state->getValue('order_id')); - // rules_invoke_event('uc_order_status_email_update', $order); - // } + if ($form_state->getValue('notify')) { + $order = Order::load($form_state->getValue('order_id')); + // rules_invoke_event('uc_order_status_email_update', $order); + $event = new OrderStatusEmailUpdateEvent($order); + $event_dispatcher = \Drupal::service('event_dispatcher'); + $event_dispatcher->dispatch(OrderStatusEmailUpdateEvent::EVENT_NAME, $event); + } drupal_set_message($this->t('Order updated.')); } diff --git a/uc_order/uc_order.rules.events.yml b/uc_order/uc_order.rules.events.yml new file mode 100644 index 0000000..1c5c2bd --- /dev/null +++ b/uc_order/uc_order.rules.events.yml @@ -0,0 +1,18 @@ +uc_order_status_update: + label: 'Order status gets updated' + category: 'Order status' + context: + original_order: + type: 'entity:uc_order' + label: 'Original order' + updated_order: + type: 'entity:uc_order' + label: 'Updated order' + +uc_order_status_email_update: + label: 'E-mail requested for order status update' + category: 'Order status' + context: + order: + type: 'entity:uc_order' + label: 'Order' diff --git a/uc_role/src/Event/NotifyGrantEvent.php b/uc_role/src/Event/NotifyGrantEvent.php new file mode 100644 index 0000000..06a920e --- /dev/null +++ b/uc_role/src/Event/NotifyGrantEvent.php @@ -0,0 +1,42 @@ +order = $uc_order; + $this->expiration = $expiration; + } + +} diff --git a/uc_role/src/Event/NotifyReminderEvent.php b/uc_role/src/Event/NotifyReminderEvent.php new file mode 100644 index 0000000..10c61a1 --- /dev/null +++ b/uc_role/src/Event/NotifyReminderEvent.php @@ -0,0 +1,42 @@ +account = $account; + $this->expiration = $expiration; + } + +} diff --git a/uc_role/src/Event/NotifyRenewEvent.php b/uc_role/src/Event/NotifyRenewEvent.php new file mode 100644 index 0000000..9ea2b77 --- /dev/null +++ b/uc_role/src/Event/NotifyRenewEvent.php @@ -0,0 +1,42 @@ +order = $uc_order; + $this->expiration = $expiration; + } + +} diff --git a/uc_role/src/Event/NotifyRevokeEvent.php b/uc_role/src/Event/NotifyRevokeEvent.php new file mode 100644 index 0000000..2a67ba7 --- /dev/null +++ b/uc_role/src/Event/NotifyRevokeEvent.php @@ -0,0 +1,43 @@ +order = $account; + $this->expiration = $expiration; + } + +} diff --git a/uc_role/uc_role.module b/uc_role/uc_role.module index 81e6857..ee6e1a4 100644 --- a/uc_role/uc_role.module +++ b/uc_role/uc_role.module @@ -17,6 +17,8 @@ use Drupal\Core\Session\AccountInterface; use Drupal\Core\Url; use Drupal\user\Entity\User; use Drupal\user\UserInterface; +use Drupal\uc_role\Event\NotifyRevokeEvent; +use Drupal\uc_role\Event\NotifyReminderEvent; /** * Implements hook_help(). @@ -66,12 +68,18 @@ function uc_role_cron() { // Role expired. elseif ($expiration->expiration <= REQUEST_TIME) { // rules_invoke_event('uc_role_notify_revoke', $account, $expiration); + $event = new NotifyRevokeEvent($account, $expiration); + $event_dispatcher = \Drupal::service('event_dispatcher'); + $event_dispatcher->dispatch(NotifyRevokeEvent::EVENT_NAME, $event); uc_role_revoke($account, $expiration->rid); } // Remind the user about an upcoming expiration. elseif ($reminder_granularity != 'never') { // rules_invoke_event('uc_role_notify_reminder', $account, $expiration); + $event = new NotifyReminderEvent($account, $expiration); + $event_dispatcher = \Drupal::service('event_dispatcher'); + $event_dispatcher->dispatch(NotifyReminderEvent::EVENT_NAME, $event); db_update('uc_roles_expirations') ->fields(array('notified' => 1)) ->condition('uid', $account->id()) diff --git a/uc_role/uc_role.rules.events.yml b/uc_role/uc_role.rules.events.yml new file mode 100644 index 0000000..064e5f1 --- /dev/null +++ b/uc_role/uc_role.rules.events.yml @@ -0,0 +1,48 @@ +# function uc_role_rules_event_info() { +# $expiration = array( +# 'type' => 'uc_role_expiration', +# 'label' => t('Role expiration'), + +uc_role_notify_grant: + label: 'E-mail for granted roles' + category: 'Role' + context: + order: + type: 'entity:uc_order' + label: 'Order' + expiration: + type: 'array' + label: 'Role expiration' + +uc_role_notify_revoke: + label: 'E-mail for revoked roles' + category: 'Role' + context: + account: + type: 'entity:user' + label: 'User' + expiration: + type: 'array' + label: 'Role expiration' + +uc_role_notify_renew: + label: 'E-mail for renewed roles' + category: 'Role' + context: + order: + type: 'entity:uc_order' + label: 'Order' + expiration: + type: 'array' + label: 'Role expiration' + +uc_role_notify_reminder: + label: 'E-mail for role expiration reminders' + category: 'Role' + context: + account: + type: 'entity:user' + label: 'User' + expiration: + type: 'array' + label: 'Role expiration' diff --git a/uc_role/uc_role.rules.inc b/uc_role/uc_role.rules.inc deleted file mode 100644 index cb86fb2..0000000 --- a/uc_role/uc_role.rules.inc +++ /dev/null @@ -1,383 +0,0 @@ - t('Ubercart role expiration'), - 'wrap' => TRUE, - 'token type' => 'uc_role', - 'property info' => array( - 'reid' => array( - 'type' => 'integer', - 'label' => t('Role expiration ID'), - 'description' => t('Primary key for role expirations.'), - ), - 'uid' => array( - 'type' => 'integer', - 'label' => t('User ID'), - 'description' => t('The user account ID.'), - ), - 'user' => array( - 'type' => 'user', - 'label' => t('User'), - 'description' => t('The user account that has the role.'), - 'getter callback' => 'uc_role_get_expiration_properties', - 'setter callback' => 'uc_role_set_expiration_properties', - ), - 'rid' => array( - 'type' => 'integer', - 'label' => t('Role ID'), - 'description' => t('The granted role.'), - ), - 'expiration' => array( - 'type' => 'date', - 'label' => t('Expiration time'), - 'description' => t('The time the role will be removed from the user.'), - ), - 'notified' => array( - 'type' => 'boolean', - 'label' => t('Notified'), - 'description' => t('Indicates the user has been warned that the role will be removed soon.'), - ), - ), - ); - - return $entities; -} - -/** - * Callback for getting role expiration properties. - * - * @see entity_metadata_node_entity_info_alter() - */ -function uc_role_get_expiration_properties($expiration, array $options, $name, $entity_type) { - switch ($name) { - case 'user': - return $expiration->uid; - } -} - -/** - * Callback for setting role expiration properties. - * - * @see entity_metadata_node_entity_info_alter() - */ -function uc_role_set_expiration_properties($expiration, $name, $value) { - if ($name == 'user') { - $expiration->uid = $value; - } -} - -/** - * Implements hook_rules_action_info(). - */ -function uc_role_rules_action_info() { - // Renew a role expiration. - $actions['uc_role_order_renew'] = array( - 'label' => t('Renew the roles on an order.'), - 'group' => t('Renewal'), - 'base' => 'uc_role_action_order_renew', - 'parameter' => array( - 'order' => array( - 'type' => 'uc_order', - 'label' => t('Order'), - ), - 'message' => array( - 'type' => 'boolean', - 'label' => t('Display messages to alert users of any new or updated roles.'), - ), - ), - ); - - $email_args = array( - 'expiration' => array( - 'type' => 'uc_role_expiration', - 'label' => t('Role expiration'), - ), - 'from' => array( - 'type' => 'text', - 'label' => t('Sender'), - ), - 'addresses' => array( - 'type' => 'text', - 'label' => t('Recipients'), - ), - 'subject' => array( - 'type' => 'text', - 'label' => t('Subject'), - ), - 'message' => array( - 'type' => 'text', - 'label' => t('Message'), - ), - 'format' => array( - 'type' => 'text', - 'label' => t('Message format'), - 'options list' => 'uc_role_message_formats', - ), - ); - - // Send an email to an order with a role expiration - $actions['uc_role_order_email'] = array( - 'label' => t('Send an order email regarding roles.'), - 'group' => t('Notification'), - 'base' => 'uc_role_action_order_email', - 'parameter' => array( - 'order' => array( - 'type' => 'uc_order', - 'label' => t('Order'), - ), - ) + $email_args, - ); - - // Send an email to a user with a role expiration - $actions['uc_role_user_email'] = array( - 'label' => t('Send a user an email regarding roles.'), - 'group' => t('Notification'), - 'base' => 'uc_role_action_user_email', - 'parameter' => array( - 'account' => array( - 'type' => 'user', - 'label' => t('User'), - ), - ) + $email_args, - ); - - return $actions; -} - -/** - * Options list callback for message formats. - */ -function uc_role_message_formats() { - global $user; - - $options = array(); - $formats = filter_formats($user); - foreach ($formats as $format) { - $options[$format->format] = $format->name; - } - - return $options; -} - -/** - * Implements hook_rules_event_info(). - */ -function uc_role_rules_event_info() { - $order = array( - 'type' => 'uc_order', - 'label' => t('Order'), - ); - $account = array( - 'type' => 'user', - 'label' => t('User'), - ); - $expiration = array( - 'type' => 'uc_role_expiration', - 'label' => t('Role expiration'), - ); - - $events['uc_role_notify_grant'] = array( - 'label' => t('E-mail for granted roles'), - 'group' => t('Notification'), - 'variables' => array( - 'order' => $order, - 'expiration' => $expiration, - ), - ); - - $events['uc_role_notify_revoke'] = array( - 'label' => t('E-mail for revoked roles'), - 'group' => t('Notification'), - 'variables' => array( - 'account' => $account, - 'expiration' => $expiration, - ), - ); - - $events['uc_role_notify_renew'] = array( - 'label' => t('E-mail for renewed roles'), - 'group' => t('Notification'), - 'variables' => array( - 'order' => $order, - 'expiration' => $expiration, - ), - ); - - $events['uc_role_notify_reminder'] = array( - 'label' => t('E-mail for role expiration reminders'), - 'group' => t('Notification'), - 'variables' => array( - 'account' => $account, - 'expiration' => $expiration, - ), - ); - - return $events; -} - -/** - * Send an email with order and role replacement tokens. - * - * The recipients, subject, and message fields take order token replacements. - * - * @see uc_role_action_order_email_form() - */ -function uc_role_action_order_email($order, $role_expiration, $from, $addresses, $subject, $message, $format) { - $settings = array( - 'from' => $from, - 'addresses' => $addresses, - 'subject' => $subject, - 'message' => $message, - 'format' => $format, - ); - // Token replacements for the subject and body - $settings['replacements'] = array( - 'uc_order' => $order, - 'uc_role' => $role_expiration, - ); - - // Replace tokens and parse recipients. - $recipients = array(); - $addresses = \Drupal::token()->replace($settings['addresses'], $settings['replacements']); - foreach (explode("\n", $addresses) as $address) { - $address = trim($address); - // Remove blank lines - if (!empty($address)) { - $recipients[] = $address; - } - } - - // Send to each recipient. - foreach ($recipients as $email) { - $sent = \Drupal::service('plugin.manager.mail')->mail('uc_order', 'action-mail', $email, uc_store_mail_recipient_langcode($email), $settings, $settings['from']); - - if (!$sent['result']) { - \Drupal::logger('uc_role')->error('Attempt to e-mail @email concerning order @order_id failed.', ['@email' => $email, '@order_id' => $order->id()]); - } - } -} - -/** - * Send an email with order and role replacement tokens. - * - * The recipients, subject, and message fields take order token replacements. - * - * @see uc_role_action_user_email_form() - */ -function uc_role_action_user_email($account, $role_expiration, $from, $addresses, $subject, $message, $format) { - $settings = array( - 'from' => $from, - 'addresses' => $addresses, - 'subject' => $subject, - 'message' => $message, - 'format' => $format, - ); - // Token replacements for the subject and body - $settings['replacements'] = array( - 'user' => $account, - 'uc_role' => $role_expiration, - ); - - // Replace tokens and parse recipients. - $recipients = array(); - $addresses = \Drupal::token()->replace($settings['addresses'], $settings['replacements']); - foreach (explode("\n", $addresses) as $address) { - $address = trim($address); - // Remove blank lines - if (!empty($address)) { - $recipients[] = $address; - } - } - - // Send to each recipient. - foreach ($recipients as $email) { - $sent = \Drupal::service('plugin.manager.mail')->mail('uc_order', 'action-mail', $email, uc_store_mail_recipient_langcode($email), $settings, $settings['from']); - - if (!$sent['result']) { - \Drupal::logger('uc_role')->error('Attempt to e-mail @email concerning role notification failed.', ['@email' => $email]); - } - } -} - -/** - * Renews an order's product roles. - * - * This function updates expiration time on all roles found on all products - * on a given order. First, the order user is loaded, then the order's products - * are scanned for role product features. If any are found, the expiration time - * of the role is set using the feature settings to determine the new length of - * time the new expiration will last. An order comment is saved, and the user - * is notified in Drupal, as well as through the email address associated with - * the order. - * - * @param $order - * An Ubercart order object. - * @param $message - * If TRUE, messages will be displayed to the user about the renewal. - */ -function uc_role_action_order_renew($order, $message) { - // Load the order's user and exit if not available. - if (!($account = $order->getOwner())) { - return; - } - - // Loop through all the products on the order. - foreach ($order->products as $product) { - // Look for any role promotion features assigned to the product. - $roles = db_query('SELECT * FROM {uc_roles_products} WHERE nid = :nid', [':nid' => $product->nid]); - - foreach ($roles as $role) { - // Product model matches, or was 'any'. - if (!empty($role->model) && $role->model != $product->model) { - continue; - } - - $existing_role = db_query('SELECT * FROM {uc_roles_expirations} WHERE uid = :uid AND rid = :rid', [':uid' => $account->id(), ':rid' => $role->rid])->fetchObject(); - - // Determine the expiration timestamp for the role. - $expiration = _uc_role_product_get_expiration($role, $product->qty, isset($existing_role->expiration) ? $existing_role->expiration : NULL); - - // Leave an order comment. - if (isset($existing_role->expiration)) { - $op = 'renew'; - $comment = t('Customer user role %role renewed.', ['%role' => _uc_role_get_name($role->rid)]); - - // Renew the user's role. - uc_role_renew($account, $role->rid, $expiration, !$message); - } - else { - $op = 'grant'; - $comment = t('Customer granted user role %role.', ['%role' => _uc_role_get_name($role->rid)]); - - // Grant the role to the user. - uc_role_grant($account, $role->rid, $expiration, TRUE, !$message); - } - - // Get the new expiration (if applicable) - $new_expiration = db_query('SELECT * FROM {uc_roles_expirations} WHERE uid = :uid AND rid = :rid', [':uid' => $account->id(), ':rid' => $role->rid])->fetchObject(); - if (!$new_expiration) { - $new_expiration = new stdClass(); - $new_expiration->uid = $account->uid; - $new_expiration->rid = $role->rid; - $new_expiration->expiration = NULL; - } - - uc_order_comment_save($order->id(), $account->id(), $comment); - - // Trigger role email. - rules_invoke_event('uc_role_notify_' . $op, $order, $new_expiration); - } - } -}