You are here

function acquia_lift_agent_list in Acquia Lift Connector 7.2

Page callback for full campaign listings.

1 string reference to 'acquia_lift_agent_list'
acquia_lift_menu_alter in ./acquia_lift.module
Implements hook_menu_alter().

File

./acquia_lift.admin.inc, line 281
acquia_lift.admin.inc Provides functions needed for the admin UI.

Code

function acquia_lift_agent_list($agents = array()) {
  $agent_type_test = t('Test only');
  $agent_type_target = t('Target only');
  $agent_type_both = t('Test and target');
  $build = array();
  $build['#attached']['library'][] = array(
    'personalize',
    'admin.campaign.list',
  );
  $build['#attached']['css'][] = drupal_get_path('module', 'acquia_lift') . '/css/acquia_lift.admin.css';
  $build['#attached']['js'][] = drupal_get_path('module', 'acquia_lift') . '/js/acquia_lift.admin.js';
  $status_map = personalize_get_agent_status_map();
  if (empty($agents)) {
    $agents = personalize_agent_load_multiple(array(), array(), FALSE, TRUE, 'acquia_lift_sort_agent_started');
  }
  $sorted_agents = array();
  foreach ($agents as $agent) {

    // If this is a nested agent, it should not be listed.
    if ($agent->plugin != 'acquia_lift_target') {
      continue;
    }
    $agent_status = personalize_agent_get_status($agent->machine_name);
    $sorted_agents[$agent_status][] = $agent;
  }
  if (empty($sorted_agents)) {
    $no_agents_intro = '<p>' . t('Acquia Lift Target uses personalizations to control how different sets of content variations are displayed to your website visitors.') . '</p>';
    $no_agents_intro .= '<p>' . t('When you create a new personalization, Acquia Lift Target will walk you through the process, helping you to add the set of variations that you want to display, create the goals that determine which variation is most effective, determine the visitor audience segments, and finally control when you want the personalization to run.') . '</p>';
    $no_agents_intro .= '<p>' . t('To create your first new personalization, click the above Add Personalization link.') . '</p>';
    return array(
      'no_agents' => array(
        '#markup' => $no_agents_intro,
      ),
    );
  }
  $build['personalizations_list'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'id' => 'personalize-personalizations-list',
    ),
  );
  $navigation_bar = array();
  ksort($sorted_agents);
  foreach ($sorted_agents as $status => $agents) {

    // Notice the 'href', 'fragment', and 'external' here are made specific to theme an anchor link.
    $navigation_bar['personalization-group-' . $status] = array(
      'title' => theme('html_tag', array(
        'element' => array(
          '#tag' => 'h3',
          '#value' => $status_map[$status],
        ),
      )),
      'href' => '',
      'fragment' => 'personalization-group-' . $status,
      'external' => TRUE,
      'html' => TRUE,
    );
    $build['personalizations_list']['navigation_bar'] = array(
      '#markup' => theme('links', array(
        'links' => $navigation_bar,
      )),
    );
    $description = '';
    switch ($status) {
      case PERSONALIZE_STATUS_NOT_STARTED:
        $description = t('These personalizations have not yet been manually started.');
        break;
      case PERSONALIZE_STATUS_SCHEDULED:
        $description = t('These personalizations are scheduled to start in the future.');
        break;
      case PERSONALIZE_STATUS_RUNNING:
        $description = t('These personalizations are currently displaying different content variations to visitors.');
        break;
      case PERSONALIZE_STATUS_PAUSED:
        $description = t('These personalizations are displaying the fallback/winner for each variation set, using JavaScript.');
        break;
      case PERSONALIZE_STATUS_COMPLETED:
        $description = t('These personalizations are displaying the fallback/winner for each variation set, using PHP whenever possible, which results in a faster display than Paused personalizations.');
        break;
    }
    $build['personalizations_list']['personalization_group_' . $status] = array(
      '#type' => 'container',
      '#attributes' => array(
        'id' => 'personalization-group-' . $status,
      ),
    );
    $build['personalizations_list']['personalization_group_' . $status]['description'] = array(
      '#markup' => theme('html_tag', array(
        'element' => array(
          '#tag' => 'p',
          '#value' => $description,
        ),
      )),
    );

    // Build the personalizations table.
    $show_status_change = $status != PERSONALIZE_STATUS_COMPLETED;
    $header = array();
    $header[] = array(
      'data' => t('Name'),
    );
    $header[] = array(
      'data' => t('Start date'),
    );
    $header[] = array(
      'data' => t('Type'),
    );
    if ($show_status_change) {
      $header[] = array(
        'data' => t('Change status'),
      );
    }
    $header[] = array(
      'data' => t('Operations'),
    );
    $rows = array();
    foreach ($agents as $agent) {

      // Load targeting audiences
      $targeting_os = acquia_lift_get_option_set_for_targeting($agent->machine_name);
      if (!empty($agent->data['lift_targeting'])) {

        // Use the currently saved targeting even if not yet implemented.
        $targeting = $agent->data['lift_targeting'];
      }
      else {

        // Use the stored targeting that is currently implemented.
        $targeting = acquia_lift_get_structure_from_targeting($targeting_os);
      }
      $agent_type = $agent_type_target;
      if (count($targeting) == 1) {
        $only_audience = reset($targeting);
        if (count($only_audience) > 1) {
          $agent_type = $agent_type_test;
        }
      }
      else {
        if (count($targeting) > 1) {
          foreach ($targeting as $audience => $variations) {
            if (count($variations) > 1) {
              $agent_type = $agent_type_both;
              break;
            }
          }
        }
      }

      // The ability to delete depends on the storage type and on whether the
      // campaign has started or contains option sets.
      $can_delete = acquia_lift_agent_delete_access($agent);
      $delete_link = '';

      // Determine storage
      if ($can_delete) {
        switch ($agent->export_type) {
          case EXPORT_IN_DATABASE | EXPORT_IN_CODE:
            $delete_link = array(
              'title' => t('Revert'),
              'href' => 'admin/structure/personalize/manage/' . $agent->machine_name . '/delete-all',
              'attributes' => array(
                'class' => 'acquia-lift-delete',
                'title' => t('Delete @campaign', array(
                  '@campaign' => $agent->label,
                )),
              ),
            );
            break;
          case EXPORT_IN_DATABASE:
            $delete_link = array(
              'title' => t('Delete'),
              'href' => 'admin/structure/personalize/manage/' . $agent->machine_name . '/delete-all',
              'attributes' => array(
                'class' => 'acquia-lift-delete',
                'title' => t('Delete @campaign', array(
                  '@campaign' => $agent->label,
                )),
              ),
            );
            break;
        }
      }

      // Determine the start date to show (either schedule or actual).
      switch ($status) {
        case PERSONALIZE_STATUS_SCHEDULED:
        case PERSONALIZE_STATUS_NOT_STARTED:
          $start_date = personalize_agent_get_start_date($agent->machine_name);
          break;
        default:
          $start_date = isset($agent->started) ? $agent->started : 0;
          break;
      }

      // Lift target agents show reports if they contain tests.
      $show_report_link = $agent->plugin == 'acquia_lift_target' && !empty($agent->started);

      // Generate operations list.
      $ops = array(
        array(
          'title' => t('Edit'),
          'href' => 'admin/structure/personalize/manage/' . $agent->machine_name . '/variations',
          'attributes' => array(
            'class' => array(
              'acquia-lift-edit',
            ),
            'title' => t('Edit @campaign', array(
              '@campaign' => $agent->label,
            )),
          ),
        ),
      );
      if ($show_report_link) {
        $href = 'admin/structure/personalize/manage/' . $agent->machine_name . '/results';
        $ops[] = array(
          'title' => t('View reports'),
          'href' => $href,
          'attributes' => array(
            'class' => array(
              'acquia-lift-report',
            ),
            'title' => t('View reports for @campaign', array(
              '@campaign' => $agent->label,
            )),
          ),
        );
      }
      if (!empty($delete_link)) {
        $delete_link['query'] = array(
          'destination' => current_path(),
        );
        $ops[] = $delete_link;
      }

      // Generate actual table row data.
      $tablerow = array();
      $tablerow[] = array(
        'data' => check_plain($agent->label),
        'class' => array(
          'acquia-lift-campaign-title',
        ),
      );
      $tablerow[] = array(
        'data' => $start_date > 0 ? format_date($start_date, 'custom', 'M d, Y') : '',
      );
      $tablerow[] = array(
        'data' => $agent_type,
      );
      if ($show_status_change) {
        $status_change_form = drupal_get_form("personalize_change_status_{$agent->machine_name}_form", $agent);
        $tablerow[] = array(
          'data' => drupal_render($status_change_form),
        );
      }
      $tablerow[] = array(
        'data' => theme('links', array(
          'links' => $ops,
          'attributes' => array(
            'class' => array(
              'acquia-lift-list-operations',
            ),
          ),
        )),
      );
      $campaign_class = $agent->plugin == 'acquia_lift_target' && !empty($targeting_os->targeting) ? array(
        'acquia-lift-personalize-list-campaign',
      ) : array();
      $rows[] = array(
        'data' => $tablerow,
        'no_striping' => TRUE,
        'class' => $campaign_class,
      );

      // Add rows for targeting.
      if (!empty($targeting_os->targeting)) {
        foreach ($targeting_os->targeting as $audience_id => $audience) {
          if (empty($targeting[$audience_id])) {
            continue;
          }
          $variations = $targeting[$audience_id];
          $tablerow = array();
          $tablerow[] = array(
            'data' => !empty($audience['label']) ? check_plain($audience['label']) : $audience_id,
            'class' => 'acquia-lift-campaign-title',
          );
          $tablerow[] = '';
          $tablerow[] = count($variations) > 1 ? t('Test') : t('Target');
          if ($show_status_change) {
            $tablerow[] = '';
          }
          $ops = array();
          if ($show_report_link && count($variations) > 1) {
            $ops[] = array(
              'title' => t('View reports'),
              'href' => 'admin/structure/personalize/manage/' . $agent->machine_name . '/results',
              'query' => array(
                'targeting_audience' => $audience_id,
              ),
              'attributes' => array(
                'class' => array(
                  'acquia-lift-report',
                ),
                'title' => t('View reports for @campaign: @audience', array(
                  '@campaign' => $agent->label,
                  '@audience' => empty($audience['label']) ? $audience_id : $audience['label'],
                )),
              ),
            );
          }
          if (count($variations) > 1 && !empty($agent->started) && $status != PERSONALIZE_STATUS_COMPLETED) {
            $ops[] = array(
              'title' => t('End test and choose winner'),
              'href' => 'admin/structure/personalize/manage/' . $agent->machine_name . '/audience/' . $audience_id . '/complete',
              'attributes' => array(
                'class' => array(
                  'acquia-lift-complete',
                  'ctools-use-modal',
                  'ctools-modal-acquia-lift-style',
                ),
                'title' => t('End test and choose winner'),
              ),
            );
          }
          $tablerow[] = array(
            'data' => theme('links', array(
              'links' => $ops,
              'attributes' => array(
                'class' => array(
                  'acquia-lift-list-operations',
                ),
              ),
            )),
          );
          $rows[] = array(
            'data' => $tablerow,
            'no_striping' => TRUE,
            'class' => array(
              'acquia-lift-personalize-list-audience',
              'element-hidden',
            ),
          );
        }
      }

      // If there are any old tests that used to run for this agent, they will
      // be listed here as well.
      $old_tests = acquia_lift_get_retired_tests($agent->machine_name);
      foreach ($old_tests as $test) {
        $tablerow = array();
        $tablerow[] = array(
          'data' => t('(retired test) ') . $test->label,
          'class' => 'acquia-lift-campaign-title',
        );
        $tablerow[] = '';
        $tablerow[] = t('Retired test');
        if ($show_status_change) {
          $tablerow[] = '';
        }
        $ops = array(
          // All retired tests have reports.
          array(
            'title' => t('View reports'),
            'href' => 'admin/structure/personalize/manage/' . $agent->machine_name . '/results',
            'query' => array(
              'lift_retired_test' => $test->machine_name,
            ),
            'attributes' => array(
              'class' => array(
                'acquia-lift-report',
              ),
              'title' => t('View reports for retired test @test', array(
                '@test' => $test->label,
              )),
            ),
          ),
          // All retired tests can be deleted.
          array(
            'title' => t('Delete'),
            'href' => 'admin/structure/personalize/manage/' . $test->machine_name . '/delete-all',
            'attributes' => array(
              'class' => 'acquia-lift-delete',
              'title' => t('Delete retired test @test', array(
                '@test' => $test->label,
              )),
            ),
          ),
        );
        $tablerow[] = array(
          'data' => theme('links', array(
            'links' => $ops,
            'attributes' => array(
              'class' => array(
                'acquia-lift-list-operations',
              ),
            ),
          )),
        );
        $rows[] = array(
          'data' => $tablerow,
          'no_striping' => TRUE,
          'class' => array(
            'acquia-lift-personalize-list-audience',
            'element-hidden',
          ),
        );
      }
    }
    $build['personalizations_list']['personalization_group_' . $status]['table'] = array(
      '#theme' => 'table',
      '#header' => $header,
      '#rows' => $rows,
      '#attributes' => array(
        'id' => 'personalize',
      ),
    );
  }
  return $build;
}