You are here

function acquia_lift_personalize_campaign_wizard_variations_alter in Acquia Lift Connector 7.2

Alter hook for the variations portions of the campaign wizard.

File

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

Code

function acquia_lift_personalize_campaign_wizard_variations_alter(&$form, &$form_state, $agent_data, $editable) {
  global $base_url;

  // Rebuild the variations form to show the customized Lift approach.
  unset($form['variations']['title']['summary']);
  unset($form['variations']['option_sets']);
  $form['variations']['#tree'] = TRUE;

  // Take over the form validation and submit handling.
  $form['#validate'] = array(
    'acquia_lift_personalize_campaign_wizard_validate',
  );
  $form['#submit'] = array(
    'acquia_lift_personalize_campaign_wizard_submit',
  );
  if (empty($agent_data->machine_name)) {
    return;
  }
  if (!$editable) {
    $form['variations']['#disabled'] = TRUE;
  }
  $variation_set_handling = ACQUIA_LIFT_DECISION_LOCKSTEP;

  /**
     * Temporarily setting all variation set handling to lock step.
     * See https://www.drupal.org/node/2505247
    $variation_set_handling = '';
    if (!empty($form_state['values']['variations']['add_variation']['variation_set_handling'])) {
      $variation_set_handling = $form_state['values']['variations']['add_variation']['variation_set_handling'];
      // If the variation set was just determined, save it so that it can persist
      // across other AJAX actions.
      $form_state['storage']['acquia_lift_variation_set_handling'] = $form_state['values']['variations']['add_variation']['variation_set_handling'];
    }
    else if (isset($agent_data->data['variation_set_handling'])) {
      $variation_set_handling = $agent_data->data['variation_set_handling'];
    }*/

  // Set the option set type into storage to allow persistence across
  // multiple form call-backs.
  if (isset($form_state['triggering_element']) && end($form_state['triggering_element']['#parents']) == 'option_set_type') {
    $delta = _acquia_lift_personalize_campaign_wizard_next_element($form_state, 'new');
    $form_state['new_option_sets'][$delta] = $form_state['values']['variations']['editing']['new'][$delta]['option_set_type'];
    $form_state['expanded_option_set'] = $delta;
  }
  else {
    if (isset($_GET['create']) && in_array($_GET['create'], array(
      'block',
      'element',
    ))) {

      // Has to be the first new option set because the base is just loading.
      $form_state['new_option_sets'][0] = $_GET['create'];
      $form_state['expanded_option_set'] = 0;
    }
  }
  $option_sets = personalize_option_set_load_by_agent($agent_data->machine_name);
  if ($variation_set_handling == ACQUIA_LIFT_DECISION_MULTIVARIATE) {

    // If this is an MVT then the option sets get saved directly on the testing
    // agent.
    $mvt_name = acquia_lift_get_mvt_name_for_agent($agent_data->machine_name);
    if ($mvt = personalize_agent_load($mvt_name)) {
      $option_sets = personalize_option_set_load_by_agent($mvt_name);
    }
  }

  // If this campaign already handles multiple variation sets then indicate
  // the type of handling in the section title.
  if (count($option_sets) > 1 && !empty($variation_set_handling)) {
    $section_title = $form['variations']['title']['#title'];
    if ($variation_set_handling == ACQUIA_LIFT_DECISION_LOCKSTEP) {
      $section_title = '<span data-help-tooltip="' . t('Each variation set should have the same number of variations so they can be evenly locked as combinations. For example: AA, BB, CC.') . '">' . $section_title . ' (' . t('Lock step') . ')' . '</span>';
    }
    else {
      $section_title .= ' (' . t('Multi-variate') . ')';
    }
    $form['variations']['title']['#title'] = $section_title;
  }

  // Keep track of all variations for display.
  $all_variations = array();
  $form['variations']['editing'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(
        'personalize-wizard-column',
      ),
    ),
  );

  // Make a container to hold existing option sets.
  if (!empty($option_sets)) {
    $form['variations']['editing']['option_sets'] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'personalize-wizard-variation-sets',
        ),
      ),
    );
  }

  // Add an option set edit card for each option set.
  foreach ($option_sets as $option_set) {
    $option_set_plugin = personalize_get_option_set_type($option_set->plugin);
    $form['variations']['editing']['option_sets']['option_set_' . $option_set->osid] = array(
      '#tree' => TRUE,
      '#type' => 'container',
      '#theme' => 'acquia_lift_card',
      '#collapsible' => TRUE,
      '#collapsed' => count($option_sets) > 1,
      '#title' => $option_set->label,
      '#attributes' => array(
        'class' => array(
          'personalize-option-set',
        ),
      ),
    );
    $form['variations']['editing']['option_sets']['option_set_' . $option_set->osid]['option_set'] = array(
      '#type' => 'value',
      '#value' => $option_set,
    );
    $form['variations']['editing']['option_sets']['option_set_' . $option_set->osid]['header'] = array(
      '#type' => 'container',
    );

    // Variation sets can be deleted only if the structure has not already been
    // implemented.
    // @see acquia_lift_implement_test_structure().
    if ($editable) {
      $delete_link = module_hook($option_set_plugin['module'], 'personalize_delete_link') ? module_invoke($option_set_plugin['module'], 'personalize_delete_link', $option_set) : '';
      if (!empty($delete_link)) {
        $form['variations']['editing']['option_sets']['option_set_' . $option_set->osid]['header']['delete'] = array(
          '#markup' => l(t('Delete'), $delete_link, array(
            'attributes' => array(
              'class' => array(
                'acquia-lift-delete',
              ),
            ),
            'query' => array(
              'destination' => current_path(),
            ),
          )),
        );
      }
    }
    switch ($option_set->plugin) {
      case 'block':
        $form['variations']['editing']['option_sets']['option_set_' . $option_set->osid]['content'] = _acquia_lift_personalize_campaign_wizard_variations_block($form, $form_state, $agent_data, array(
          'variations',
          'editing',
          'option_sets',
          'option_set_' . $option_set->osid,
          'content',
        ), $option_set);
        break;
      case 'elements':
        $form['variations']['editing']['option_sets']['option_set_' . $option_set->osid]['content'] = _acquia_lift_personalize_campaign_wizard_variations_element($form, $form_state, $agent_data, $option_set);
        break;
      case 'fields':
        $form['variations']['editing']['option_sets']['option_set_' . $option_set->osid]['content'] = _acquia_lift_personalize_campaign_wizard_variations_fields($form, $form_state, $agent_data, $option_set);
        break;

      // Unsupported display - get an edit link if provided by the plugin.
      default:
        $form['variations']['editing']['option_sets']['option_set_' . $option_set->osid]['content'] = _acquia_lift_personalize_campaign_wizard_variations_general_edit($agent_data, $option_set);
        $edit_link = module_hook($option_set_plugin['module'], 'personalize_edit_link') ? module_invoke($option_set_plugin['module'], 'personalize_edit_link', $option_set) : '';
        if (!empty($edit_link)) {
          $markup = l(t('Edit this variation set'), $edit_link);
        }
        else {
          $markup = t('Variation set type ' . $option_set->plugin . ' does not provide a link for editing.');
        }
        $form['variations']['editing']['option_sets']['option_set_' . $option_set->osid]['content']['edit'] = array(
          '#markup' => $markup,
        );
    }

    // Add the advanced settings form.
    $form['variations']['editing']['option_sets']['option_set_' . $option_set->osid]['advanced'] = personalize_campaign_wizard_variations_advanced($form, $form_state, $option_set);

    // Option set label is edited in a different spot so don't show it twice.
    $form['variations']['editing']['option_sets']['option_set_' . $option_set->osid]['advanced']['label']['#access'] = FALSE;

    // Decision name is handled based on the type of tests created.
    $form['variations']['editing']['option_sets']['option_set_' . $option_set->osid]['advanced']['decision_name']['#access'] = FALSE;

    // Add the pages options to the settings form for element variations.
    if ($option_set->plugin == 'elements') {
      $form['variations']['editing']['option_sets']['option_set_' . $option_set->osid]['advanced']['pages_all'] = array(
        '#type' => 'radios',
        '#title' => t('Execute on'),
        '#options' => array(
          '0' => t('Selected pages'),
          '1' => t('All pages'),
        ),
        '#default_value' => isset($option_set->data['pages']) && empty($option_set->data['pages']) ? 1 : 0,
      );
      $form['variations']['editing']['option_sets']['option_set_' . $option_set->osid]['advanced']['pages'] = array(
        '#type' => 'textarea',
        '#title' => t('Pages'),
        '#title_display' => 'invisible',
        '#field_prefix' => $base_url . base_path(),
        '#default_value' => isset($option_set->data['pages']) ? $option_set->data['pages'] : '',
        '#description' => t("Specify pages by using one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page. If the URL's language prefix is skipped, all languages of the same URL will be selected.", array(
          '%blog' => 'blog',
          '%blog-wildcard' => 'blog/*',
          '%front' => '<front>',
        )),
        '#states' => array(
          'disabled' => array(
            ':input[name="variations[editing][option_sets][option_set_' . $option_set->osid . '][advanced][pages_all]"]' => array(
              'value' => '1',
            ),
          ),
        ),
        // Specify the requirements for visitor_actions_form_element_path_validate.
        '#allow_dynamic' => TRUE,
        '#allow_external' => FALSE,
      );
    }

    // Show save button specific to this option set.
    $form['variations']['editing']['option_sets']['option_set_' . $option_set->osid]['save'] = array(
      '#name' => $option_set->plugin . '_existing_' . $option_set->osid,
      '#type' => 'submit',
      '#value' => t('Save'),
      '#submit' => array(
        'acquia_lift_personalize_campaign_wizard_variations_single_submit',
      ),
      '#limit_validation_errors' => array(
        array(
          'variations',
          'editing',
          'option_sets',
          'option_set_' . $option_set->osid,
        ),
      ),
    );
  }

  // Hide the agent advanced options which will be shown on the review screen.
  $form['variations']['advanced']['#access'] = FALSE;
  $new_option_sets_id = 'acquia-lift-option-sets-new';
  $form['variations']['editing']['new'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'id' => $new_option_sets_id,
    ),
    '#access' => $editable,
  );
  if (!empty($form_state['new_option_sets'])) {

    // Get the option set type UI display details.
    module_load_include('inc', 'acquia_lift', 'acquia_lift.ui');
    $option_set_types = acquia_lift_option_set_types_ui();
    $option_set_options = array();
    foreach ($option_set_types as $type => $details) {
      $option_set_options[$type] = $details['title'];
    }
    foreach ($form_state['new_option_sets'] as $delta => $new_option_set_type) {
      $new_option_set_details_id = 'acquia-lift-new-option-set-' . $delta;
      $new_option_set_type = $form_state['new_option_sets'][$delta] ?: '';
      $expanded = isset($form_state['expanded_option_set']) && $form_state['expanded_option_set'] == $delta || count($form_state['new_option_sets']) == 1;
      $form['variations']['editing']['new'][$delta] = array(
        '#type' => 'container',
        '#title' => t('New variation set #@num', array(
          '@num' => $delta + 1,
        )),
        '#theme' => 'acquia_lift_card',
        '#collapsed' => !$expanded,
        '#access' => $editable,
        '#attributes' => array(
          'id' => $new_option_set_details_id,
        ),
      );
      if (empty($new_option_set_type)) {

        // Show option set type selection.
        $form['variations']['editing']['new'][$delta]['option_set_type'] = array(
          '#type' => 'radios',
          '#options' => $option_set_options,
          '#theme' => 'acquia_lift_radio_list',
          '#ajax' => array(
            'wrapper' => $new_option_set_details_id,
            'callback' => 'acquia_lift_personalize_campaign_wizard_variations_ajax_delta',
          ),
          '#attributes' => array(
            'autocomplete' => 'off',
          ),
        );
        foreach ($option_set_types as $type => $details) {
          $form['variations']['editing']['new'][$delta]['option_set_type'][$type]['#description'] = $details['description'];
          $form['variations']['editing']['new'][$delta]['option_set_type'][$type]['#image'] = $details['logo'];
        }
        $form['variations']['editing']['new'][$delta]['cancel'] = array(
          '#type' => 'submit',
          '#name' => 'cancel_option_set_' . $delta,
          '#value' => t('Cancel'),
          '#submit' => array(
            'acquia_lift_personalize_campaign_wizard_variations_ajax_cancel',
          ),
          '#limit_validation_errors' => array(),
          '#ajax' => array(
            'callback' => 'acquia_lift_personalize_campaign_wizard_variations_ajax',
            'wrapper' => $new_option_sets_id,
          ),
        );
      }
      else {

        // Display a option set type specific form for the new option set.
        $selected_option_set_type = $option_set_types[$new_option_set_type]['title'];
        $change_type_options = array(
          '#submit' => array(
            'acquia_lift_personalize_campaign_wizard_variations_ajax_change',
          ),
          '#ajax' => array(
            'callback' => 'acquia_lift_personalize_campaign_wizard_variations_ajax_delta',
            'wrapper' => $new_option_set_details_id,
          ),
        );
        switch ($new_option_set_type) {
          case 'block':
            $form['variations']['editing']['new'][$delta] += _acquia_lift_personalize_campaign_wizard_change_type_form($selected_option_set_type, $change_type_options);
            $form['variations']['editing']['new'][$delta]['block'] = array(
              '#type' => 'container',
              '#attributes' => array(
                'class' => array(
                  'acquia-lift-block-variation-set',
                ),
              ),
            );
            $form['variations']['editing']['new'][$delta]['block']['content'] = _acquia_lift_personalize_campaign_wizard_variations_block($form, $form_state, $agent_data, array(
              'variations',
              'editing',
              'new',
              $delta,
              'block',
              'content',
            ));

            // TRICKY: Name must be specified or else it will default to "op" and
            // conflict with the main process bar save button.
            $form['variations']['editing']['new'][$delta]['block']['save'] = array(
              '#name' => 'block_new_' . $delta,
              '#type' => 'submit',
              '#value' => t('Save'),
              '#submit' => array(
                'acquia_lift_personalize_campaign_wizard_variations_single_submit',
              ),
              '#limit_validation_errors' => array(
                array(
                  'variations',
                  'editing',
                  'new',
                  $delta,
                ),
              ),
            );
            $form['variations']['editing']['new'][$delta]['block']['cancel'] = array(
              '#type' => 'submit',
              '#name' => 'cancel_block_option_set_' . $delta,
              '#value' => t('Cancel'),
              '#submit' => array(
                'acquia_lift_personalize_campaign_wizard_variations_ajax_cancel',
              ),
              '#limit_validation_errors' => array(),
              '#ajax' => array(
                'callback' => 'acquia_lift_personalize_campaign_wizard_variations_ajax',
                'wrapper' => $new_option_sets_id,
              ),
            );
            break;
          case 'element':
            $form['variations']['editing']['new'][$delta] += _acquia_lift_personalize_campaign_wizard_change_type_form($selected_option_set_type, $change_type_options);
            $form['variations']['editing']['new'][$delta]['element'] = array(
              '#type' => 'container',
              '#attributes' => array(
                'class' => array(
                  'acquia-lift-element-variation-set',
                ),
              ),
            );
            $form['variations']['editing']['new'][$delta]['element']['content'] = _acquia_lift_personalize_campaign_wizard_variations_element($form, $form_state, $agent_data);
            $form['variations']['editing']['new'][$delta]['element']['cancel'] = array(
              '#type' => 'submit',
              '#name' => 'cancel_element_option_set_' . $delta,
              '#value' => t('Cancel'),
              '#submit' => array(
                'acquia_lift_personalize_campaign_wizard_variations_ajax_cancel',
              ),
              '#limit_validation_errors' => array(),
              '#ajax' => array(
                'callback' => 'acquia_lift_personalize_campaign_wizard_variations_ajax',
                'wrapper' => $new_option_sets_id,
              ),
            );
            break;
        }
      }
    }
  }

  // The button to add a new option set.
  $form['variations']['editing']['new']['add'] = array(
    '#type' => 'submit',
    '#value' => t('Add variation set'),
    '#theme_wrappers' => array(
      'acquia_lift_add_card_button',
    ),
    '#access' => $editable,
    '#submit' => array(
      'acquia_lift_personalize_campaign_wizard_variations_ajax_add',
    ),
    '#limit_validation_errors' => array(),
    '#ajax' => array(
      'callback' => 'acquia_lift_personalize_campaign_wizard_variations_ajax',
      'wrapper' => $new_option_sets_id,
    ),
  );

  // The rest of the display is for displaying current variations.
  if (count($option_sets) == 0) {
    return;
  }

  // Display all the current variation options.
  $form['variations']['display'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(
        'personalize-wizard-column',
      ),
    ),
  );

  // If there are any errors in the multiple variation set handling for this
  // agent then show them here while the user can do something about it.
  if (count($option_sets) > 1) {
    if (!acquia_lift_target_validate_lock_step($option_sets)) {
      $form['variations']['display']['errors'] = array(
        '#type' => 'container',
        '#theme' => 'acquia_lift_card',
        '#title' => t('Let\'s fix this'),
        '#collapsible' => FALSE,
      );
      $form['variations']['display']['errors']['details'] = array(
        '#markup' => theme('item_list', array(
          'items' => array(
            t('Variation sets must contain an equal number of variations.'),
          ),
          'attributes' => array(
            'class' => array(
              'personalize-wizard-review-warnings',
            ),
          ),
        )),
      );
    }
  }
  $form['variations']['display']['options'] = array(
    '#type' => 'container',
    '#theme' => 'acquia_lift_card',
    '#title' => t('All variations'),
    '#collapsible' => FALSE,
    '#attributes' => array(
      'class' => array(
        'acquia-lift-informational',
      ),
    ),
  );
  $all_variations = _acquia_lift_personalize_campaign_wizard_variation_displays($option_sets, $variation_set_handling);
  $form['variations']['display']['options']['variations'] = array(
    '#markup' => theme('acquia_lift_variations_list', array(
      'items' => $all_variations,
    )),
  );
}