You are here

function uc_roles_action_order_renew in Ubercart 6.2

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

Renews an orders 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

$order: An Ubercart order object.

See also

uc_roles_action_order_renew_form()

1 string reference to 'uc_roles_action_order_renew'
uc_roles_ca_action in uc_roles/uc_roles.ca.inc
Implements hook_ca_action().

File

uc_roles/uc_roles.ca.inc, line 395
This file contains the Conditional Actions hooks and functions necessary to make the roles-related entity, conditions, events, and actions work.

Code

function uc_roles_action_order_renew($order, $settings) {

  // 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 = %d", $product->nid);
    while ($role = db_fetch_object($roles)) {

      // Product model matches, or was 'any'.
      if (!empty($role->model) && $role->model != $product->model) {
        continue;
      }
      $existing_role = db_fetch_object(db_query("SELECT * FROM {uc_roles_expirations} WHERE uid = %d AND rid = %d", $account->uid, $role->rid));

      // 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, !$settings['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, !$settings['message']);
      }

      // Get the new expiration (if applicable)
      $new_expiration = db_fetch_object(db_query("SELECT * FROM {uc_roles_expirations} WHERE uid = %d AND rid = %d", $account->uid, $role->rid));
      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.
      ca_pull_trigger('uc_roles_notify_' . $op, $order, $new_expiration);
    }
  }
}