You are here

function acquia_lift_personalize_campaign_wizard_goals_alter in Acquia Lift Connector 7.2

Alter the goals form.

File

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

Code

function acquia_lift_personalize_campaign_wizard_goals_alter(&$form, &$form_state, $agent_data, $editable) {
  unset($form['goals']['title']['title_summary']);
  $form['goals']['#tree'] = TRUE;

  // Take over form validation and submit handling.
  $form['#validate'] = array(
    'acquia_lift_personalize_campaign_wizard_validate',
  );
  $form['#submit'] = array(
    'acquia_lift_personalize_campaign_wizard_submit',
  );
  if (!$editable) {
    $form['goals']['#disabled'] = TRUE;
  }

  // Alter the existing goals list form items.
  $goal_deltas = element_children($form['goals']['all_goals']);
  if (count($goal_deltas) === 1) {
    $only_goal_delta = current($goal_deltas);
    if (empty($form['goals']['all_goals'][$only_goal_delta]['goal_id']['#value'])) {

      // The only goal form element is a placeholder for a new goal.
      unset($form['goals']['all_goals'][$only_goal_delta]);
      $goal_deltas = array();
    }
  }
  $params = drupal_get_query_parameters();
  $all_actions = visitor_actions_get_actions();
  foreach ($goal_deltas as $delta) {
    $goal_id = $form['goals']['all_goals'][$delta]['goal_id']['#value'];
    $expand = FALSE;
    if ($editable) {

      // Add delete button to the header.
      // We cannot make this an AJAX callback because the form values cannot
      // be carried forward with an updated form build due to the storing of
      // goal values by array index order.
      $form['goals']['all_goals'][$delta]['header'] = array(
        '#type' => 'container',
        'delete' => array(
          '#name' => 'delete_goal_' . $delta,
          '#type' => 'submit',
          '#value' => t('Delete'),
          '#attributes' => array(
            'data-acquia-lift-personalize-goal-id' => $goal_id,
            'title' => t('Delete the goal.'),
            'class' => array(
              'acquia-lift-delete',
            ),
          ),
          '#submit' => array(
            'acquia_lift_personalize_campaign_wizard_goals_submit_delete',
          ),
          '#limit_validation_errors' => array(),
        ),
      );

      // Determine the current action selected.
      $edit_action = $form['goals']['all_goals'][$delta]['action_name']['#default_value'];
      if (isset($form_state['triggering_element']) && end($form_state['triggering_element']['#array_parents']) == 'action_name') {
        $trigger_delta = _acquia_lift_personalize_campaign_wizard_next_element($form_state, 'all_goals');
        if ($trigger_delta === $delta) {
          $edit_action = $form_state['triggering_element']['#value'];
          $expand = TRUE;
        }
      }
      $visitor_action = $all_actions[$edit_action];

      // If this was an automatically created goal then add an explanation.
      if (!empty($visitor_action['data']['auto_created'])) {
        $form['goals']['all_goals'][$delta]['#title_attributes']['data-help-tooltip'] = t('This goal was automatically created for you. Use it if you want to track any click within any variation in this variation set. If you do not want to track clicks in this variation set, feel free to delete this goal.');
      }
      if (!empty($visitor_action['plugin'])) {

        // Show a link to edit the associated action within the label.
        $edit_action_label = t('Visitor action') . ' ' . l(t('Edit'), 'admin/structure/acquia_lift/visitor_action/' . $edit_action, array(
          'attributes' => array(
            'class' => array(
              'ctools-use-modal',
              'ctools-modal-acquia-lift-style',
              'acquia-lift-configure',
            ),
            'id' => 'edit-visitor-action-' . $edit_action,
          ),
        ));
        $form['goals']['all_goals'][$delta]['action_name']['#title'] = $edit_action_label;
      }
      else {

        // Not a custom action and therefore is not editable.
        $form['goals']['all_goals'][$delta]['action_name']['#title'] = t('Visitor action');
      }
    }

    // Make sure the action label/link gets updated when the action
    // selection changes.
    $card_id = 'goals-all-goals-' . $delta;
    $form['goals']['all_goals'][$delta]['#attributes']['id'] = $card_id;
    $form['goals']['all_goals'][$delta]['action_name']['#ajax'] = array(
      'callback' => 'acquia_lift_personalize_campaign_wizard_goals_ajax_existing',
      'wrapper' => $card_id,
    );

    // Convert the goals display into a card.
    $expand = $expand || count(element_children($form['goals']['all_goals'])) === 1 || isset($params['goal']) && $params['goal'] == $goal_id;
    $form['goals']['all_goals'][$delta]['#type'] = 'container';
    $form['goals']['all_goals'][$delta]['#theme'] = 'acquia_lift_card';
    $form['goals']['all_goals'][$delta]['#collapsible'] = TRUE;
    $form['goals']['all_goals'][$delta]['#collapsed'] = !$expand;
    unset($form['goals']['all_goals'][$delta]['remove']);
  }

  // If the triggering element is the goal type selection, then set it to
  // the form_state for the selected goal delta.
  if (isset($form_state['triggering_element']) && end($form_state['triggering_element']['#parents']) == 'goal_type') {
    $delta = _acquia_lift_personalize_campaign_wizard_next_element($form_state, 'new');
    $form_state['new_goals'][$delta] = $form_state['values']['goals']['new'][$delta]['goal_type'];
    $form_state['expanded_goal'] = $delta;
  }

  // Show the card to add a new goal last.
  $new_goals_id = 'acquia-lift-goals-new';
  $form['goals']['new'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'id' => $new_goals_id,
    ),
  );
  if (!empty($form_state['new_goals'])) {

    // Get the goal type UI display details.
    module_load_include('inc', 'acquia_lift', 'acquia_lift.ui');
    $goal_types = acquia_lift_goal_types_ui();
    $goal_type_options = array();
    foreach ($goal_types as $type => $details) {
      $goal_type_options[$type] = $details['title'];
    }
    foreach ($form_state['new_goals'] as $delta => $new_goal_details) {
      $new_goal_type = $form_state['new_goals'][$delta] ?: '';

      // Generate the ID to use when replacing the individual new goal form.
      $new_goal_details_id = 'acquia-lift-new-' . $delta;
      $expanded = isset($form_state['expanded_goal']) && $form_state['expanded_goal'] == $delta || count($form_state['new_goals']) == 1;
      $form['goals']['new'][$delta] = array(
        '#type' => 'container',
        '#theme' => 'acquia_lift_card',
        '#collapsed' => !$expanded,
        '#title' => t('New goal #@num', array(
          '@num' => $delta + 1,
        )),
        '#collapsible' => TRUE,
        '#access' => $editable,
        '#attributes' => array(
          'id' => $new_goal_details_id,
        ),
      );
      if (empty($new_goal_type)) {
        $form['goals']['new'][$delta]['goal_type'] = array(
          '#type' => 'radios',
          '#options' => $goal_type_options,
          '#theme' => 'acquia_lift_radio_list',
          '#ajax' => array(
            'callback' => 'acquia_lift_personalize_campaign_wizard_goals_ajax_delta',
            'wrapper' => $new_goal_details_id,
            'progress' => array(
              'type' => 'throbber',
              'message' => '',
            ),
          ),
          '#attributes' => array(
            'autocomplete' => 'off',
          ),
        );
        foreach ($goal_types as $type => $details) {
          $form['goals']['new'][$delta]['goal_type'][$type]['#description'] = $details['description'];
          $form['goals']['new'][$delta]['goal_type'][$type]['#image'] = $details['logo'];
        }
        $form['goals']['new'][$delta]['cancel'] = array(
          '#type' => 'submit',
          '#name' => 'cancel_goal_' . $delta,
          '#value' => t('Cancel'),
          '#submit' => array(
            'acquia_lift_personalize_campaign_wizard_goals_ajax_cancel',
          ),
          '#limit_validation_errors' => array(),
          '#ajax' => array(
            'callback' => 'acquia_lift_personalize_campaign_wizard_goals_ajax',
            'wrapper' => $new_goals_id,
          ),
        );
      }
      else {

        // Display a goal-type specific form for the new goal.
        module_load_include('inc', 'acquia_lift', 'acquia_lift.admin.unibar');

        // Add the form to change the type of goal selected.
        $selected_goal_type = $goal_types[$new_goal_type]['title'];
        $change_type_options = array(
          '#submit' => array(
            'acquia_lift_personalize_campaign_wizard_goals_ajax_change',
          ),
          '#ajax' => array(
            'callback' => 'acquia_lift_personalize_campaign_wizard_goals_ajax_delta',
            'wrapper' => $new_goal_details_id,
          ),
        );
        $form['goals']['new'][$delta] += _acquia_lift_personalize_campaign_wizard_change_type_form($selected_goal_type, $change_type_options);
        switch ($new_goal_type) {
          case 'existing':
            $form['goals']['new'][$delta]['details'] = _acquia_lift_existing_goal_create_form($agent_data);
            $form['goals']['new'][$delta]['save'] = array(
              '#name' => 'single_save_' . $delta,
              '#type' => 'submit',
              '#value' => t('Save'),
              '#limit_validation_errors' => array(
                array(
                  'goals',
                  'new',
                  $delta,
                ),
              ),
              '#submit' => array(
                'acquia_lift_personalize_campaign_wizard_goals_single_submit',
              ),
            );
            break;
          case 'page':
            $form['goals']['new'][$delta]['details'] = _acquia_lift_page_goal_create_form($agent_data);

            // Update the states properties for each page option.
            foreach (element_children($form['goals']['new'][$delta]['details']['options']['page']) as $page_option) {

              // Assumes that there is a single value being checked in
              // the event input list.
              $visible_states = $form['goals']['new'][$delta]['details']['options']['page'][$page_option]['#states']['visible'][':input[name="event[page]"]'];
              $form['goals']['new'][$delta]['details']['options']['page'][$page_option]['#states']['visible'] = array(
                ':input[name="goals[new][' . $delta . '][details][event][page]"]' => $visible_states,
              );
            }

            // Clear the element validation - instead it will be handled
            // in the form validator.
            unset($form['goals']['new'][$delta]['details']['#element_validate']);

            // Include the page for the validation callback.
            $form_state['build_info']['files'][] = drupal_get_path('module', 'acquia_lift') . '/acquia_lift.admin.unibar.inc';

            // The pages field needs to be manually populated.
            $form['goals']['new'][$delta]['details']['pages'] = array(
              '#type' => 'textarea',
              '#element_validate' => array(
                'visitor_actions_form_element_path_validate',
              ),
              '#title' => t('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>',
              )),
              '#allow_dynamic' => TRUE,
              '#allow_external' => FALSE,
              '#required' => TRUE,
              '#weight' => 10,
            );
            $form['goals']['new'][$delta]['save'] = array(
              '#name' => 'single_save_' . $delta,
              '#type' => 'submit',
              '#value' => t('Save'),
              '#limit_validation_errors' => array(
                array(
                  'goals',
                  'new',
                  $delta,
                ),
              ),
              '#submit' => array(
                'acquia_lift_personalize_campaign_wizard_goals_single_submit',
              ),
            );
            break;
          case 'element':
            $form['goals']['new'][$delta]['details'] = _acquia_lift_personalize_campaign_wizard_goal_element_form();
            break;
        }
        $form['goals']['new'][$delta]['cancel'] = array(
          '#type' => 'submit',
          '#name' => 'cancel_goal_' . $delta,
          '#value' => t('Cancel'),
          '#submit' => array(
            'acquia_lift_personalize_campaign_wizard_goals_ajax_cancel',
          ),
          '#limit_validation_errors' => array(),
          '#ajax' => array(
            'callback' => 'acquia_lift_personalize_campaign_wizard_goals_ajax',
            'wrapper' => $new_goals_id,
          ),
        );
      }
    }
  }

  // The button to add a new goal.
  $form['goals']['new']['add'] = array(
    '#type' => 'submit',
    '#value' => t('Add goal'),
    '#theme_wrappers' => array(
      'acquia_lift_add_card_button',
    ),
    '#access' => $editable,
    '#submit' => array(
      'acquia_lift_personalize_campaign_wizard_goals_ajax_add',
    ),
    '#limit_validation_errors' => array(),
    '#ajax' => array(
      'callback' => 'acquia_lift_personalize_campaign_wizard_goals_ajax',
      'wrapper' => $new_goals_id,
    ),
  );
}