You are here

function acquia_lift_element_variation_details_form_ajax_callback in Acquia Lift Connector 7.2

Same name and namespace in other branches
  1. 7 acquia_lift.admin.unibar.inc \acquia_lift_element_variation_details_form_ajax_callback()

Responds to AJAX submission of a variation type details page.

1 string reference to 'acquia_lift_element_variation_details_form_ajax_callback'
acquia_lift_element_variation_details_form in ./acquia_lift.admin.unibar.inc
Form handler for an element variation details form.

File

./acquia_lift.admin.unibar.inc, line 271
acquia_lift.admin.unibar.inc

Code

function acquia_lift_element_variation_details_form_ajax_callback($form, &$form_state) {
  $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
  $add_another = $op == t('Add another');
  $add_and_close = $op == t('Save');
  $cancel = $op == t('Cancel');
  $has_destination = !empty($_SESSION['acquia_lift_element_destination']);
  if ($has_destination) {
    ctools_include('ajax');
  }
  $commands = array();
  if ($add_another || $add_and_close) {
    $errors = form_get_errors();

    // If validation errors exist, return the form.
    if (!empty($errors)) {
      return $form;
    }
    $agent_name = $form_state['values']['agent'];
    if ($add_another || !$has_destination) {
      $settings = array();

      // Get the updated option sets for the element variation so that the
      // unibar can display the latest information.
      $option_sets = $form_state['storage']['option_sets'];
      $option_set_settings = array_map('_personalize_convert_option_set_to_js_setting', $option_sets);
      foreach ($option_set_settings as $option_set_setting) {
        $settings['personalize']['option_sets'][key($option_set_setting)] = reset($option_set_setting);
      }

      // Include whether the option sets are editable for unibar.
      foreach ($option_sets as $option_set) {
        $settings += acquia_lift_get_option_set_editable_settings($option_set);
      }

      // Also get the updated personalize elements options.
      personalize_elements_limit_option_sets_by_path($option_sets, $form_state['values']['pages']);
      $settings['personalize_elements']['elements'] = personalize_elements_element_settings($option_sets);

      // We need to update the campaigns as well in case a campaign was paused by this action.
      $settings['acquia_lift']['campaigns'] = acquia_lift_get_campaign_details();

      // And the agent map.
      $settings['personalize']['agent_map'][$agent_name] = personalize_agent_get_map_settings($agent_name);

      // Note: JavaScript assets for personalize elements are included in
      // personalize_elements_page_build when the module is enabled and therefore
      // do not need to be explicitly added here.
      $commands[] = ajax_command_settings($settings, TRUE);
    }
    if ($add_and_close) {

      // End the editing mode.
      $commands[] = acquia_lift_command_variation_toggle(FALSE);
      if ($has_destination) {
        $commands[] = ctools_ajax_command_redirect($_SESSION['acquia_lift_element_destination']);
        unset($_SESSION['acquia_lift_element_destination']);
      }
      else {

        // Preview the new variation.
        $commands[] = acquia_lift_command_variation_preview($form_state['values']['agent'], $form_state['storage']['osid'], $form_state['storage']['option_id']);

        // Show a confirmation message.
        $current_status = personalize_agent_get_status($agent_name);
        $message = $form_state['storage']['confirmation_message'];
        if ($current_status === PERSONALIZE_STATUS_NOT_STARTED) {
          $errors = personalize_verify_agent($agent_name);
          if (empty($errors)) {
            $message .= ' ' . t('You can now start the personalization.');
          }
          else {
            $message .= ' ' . t('Add one or more goals by clicking Why > Add goal.');
          }
        }
        $commands[] = acquia_lift_command_messagebox($message, 10);
      }
    }
    else {

      // Adding another variation.
      $option_set = personalize_option_set_load($form_state['storage']['osid']);
      $commands[] = acquia_lift_command_variation_edit($option_set);
    }
  }
  else {
    if ($cancel) {

      // Turn off edit mode and redirect them.
      $commands[] = acquia_lift_command_variation_toggle(FALSE);
      if ($has_destination) {
        $commands[] = ctools_ajax_command_redirect($_SESSION['acquia_lift_element_destination']);
        unset($_SESSION['acquia_lift_element_destination']);
      }
    }
  }
  return personalize_ajax_commands_deliver($commands, TRUE);
}