View source  
  <?php
define('ACQUIA_LIFT_THUMBNAIL_WIDGET_SEPARATOR', '|');
define('ACQUIA_LIFT_DEFAULT_IDENTITY_TYPE_DEFAULT', 'email');
define('ACQUIA_LIFT_DEFAULT_IDENTITY_TYPE_IDENTIFIER', 'account');
define('ACQUIA_LIFT_ENGAGEMENT_SCORE_DEFAULT', 1);
define('ACQUIA_LIFT_GLOBAL_VALUE_DEFAULT', 1);
define('ACQUIA_LIFT_ADMIN_SEPARATOR', '__');
function acquia_lift_menu() {
  $items = array();
  $items['admin/config/services/acquia_lift'] = array(
    'type' => MENU_NORMAL_ITEM,
    'title' => 'Acquia Lift',
    'description' => 'Configure Acquia Lift settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'acquia_lift_admin_form',
    ),
    'access arguments' => array(
      'administer acquia_lift configuration',
    ),
    'file' => 'acquia_lift.admin.inc',
  );
  return $items;
}
function acquia_lift_permission() {
  $permissions = array(
    'administer acquia_lift configuration' => array(
      'title' => t('Administer acquia_lift settings'),
      'description' => t('Administer configuration settings for Acquia Lift.'),
    ),
  );
  return $permissions;
}
function acquia_lift_help($path, $arg) {
  switch ($path) {
    case 'admin/help#acquia_lift':
      $output = '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The Acquia Lift module provides machine-learning-based personalization for Drupal sites.') . '</p>';
      $output .= '<h3>' . t('Configuration') . '</h3>';
      $output .= '<p>' . t('Go to the !configlink to configure your Acquia Lift credentials.', array(
        '!configlink' => l(t('configuration settings page'), 'admin/config/services/acquia_lift'),
      )) . '</p>';
      $output .= '<p>' . t('You can find more info in <a href="@lift-documentation" target="_blank">Documentation</a>.', array(
        '@lift-documentation' => 'https://docs.acquia.com/lift',
      ));
      return $output;
  }
}
function acquia_lift_is_configured() {
  foreach (array(
    'acquia_lift_account_id',
    'acquia_lift_site_id',
    'acquia_lift_assets_url',
  ) as $api_config) {
    $val = variable_get($api_config, '');
    if (empty($val)) {
      return FALSE;
    }
  }
  return TRUE;
}
function acquia_lift_page_build(&$page) {
  
  if (!acquia_lift_is_configured()) {
    return;
  }
  
  $current_path = '/' . current_path();
  $alias_path = drupal_lookup_path('alias', current_path());
  $alias_path = $alias_path == FALSE ? $current_path : '/' . $alias_path;
  $patterns = variable_get('acquia_lift_ignore_path_patterns', '');
  if (drupal_match_path($current_path, $patterns) || drupal_match_path($alias_path, $patterns)) {
    return;
  }
  $assets_url = variable_get('acquia_lift_assets_url');
  $decision_api_url = variable_get('acquia_lift_decision_api_url', '');
  $oauth_url = variable_get('acquia_lift_oauth_url', '');
  
  _acquia_lift_add_meta_tag('account_id', variable_get('acquia_lift_account_id'));
  _acquia_lift_add_meta_tag('site_id', variable_get('acquia_lift_site_id'));
  _acquia_lift_add_meta_tag('liftAssetsURL', $assets_url);
  if (!empty($decision_api_url)) {
    _acquia_lift_add_meta_tag('liftDecisionAPIURL', variable_get('acquia_lift_decision_api_url'));
  }
  if (!empty($oauth_url)) {
    _acquia_lift_add_meta_tag('authEndpoint', variable_get('acquia_lift_oauth_url'));
  }
  _acquia_lift_add_meta_tag('bootstrapMode', variable_get('acquia_lift_bootstrap_mode', 'auto'));
  _acquia_lift_add_meta_tag('contentReplacementMode', variable_get('acquia_lift_content_replacement_mode', 'untrusted'));
  _acquia_lift_add_meta_tag('contentOrigin', variable_get('acquia_lift_content_origin', ''));
  _acquia_lift_add_meta_tag('userAccess', variable_get('acquia_lift_user_access', ''));
  
  $identity_param = variable_get('acquia_lift_identity_param', '');
  $identity_type_param = variable_get('acquia_lift_identity_type_param', '');
  if (!empty($identity_param)) {
    $all_params = drupal_get_query_parameters();
    if (isset($all_params[$identity_param])) {
      
      $identity_type = variable_get('acquia_lift_default_identity_type') ?: ACQUIA_LIFT_DEFAULT_IDENTITY_TYPE_DEFAULT;
      if (!empty($identity_type_param) && !empty($all_params[$identity_type_param])) {
        
        
        $identity_type = check_plain($all_params[$identity_type_param]);
      }
      $identity_value = check_plain($all_params[$identity_param]);
      _acquia_lift_add_meta_tag('identity:' . $identity_type, $identity_value);
    }
  }
  $page_context = acquia_lift_get_page_context();
  foreach ($page_context as $name => $value) {
    _acquia_lift_add_meta_tag($name, is_array($value) ? implode(',', $value) : $value);
  }
  
  $js_file = $assets_url . '/lift.js';
  $attached['js'][$js_file] = array(
    'type' => 'external',
    'weight' => JS_DEFAULT - 10,
    
    'async' => TRUE,
  );
  $css_path = drupal_get_path('module', 'acquia_lift') . '/css/acquia_lift.css';
  $attached['css'][$css_path] = array();
  $page['page_top']['acquia_lift'] = array(
    '#attached' => $attached,
  );
}
function acquia_lift_node_view($node, $view_mode) {
  $page = menu_get_object();
  if (!$page) {
    
    return;
  }
  acquia_lift_get_page_context($node, $view_mode == 'full');
}
function acquia_lift_ctools_render_alter(&$info, &$page, &$context) {
  $task = $context['task'];
  if ($page && $task['module'] == 'page_manager' && $task['name'] == 'node_view' && !empty($context['args'])) {
    $nid = $context['args'][0];
    if (isset($context['contexts']['argument_entity_id:node_1']) && $context['contexts']['argument_entity_id:node_1']->argument == $nid) {
      $node = $context['contexts']['argument_entity_id:node_1']->data;
      acquia_lift_get_page_context($node, TRUE);
    }
  }
}
function acquia_lift_get_page_context($node = NULL, $primary = FALSE) {
  
  module_load_include('inc', 'acquia_lift', 'acquia_lift.context');
  $page_context = _acquia_lift_get_page_context($node, $primary);
  if (!isset($page_context['content_title']) && is_null($node)) {
    $page_context['content_title'] = drupal_get_title();
  }
  
  drupal_alter('acquia_lift_page_context', $page_context);
  return $page_context;
}
function acquia_lift_get_udf_types() {
  return array(
    'person' => 50,
    'touch' => 20,
    'event' => 50,
  );
}
function acquia_lift_get_udfs($type) {
  $counts = acquia_lift_get_udf_types();
  if (!array_key_exists($type, $counts)) {
    return array();
  }
  $count = $counts[$type];
  for ($i = 1; $i <= $count; $i++) {
    $udfs[] = $type . '_udf' . $i;
  }
  return $udfs;
}
function acquia_lift_get_grouped_context_options() {
  module_load_include('inc', 'acquia_lift', 'acquia_lift.context');
  $context_options = _acquia_lift_get_context_options();
  foreach ($context_options as $context_type => $options) {
    foreach ($options as $code => $info) {
      $option_name = $context_type . ACQUIA_LIFT_ADMIN_SEPARATOR . $code;
      if (isset($info['group'])) {
        $group = $info['group'];
        if (!isset($groups[$group])) {
          $groups[$group] = array();
        }
        $groups[$group][$option_name] = $info['name'];
      }
      else {
        $groups['Miscellaneous'][$option_name] = $info['name'];
      }
    }
  }
  return $groups;
}
function _acquia_lift_add_meta_tag($name, $value) {
  $tag = array(
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#attributes' => array(
      'itemprop' => 'acquia_lift:' . $name,
      'content' => $value,
    ),
  );
  drupal_add_html_head($tag, 'acquia_lift_' . $name);
}