You are here

commerce_registration.rules.inc in Commerce Registration 7.3

Same filename and directory in other branches
  1. 7 commerce_registration.rules.inc
  2. 7.2 commerce_registration.rules.inc

Commerce Registration rules file.

File

commerce_registration.rules.inc
View source
<?php

/**
 * @file
 * Commerce Registration rules file.
 */

/**
 * Implements hook_rules_condition_info().
 */
function commerce_registration_rules_condition_info() {
  $conditions = array();
  $conditions['commerce_registration_line_item_is_registration'] = array(
    'label' => t('Line item is for a registration enabled product'),
    'group' => t('Commerce Registration'),
    'parameter' => array(
      'commerce_line_item' => array(
        'type' => 'commerce_line_item',
        'label' => t('Line item'),
        'description' => t('Does this line item have a register enabled product or not.'),
      ),
    ),
  );
  $conditions['commerce_registration_line_item_registration_allowed'] = array(
    'label' => t('Line item product has registration available'),
    'group' => t('Commerce Registration'),
    'parameter' => array(
      'commerce_line_item' => array(
        'type' => 'commerce_line_item',
        'label' => t('Line item'),
        'description' => t('Line item whose product is being checked for registration availability.'),
      ),
    ),
  );
  $conditions['commerce_registration_registration_count_comparison'] = array(
    'label' => t('Registration count comparison'),
    'group' => t('Commerce Registration'),
    'parameter' => array(
      'commerce_line_item' => array(
        'type' => 'commerce_line_item',
        'label' => t('Line Item'),
        'description' => t('Line item containing the registrations to count.'),
      ),
      'operator' => array(
        'type' => 'text',
        'label' => t('Operator'),
        'options list' => 'commerce_registration_comparison_operators',
      ),
      'amount' => array(
        'type' => 'integer',
        'label' => t('Amount'),
      ),
    ),
  );
  $conditions['commerce_registration_order_registration_count_comparison'] = array(
    'label' => t('Order total registration count comparison'),
    'group' => t('Commerce Registration'),
    'parameter' => array(
      'commerce_order' => array(
        'type' => 'commerce_order',
        'label' => t('Order'),
        'description' => t('The order of which to count the total number of registrations.'),
      ),
      'operator' => array(
        'type' => 'text',
        'label' => t('Operator'),
        'options list' => 'commerce_registration_comparison_operators',
      ),
      'amount' => array(
        'type' => 'integer',
        'label' => t('Amount'),
      ),
    ),
  );
  $conditions['commerce_registration_registration_slot_comparison'] = array(
    'label' => t('Registration slot comparison'),
    'group' => t('Commerce Registration'),
    'parameter' => array(
      'registration' => array(
        'type' => 'registration',
        'label' => t('Registration'),
        'description' => t('Registration to check the total slots used'),
      ),
      'operator' => array(
        'type' => 'text',
        'label' => t('Operator'),
        'options list' => 'commerce_registration_comparison_operators',
      ),
      'amount' => array(
        'type' => 'integer',
        'label' => t('Amount'),
      ),
    ),
  );
  return $conditions;
}

/**
 * Implements hook_rules_action_info().
 */
function commerce_registration_rules_action_info() {
  $actions = array();
  $actions['commerce_registration_set_state'] = array(
    'label' => t('Set all Registrations to a specific state'),
    'group' => t('Commerce Registration'),
    'parameter' => array(
      'commerce_line_item' => array(
        'type' => 'commerce_line_item',
        'label' => t('Line item'),
        'description' => t('All registrations on this line item will have their state changed.'),
      ),
      'registration_state' => array(
        'type' => 'list<integer>',
        'label' => t('Registration state'),
        'description' => t('Registrations will be set to this state.'),
        'options list' => 'commerce_registration_get_states',
      ),
    ),
  );
  $actions['commerce_registration_delete_registrations'] = array(
    'label' => t('Delete all registrations on a line item'),
    'group' => t('Commerce Registration'),
    'parameter' => array(
      'commerce_line_item' => array(
        'type' => 'commerce_line_item',
        'label' => t('Commerce line item'),
        'description' => t('All registrations on this line item will be deleted.'),
      ),
    ),
  );
  return $actions;
}

/**
 * @defgroup commerce_registration_rules_conditions Rules Conditions
 * @{
 * Condition callbacks for Commerce Registration rules.
 */

/**
 * Condition callback. Compare the total number of registrations on the order.
 */
function commerce_registration_order_registration_count_comparison($order, $operator, $amount) {
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  $total = 0;
  foreach ($order_wrapper->commerce_line_items as $delta => $line_item_wrapper) {
    if (!in_array($line_item_wrapper->type
      ->value(), commerce_product_line_item_types())) {
      continue;
    }
    $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
    $registrations = $line_item_wrapper->registrations
      ->value();
    if (!is_array($registrations)) {
      continue;
    }

    // Get the total registration count. There is at most a depth of 2.
    foreach ($registrations as $registration) {
      if (is_array($registration)) {
        $total += count($registration);
      }
      else {
        $total++;
      }
    }
  }
  switch ($operator) {
    case '=':
      return $total == $amount;
    case '>=':
      return $total >= $amount;
    case '>':
      return $total > $amount;
    case '<':
      return $total < $amount;
    case '<=':
      return $total <= $amount;
    case '!=':
      return $total != $amount;
  }
  return FALSE;
}

/**
 * Condition callback. Compare the total number of registrations on the line item.
 */
function commerce_registration_registration_count_comparison($line_item, $operator, $amount) {
  $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
  $registrations = $line_item_wrapper->registrations
    ->value();
  if (!is_array($registrations)) {
    return FALSE;
  }
  $total = 0;

  // Get the total registration count. There is at most a depth of 2.
  foreach ($registrations as $registration) {
    if (is_array($registration)) {
      $total += count($registration);
    }
    else {
      $total++;
    }
  }
  switch ($operator) {
    case '=':
      return $total == $amount;
    case '>=':
      return $total >= $amount;
    case '>':
      return $total > $amount;
    case '<':
      return $total < $amount;
    case '<=':
      return $total <= $amount;
    case '!=':
      return $total != $amount;
  }
  return FALSE;
}

/**
 * Condition callback. Compare the total slots on a registration.
 */
function commerce_registration_registration_slot_comparison($registration, $operator, $amount) {
  $total = $registration->count;
  switch ($operator) {
    case '=':
      return $total == $amount;
    case '>=':
      return $total >= $amount;
    case '>':
      return $total > $amount;
    case '<':
      return $total < $amount;
    case '<=':
      return $total <= $amount;
    case '!=':
      return $total != $amount;
  }
  return FALSE;
}

/**
 * Condition callback.
 *
 * Checks the commerce line item to see if the product is register enabled.
 *
 * @param CommerceLineItem $line_item
 *   The Commerce Line Item to check for a register enabled product.
 *
 * @return bool
 *   Boolean TRUE if the line item product is register enabled.
 */
function commerce_registration_line_item_is_registration($line_item) {
  $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
  if (!in_array($line_item_wrapper->type
    ->value(), commerce_product_line_item_types())) {
    return FALSE;
  }
  $product_id = $line_item_wrapper->commerce_product->product_id
    ->value();
  return commerce_registration_product_has_registration_field($product_id);
}

/**
 * Condition callback.
 *
 * Checks if the line item product has registration capacity left. Capacity is
 * only taken if an order is paid for in full.
 *
 * @param $line_item
 *   Commerce Line Item to check the product for registration capacity.
 *
 * @return bool
 *   Boolean TRUE if the product has registration capacity.
 */
function commerce_registration_line_item_registration_allowed($line_item) {
  $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
  if (!in_array($line_item_wrapper->type
    ->value(), commerce_product_line_item_types())) {
    return FALSE;
  }
  $product_id = $line_item_wrapper->commerce_product->product_id
    ->value();
  $quantity = $line_item_wrapper->quantity
    ->value();
  return registration_status('commerce_product', $product_id, TRUE, $quantity);
}

/**
 * @} End of "defgroup commerce_registration_rules_conditions".
 */

/**
 * @defgroup commerce_registration_rules_actions Rules Actions
 * @{
 * Action callbacks for Commerce Registration rules.
 */

/**
 * Action callback.
 *
 * Sets all registrations on a line item to a given state.
 *
 * @param $line_item
 *   Commerce Line Item to check the product for registration capacity.
 * @param $registration_state
 *   The registration state to set all registrations to on this line item.
 */
function commerce_registration_set_state($line_item, $registration_state) {

  // Make sure the line item even has a registration product before continuing.
  if (!commerce_registration_line_item_is_registration($line_item)) {
    return;
  }
  $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
  $registrations = $line_item_wrapper->registrations
    ->value();
  if (!is_array($registrations)) {
    $registrations = unserialize($registrations);
  }
  foreach ($registrations as $key => $registration) {
    $state_key = array_keys($registration_state);
    $registration->state = $registration_state[$state_key[0]];
    registration_save($registration);
    $registrations[$key] = $registration;
  }
  $line_item_wrapper->registrations = $registrations;
  $line_item_wrapper
    ->save();
}

/**
 * Action callback.
 *
 * Deletes all registrations on a line item.
 */
function commerce_registration_delete_registrations($line_item) {

  // Make sure the line item even has a registration product before continuing.
  if (!commerce_registration_line_item_is_registration($line_item)) {
    return;
  }
  $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
  $registrations = $line_item_wrapper->registrations
    ->value();
  if (!is_array($registrations)) {
    $registrations = unserialize($registrations);
  }
  $reg_ids = array();
  foreach ($registrations as $registration) {
    $reg_ids[] = $registration->registration_id;
  }
  registration_delete_multiple($reg_ids);
  $line_item_wrapper->registrations = array();
  $line_item_wrapper
    ->save();
}

/**
 * @} End of "defgroup commerce_registration_rules_actions".
 */

/**
 * Rules helper. Return options list of registration states.
 */
function commerce_registration_get_states() {
  $states = registration_states();
  foreach ($states as $state) {
    $options[$state->name] = $state->label;
  }
  return $options;
}

/**
 * Rules helper. Return options list of comparison operators.
 */
function commerce_registration_comparison_operators() {
  return array(
    '=' => t('equal to'),
    '>' => t('greater than'),
    '>=' => t('greater than or equal to'),
    '<' => t('less than'),
    '<=' => t('less than or equal to'),
    '!=' => t('not equal to'),
  );
}

Functions

Namesort descending Description
commerce_registration_comparison_operators Rules helper. Return options list of comparison operators.
commerce_registration_delete_registrations Action callback.
commerce_registration_get_states Rules helper. Return options list of registration states.
commerce_registration_line_item_is_registration Condition callback.
commerce_registration_line_item_registration_allowed Condition callback.
commerce_registration_order_registration_count_comparison Condition callback. Compare the total number of registrations on the order.
commerce_registration_registration_count_comparison Condition callback. Compare the total number of registrations on the line item.
commerce_registration_registration_slot_comparison Condition callback. Compare the total slots on a registration.
commerce_registration_rules_action_info Implements hook_rules_action_info().
commerce_registration_rules_condition_info Implements hook_rules_condition_info().
commerce_registration_set_state Action callback.