You are here

function _acquia_lift_personalize_campaign_wizard_variations_block in Acquia Lift Connector 7.2

Generate the blocks portion of the campaign wizard form.

Parameters

array $form: The form array reference

array $form_state: The current form state

stdClass $agent_data: The data for the current campaign

array $parents: The array of parents for this element

stdClass $option_set: The option set class that should be shown within the form

Return value

array The form section for the requestd block

1 call to _acquia_lift_personalize_campaign_wizard_variations_block()
acquia_lift_personalize_campaign_wizard_variations_alter in ./acquia_lift.admin.wizard.inc
Alter hook for the variations portions of the campaign wizard.

File

./acquia_lift.admin.wizard.inc, line 2607
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_block(&$form, &$form_state, $agent_data, $parents, $option_set = NULL) {
  $element = array();
  $values = isset($form_state['values']) ? $form_state['values'] : array();
  if (empty($option_set->osid)) {
    if (isset($form_state['values'])) {
      $block_content = drupal_array_get_nested_value($form_state['values'], $parents);
      $form_state_key = 'add_' . _acquia_lift_personalize_campaign_wizard_next_element($form_state, 'new', $parents);
    }
    else {
      $block_content = array();
      $form_state_key = 'add_0';
    }
  }
  else {
    $block_content = empty($values['variations']['editing']['option_sets']['option_set_' . $option_set->osid]['content']) ? array() : $values['variations']['editing']['option_sets']['option_set_' . $option_set->osid]['content'];
    $form_state_key = $option_set->osid;
  }
  module_load_include('inc', 'personalize_blocks', 'personalize_blocks.admin');
  module_load_include('inc', 'acquia_lift', 'acquia_lift.ui');
  module_load_include('inc', 'acquia_lift', 'acquia_lift.admin');
  $changes_enabled = empty($option_set) || acquia_lift_target_definition_changes_allowed($agent_data);
  _acquia_lift_personalize_campaign_wizard_form_state_blocks_alter($form_state, $block_content, $form_state_key, $option_set);
  $element = personalize_blocks_form($element, $form_state, 'embed', $option_set);

  // This will add the current number of blocks and the personalized block
  // into the form state.  We move it into a more structured format to support
  // multiple blocks forms per page.
  $form_state['option_set_num_blocks'][$form_state_key] = $form_state['num_blocks'];
  $form_state['option_set_pblock'][$form_state_key] = $form_state['pblock'];

  // Clear the to_remove from the main option set keys
  unset($form_state['option_set_to_remove'][$form_state_key]);

  // Set the form state values back.
  $form_state['values'] = $values;

  // The agent cannot be changed.
  $element['personalize']['#access'] = FALSE;
  $element['agent_select'] = array(
    '#type' => 'value',
    '#value' => $agent_data->machine_name,
  );

  // Add help text to the title.
  $element['title']['#attributes']['placeholder'] = t('Example: Front page offers');

  // Make the variations wrapper a plain container rather than a fieldset.
  $element['pblock_wrapper']['#theme_wrappers'] = array(
    'container',
  );

  // Adjust the blocks container for our own user interface.
  unset($element['pblock_wrapper']['blocks']['#theme']);
  unset($element['pblock_wrapper']['#tree']);
  unset($element['pblock_wrapper']['blocks']['#tree']);
  $pblock_id = drupal_html_id('edit-' . implode('-', $parents) . '-pblock-wrapper');

  // Remove any duplicate indicator that Drupal may have added from seeing this
  // id generated more than once. This can happen in the AJAX callback.
  $dup_re = "/\\-\\-[a-z0-9]+\$/";
  $pblock_id = preg_replace($dup_re, '', $pblock_id);
  $more_id = $pblock_id . '-blocks-more';
  $blocks_id = $pblock_id . '-blocks';

  // Need to update the id so that it is unique on the page.
  $element['pblock_wrapper']['blocks']['#attributes']['id'] = $blocks_id;
  $element['pblock_wrapper']['blocks']['#attributes']['class'][] = 'acquia-lift-personalize-blocks';
  $pblock_blocks = element_children($element['pblock_wrapper']['blocks']);
  $placeholder_help = array(
    t('Example: Winter vacation'),
    t('Example: Island retreat'),
  );
  foreach ($pblock_blocks as $i => $key) {
    $block =& $element['pblock_wrapper']['blocks'][$key];
    $block['block']['#attributes']['class'][] = 'acquia-lift-personalize-block-wrapper';
    $option_id = isset($block['option_id']['#value']) ? $block['option_id']['#value'] : '';

    // Set the option label as a revealing input and give it a label.
    if ($option_set && !empty($option_id)) {
      $block['option_label']['#theme_wrappers'][] = 'acquia_lift_revealing_input';
      $block['option_label']['#title'] = _personalize_generate_option_index($key);
      $block['option_label']['#fix'] = FALSE;
    }
    else {

      // Do not allow automatically generated option labels.
      $block['option_label']['#default_value'] = '';
      $block['option_label']['#title'] = t('Variation title') . ' <span class="form-required" title="This field is required.">*</span>';
      unset($block['option_label']['#size']);
      if (isset($placeholder_help[$i])) {
        $block['option_label']['#attributes']['placeholder'] = $placeholder_help[$i];
      }
    }
    if (!empty($option_set->preview_link) && !empty($option_id)) {
      $block['preview'] = array(
        '#markup' => '<div class="acquia-lift-preview clearfix">' . l(t('Preview variation'), $option_set->preview_link, array(
          'attributes' => array(
            'target' => 'preview',
          ),
          'query' => array(
            PERSONALIZE_PRESELECTION_PARAM => personalize_stringify_osid($option_set->osid) . '--' . $option_id,
          ),
        )) . '</div>',
      );
    }

    // Weights are not manipulated here.
    $block['weight']['#access'] = FALSE;

    // Set the order for inputs within a block option.
    $block['remove']['#weight'] = 10;
    $block['option_label']['#weight'] = 20;
    $block['preview']['#weight'] = 30;
    $block['block']['#weight'] = 40;

    // Block type selection states
    $select_class = 'select-' . $pblock_id . '-' . $key;
    $block['block']['block_type']['#attributes']['class'][] = $select_class;
    $block['block']['bid']['#states']['visible'] = array(
      ':input.' . $select_class => array(
        'value' => 'select',
      ),
    );
    $block['block']['add']['#title'] = '';
    $block['block']['add']['#states']['visible'] = array(
      ':input.' . $select_class => array(
        'value' => 'add',
      ),
    );
    acquia_lift_chosenify_element($block['block']['bid'], array(
      'acquia-lift-chosen-select-third',
    ));

    // Hide the remove button if the option is currently targeted in a running
    // campaign.
    if (!empty($option_id) && !$changes_enabled && acquia_lift_target_option_targeted($agent_data->machine_name, $option_id)) {
      $block['remove']['#access'] = FALSE;
    }
    if (count($pblock_blocks) <= 2) {
      $block['remove']['#access'] = FALSE;
    }

    // If the block still allows removal, then adjust the callbacks based on
    // whether it is an option that requires deletion or just an AJAX
    // re-display of the form.
    if (!isset($block['remove']['#access']) || $block['remove']['#access'] == TRUE) {
      if (!empty($option_id)) {

        // If the option is an existing one, then redirect to a confirmation form
        // prior to deleting.
        $remove_url = "admin/structure/personalize/variations/personalize-blocks/manage/{$option_set->osid}/{$option_id}/delete";
        $block['remove'] = array(
          '#markup' => l(t('Delete'), $remove_url, array(
            'attributes' => array(
              'class' => array(
                'acquia-lift-delete',
              ),
            ),
            'query' => array(
              'destination' => 'admin/structure/personalize/manage/' . $agent_data->machine_name . '/variations',
            ),
          )),
        );
      }
      else {

        // If the block is a new option that has never been saved, then just
        // allow a quick AJAX adjustment for the number to show.
        $block['remove']['#attributes']['id'] = $blocks_id . '-' . $key . '-remove';
        $block['remove']['#attributes']['class'][] = 'acquia-lift-delete';
        $block['remove']['#submit'] = array(
          'acquia_lift_personalize_campaign_wizard_blocks_remove',
        );
        $block['remove']['#ajax'] = array(
          'callback' => 'acquia_lift_personalize_campaign_wizard_blocks_ajax',
          'wrapper' => $blocks_id,
          'effect' => 'fade',
        );
        $block['remove']['#value'] .= '_' . (empty($option_set) ? 'add' : $option_set->osid);
      }
    }
  }

  // Update the "Add another" button to match the desired styling and language.
  $element['pblock_wrapper']['#attributes']['id'] = $pblock_id;
  $element['pblock_wrapper']['blocks_more']['#tag'] = 'button';
  $element['pblock_wrapper']['blocks_more']['#text'] = t('Add variation');
  $element['pblock_wrapper']['blocks_more']['#value'] = 'blocks_' . (empty($option_set) ? 'add' : $option_set->osid);
  $element['pblock_wrapper']['blocks_more']['#theme_wrappers'] = array(
    'personalize_html_tag',
  );
  $element['pblock_wrapper']['blocks_more']['#attributes']['id'] = $more_id;
  $element['pblock_wrapper']['blocks_more']['#attributes']['class'][] = 'personalize-add-link';
  $element['pblock_wrapper']['blocks_more']['#attributes']['name'] = 'blocks_add_variation_' . $form_state_key;
  $element['pblock_wrapper']['blocks_more']['#prefix'] = '<span class="personalize-add-link-prefix"></span>';
  $element['pblock_wrapper']['blocks_more']['#submit'] = array(
    'acquia_lift_personalize_campaign_wizard_blocks_add',
  );
  $element['pblock_wrapper']['blocks_more']['#ajax'] = array(
    'callback' => 'acquia_lift_personalize_campaign_wizard_blocks_ajax',
    'wrapper' => $blocks_id,
    'effect' => 'fade',
  );

  // Remove the form's action buttons
  unset($element['actions']);
  return $element;
}