You are here

function acquia_lift_element_variation_details_form_submit 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_submit()

Form submit handler for acquia_lift_element_variation_details_form to create a new personalize elements option set or edit and existing option.

Calls the appropriate handler for the creation of the personalize elements variation based on the type of campaign.

File

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

Code

function acquia_lift_element_variation_details_form_submit($form, &$form_state) {

  // Prepare the values to those expected by the personalize elements module.
  $values = $form_state['values'];
  if (empty($values['osid'])) {

    // Creating a new option set and a new option within it.
    $values['options'] = array(
      array(
        'option_label' => $values['option_label'],
        'personalize_elements_content' => $values['personalize_elements_content'],
      ),
    );
    $values['add_control_option'] = TRUE;
    $values['agent_select'] = $values['agent'];
    module_load_include('inc', 'personalize_elements', 'personalize_elements.admin');
    $option_set = personalize_elements_convert_form_values_to_option_set($values);

    // For new variation sets the pages value is set to the current page by
    // the JavaScript.
    $option_set->preview_link = $values['pages'];
    $message = t('The variation set has been created.');
  }
  else {
    $option_set = personalize_option_set_load($values['osid']);
    $option_set->label = $values['title'];
    if (empty($values['variation_number'])) {

      // Adding a variation to an existing option set.
      $option_set->options[] = array(
        'option_label' => $values['option_label'],
        'personalize_elements_content' => $values['personalize_elements_content'],
      );
      $message = t('The variation has been created.');
    }
    else {

      // Editing an existing option set option.
      foreach ($option_set->options as &$option) {
        if ($option['option_id'] === $values['variation_number']) {
          $option['personalize_elements_content'] = $values['personalize_elements_content'];
          $option['option_label'] = $values['option_label'];
          break;
        }
      }
      $message = t('The variation has been updated.');
    }
  }
  $option_set = personalize_option_set_save($option_set);

  // Now re-load the option set so that all hooks get run.
  // This is particularly important to set the selector property.
  $option_set = personalize_option_set_load($option_set->osid);

  // Save the updated option sets for use within the Ajax callback.
  $form_state['storage']['option_sets'] = array(
    $option_set,
  );
  $form_state['storage']['osid'] = $option_set->osid;

  // Determine the option set to preview after save.
  if (!empty($values['variation_number'])) {

    // Preview the edited variation.
    $form_state['storage']['option_id'] = $values['variation_number'];
  }
  else {

    // It was a new option, so preview the last one.
    $updated_options = array_values($option_set->options);
    $updated_option = end($updated_options);
    $form_state['storage']['option_id'] = $updated_option['option_id'];
  }
  $form_state['storage']['confirmation_message'] = $message;
}