You are here

function acquia_lift_page_variation_create in Acquia Lift Connector 7

Creates an option set option for a page variation based on the passed in option info.

Parameters

$variation_set_name: The variation set this belongs to.

$option_set: The option set object to add the option to

$option_info: The information about the individual option to be added for this variation.

$variation_number: The variation number to add this option to. If unspecified it will be created as a new variation.

Return value

number The variation number that was created or added to.

3 calls to acquia_lift_page_variation_create()
AcquiaLiftWebTestVariationSets::testPersonalizeElementsVariationSets in tests/acquia_lift.test
AcquiaLiftWebTestVariationSets::testVariationSets in tests/acquia_lift.test
Tests variation set creation.
_acquia_lift_page_variation_details_form_submit in ./acquia_lift.admin.unibar.inc
Submit handler to create a new page variation for an AB agent.

File

./acquia_lift.page_variations.inc, line 23
acquia_lift.admin.page_variations.inc

Code

function acquia_lift_page_variation_create($variation_set_name, $option_set, $option_info, $variation_number = NULL) {

  // Find all other option sets in this variation set (i.e. with the same
  // decision name and agent.)
  $option_sets = personalize_option_set_load_multiple(FALSE, array(
    'decision_name' => $variation_set_name,
    'agent' => $option_set->agent,
  ));

  // To figure out the variation number, check the number of options in the first
  // existing option set and the number of options in the passed in option set and
  // use whichever is higher.
  if (!empty($option_sets)) {
    $first_os = reset($option_sets);
    $max_first = max(array_keys($first_os->options));
  }
  else {
    $max_first = 0;
  }

  // The maximum new variation number must take into account the number of
  // options for both the passed in option set and the original first option set
  // as well as the indexes on those options which may no longer be
  // sequential.
  $max_variation_number = max(array_merge(array(
    0,
    $max_first,
  ), array_keys($option_set->options)));

  // The above would give us the maximum existing number so increment for new.
  $max_variation_number++;
  if ($variation_number === NULL || $variation_number > $max_variation_number) {
    $variation_number = $max_variation_number;
  }
  $option_set->decision_name = $variation_set_name;
  $has_control_option = FALSE;
  $new_option_id = 'variation-' . $variation_number;
  $new_option_label = t('Variation #@num', array(
    '@num' => $variation_number,
  ));
  $i = 0;
  foreach ($option_set->options as $i => $option) {

    // Check for the control variation.
    if ($option['option_id'] == PERSONALIZE_CONTROL_OPTION_ID) {
      if ($i !== 0) {

        // The control option has to be index 0.
        unset($option_set->options[$i]);
      }
      else {
        $has_control_option = TRUE;
      }
    }
  }
  $next_option = $i + 1;

  // Add any missing options before adding the one for this variation.
  for ($j = $next_option; $j < $variation_number; $j++) {
    $option_label = empty($first_os) || !isset($first_os->options[$j]) ? t('Variation #@num', array(
      '@num' => $j,
    )) : $first_os->options[$j]['option_label'];
    $option_set->options[$j] = personalize_elements_get_option_for_variation('variation-' . $j, $option_label);
  }

  // Now add the option for this variation.
  $option_set->options[$variation_number] = array(
    'option_id' => $new_option_id,
    'option_label' => $new_option_label,
  ) + $option_info;
  $control_option = personalize_elements_get_option_for_variation(PERSONALIZE_CONTROL_OPTION_ID, PERSONALIZE_CONTROL_OPTION_LABEL);
  if (!$has_control_option) {
    array_unshift($option_set->options, $control_option);
  }

  // If the variation set contains variations with higher numbers than this, make
  // sure options exist in this option set for those variation numbers.
  for ($k = $variation_number + 1; $k < $max_variation_number; $k++) {
    if (!isset($option_set->options[$k])) {
      $option_label = empty($first_os) || !isset($first_os->options[$k]) ? t('Variation #@num', array(
        '@num' => $k,
      )) : $first_os->options[$k]['option_label'];
      $option_set->options[$k] = personalize_elements_get_option_for_variation('variation-' . $k, $option_label);
    }
  }

  // Save the option set, specifying *not* to enforce decision matching as we know
  // the options will not match at this point.
  $option_set = personalize_option_set_save($option_set, FALSE);

  // Go through the other option sets in the variation set to update their options.
  foreach ($option_sets as $osid => $os) {
    if (isset($option_set->osid) && $osid == $option_set->osid) {
      continue;
    }

    // Make sure we have a control option.
    if (!isset($os->options[0])) {
      $os->options[0] = array();
    }
    $os->options[0] = array_merge($os->options[0], $control_option);

    // Add empty options for any missing indexes up to and including the variation number.
    for ($i = 1; $i <= $variation_number; $i++) {
      if (!isset($os->options[$i])) {
        $op_id = 'variation-' . $i;
        $op_label = empty($first_os) || !isset($first_os->options[$i]) ? t('Variation #@num', array(
          '@num' => $i,
        )) : $first_os->options[$i]['option_label'];
        $os->options[$i] = personalize_elements_get_option_for_variation($op_id, $op_label);
      }
    }
    personalize_option_set_save($os, FALSE);
  }
  return $variation_number;
}