You are here

protected function RenewRole::doExecute in Ubercart 8.4

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.

Parameters

\Drupal\uc_order\OrderInterface $order: The order object.

bool $message: If TRUE, messages will be displayed to the user about the renewal.

File

uc_role/src/Plugin/RulesAction/RenewRole.php, line 158

Class

RenewRole
Provides a 'Renew role' action.

Namespace

Drupal\uc_role\Plugin\RulesAction

Code

protected function doExecute(OrderInterface $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 = $this->database
      ->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 = $this->database
        ->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 = $this
        ->getExpiration($role, $product->qty, isset($existing_role->expiration) ? $existing_role->expiration : NULL);

      // Leave an order comment.
      if (isset($existing_role->expiration)) {
        $op = 'renew';
        $comment = $this
          ->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 = $this
          ->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 = $this->database
        ->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); */
      if ($op == 'grant') {
        $event = new NotifyGrantEvent($order, $new_expiration);
      }
      elseif ($op == 'renew') {
        $event = new NotifyRenewEvent($order, $new_expiration);
      }
      $this->eventDispatcher
        ->dispatch($event::EVENT_NAME, $event);
    }
  }
}