You are here

function acquia_lift_personalize_campaign_wizard_goals_submit in Acquia Lift Connector 7.2

Submit function for goals form.

File

./acquia_lift.admin.wizard.inc, line 2258
acquia_lift.admin.wizard.inc Functions specific to the Acquia Lift alteration of the campaign creation wizard.

Code

function acquia_lift_personalize_campaign_wizard_goals_submit(&$form, &$form_state, &$agent_data, $agent_instance) {
  if (!acquia_lift_target_definition_changes_allowed($agent_data)) {
    return;
  }

  // Check if any goals have been removed during editing.
  $saved_goals = $existing_goals = array();
  foreach (personalize_goal_load_by_conditions(array(
    'agent' => $agent_data->machine_name,
  )) as $goal) {

    // We can only have one goal with a given action per agent, so
    // we key the array of existing goals by action name.
    $existing_goals[$goal->action] = $goal->id;
  }

  // Check for any new goals.
  if (isset($form_state['new_goals'])) {
    foreach ($form_state['new_goals'] as $delta => $type) {
      _acquia_lift_personalize_campaign_wizard_goals_single_add($delta, $form, $form_state, $agent_data);
    }
  }

  // Update any existing goals.
  if (isset($form_state['values']['goals']['all_goals'])) {
    foreach ($form_state['values']['goals']['all_goals'] as $goal) {
      if (!empty($goal['action_name'])) {
        try {
          personalize_goal_save($agent_data->machine_name, $goal['action_name'], $goal['value'], $goal['goal_id']);
          $saved_goals[$goal['action_name']] = $goal['goal_id'];
        } catch (Exception $e) {
          drupal_set_message($e
            ->getMessage(), 'error');
        }
      }
    }
  }

  // The difference between existing goals and saved goals are the ones
  // that need to be deleted.
  $to_delete = array_diff_key($existing_goals, $saved_goals);
  foreach ($to_delete as $goal_id) {
    personalize_goal_delete($goal_id);
  }
}