You are here

function acquia_lift_get_sync_operations_for_agents in Acquia Lift Connector 7

Gets the required operations for syncing the specified agents to Lift.

Parameters

$agents: An array of agents with agent names as keys and stdClass objects rep- resenting the agents as values.

Return value

array An array of operation arrays, each with a 'method' key and an 'args' key, that can be processed by acquia_lift_sync_item().

3 calls to acquia_lift_get_sync_operations_for_agents()
AcquiaLiftWebTestFundamentals::testBatchSyncOperations in tests/acquia_lift.test
Tests the function that gathers operations for syncing of agents and their components to Lift.
acquia_lift_batch_sync_campaigns in ./acquia_lift.batch.inc
Batch syncs all campaigns to Lift.
drush_acquia_lift_campaign_sync in ./acquia_lift.drush.inc
Syncs the Acquia Lift Campaigns.

File

./acquia_lift.batch.inc, line 131
acquia_lift.batch.inc

Code

function acquia_lift_get_sync_operations_for_agents($agents) {
  $operations = array();
  $lift_api = AcquiaLiftAPI::getInstance(variable_get('acquia_lift_account_info', array()));
  foreach ($agents as $agent_name => $agent) {
    if (!acquia_lift_is_testing_agent($agent)) {
      continue;
    }
    if ($agent_instance = personalize_agent_load_agent($agent_name)) {
      if (!$agent_instance instanceof AcquiaLiftAgent) {
        continue;
      }

      // If we're dealing with an agent that already exists in Lift
      // then we may need to delete some of its components.
      try {
        $existing_agent = $lift_api
          ->getAgent($agent_name);
      } catch (AcquiaLiftException $e) {
        if ($e instanceof AcquiaLiftNotFoundException) {
          $existing_agent = FALSE;
        }
        else {

          // Any other exception means we can't communicate with Lift so just
          // return an empty operations array.
          return array();
        }
      }

      // First sync the agent itself. We need to know if Lift already has
      // a targeting rule set up for an agent with this name. It will need
      // to be deleted if the version of the agent we are now pushing does
      // not have one.
      $targeting_rule_exists = $lift_api
        ->hasAutoTargetingRule($agent_name);
      $operations = array_merge($operations, $agent_instance
        ->getAgentSyncOperations($targeting_rule_exists));

      // Now go through all option sets and sync those.
      $option_sets = personalize_option_set_load_by_agent($agent_name);
      $decisions = empty($option_sets) ? array() : AcquiaLiftAgent::convertOptionSetsToDecisions($option_sets);
      $old_decisions = array();
      if ($existing_agent) {

        // If this is an existing agent then the decision structure
        // may be different and we'll need to perform some DELETEs
        // as well as PUTs.
        if ($existing_points = $lift_api
          ->getPointsForAgent($agent_name)) {

          // Build up the old decision tree for comparison with the
          // new tree.
          foreach ($existing_points as $point) {
            $old_decisions[$point] = array();
            if ($existing_decisions = $lift_api
              ->getDecisionsForPoint($agent_name, $point)) {
              foreach ($existing_decisions as $decision) {
                $old_decisions[$point][$decision] = array();
                if ($existing_choices = $lift_api
                  ->getChoicesForDecision($agent_name, $point, $decision)) {
                  foreach ($existing_choices as $choice) {
                    $old_decisions[$point][$decision][] = $choice;
                  }
                }
              }
            }
          }
        }
      }
      $operations = array_merge($operations, $agent_instance
        ->getDecisionSyncOperations($old_decisions, $decisions));
      if (!empty($option_sets)) {

        // Tell Acquia Lift about the fixed targeting for the option sets.
        $operations = array_merge($operations, $agent_instance
          ->getFixedTargetingSyncOperations($option_sets));
      }
      $goals = personalize_goal_load_by_conditions(array(
        'agent' => $agent_name,
      ));
      $new_goals = $old_goals = array();
      foreach ($goals as $goal) {
        $new_goals[$goal->action] = $goal->value;
      }
      if ($existing_agent && ($existing_goals = $lift_api
        ->getGoalsForAgent($agent_name))) {

        // If it's an existing agent there may be goals that need
        // to be deleted.
        foreach ($existing_goals as $goal) {

          // The array of goals has goal names as keys and goal values
          // as values, but the values are not used so we can pass
          // any value for each old goal.
          $old_goals[$goal] = 1;
        }
      }
      $operations = array_merge($operations, $agent_instance
        ->getGoalSyncOperations($old_goals, $new_goals));
    }
  }
  return $operations;
}