You are here

function acquia_lift_profiles_page_build in Acquia Lift Connector 7

Same name and namespace in other branches
  1. 7.2 acquia_lift_profiles/acquia_lift_profiles.module \acquia_lift_profiles_page_build()

Implements hook_page_build().

File

acquia_lift_profiles/acquia_lift_profiles.module, line 125
acquia_lift_profiles.module Provides Acquia Lift Profiles integration.

Code

function acquia_lift_profiles_page_build(&$page) {
  global $base_root;
  if (user_access('manage personalized content')) {

    // Add js and css for positioning the Web Admin link in the navbar.
    $page['page_top']['acquia_lift_profiles_admin'] = array(
      '#attached' => array(
        'js' => array(
          drupal_get_path('module', 'acquia_lift_profiles') . '/js/acquia_lift_profiles.admin.js' => array(),
        ),
        'css' => array(
          drupal_get_path('module', 'acquia_lift_profiles') . '/css/acquia_lift_profiles.admin.css' => array(),
        ),
      ),
    );
  }
  if (path_is_admin(current_path())) {
    return;
  }

  // Bail if Acquia Lift Profiles has not yet been configured.
  if (!acquia_lift_profiles_is_configured()) {
    return;
  }
  $js_path = variable_get('acquia_lift_profiles_js_path', '');
  $account_name = variable_get('acquia_lift_profiles_account_name', '');
  $capture_identity = variable_get('acquia_lift_profiles_capture_identity', FALSE);
  $mappings = variable_get('acquia_lift_profiles_udf_mappings', array());
  $acquia_lift_profiles_field_mappings = variable_get('acquia_lift_profiles_field_mappings', array());
  if (!empty($acquia_lift_profiles_field_mappings)) {
    $mappings['field'] = $acquia_lift_profiles_field_mappings;
  }
  $segments = acquia_lift_profiles_get_segments();
  $settings = array(
    'request_url' => $base_root . request_uri(),
    'available_segments' => $segments,
    'account_name' => $account_name,
    'currentPath' => current_path(),
    'captureIdentity' => $capture_identity,
    'mappings' => $mappings,
    // Since we are using the same separator as provided by personalize
    // module for targeting values, we need to tell our JS settings about
    // it.
    'mappingContextSeparator' => PERSONALIZE_TARGETING_ADMIN_SEPARATOR,
  );

  // We need to make sure that all assets for visitor context plugins used in our udf
  // mappings are avaiable so that we can get the actual context values.
  $assets = $attached = $contexts = array();
  ctools_include('plugins');

  // Group the mappings by context plugins so we can load each plugin only once.
  foreach ($mappings as $field_mappings) {
    foreach ($field_mappings as $mapping) {
      if (empty($mapping)) {
        continue;
      }
      list($plugin, $context) = explode(PERSONALIZE_TARGETING_ADMIN_SEPARATOR, $mapping);
      if (!isset($contexts[$plugin])) {
        $contexts[$plugin] = array();
      }
      $contexts[$plugin][] = $context;
    }
  }

  // Now load the context plugins grab the required assets.
  foreach ($contexts as $plugin_name => $plugin_contexts) {
    if ($class = ctools_plugin_load_class('personalize', 'visitor_context', $plugin_name, 'handler')) {
      if ($plugin_instance = call_user_func_array(array(
        $class,
        'create',
      ), array(
        NULL,
        $plugin_contexts,
      ))) {

        // Ensure js assets for visitor_context plugins are loaded.
        $assets = array_merge_recursive($assets, $plugin_instance
          ->getAssets());
      }
    }
  }

  // If there are querystring parameters present corresponding to the configured
  // identity params, we add the identity and identity type to the js settings.
  $identity_param = variable_get('acquia_lift_profiles_identity_param', '');
  $identity_type_param = variable_get('acquia_lift_profiles_identity_type_param', '');
  if (!empty($identity_param)) {
    $all_params = drupal_get_query_parameters();
    if (isset($all_params[$identity_param])) {
      $settings['identity'] = check_plain($all_params[$identity_param]);
      $settings['identityType'] = variable_get('acquia_lift_profiles_default_identity_type') ?: ACQUIA_LIFT_PROFILES_DEFAULT_IDENTITY_TYPE_DEFAULT;
      if (!empty($identity_type_param) && isset($all_params[$identity_type_param])) {
        $settings['identityType'] = check_plain($all_params[$identity_type_param]);
      }
    }
  }
  $page_context = acquia_lift_profiles_get_page_context();
  foreach ($page_context as $name => $value) {
    if (is_array($value)) {
      $page_context[$name] = implode(',', $value);
    }
  }
  $settings['pageContext'] = $page_context;
  global $user;
  if ($user->uid > 0) {

    // If this is an administrator, add an admin mode setting so that
    // no interactions happen with decision agents.
    if (user_access('administer acquia_lift_profiles configuration')) {
      $settings['adminMode'] = TRUE;
    }
  }
  $account_name = $settings['account_name'];
  $init = <<<EOT
var _tcaq = _tcaq || [];
var _tcwq = _tcwq || [];
_tcaq.push(['setAccount', '{<span class="php-variable">$account_name</span>}']);
(function() {
  function async_load()
  {
    var s = document.createElement('script');
    s.type = 'text/javascript';
    s.async = true;
    s.src = ('https:' == document.location.protocol ? 'https' : 'http')
      + '://{<span class="php-variable">$js_path</span>}';
    var x = document.getElementsByTagName('script')[0];
    x.parentNode.insertBefore(s, x);
  }
  if (window.attachEvent)
    window.attachEvent('onload', async_load);
  else
    window.addEventListener('load', async_load, false);
})();
EOT;

  // Now we need to compile all the assets together with the js settings and files
  // needed for Acquia Lift Profiles itself.
  if (!isset($assets['js'])) {
    $assets['js'] = array();
  }
  $js_settings = array(
    'data' => array(
      'acquia_lift_profiles' => $settings,
    ),
    'type' => 'setting',
  );
  $assets['js'] = array_merge_recursive($assets['js'], array(
    $js_settings,
  ));
  foreach ($assets as $type => $data_array) {
    if (!isset($attached[$type])) {
      $attached[$type] = array();
    }
    $attached[$type] = array_merge($attached[$type], $data_array);
  }
  $js_file = drupal_get_path('module', 'acquia_lift_profiles') . '/js/acquia_lift_profiles.js';
  $attached['js'][$js_file] = array(
    'weight' => JS_DEFAULT - 10,
  );
  $attached['js'][] = array(
    'data' => $init,
    'type' => 'inline',
  );
  if (!isset($attached['library'])) {
    $attached['library'] = array();
  }
  $attached['library'][] = array(
    'system',
    'jquery.cookie',
  );
  $page['page_top']['acquia_lift_profiles'] = array(
    '#attached' => $attached,
  );
  acquia_lift_profiles_process_server_side_actions($page);
}