You are here

acquia_lift.install in Acquia Lift Connector 7.2

Acquia Lift - Installation file.

File

acquia_lift.install
View source
<?php

/**
 * @file
 * Acquia Lift - Installation file.
 */

/**
 * Implements hook_install().
 */
function acquia_lift_install() {
  $menu_name = 'acquia-lift-controls';
  $menu = array(
    'menu_name' => $menu_name,
    'title' => 'Acquia Lift personalize controls',
    'description' => 'The <em>Acquia Lift personalize controls</em> menu contains actions to create and manage personalizations.',
  );
  menu_save($menu);
}

/**
 * Implements hook_enable().
 */
function acquia_lift_enable() {
  _acquia_lift_build_menu('acquia-lift-controls');
}

/**
 * Implements hook_disable().
 */
function acquia_lift_disable() {

  // Delete the menu links and rebuild router information.
  menu_delete_links('acquia-lift-controls');
  menu_rebuild();
}

/**
 * Implements hook_uninstall().
 */
function acquia_lift_uninstall() {
  $vars = array(
    'acquia_lift_account_info',
    'acquia_lift_api_url',
    'acquia_lift_auto_goal',
    'acquia_lift_client_side_goals',
    'acquia_lift_confidence_measure',
    'acquia_lift_html_context_strip',
    'acquia_lift_legacy_agents',
    'acquia_lift_version',
    'acquia_lift_report_max_days',
    'acquia_lift_unibar_allow_status_change',
    'acquia_lift_report_upgrade_timestamp',
    'acquia_lift_validate_response',
    'acquia_lift_profiles_access_key',
    'acquia_lift_profiles_account_name',
    'acquia_lift_profiles_api_url',
    'acquia_lift_profiles_secret_key',
    'acquia_lift_profiles_js_path',
    'acquia_lift_profiles_site_name',
  );
  foreach ($vars as $var) {
    variable_del($var);
  }

  // Delete the menu items and rebuild router information.
  menu_delete_links('acquia-lift-controls');
  menu_rebuild();

  // Delete any variables used to store report data sources for agents.
  if (function_exists('personalize_agent_load_multiple')) {
    foreach (personalize_agent_load_multiple() as $agent) {
      $variable_name = "acquia_lift_report_source_{$agent->machine_name}";
      variable_del($variable_name);
    }
  }
}

/**
 * Implements hook_schema().
 */
function acquia_lift_schema() {
  return array(
    'cache_acquia_lift_reports' => acquia_lift_get_report_cache_table_schema(),
  );
}

/**
 * Implements hook_requirements().
 */
function acquia_lift_requirements($phase) {
  $requirements = array();
  if ($phase == 'runtime') {

    // Required JavaScript libraries.
    $libraries = array(
      'underscore',
      'backbone',
      'modernizr',
      'chosen',
      'qtip',
      'd3',
      'rickshaw',
    );
    foreach ($libraries as $lib) {
      $requirements['acquia_lift_' . $lib] = array(
        'title' => t('Acquia Lift: @library', array(
          '@library' => $lib,
        )),
        'value' => t('The @library library is not present', array(
          '@library' => $lib,
        )),
        'severity' => REQUIREMENT_ERROR,
      );
      if (function_exists('libraries_detect')) {
        if (($library = libraries_detect($lib)) && !empty($library['installed'])) {
          $requirements['acquia_lift_' . $lib]['value'] = $library['version'];
          $requirements['acquia_lift_' . $lib]['severity'] = REQUIREMENT_OK;
        }
        elseif (!empty($library['error'])) {
          $requirements['acquia_lift_' . $lib]['value'] = $library['error message'];
        }
      }
    }
    $subscription = acquia_agent_settings('acquia_subscription_data');
    $is_active = FALSE;
    if (isset($subscription['heartbeat_data']['acquia_lift']['status']) && $subscription['heartbeat_data']['acquia_lift']['status']) {
      $is_active = TRUE;
    }
    if ($is_active) {
      $requirements['acquia_lift_status'] = array(
        'title' => t('Acquia Lift Subscription status'),
        'severity' => REQUIREMENT_OK,
        'value' => t('Active'),
        'description' => t('You can manually refresh the subscription status in the Acquia Subscription status section on this page.'),
      );
    }
    else {
      $requirements['acquia_lift_status'] = array(
        'title' => t('Acquia Lift Subscription status'),
        'severity' => REQUIREMENT_WARNING,
        'value' => t('Inactive'),
        'description' => t('Your subscription is expired or you are using an invalid identifier and key pair. You can check the subscription identifier and the subscription key at the <a href="@settings">Acquia Lift settings</a> page.', array(
          '@settings' => url('admin/config/content/personalize/acquia_lift'),
        )),
      );
    }
  }

  // Update the cached version whenever we may be updating the module.
  if ($phase == 'runtime' || $phase == 'update') {
    _acquia_lift_set_version();
  }
  return $requirements;
}

/**
 * Helper function to generate the menus for the Acquia Lift controls.
 *
 * @param $menu_name
 *   The name of the acquia lift controls menu in router system.
 */
function _acquia_lift_build_menu($menu_name) {

  // Create a link for attaching option set preview triggers.
  $item = array(
    'link_title' => 'Personalizations',
    'link_path' => 'admin/structure/personalize',
    'menu_name' => $menu_name,
    'options' => array(
      'attributes' => array(
        'data-acquia-lift-personalize' => 'campaigns',
        'class' => array(
          'acquia-lift-campaign-list',
          'visitor-actions-ui-ignore',
        ),
        'id' => 'acquia-lift-menu-campaigns',
      ),
      'html' => FALSE,
    ),
    'expanded' => 1,
    'weight' => 1,
  );
  $item_campaign = menu_link_save($item);

  // Create a new campaign link.
  $item = array(
    'link_title' => 'Add personalization',
    'link_path' => 'admin/structure/personalize/add',
    'menu_name' => $menu_name,
    'options' => array(
      'attributes' => array(
        'data-acquia-lift-personalize-mode' => 'campaign',
        'aria-role' => 'button',
        'aria-pressed' => 'false',
        'class' => array(
          'visitor-actions-ui-ignore',
          'acquia-lift-menu-create',
          'acquia-lift-menu-link',
          'overlay-exclude',
        ),
        'id' => 'acquia-lift-menu-campaign-add',
      ),
      'html' => FALSE,
    ),
    'expanded' => 1,
    'weight' => 1,
    'plid' => $item_campaign,
  );
  menu_link_save($item);

  // Create a link to the all campaigns page.
  $item = array(
    'link_title' => 'All personalizations',
    'link_path' => 'admin/structure/personalize',
    'menu_name' => $menu_name,
    'options' => array(
      'attributes' => array(
        'aria-role' => 'button',
        'aria-pressed' => 'false',
        'class' => array(
          'visitor-actions-ui-ignore',
          'acquia-lift-menu-all',
          'acquia-lift-menu-link',
          'overlay-exclude',
        ),
        'id' => 'acquia-lift-menu-campaign-all',
      ),
      'html' => FALSE,
    ),
    'expanded' => 1,
    'weight' => 9,
    'plid' => $item_campaign,
  );
  menu_link_save($item);

  // Create a link for attaching option sets.
  $item = array(
    'link_title' => 'What',
    'link_path' => 'admin/structure/personalize',
    'menu_name' => $menu_name,
    'options' => array(
      'attributes' => array(
        'data-acquia-lift-personalize' => 'option_sets',
        'class' => array(
          'acquia-lift-option-sets-list',
          'visitor-actions-ui-ignore',
          'overlay-exclude',
        ),
        'id' => 'acquia-lift-menu-option-sets',
      ),
      'html' => FALSE,
    ),
    'expanded' => 1,
    'weight' => 2,
  );
  $item_cv = menu_link_save($item);

  // Create a link for attaching option set preview triggers.
  $item = array(
    'link_title' => 'Add variation set',
    'link_path' => 'admin/structure/acquia_lift/variations/add/nojs',
    'menu_name' => $menu_name,
    'options' => array(
      'attributes' => array(
        'data-acquia-lift-personalize-mode' => 'content-variation',
        'role' => 'button',
        'aria-pressed' => 'false',
        'class' => array(
          'acquia-lift-option-sets-new',
          'visitor-actions-ui-ignore',
          'acquia-lift-menu-create',
          'acquia-lift-menu-link',
          'overlay-exclude',
          'ctools-use-modal',
          'ctools-modal-acquia-lift-style',
        ),
        'id' => 'acquia-lift-menu-option-set-add',
      ),
      'html' => FALSE,
      'fragment' => 'content-variations',
    ),
    'expanded' => 1,
    'weight' => 3,
    'plid' => $item_cv,
  );
  menu_link_save($item);

  // Create a link for listing Goals.
  $item = array(
    'link_title' => 'Why',
    'link_path' => 'admin/structure/personalize',
    'menu_name' => $menu_name,
    'options' => array(
      'attributes' => array(
        'data-acquia-lift-personalize' => 'goals',
        'class' => array(
          'acquia-lift-goals-list',
          'visitor-actions-ui-ignore',
          'overlay-exclude',
        ),
        'id' => 'acquia-lift-menu-goals',
      ),
      'html' => FALSE,
    ),
    'expanded' => 1,
    'weight' => 3,
  );
  $item_goal = menu_link_save($item);

  // Create a link for attaching Goals.
  $item = array(
    'link_title' => 'Add goal',
    'link_path' => 'admin/structure/acquia_lift/goal/add/nojs',
    'menu_name' => $menu_name,
    'options' => array(
      'attributes' => array(
        'data-acquia-lift-personalize-mode' => 'goals',
        'class' => array(
          'acquia-lift-goals-new',
          'visitor-actions-ui-ignore',
          'acquia-lift-menu-create',
          'acquia-lift-menu-link',
          'ctools-use-modal',
          'ctools-modal-acquia-lift-style',
          'overlay-exclude',
        ),
        'id' => 'acquia-lift-menu-goal-add',
      ),
      'html' => FALSE,
    ),
    'expanded' => 1,
    'weight' => 3,
    'plid' => $item_goal,
  );
  menu_link_save($item);

  // Create a link to targeting.
  $item = array(
    'link_title' => 'Who',
    'link_path' => 'admin/structure/personalize/manage/acquia-lift-placeholder/targeting',
    'menu_name' => $menu_name,
    'options' => array(
      'attributes' => array(
        'data-acquia-lift-personalize' => 'targeting',
        'class' => array(
          'visitor-actions-ui-ignore',
          'overlay-exclude',
        ),
        'id' => 'acquia-lift-menu-targeting',
      ),
      'html' => FALSE,
    ),
    'expanded' => 1,
    'weight' => 4,
  );
  menu_link_save($item);

  // Create a link for scheduling
  $item = array(
    'link_title' => 'When',
    'link_path' => 'admin/structure/personalize/manage/acquia-lift-placeholder/scheduling',
    'menu_name' => $menu_name,
    'options' => array(
      'attributes' => array(
        'data-acquia-lift-personalize' => 'scheduling',
        'class' => array(
          'visitor-actions-ui-ignore',
          'overlay-exclude',
        ),
        'id' => 'acquia-lift-menu-scheduling',
      ),
      'html' => FALSE,
    ),
    'expanded' => 1,
    'weight' => 5,
  );
  menu_link_save($item);

  // Create a link for review
  $item = array(
    'link_title' => 'Review',
    'link_path' => 'admin/structure/personalize/manage/acquia-lift-placeholder/review',
    'menu_name' => $menu_name,
    'options' => array(
      'attributes' => array(
        'data-acquia-lift-personalize' => 'review',
        'class' => array(
          'visitor-actions-ui-ignore',
          'overlay-exclude',
        ),
        'id' => 'acquia-lift-menu-review',
      ),
      'html' => FALSE,
    ),
    'expanded' => 1,
    'weight' => 6,
  );
  menu_link_save($item);

  // Create a link to the reports page.
  $item = array(
    'link_title' => 'Reports',
    'link_path' => 'admin/structure/personalize/manage/acquia-lift-placeholder/report',
    'menu_name' => $menu_name,
    'options' => array(
      'attributes' => array(
        'data-acquia-lift-personalize' => 'reports',
        'class' => array(
          'acquia-lift-results-list',
          'visitor-actions-ui-ignore',
        ),
        'id' => 'acquia-lift-menu-reports',
      ),
      'html' => FALSE,
    ),
    'expanded' => 1,
    'weight' => 20,
  );
  menu_link_save($item);

  // Update the menu router information.
  menu_rebuild();
}

/**
 * Expand the acquia-lift-controls menu items by default.
 */
function acquia_lift_update_7001() {
  $items = db_select('menu_links', 'ml')
    ->fields('ml')
    ->condition('module', 'menu')
    ->condition('menu_name', 'acquia-lift-controls')
    ->execute()
    ->fetchAllAssoc('mlid', PDO::FETCH_ASSOC);
  if (!empty($items)) {
    foreach ($items as $item) {
      $item['options'] = unserialize($item['options']);
      $item['expanded'] = 1;
      menu_link_save($item);
    }
  }

  // Update the menu router information.
  menu_rebuild();
}

/**
 * Fix control rate percentage to not be the inverse percentage.
 */
function acquia_lift_update_7002() {
  $result = db_select('personalize_agent', 'a')
    ->fields('a', array(
    'machine_name',
    'data',
  ))
    ->execute();
  foreach ($result as $row) {
    $data = unserialize($row->data);
    if (isset($data['control_rate'])) {
      $data['control_rate'] = 100 - $data['control_rate'];
    }
    db_update('personalize_agent')
      ->condition('machine_name', $row->machine_name)
      ->fields(array(
      'data' => serialize($data),
    ))
      ->execute();
  }
}

/**
 * Update the menu links for campaigns.
 */
function acquia_lift_update_7003() {

  // Change the add campaign link title.
  menu_link_maintain('menu', 'update', 'admin/structure/personalize/add', 'Add campaign');
  menu_link_maintain('menu', 'update', 'admin/structure/personalize', 'All campaigns');
  menu_link_maintain('menu', 'update', 'admin/structure/personalize/manage/acquia-lift-placeholder/report', 'Reports');
}

/**
 * Fix options for links.
 *
 * Delete and rebuilds the menu items so that the corrected option classes can
 * be assigned.
 */
function acquia_lift_update_7004() {
  menu_link_delete(NULL, 'admin/structure/personalize');
  menu_link_delete(NULL, 'admin/structure/personalize/add');
  menu_link_delete(NULL, 'admin/help/acquia_lift');
  menu_link_delete(NULL, 'admin/structure/visitor_actions');
  menu_link_delete(NULL, 'admin/structure/visitor_actions/add');
  menu_link_delete(NULL, 'admin/structure/personalize/manage/acquia-lift-placeholder/report');
  _acquia_lift_build_menu('acquia-lift-controls');
}

/**
 * Update Acquia Lift control links to simplified IA structure.
 */
function acquia_lift_update_7005() {
  menu_delete_links('acquia-lift-controls');
  _acquia_lift_build_menu('acquia-lift-controls');
}

/**
 * Update Acquia Lift links for usability.
 */
function acquia_lift_update_7006() {
  menu_delete_links('acquia-lift-controls');
  _acquia_lift_build_menu('acquia-lift-controls');
}

/**
 * Update the campaigns menu to use simplified campaign modal process.
 */
function acquia_lift_update_7007() {
  menu_delete_links('acquia-lift-controls');
  _acquia_lift_build_menu('acquia-lift-controls');
}

/**
 * Add a cache table for reporting data.
 */
function acquia_lift_update_7008() {
  $schema = acquia_lift_get_report_cache_table_schema();
  db_create_table('cache_acquia_lift_reports', $schema);
}

/**
 * Update the goals menu to use simplified campaign modal process.
 */
function acquia_lift_update_7009() {
  menu_delete_links('acquia-lift-controls');
  _acquia_lift_build_menu('acquia-lift-controls');
}

/**
 * Update the goals menu to take advantage of dynamically resized modals.
 */
function acquia_lift_update_7010() {
  menu_delete_links('acquia-lift-controls');
  _acquia_lift_build_menu('acquia-lift-controls');
}

/**
 * Update goals to the visitor actions machine name specification.
 */
function acquia_lift_update_7011() {
  $result = db_select('personalize_campaign_goals', 'g')
    ->fields('g', array(
    'id',
    'action',
  ))
    ->execute();
  foreach ($result as $row) {
    $updated = str_replace('-', '_', $row->action);
    if ($updated == $row->action) {
      continue;
    }
    db_update('personalize_campaign_goals')
      ->condition('id', $row->id)
      ->fields(array(
      'action' => $updated,
    ))
      ->execute();
    db_update('visitor_actions_actions')
      ->condition('machine_name', $row->action)
      ->fields(array(
      'machine_name' => $updated,
    ))
      ->execute();
  }
}

/**
 * Update Acquia Lift menu bar with id selectors.
 */
function acquia_lift_update_7012() {
  menu_delete_links('acquia-lift-controls');
  _acquia_lift_build_menu('acquia-lift-controls');
}

/**
 * Update Acquia Lift menu bar to add 'overlay-exclude' class.
 */
function acquia_lift_update_7013() {
  menu_delete_links('acquia-lift-controls');
  _acquia_lift_build_menu('acquia-lift-controls');
}

/**
 * Update Acquia Lift menu bar "Add a variation" link to open a modal.
 */
function acquia_lift_update_7014() {
  menu_delete_links('acquia-lift-controls');
  _acquia_lift_build_menu('acquia-lift-controls');
}

/**
 * Update Acquia Lift menu bar to ensure consistent language.
 */
function acquia_lift_update_7015() {
  menu_delete_links('acquia-lift-controls');
  _acquia_lift_build_menu('acquia-lift-controls');
}

/**
 * Converts the 'stop_on_winner' property to 'auto_stop' in both field settings
 * and agent config.
 */
function acquia_lift_update_7016() {

  // First update the agent config.
  $result = db_select('personalize_agent', 'a')
    ->fields('a', array(
    'machine_name',
    'data',
  ))
    ->execute();
  foreach ($result as $row) {
    $data = unserialize($row->data);
    if (isset($data['stop_on_winner'])) {
      $data['auto_stop'] = $data['stop_on_winner'];
      unset($data['stop_on_winner']);
    }
    db_update('personalize_agent')
      ->condition('machine_name', $row->machine_name)
      ->fields(array(
      'data' => serialize($data),
    ))
      ->execute();
  }

  // Now update field settings.
  $result = db_select('field_config', 'f')
    ->fields('f', array(
    'id',
    'field_name',
    'data',
  ))
    ->execute();
  foreach ($result as $row) {
    $data = unserialize($row->data);
    if (isset($data['settings']['personalize']) && isset($data['settings']['personalize']['stop_on_winner'])) {
      $data['settings']['personalize']['auto_stop'] = $data['settings']['personalize']['stop_on_winner'];
      unset($data['settings']['personalize']['stop_on_winner']);
      db_update('field_config')
        ->fields(array(
        'data' => serialize($data),
      ))
        ->condition('id', $row->id)
        ->execute();
    }
  }
}

/**
 * Update Acquia Lift menu bar to disable click-through on the root items.
 */
function acquia_lift_update_7017() {
  menu_delete_links('acquia-lift-controls');
  _acquia_lift_build_menu('acquia-lift-controls');
}

/**
 * Miscellaneous changes required for the new workflow, includes converting legacy
 * campaigns to "personalizations".
 */
function acquia_lift_update_7200() {

  // The queue is now gone so delete the queue class variable.
  variable_del('queue_class_acquia_lift_sync');

  // Clear the ctools plugin "agent_type" cache for personalize, clear loaded
  // files cache, and rebuild the autoloader class definitions.
  cache_clear_all('plugins:personalize:agent_type', 'cache', TRUE);
  cache_clear_all('ctools_plugin_files:personalize:agent_type', 'cache', TRUE);
  registry_rebuild();

  // Convert simple A/B agents to regular Lift agents.
  db_update('personalize_agent')
    ->condition('plugin', 'acquia_lift_simple_ab')
    ->fields(array(
    'plugin' => 'acquia_lift',
  ))
    ->execute();

  // Get all old-style agents and convert them where possible.
  $result = db_query("SELECT a.machine_name, a.label, a.data, o.plugin, o.osid, o.decision_name, o.options, o.targeting FROM {personalize_agent} a INNER JOIN {personalize_option_sets} o ON o.agent = a.machine_name WHERE a.plugin = 'acquia_lift'");

  // First organize our results into arrays of agents with their option sets.
  $agents = array();
  foreach ($result as $row) {
    $options = unserialize($row->options);
    $agents[$row->machine_name][] = array(
      'agent_name' => $row->machine_name,
      'agent_label' => $row->label,
      'agent_data' => unserialize($row->data),
      'osid' => $row->osid,
      'decision_name' => empty($row->decision_name) ? 'osid-' . $row->osid : $row->decision_name,
      'option_count' => count($options),
      'options' => $options,
      'targeting' => unserialize($row->targeting),
    );
  }

  // If an agent has more than one option set or if its option set contains fixed
  // targeting rules, it cannot be converted and will be treated as a legacy agent.
  $legacy_agents = $convertibles = array();

  // If personalize module is not enabled all campaigns will be treated as legacy.
  $personalize_enabled = module_exists('personalize');
  foreach ($agents as $agent_name => $details) {
    $option_set = reset($details);
    if (!empty($option_set['targeting']) || !empty($option_set['agent_data']['visitor_context']) || !$personalize_enabled) {
      $legacy_agents[] = $agent_name;
      continue;
    }
    if (count($details) > 1) {

      // We can convert this if all option sets have the same decision name.
      $decision_name = '';
      $option_count = 0;
      $supported = TRUE;
      foreach ($details as $i => $option_set) {
        if ($i == 0) {
          $decision_name = $option_set['decision_name'];
          $option_count = $option_set['option_count'];
        }
        else {

          // If any of the option sets has a different decision name or option
          // count, then this agent cannot be converted.
          if ($option_set['decision_name'] != $decision_name || $option_set['option_count'] != $option_count) {
            $supported = FALSE;
          }
        }
      }
      if ($supported) {
        $convertibles[$agent_name] = $details;
      }
      else {
        $legacy_agents[] = $agent_name;
      }
    }
    else {
      $convertibles[$agent_name] = $details;
    }
  }

  // If personalize module is not enabled we just want to save the legacy agents
  // and be done.
  if (!$personalize_enabled && !empty($legacy_agents)) {
    drupal_set_message(t('Personalize module was disabled so existing campaigns could not be converted to the new style personalizations. They will be treated as legacy campaigns.'), 'warning');
    variable_set('acquia_lift_legacy_agents', $legacy_agents);
    return;
  }

  // Now convert anything that can be converted.
  foreach ($convertibles as $original_agent_name => $details) {

    // Create a new acquia_lift_target agent which will delegate to this one.
    $agent = new stdClass();
    $agent->label = 'Converted: ' . $details[0]['agent_label'];
    $agent->plugin = 'acquia_lift_target';
    $agent->data = $details[0]['agent_data'];

    // Preserve some data by keys, and discard others.
    $preserved_data_keys = array(
      'control_rate',
      'decision_style',
      'explore_rate',
      'cache_decisions',
    );
    foreach ($agent->data as $key => $data_entry) {
      if (in_array($key, $preserved_data_keys)) {
        continue;
      }
      unset($agent->data[$key]);
    }
    $agent->machine_name = personalize_generate_machine_name($original_agent_name . '-parent', 'personalize_agent_machine_name_exists');
    $nested_agent = personalize_agent_load($original_agent_name);
    try {
      $agent = personalize_agent_save($agent);

      // Set the status of our new agent to that of the original.
      $nested_agent_status = personalize_agent_get_status($original_agent_name);

      // Move goals over to the new agent.
      $goals = personalize_goal_load_by_conditions(array(
        'agent' => $original_agent_name,
      ));
      foreach ($goals as $goal) {
        db_update('personalize_campaign_goals')
          ->condition('id', $goal->id)
          ->fields(array(
          'agent' => $agent->machine_name,
        ))
          ->execute();
      }

      // Remove the "decisions" info from the data property of the original
      // agent as this no longer serves any function.
      unset($nested_agent->data['decisions']);
      personalize_agent_save($nested_agent);
      $nested_osid = NULL;

      // If there are mulitple option sets, they constitute one decision.
      foreach ($details as $i => $option_set_details) {
        $nested_os = personalize_option_set_load($option_set_details['osid']);
        if ($i == 0) {
          $nested_osid = $nested_os->osid;
        }
        $main_os = new stdClass();
        $main_os->agent = $agent->machine_name;
        $main_os->data = $nested_os->data;
        $main_os->label = $nested_os->label;
        $main_os->options = $nested_os->options;
        $main_os->plugin = $nested_os->plugin;
        $main_os->executor = $nested_os->executor;
        $main_os->winner = $nested_os->winner;
        $main_os->stateful = $nested_os->stateful;
        $main_os->preview_link = $nested_os->preview_link;
        $main_os->decision_name = $option_set_details['decision_name'];
        $main_os->targeting = array(
          ACQUIA_LIFT_TARGETING_EVERYONE_ELSE => array(
            'label' => t('Everyone'),
            'weight' => 1,
            'targeting_features' => array(),
            'targeting_rules' => array(),
            'targeting_strategy' => 'OR',
            'osid' => $nested_osid,
          ),
        );
        personalize_option_set_save($main_os);

        // Convert the first of the original option sets to a nested option set
        // and delete the others.
        if ($i == 0) {
          $nested_os->plugin = 'options';
          $nested_os->options = array();
          foreach ($option_set_details['options'] as $option) {
            $nested_os->options[] = array(
              'option_id' => $option['option_id'],
            );
          }
          personalize_option_set_save($nested_os);
        }
        else {
          personalize_option_set_delete($option_set_details['osid']);
        }
      }
      personalize_agent_set_status($agent->machine_name, $nested_agent_status);
      $start_date = personalize_agent_get_start_date($original_agent_name);
      personalize_agent_set_start_date($agent->machine_name, $start_date);
      $end_date = personalize_agent_get_stop_date($original_agent_name);
      personalize_agent_set_stop_date($agent->machine_name, $end_date);
      $agent->started = $nested_agent->started;
      personalize_agent_save($agent);
    } catch (PersonalizeException $e) {
      personalize_agent_delete($agent->machine_name);

      // Add this to the legacy agents, set a message to that effect and continue
      // to the next agent.
      $legacy_agents[] = $original_agent_name;
      drupal_set_message(t('There was a problem converting the campaign @agent to a new style campaign. It will be treated as a legacy agent.', array(
        '@agent' => $original_agent_name,
      )), 'error');
      continue;
    }
  }
  if (!empty($legacy_agents)) {
    drupal_set_message(t('The following campaigns could not be converted to new style personalizations and will be treated as legacy campaigns: ' . implode(', ', $legacy_agents)), 'warning');
    variable_set('acquia_lift_legacy_agents', $legacy_agents);
  }

  // Fix settings for personalized fields.
  $result = db_select('field_config', 'f')
    ->fields('f', array(
    'id',
    'field_name',
    'data',
  ))
    ->execute();
  foreach ($result as $row) {
    $data = unserialize($row->data);
    if (isset($data['settings']['personalize']) && in_array($data['settings']['personalize']['agent_type'], array(
      'acquia_lift',
      'acquia_lift_simple_ab',
    ))) {
      $data['settings']['personalize']['agent_type'] = 'acquia_lift_target';
      if (isset($data['settings']['personalize']['options']['acquia_lift'])) {
        unset($data['settings']['personalize']['options']['acquia_lift']);
      }
      if (isset($data['settings']['personalize']['options']['acquia_lift_simple_ab'])) {
        unset($data['settings']['personalize']['options']['acquia_lift_simple_ab']);
      }
      unset($data['settings']['personalize']['auto_stop']);
      unset($data['settings']['personalize']['auto_start']);
      db_update('field_config')
        ->fields(array(
        'data' => serialize($data),
      ))
        ->condition('id', $row->id)
        ->execute();
    }
  }

  // Update menu to match updated "Personalizations" terminology.
  // Remove Variations and Goals listings pages.
  menu_delete_links('acquia-lift-controls');
  _acquia_lift_build_menu('acquia-lift-controls');
}

/**
 * Removes some variables that are no longer needed.
 */
function acquia_lift_update_7201() {
  $variables = array(
    'acquia_lift_min_runtime_num',
    'acquia_lift_min_runtime_unit',
    'acquia_lift_min_decisions',
  );
  foreach ($variables as $var) {
    variable_del($var);
  }
}

/**
 * Removes unused variable.
 */
function acquia_lift_update_7202() {
  variable_del('acquia_lift_batch_decisions');
}

/**
 * Update personalize elements variation sets to populate the preview link.
 */
function acquia_lift_update_7203() {
  $result = db_query("SELECT o.osid, o.data, o.preview_link FROM {personalize_agent} a INNER JOIN {personalize_option_sets} o ON o.agent = a.machine_name WHERE a.plugin = 'acquia_lift_target' and o.plugin = 'elements'");
  foreach ($result as $row) {
    if (!empty($row->preview_link)) {
      continue;
    }
    $data = unserialize($row->data);

    // If pages is filled out and its only a single page without wild card
    // then assign it as the preview link as well.
    if (!empty($data['pages']) && preg_match('~[\\r\\n\\*]+~', $data['pages']) === 0) {
      db_update('personalize_option_sets')
        ->fields(array(
        'preview_link' => $data['pages'],
      ))
        ->condition('osid', $row->osid)
        ->execute();
    }
  }
}

/**
 * Clears the cache of personalize plugins in ctools.
 */
function acquia_lift_update_7204() {

  // Clear the ctools plugin "agent_type" cache for personalize, clear loaded
  // files cache, and rebuild the autoloader class definitions.
  cache_clear_all('plugins:personalize:agent_type', 'cache', TRUE);
  cache_clear_all('ctools_plugin_files:personalize:agent_type', 'cache', TRUE);
  registry_rebuild();
}

/**
 * Retire first generation decision engine's tests.
 * Store the time that the system has performed V2 report upgrade.
 */
function acquia_lift_update_7205() {

  // Delete the 'acquia_lift_v2_enabled' variable, because "true" became default.
  variable_del('acquia_lift_v2_enabled');

  // Store the time that the system has performed V2 report upgrade.
  variable_set('acquia_lift_report_upgrade_timestamp', time());

  // Delete the V1 account info.
  variable_del('acquia_lift_account_info');

  // Rename the variable that stores api url.
  $api_url = variable_get('acquia_lift_apiv2_url', '');
  if (!empty($api_url)) {
    variable_set('acquia_lift_api_url', $api_url);
  }
  variable_del('acquia_lift_apiv2_url');

  // Retire all V1 tests.
  if (!module_exists('acquia_lift')) {

    // The best we can do is set all Lift personalizations' status to completed.
    // We can't rely on personalize module being enabled so we can't use any of
    // its API functions or constants. Just talk directly to the db and hard-code
    // variable names and status codes.
    $result = db_query("SELECT machine_name, label FROM {personalize_agent} WHERE plugin IN ('acquia_lift', 'acquia_lift_target', 'acquia_lift_learn')");
    $now = time();
    foreach ($result as $row) {

      // Set the stop time of the agent.
      variable_set("personalize_campaign_{$row->machine_name}_stop", $now);

      // Set the status to 8, which is "completed"
      variable_set("personalize_campaign_{$row->machine_name}_status", 8);
    }
    drupal_set_message(t('Acquia Lift module was disabled so v1 style tests could not be retired. All Lift personalizations have been set to completed.'), 'warning');
  }
  else {
    module_load_include('inc', 'acquia_lift', 'acquia_lift.admin');
    $parent_personalizations = personalize_agent_load_by_type('acquia_lift_target', TRUE);

    // We'll create a mapping of parent names to test names.
    $parents = array();
    foreach ($parent_personalizations as $parent_name => $parent) {
      $nested = acquia_lift_get_nested_tests($parent);
      foreach ($nested as $test_name) {
        $parents[$test_name] = $parent_name;
      }
    }

    // Go through each test and retire it.
    $agents = personalize_agent_load_by_type('acquia_lift', TRUE);
    foreach ($agents as $name => $agent) {
      if (!isset($parents[$name])) {
        if (empty($agent->data['lift_retired'])) {

          // This test has no parent, must be legacy - just complete it.
          personalize_agent_set_status($name, PERSONALIZE_STATUS_COMPLETED);
        }
        continue;
      }
      $option_sets = personalize_option_set_load_by_agent($name);
      if (empty($option_sets)) {
        continue;
      }
      $os = reset($option_sets);
      $parent_name = $parents[$name];

      // The goals will be on the parent personalization, not the test itself.
      $goals = personalize_goal_load_by_conditions(array(
        'agent' => $parent_name,
      ));
      $goal_names = array();
      foreach ($goals as $goal) {
        $goal_names[] = $goal->action;
      }

      // We need to find which audience in the parent personalization this test
      // is running for.
      $parent_os = acquia_lift_get_option_set_for_targeting($parent_name);
      foreach ($parent_os->targeting as $audience => $targeting) {
        if (isset($targeting['osid']) && $targeting['osid'] == $os->osid) {
          $audience_name = $audience;
          break;
        }
      }
      if (isset($audience_name)) {
        acquia_lift_stop_test_for_audience($parent_personalizations[$parent_name], $audience_name);
      }
      else {

        // At least just set the status of the test to completed.
        personalize_agent_set_status($name, PERSONALIZE_STATUS_COMPLETED);
      }
    }
    if (!empty($agents)) {
      $names = array_keys($agents);
      drupal_set_message(t("The following v1 tests have been retired: @agents", array(
        "@agents" => implode(',', $names),
      )));
    }
  }
}

/**
 * Helper to get the schema for the cache table.
 */
function acquia_lift_get_report_cache_table_schema() {
  $table_schema = drupal_get_schema_unprocessed('system', 'cache');
  $table_schema['description'] = 'Cache table for Acquia Lift to store reporting data.';
  return $table_schema;
}

Functions

Namesort descending Description
acquia_lift_disable Implements hook_disable().
acquia_lift_enable Implements hook_enable().
acquia_lift_get_report_cache_table_schema Helper to get the schema for the cache table.
acquia_lift_install Implements hook_install().
acquia_lift_requirements Implements hook_requirements().
acquia_lift_schema Implements hook_schema().
acquia_lift_uninstall Implements hook_uninstall().
acquia_lift_update_7001 Expand the acquia-lift-controls menu items by default.
acquia_lift_update_7002 Fix control rate percentage to not be the inverse percentage.
acquia_lift_update_7003 Update the menu links for campaigns.
acquia_lift_update_7004 Fix options for links.
acquia_lift_update_7005 Update Acquia Lift control links to simplified IA structure.
acquia_lift_update_7006 Update Acquia Lift links for usability.
acquia_lift_update_7007 Update the campaigns menu to use simplified campaign modal process.
acquia_lift_update_7008 Add a cache table for reporting data.
acquia_lift_update_7009 Update the goals menu to use simplified campaign modal process.
acquia_lift_update_7010 Update the goals menu to take advantage of dynamically resized modals.
acquia_lift_update_7011 Update goals to the visitor actions machine name specification.
acquia_lift_update_7012 Update Acquia Lift menu bar with id selectors.
acquia_lift_update_7013 Update Acquia Lift menu bar to add 'overlay-exclude' class.
acquia_lift_update_7014 Update Acquia Lift menu bar "Add a variation" link to open a modal.
acquia_lift_update_7015 Update Acquia Lift menu bar to ensure consistent language.
acquia_lift_update_7016 Converts the 'stop_on_winner' property to 'auto_stop' in both field settings and agent config.
acquia_lift_update_7017 Update Acquia Lift menu bar to disable click-through on the root items.
acquia_lift_update_7200 Miscellaneous changes required for the new workflow, includes converting legacy campaigns to "personalizations".
acquia_lift_update_7201 Removes some variables that are no longer needed.
acquia_lift_update_7202 Removes unused variable.
acquia_lift_update_7203 Update personalize elements variation sets to populate the preview link.
acquia_lift_update_7204 Clears the cache of personalize plugins in ctools.
acquia_lift_update_7205 Retire first generation decision engine's tests. Store the time that the system has performed V2 report upgrade.
_acquia_lift_build_menu Helper function to generate the menus for the Acquia Lift controls.