You are here

function acquia_lift_personalize_campaign_wizard_targeting_submit in Acquia Lift Connector 7.2

Submit function for targeting form.

File

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

Code

function acquia_lift_personalize_campaign_wizard_targeting_submit(&$form, &$form_state, &$agent_data, $agent_instance) {
  if ($form_state['triggering_element']['#value'] == t('Revert changes')) {
    $form_state['redirect'] = array(
      'admin/structure/personalize/manage/' . $agent_data->machine_name . '/targeting/revert',
      array(
        'query' => array(
          'destination' => 'admin/structure/personalize/manage/' . $agent_data->machine_name . '/targeting',
        ),
      ),
    );
    return;
  }

  // Handle targeting audiences.
  if (empty($form_state['values']['audiences'])) {
    return;
  }
  module_load_include('inc', 'acquia_lift', 'acquia_lift.admin');
  $targeting = array();
  $audience_count = 0;

  // Determine the values for the "Everyone else" audience
  $final_audience = _acquia_lift_personalize_campaign_wizard_everyone_else_audience();

  // Set the weight higher than any of the audience options.
  foreach ($form_state['values']['audiences'] as $type_id => $type) {
    foreach ($form_state['values']['audiences'][$type_id] as $audience_id => $audience) {

      // The machine name of the "everyone-else" audience may have changed, if
      // tests for hte audience have changed, so it may have a suffix, like '-2'.
      $is_everyone_audience = strpos($audience_id, ACQUIA_LIFT_TARGETING_EVERYONE_ELSE) === 0;
      if ($is_everyone_audience) {
        $final_audience['id'] = $audience_id;
      }
      if ($is_everyone_audience || !isset($audience['details']['weight']) || empty($audience['details']['name'])) {
        continue;
      }
      $final_audience['weight'] = $audience['details']['weight'] > $final_audience['weight'] ? $audience['details']['weight'] : $final_audience['weight'];
    }
  }
  $final_audience['weight'] += 10;

  // Now process all of the audiences.
  $has_tests = FALSE;
  foreach ($form_state['values']['audiences'] as $type_id => $type) {
    foreach ($form_state['values']['audiences'][$type_id] as $audience_id => $audience) {
      if (empty($audience['details']['name']) && strpos($audience_id, ACQUIA_LIFT_TARGETING_EVERYONE_ELSE) === FALSE) {
        continue;
      }
      if (strpos($audience_id, ACQUIA_LIFT_TARGETING_EVERYONE_ELSE) === 0) {
        $audience_values = $final_audience;
      }
      else {
        $audience_values = array(
          'name' => $audience['details']['name'],
          'weight' => $audience['details']['weight'],
          'contexts' => array(),
          'strategy' => $audience['details']['strategy'],
          'id' => $audience_id === 'add' ? NULL : $audience_id,
        );

        // We need to massage the context information as submitted in the form into
        // an array of contexts that can be consumed by the
        // acquia_lift_target_audience_save() function.
        foreach ($audience['details']['mapping']['contexts'] as $context_values) {
          if ($context_values['context'] == '') {
            continue;
          }
          $context_values['match'] = $context_values['value']['match'];
          $context_values['operator'] = $context_values['value']['operator'];
          unset($context_values['value'], $context_values['remove']);
          $audience_values['contexts'][] = $context_values;
        }
      }
      if (acquia_lift_target_audience_save($audience_values['name'], $agent_data->machine_name, $audience_values['contexts'], $audience_values['strategy'], $audience_values['weight'], $audience_values['id'])) {
        if ($type_id === 'new') {
          drupal_set_message(t('The target audience %audience was created successfully', array(
            '%audience' => $audience['details']['name'],
          )));
        }
        $audience_count++;
      }
      else {
        drupal_set_message(t('There was a problem saving the target audience %audience.', array(
          '%audience' => $audience['details']['name'],
        )), 'error');
      }

      // Handle the assignment of options to audiences.
      if (!empty($audience['assignment_order'])) {
        $targeting[$audience_id] = explode(',', $audience['assignment_order']);
        $has_tests = $has_tests || count($targeting[$audience_id]) > 1;
      }
    }
  }
  acquia_lift_save_targeting_structure($agent_data, $targeting);

  // Save advanced test settings if applicable.
  if ($has_tests) {

    // Save the test options on the main targeting agent.  When the agent creates
    // embedded tests then it will read these properties and set them on each
    // test agent.
    if (isset($form_state['values']['decision_style'])) {
      $agent_data->data['decision_style'] = $form_state['values']['decision_style'];
    }
    if (isset($form_state['values']['control_rate'])) {
      $agent_data->data['control_rate'] = $form_state['values']['control_rate'];
    }
    if (isset($form_state['values']['explore_rate'])) {
      $agent_data->data['explore_rate'] = $form_state['values']['explore_rate'];
    }
  }
}