You are here

function _ca_convert_configurations in Ubercart 6.2

Batch API callback for Workflow-ng configuration conversion.

1 string reference to '_ca_convert_configurations'
ca_conversion_form_submit in ca/ca.admin.inc

File

ca/ca.admin.inc, line 1106
Conditional actions overview UI.

Code

function _ca_convert_configurations(&$context) {
  global $user;
  if (db_table_exists('workflow_ng_cfgs')) {
    if (!isset($context['sandbox']['progress'])) {

      // Initialize batch data.
      $context['sandbox']['progress'] = 0;
      $context['sandbox']['current_cfg'] = '';
      $context['sandbox']['max'] = db_result(db_query("SELECT COUNT(name) FROM {workflow_ng_cfgs} WHERE altered = 1"));
    }
    $limit = 25;
    $result = db_query_range("SELECT name, data FROM {workflow_ng_cfgs} WHERE altered = 1 AND name > '%s' ORDER BY name", $context['sandbox']['current_cfg'], 0, $limit);
    $predicates = array();
    while ($configuration_row = db_fetch_object($result)) {
      $predicate = array(
        '#pid' => $configuration_row->name,
      );
      $conditions = array();
      $actions = array();
      if ($configuration = unserialize($configuration_row->data)) {
        $predicate['#class'] = $configuration['#module'];
        $predicate['#title'] = $configuration['#label'];

        // Convert event names to corresponding triggers.
        switch ($configuration['#event']) {
          case 'order_status_update':
            $trigger = 'uc_order_status_update';
            break;
          case 'payment_entered':
            $trigger = 'uc_payment_entered';
            break;
          case 'checkout_complete':
            $trigger = 'uc_checkout_complete';
            break;
          default:
            $trigger = $configuration['#event'];
            break;
        }

        // In UC 1.x, taxes had an event for each tax rate, using the same
        // action. In UC 2.x, they use the same trigger, but have separate
        // actions.
        if (strpos($trigger, 'calculate_tax_') === 0) {
          $tax_id = substr($trigger, 14);
          $trigger = 'calculate_taxes';
        }
        $predicate['#trigger'] = $trigger;
        $predicate['#weight'] = $configuration['#weight'];
        $predicate['#status'] = $configuration['#active'];

        // Numeric keys in $configuration could be for conditions or actions.
        // Take each and categorize them to add them to the predicate later.
        for ($i = 0; isset($configuration[$i]); $i++) {
          if ($configuration[$i]['#type'] == 'action') {
            $action = _ca_convert_actions($configuration[$i], $tax_id);
            if ($action) {
              $actions[] = $action;
            }
          }
          else {
            $condition = _ca_convert_conditions($configuration[$i]);
            if ($condition) {
              $conditions[] = $condition;
            }
          }
        }

        // Conditions are optional. If there are no actions, there's no reason
        // to make a predicate.
        if ($conditions) {
          $predicate['#conditions'] = array(
            '#operator' => 'AND',
            '#conditions' => $conditions,
          );
        }
        if ($actions) {
          $predicate['#actions'] = $actions;
          $predicate['#uid'] = $user->uid;
          ca_save_predicate($predicate);
        }
      }

      // Store some result for post-processing in the finished callback.
      $context['results'][] = $configuration_row->name;

      // Update our progress information.
      $context['sandbox']['progress']++;
      $context['sandbox']['current_cfg'] = $configuration_row->name;
      $context['message'] = t('Now processing %cfg', array(
        '%cfg' => $configuration_row->name,
      ));
    }

    // Inform the batch engine that we are not finished,
    // and provide an estimation of the completion level we reached.
    if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
      $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
    }
  }
}