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..ede17ee --- /dev/null +++ b/shipping/uc_fulfillment/src/Event/ShipmentSaveEvent.php @@ -0,0 +1,43 @@ +order = $uc_order; + $this->shipment = $shipment; + } + +} diff --git a/shipping/uc_fulfillment/src/Shipment.php b/shipping/uc_fulfillment/src/Shipment.php index 31c459b..2f59ea6 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\Event\ShipmentSaveEvent; use Drupal\uc_order\Entity\Order; use Drupal\uc_store\Address; use Drupal\uc_store\AddressInterface; @@ -23,7 +24,7 @@ class Shipment implements ShipmentInterface { * * @var int */ - protected $order_id; + protected $orderId; /** * Name of the shipping method. @@ -54,7 +55,7 @@ class Shipment implements ShipmentInterface { protected $transaction_id = ''; /** - * Shipment tracking number, + * Shipment tracking number. * * @var string */ @@ -100,7 +101,7 @@ class Shipment implements ShipmentInterface { * * @var \Drupal\uc_fulfillment\Package[] */ - protected $packages = array(); + protected $packages = []; /** * Shipment origin address. @@ -126,8 +127,8 @@ class Shipment implements ShipmentInterface { /** * {@inheritdoc} */ - public function setOrderId($order_id) { - $this->order_id = $order_id; + public function setOrderId($orderId) { + $this->orderId = $orderId; return $this; } @@ -135,7 +136,7 @@ class Shipment implements ShipmentInterface { * {@inheritdoc} */ public function getOrderId() { - return $this->order_id; + return $this->orderId; } /** @@ -361,15 +362,15 @@ class Shipment implements ShipmentInterface { /** * Loads a shipment and its packages for a given order. * - * @param array $order_id + * @param int $orderId * An order ID. * * @return \Drupal\uc_fulfillment\Shipment[] * Array of shipment objects for the given order. */ - public static function loadByOrder($order_id) { - $shipments = array(); - $result = db_query('SELECT sid FROM {uc_shipments} WHERE order_id = :id', [':id' => $order_id]); + public static function loadByOrder($orderId) { + $shipments = []; + $result = db_query('SELECT sid FROM {uc_shipments} WHERE order_id = :id', [':id' => $orderId]); while ($shipment_id = $result->fetchField()) { $shipments[] = Shipment::load($shipment_id); } @@ -391,8 +392,8 @@ class Shipment implements ShipmentInterface { $result = db_query('SELECT * FROM {uc_shipments} WHERE sid = :sid', [':sid' => $shipment_id]); if ($assoc = $result->fetchAssoc()) { $shipment = Shipment::create(); - $origin_fields = array(); - $destination_fields = array(); + $origin_fields = []; + $destination_fields = []; foreach ($assoc as $key => $value) { $subkey = substr($key, 0, 2); @@ -411,13 +412,13 @@ class Shipment implements ShipmentInterface { $shipment->setDestination(Address::create($destination_fields)); $result2 = db_query('SELECT package_id FROM {uc_packages} WHERE sid = :sid', [':sid' => $shipment_id]); - $packages = array(); + $packages = []; foreach ($result2 as $package) { $packages[$package->package_id] = Package::load($package->package_id); } $shipment->setPackages($packages); - $extra = \Drupal::moduleHandler()->invokeAll('uc_shipment', array('load', $shipment)); + $extra = \Drupal::moduleHandler()->invokeAll('uc_shipment', ['load', $shipment]); if (is_array($extra)) { foreach ($extra as $key => $value) { $shipment->$key = $value; @@ -435,7 +436,7 @@ class Shipment implements ShipmentInterface { $this->changed = time(); // Break Address objects into individual fields for saving. - $fields = array(); + $fields = []; if (isset($this->origin)) { foreach ($this->origin as $field => $value) { $field = 'o_' . $field; @@ -450,8 +451,8 @@ class Shipment implements ShipmentInterface { } // Yuck. - $fields += array( - 'order_id' => $this->order_id, + $fields += [ + 'order_id' => $this->orderId, 'shipping_method' => $this->shipping_method, 'accessorials' => $this->accessorials, 'carrier' => $this->carrier, @@ -462,7 +463,7 @@ class Shipment implements ShipmentInterface { 'cost' => $this->cost, 'currency' => $this->currency, 'changed' => $this->changed, - ); + ]; if (!isset($this->sid)) { $this->sid = db_insert('uc_shipments') ->fields($fields) @@ -491,9 +492,12 @@ 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); + \Drupal::moduleHandler()->invokeAll('uc_shipment', ['save', $this]); + $order = Order::load($this->orderId); + // rules_invoke_event('uc_shipment_save', $order, $this); + $event = new ShipmentSaveEvent($order, $this); + $event_dispatcher = \Drupal::service('event_dispatcher'); + $event_dispatcher->dispatch(ShipmentSaveEvent::EVENT_NAME, $event); } /** @@ -501,11 +505,11 @@ class Shipment implements ShipmentInterface { */ public function delete() { db_update('uc_packages') - ->fields(array( + ->fields([ 'sid' => NULL, 'tracking_number' => NULL, 'label_image' => NULL, - )) + ]) ->condition('sid', $this->sid) ->execute(); @@ -520,7 +524,7 @@ class Shipment implements ShipmentInterface { } } - \Drupal::moduleHandler()->invokeAll('uc_shipment', array('delete', $this)); + \Drupal::moduleHandler()->invokeAll('uc_shipment', ['delete', $this]); drupal_set_message(t('Shipment @id has been deleted.', ['@id' => $this->sid])); } 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..f5d0fbe 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)); + \Drupal::moduleHandler()->invokeAll('uc_checkout_complete', [$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; @@ -104,17 +110,17 @@ class CartManager implements CartManagerInterface { } $message = \Drupal::config('uc_cart.messages')->get($type); - $message = \Drupal::token()->replace($message, array('uc_order' => $order)); + $message = \Drupal::token()->replace($message, ['uc_order' => $order]); $variables['!new_username'] = isset($order->data->new_user_name) ? $order->data->new_user_name : ''; $variables['!new_password'] = isset($order->password) ? $order->password : t('Your password'); $message = strtr($message, $variables); - return array( + return [ '#theme' => 'uc_cart_complete_sale', - '#message' => array('#markup' => $message), + '#message' => ['#markup' => $message], '#order' => $order, - ); + ]; } /** @@ -139,14 +145,14 @@ class CartManager implements CartManagerInterface { // Set up a new user. $cart_config = \Drupal::config('uc_cart.settings'); - $fields = array( + $fields = [ 'name' => uc_store_email_to_username($order->getEmail()), 'mail' => $order->getEmail(), 'init' => $order->getEmail(), 'pass' => user_password(), - 'roles' => array(), + 'roles' => [], 'status' => $cart_config->get('new_customer_status_active') ? 1 : 0, - ); + ]; // Override the username, if specified. if (isset($order->data->new_user_name)) { @@ -170,12 +176,12 @@ class CartManager implements CartManagerInterface { // Send the customer their account details if enabled. if ($cart_config->get('new_customer_email')) { $type = $cart_config->get('new_customer_status_active') ? 'register_no_approval_required' : 'register_pending_approval'; - \Drupal::service('plugin.manager.mail')->mail('user', $type, $order->getEmail(), uc_store_mail_recipient_langcode($order->getEmail()), array('account' => $account), uc_store_email_from()); + \Drupal::service('plugin.manager.mail')->mail('user', $type, $order->getEmail(), uc_store_mail_recipient_langcode($order->getEmail()), ['account' => $account], uc_store_email_from()); } $order->setOwner($account); $order->data->new_user_name = $fields['name']; - $order->data->complete_sale = 'new_user'; + $order->data->complete_sale = 'new_user'; } } diff --git a/uc_cart/src/Controller/CheckoutController.php b/uc_cart/src/Controller/CheckoutController.php index e4e38dd..a7e2b93 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; @@ -89,8 +90,9 @@ class CheckoutController extends ControllerBase implements ContainerInjectionInt if ($this->session->has('cart_order')) { $order = $this->loadOrder(); if ($order) { - // Don't use an existing order if it has changed status or owner, or if - // there has been no activity for 10 minutes (to prevent identity theft). + // To prevent identity theft, don't use an existing order if it has + // changed status or owner, or if there has been no activity for 10 + // minutes. if ($order->getStateId() != 'in_checkout' || ($this->currentUser()->isAuthenticated() && $this->currentUser()->id() != $order->getOwnerId()) || $order->getChangedTime() < REQUEST_TIME - CartInterface::CHECKOUT_TIMEOUT) { @@ -109,7 +111,7 @@ class CheckoutController extends ControllerBase implements ContainerInjectionInt } } - // Determine whether the form is being submitted or built for the first time. + // Determine if the form is being submitted or built for the first time. if (isset($_POST['form_id']) && $_POST['form_id'] == 'uc_cart_checkout_form') { // If this is a form submission, make sure the cart order is still valid. if (!isset($order)) { @@ -126,9 +128,9 @@ class CheckoutController extends ControllerBase implements ContainerInjectionInt $rebuild = FALSE; if (!isset($order)) { // Create a new order if necessary. - $order = Order::create(array( + $order = Order::create([ 'uid' => $this->currentUser()->id(), - )); + ]); $order->save(); $this->session->set('cart_order', $order->id()); $rebuild = TRUE; @@ -149,7 +151,7 @@ class CheckoutController extends ControllerBase implements ContainerInjectionInt if ($rebuild) { // Copy the cart contents to the cart order. - $order->products = array(); + $order->products = []; foreach ($items as $item) { $order->products[] = $item->toOrderProduct(); } @@ -167,9 +169,14 @@ class CheckoutController extends ControllerBase implements ContainerInjectionInt return $this->redirect('uc_cart.cart'); } - // Trigger the "Customer starts checkout" hook and event. - $this->moduleHandler()->invokeAll('uc_cart_checkout_start', array($order)); + // Invoke the customer starts checkout hook. + $this->moduleHandler()->invokeAll('uc_cart_checkout_start', [$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); } @@ -193,7 +200,7 @@ class CheckoutController extends ControllerBase implements ContainerInjectionInt return $this->redirect('uc_cart.cart'); } - $filter = array('enabled' => FALSE); + $filter = ['enabled' => FALSE]; // If the cart isn't shippable, bypass panes with shippable == TRUE. if (!$order->isShippable() && $this->config('uc_cart.settings')->get('panes.delivery.settings.delivery_not_shippable')) { @@ -208,11 +215,11 @@ class CheckoutController extends ControllerBase implements ContainerInjectionInt } } - $build = array( + $build = [ '#theme' => 'uc_cart_checkout_review', '#panes' => $data, '#form' => $this->formBuilder()->getForm('Drupal\uc_cart\Form\CheckoutReviewForm', $order), - ); + ]; $build['#attached']['library'][] = 'uc_cart/uc_cart.styles'; $build['#attached']['library'][] = 'uc_cart/uc_cart.review.scripts'; 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..f4b408d 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; @@ -55,8 +56,8 @@ class Order extends ContentEntityBase implements OrderInterface { use EntityChangedTrait; - public $products = array(); - public $line_items = array(); + public $products = []; + public $line_items = []; /** * {@inheritdoc} @@ -139,10 +140,13 @@ class Order extends ContentEntityBase implements OrderInterface { (string) t('Order status') => [ 'old' => $this->original->getStatus()->getName(), 'new' => $this->getStatus()->getName(), - ] + ], ]); // 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); } } @@ -186,7 +190,6 @@ class Order extends ContentEntityBase implements OrderInterface { return $this->get('created')->value; } - /** * {@inheritdoc} */ @@ -199,18 +202,18 @@ class Order extends ContentEntityBase implements OrderInterface { * {@inheritdoc} */ public function getLineItems() { - $items = array(); + $items = []; $result = db_query("SELECT * FROM {uc_order_line_items} WHERE order_id = :id", [':id' => $this->id()]); foreach ($result as $row) { - $items[] = array( + $items[] = [ 'line_item_id' => $row->line_item_id, 'type' => $row->type, 'title' => $row->title, 'amount' => $row->amount, 'weight' => $row->weight, 'data' => unserialize($row->data), - ); + ]; } $line_item_manager = \Drupal::service('plugin.manager.uc_order.line_item'); @@ -219,14 +222,14 @@ class Order extends ContentEntityBase implements OrderInterface { $result = $line_item_manager->createInstance($type['id'])->load($this); if ($result !== FALSE && is_array($result)) { foreach ($result as $line) { - $items[] = array( + $items[] = [ 'line_item_id' => $line['id'], 'type' => $type['id'], 'title' => $line['title'], 'amount' => $line['amount'], 'weight' => isset($line['weight']) ? $line['weight'] : $type['weight'], - 'data' => isset($line['data']) ? $line['data'] : array(), - ); + 'data' => isset($line['data']) ? $line['data'] : [], + ]; } } } @@ -249,14 +252,14 @@ class Order extends ContentEntityBase implements OrderInterface { $result = $line_item_manager->createInstance($item['id'])->display($this); if (is_array($result)) { foreach ($result as $line) { - $line_items[] = array( + $line_items[] = [ 'line_item_id' => $line['id'], 'type' => $item['id'], 'title' => $line['title'], 'amount' => $line['amount'], 'weight' => isset($line['weight']) ? $line['weight'] : $item['weight'], - 'data' => isset($line['data']) ? $line['data'] : array(), - ); + 'data' => isset($line['data']) ? $line['data'] : [], + ]; } } } @@ -465,15 +468,15 @@ class Order extends ContentEntityBase implements OrderInterface { $entry = (string) $value; } - $markup = array('#markup' => $entry); + $markup = ['#markup' => $entry]; db_insert('uc_order_log') - ->fields(array( + ->fields([ 'order_id' => $this->id(), 'uid' => \Drupal::currentUser()->id(), 'changes' => \Drupal::service('renderer')->renderPlain($markup), 'created' => REQUEST_TIME, - )) + ]) ->execute(); } } @@ -621,7 +624,7 @@ class Order extends ContentEntityBase implements OrderInterface { $fields['currency'] = BaseFieldDefinition::create('string') ->setLabel(t('Currency')) ->setDescription(t('The ISO currency code for the order.')) - ->setPropertyConstraints('value', array('Length' => array('max' => 3))) + ->setPropertyConstraints('value', ['Length' => ['max' => 3]]) ->setSetting('default_value', '') ->setSetting('max_length', 3); @@ -637,7 +640,7 @@ class Order extends ContentEntityBase implements OrderInterface { * An array of default values. */ public static function getCurrentUserId() { - return array(\Drupal::currentUser()->id()); + return [\Drupal::currentUser()->id()]; } } 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..e2799ec 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; /** @@ -24,56 +25,56 @@ class OrderUpdateForm extends FormBase { * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, OrderInterface $order = NULL) { - $form['order_comment_field'] = array( + $form['order_comment_field'] = [ '#type' => 'details', '#title' => $this->t('Add an order comment'), - ); - $form['order_comment_field']['order_comment'] = array( + ]; + $form['order_comment_field']['order_comment'] = [ '#type' => 'textarea', '#description' => $this->t('Order comments are used primarily to communicate with the customer.'), - ); + ]; - $form['admin_comment_field'] = array( + $form['admin_comment_field'] = [ '#type' => 'details', '#title' => $this->t('Add an admin comment'), - ); - $form['admin_comment_field']['admin_comment'] = array( + ]; + $form['admin_comment_field']['admin_comment'] = [ '#type' => 'textarea', '#description' => $this->t('Admin comments are only seen by store administrators.'), - ); + ]; - $form['current_status'] = array( + $form['current_status'] = [ '#type' => 'value', '#value' => $order->getStatusId(), - ); + ]; - $form['order_id'] = array( + $form['order_id'] = [ '#type' => 'value', '#value' => $order->id(), - ); + ]; - $form['controls'] = array( + $form['controls'] = [ '#type' => 'container', - '#attributes' => array('class' => array('uc-inline-form')), + '#attributes' => ['class' => ['uc-inline-form']], '#weight' => 10, - ); - $form['controls']['status'] = array( + ]; + $form['controls']['status'] = [ '#type' => 'select', '#title' => $this->t('Order status'), '#default_value' => $order->getStatusId(), '#options' => OrderStatus::getOptionsList(), - ); - $form['controls']['notify'] = array( + ]; + $form['controls']['notify'] = [ '#type' => 'checkbox', '#title' => $this->t('Send e-mail notification on update.'), - ); + ]; - $form['controls']['actions'] = array('#type' => 'actions'); - $form['controls']['actions']['submit'] = array( + $form['controls']['actions'] = ['#type' => 'actions']; + $form['controls']['actions']['submit'] = [ '#type' => 'submit', '#value' => $this->t('Update'), '#button_type' => 'primary', - ); + ]; return $form; } @@ -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); - } - } -}