You are here

function uc_roles_action_order_renew in Ubercart 7.3

Same name and namespace in other branches
  1. 6.2 uc_roles/uc_roles.ca.inc \uc_roles_action_order_renew()

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

object $order: An Ubercart order object.

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

1 string reference to 'uc_roles_action_order_renew'
uc_roles_rules_action_info in uc_roles/uc_roles.rules.inc
Implements hook_rules_action_info().

File

uc_roles/uc_roles.rules.inc, line 330
Rules hooks for uc_roles.module.

Code

function uc_roles_action_order_renew($order, $message) {

  // Load the order's user and exit if not available.
  if (!($account = user_load($order->uid))) {
    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", array(
      ':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", array(
        ':uid' => $account->uid,
        ':rid' => $role->rid,
      ))
        ->fetchObject();

      // Determine the expiration timestamp for the role.
      $expiration = _uc_roles_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.', array(
          '%role' => _uc_roles_get_name($role->rid),
        ));

        // Renew the user's role.
        uc_roles_renew($account, $role->rid, $expiration, !$message);
      }
      else {
        $op = 'grant';
        $comment = t('Customer granted user role %role.', array(
          '%role' => _uc_roles_get_name($role->rid),
        ));

        // Grant the role to the user.
        uc_roles_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", array(
        ':uid' => $account->uid,
        ':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->order_id, $account->uid, $comment);

      // Trigger role email.
      rules_invoke_event('uc_roles_notify_' . $op, $order, $new_expiration);
    }
  }
}