You are here

function acquia_lift_profiles_page_build in Acquia Lift Connector 7.2

Same name and namespace in other branches
  1. 7 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 192
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(),
        ),
      ),
    );
  }
  if (path_is_admin(current_path())) {
    return;
  }

  // Bail if Acquia Lift Profiles has not yet been configured.
  if (!acquia_lift_profiles_is_configured()) {
    return;
  }
  $api_url = variable_get('acquia_lift_profiles_api_url', '');
  $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,
    'apiUrl' => $api_url,
    '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');
  $cache_expiration = array();
  foreach ($mappings as $field_mappings) {
    foreach ($field_mappings as $mapping) {
      if (empty($mapping)) {
        continue;
      }

      // Set the cache expiration strategy for any mapped visitor context keys,
      // so the values are stored and retrievable.
      $cache_expiration_key = 'visitor_context:' . str_replace('__', ':', $mapping);
      $cache_expiration[$cache_expiration_key] = 'session';

      // Group the mappings by context plugins so we can load each plugin only once.
      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;
    }
  }

  // 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,
      'personalize' => array(
        'cacheExpiration' => $cache_expiration,
      ),
    ),
    '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,
  );
  if (!isset($attached['library'])) {
    $attached['library'] = array();
  }
  $attached['library'][] = array(
    'system',
    'jquery.cookie',
  );
  if (acquia_lift_debug_mode_enabled() && acquia_lift_profiles_is_configured()) {
    $attached['library'][] = array(
      'acquia_lift_profiles',
      'debugger',
    );
  }
  $page['page_top']['acquia_lift_profiles'] = array(
    '#attached' => $attached,
  );
  acquia_lift_profiles_process_server_side_actions($page);
}