View source  
  <?php
define('ACQUIA_LIFT_PROFILES_THUMBNAIL_WIDGET_SEPARATOR', '|');
define('ACQUIA_LIFT_PROFILES_DEFAULT_IDENTITY_TYPE_DEFAULT', 'email');
function acquia_lift_profiles_menu() {
  $items = array();
  $items['admin/config/content/personalize/acquia_lift_profiles'] = array(
    'type' => MENU_LOCAL_TASK,
    'title' => 'Acquia Lift Profiles',
    'description' => 'Configuration settings for the Acquia Lift Profiles integration module.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'acquia_lift_profiles_admin_form',
    ),
    'access arguments' => array(
      'administer acquia_lift_profiles configuration',
    ),
    'file' => 'acquia_lift_profiles.admin.inc',
  );
  return $items;
}
function acquia_lift_profiles_translated_menu_link_alter(&$link) {
  
  if ($link['menu_name'] === 'acquia-lift-controls' && $link['link_path'] === 'http://lift.acquia.com') {
    $updated_link = 'http://' . variable_get('acquia_lift_profiles_api_url', 'lift.acquia.com');
    if (valid_url($updated_link, TRUE)) {
      $link['href'] = $updated_link;
    }
  }
}
function acquia_lift_profiles_permission() {
  $permissions = array(
    'administer acquia_lift_profiles configuration' => array(
      'title' => t('Administer acquia_lift_profiles settings'),
      'description' => t('Administer configuration settings for Acquia Lift Profiles.'),
    ),
  );
  return $permissions;
}
function acquia_lift_profiles_is_configured($api_only = FALSE) {
  $api_configured = TRUE;
  foreach (array(
    'acquia_lift_profiles_account_name',
    'acquia_lift_profiles_api_url',
    'acquia_lift_profiles_access_key',
    'acquia_lift_profiles_secret_key',
  ) as $api_config) {
    $val = variable_get($api_config, '');
    if (empty($val)) {
      $api_configured = FALSE;
    }
  }
  if ($api_only) {
    return $api_configured;
  }
  $js_path = variable_get('acquia_lift_profiles_js_path', '');
  return $api_configured && !empty($js_path);
}
function acquia_lift_profiles_refresh_segments_cache() {
  if (!acquia_lift_profiles_is_configured(TRUE)) {
    return FALSE;
  }
  try {
    $segments = ALProfilesAPI::getInstance(variable_get('acquia_lift_profiles_account_name', ''), variable_get('acquia_lift_profiles_site_name', ''), variable_get('acquia_lift_profiles_api_url', ''))
      ->getSegments();
    variable_set('acquia_lift_profiles_segments', $segments);
    return TRUE;
  } catch (Exception $e) {
    watchdog('acquia_lift_profiles', $e
      ->getMessage());
    return FALSE;
  }
}
function acquia_lift_profiles_get_segments() {
  $segments =& drupal_static(__FUNCTION__, NULL);
  if ($segments === NULL) {
    $cached = variable_get('acquia_lift_profiles_segments', NULL);
    if ($cached === NULL) {
      
      acquia_lift_profiles_refresh_segments_cache();
      $cached = variable_get('acquia_lift_profiles_segments', array());
    }
    $segments = $cached;
  }
  return $segments;
}
function acquia_lift_profiles_page_build(&$page) {
  global $base_root;
  if (user_access('manage personalized content')) {
    
    $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;
  }
  
  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,
    
    'mappingContextSeparator' => PERSONALIZE_TARGETING_ADMIN_SEPARATOR,
  );
  
  $assets = $attached = $contexts = array();
  ctools_include('plugins');
  
  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;
    }
  }
  
  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,
      ))) {
        
        $assets = array_merge_recursive($assets, $plugin_instance
          ->getAssets());
      }
    }
  }
  
  $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 (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;
  
  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);
}
function acquia_lift_profiles_personalize_agent_save($agent) {
  
  acquia_lift_profiles_refresh_segments_cache();
}
function acquia_lift_profiles_visitor_actions_page_build(&$page, $actions) {
  
  if (!empty($actions)) {
    $page['page_top']['acquia_lift_profiles_actions'] = array(
      '#attached' => array(
        'js' => array(
          array(
            'data' => array(
              'acquia_lift_profiles' => array(
                'tracked_actions' => $actions,
              ),
            ),
            'type' => 'setting',
          ),
        ),
      ),
    );
  }
}
function acquia_lift_profiles_node_view($node, $view_mode, $langcode) {
  acquia_lift_profiles_node_page_context($node, $view_mode == 'full');
}
function acquia_lift_profiles_form_node_type_form_alter(&$form, &$form_state) {
  if (!isset($form['#node_type']->type)) {
    return;
  }
  $type = 'node';
  $bundle = $form['#node_type']->type;
  
  form_load_include($form_state, 'inc', 'acquia_lift_profiles', 'acquia_lift_profiles.admin');
  acquia_lift_profiles_thumbnail_entity_settings_form($form, $form_state, 'node', entity_get_info('node'), $bundle);
  
  $form['acquia_lift_profiles']['#title'] = t('Acquia Lift Profiles');
  $form['acquia_lift_profiles']['#group'] = 'additional_settings';
}
function acquia_lift_profiles_put_action($action_name, $label, &$errors = array()) {
  try {
    $acquia_lift_profiles_api = ALProfilesAPI::getInstance(variable_get('acquia_lift_profiles_account_name', ''), variable_get('acquia_lift_profiles_site_name', ''), variable_get('acquia_lift_profiles_api_url', ''));
    $acquia_lift_profiles_api
      ->saveEvent($action_name, $label);
    watchdog('acquia_lift_profiles', 'Synchronized the action @action to Acquia Lift Web.', array(
      '@action' => $action_name,
    ), WATCHDOG_INFO);
  } catch (ALProfilesException $e) {
    watchdog('acquia_lift_profiles', 'There was a problem saving the action @action to Acquia Lift Profiles. Please try again later.', array(
      '@action' => $action_name,
    ), WATCHDOG_ERROR);
    $errors[] = $e
      ->getMessage();
  }
}
function acquia_lift_profiles_visitor_actions_save_action($action) {
  $errors = array();
  acquia_lift_profiles_put_action($action['machine_name'], acquia_lift_profiles_get_label_for_action($action), $errors);
  foreach ($errors as $error) {
    drupal_set_message($error, 'error');
  }
}
function acquia_lift_profiles_delete_action($action_name) {
  try {
    $acquia_lift_profiles_api = ALProfilesAPI::getInstance(variable_get('acquia_lift_profiles_account_name', ''), variable_get('acquia_lift_profiles_site_name', ''), variable_get('acquia_lift_profiles_api_url', ''));
    $acquia_lift_profiles_api
      ->deleteEvent($action_name);
  } catch (ALProfilesException $e) {
    watchdog('acquia_lift_profiles', 'There was a problem deleting the action @action from Acquia Lift Profiles. Please try again later.', array(
      '@action' => $action_name,
    ), WATCHDOG_ERROR);
  }
}
function acquia_lift_profiles_visitor_actions_delete_action($action) {
  acquia_lift_profiles_delete_action($action['machine_name']);
}
function acquia_lift_profiles_get_label_for_action($action) {
  $label = !empty($action['label']) ? $action['label'] : $action['machine_name'];
  if ($label == 'unlabeled') {
    $label = $action['machine_name'];
  }
  return $label;
}
function acquia_lift_profiles_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_profiles_node_page_context($node, TRUE);
    }
  }
}
function acquia_lift_profiles_node_page_context($node = NULL, $primary = FALSE) {
  $page_context =& drupal_static(__FUNCTION__);
  if (!isset($page_context)) {
    $page_context = array();
  }
  
  if ($primary && empty($page_context['post_id'])) {
    $page_context['content_type'] = $node->type;
    $page_context['content_title'] = $node->title;
    $page_context['published_date'] = $node->created;
    $page_context['post_id'] = $node->nid;
    $account = user_load($node->uid);
    $page_context['author'] = $account->name;
    $page_context['page_type'] = 'node page';
    
    $page_context['thumbnail_url'] = acquia_lift_profiles_thumbnail_image('node', $node, $node->type);
  }
  
  drupal_alter('acquia_lift_profiles_page_context', $page_context);
  return $page_context;
}
function acquia_lift_profiles_get_page_context() {
  
  $page_context = acquia_lift_profiles_node_page_context();
  if (!isset($page_context['content_title'])) {
    $page_context['content_title'] = drupal_get_title();
  }
  
  drupal_alter('acquia_lift_profiles_page_context', $page_context);
  return $page_context;
}
function acquia_lift_profiles_personalize_visitor_context() {
  $info = array();
  $path = drupal_get_path('module', 'acquia_lift_profiles') . '/plugins';
  $info['acquia_lift_profiles_context'] = array(
    'path' => $path . '/visitor_context',
    'handler' => array(
      'file' => 'ALProfilesContext.inc',
      'class' => 'ALProfilesContext',
    ),
  );
  return $info;
}
function acquia_lift_profiles_visitor_action_subscribe($name) {
  return array(
    'acquia_lift_profiles_action_subscriber',
  );
}
function acquia_lift_profiles_action_subscriber($name, $context) {
  acquia_lift_profiles_set_server_side_action($name, $context);
}
function acquia_lift_profiles_set_server_side_action($action_name, $context) {
  if (!isset($_SESSION['acquia_lift_profiles_actions'])) {
    $_SESSION['acquia_lift_profiles_actions'] = array();
  }
  $_SESSION['acquia_lift_profiles_actions'][$action_name][] = serialize($context);
}
function acquia_lift_profiles_get_server_side_actions() {
  if (isset($_SESSION['acquia_lift_profiles_actions'])) {
    $actions = array();
    foreach ($_SESSION['acquia_lift_profiles_actions'] as $action_name => $contexts) {
      foreach ($contexts as $context) {
        $actions[$action_name][] = unserialize($context);
      }
    }
    return $actions;
  }
  return NULL;
}
function acquia_lift_profiles_clear_server_side_actions() {
  unset($_SESSION['acquia_lift_profiles_actions']);
}
function acquia_lift_profiles_process_server_side_actions(&$page) {
  if ($actions = acquia_lift_profiles_get_server_side_actions()) {
    $page['page_top']['acquia_lift_profiles']['#attached']['js'][] = array(
      'type' => 'setting',
      'data' => array(
        'acquia_lift_profiles' => array(
          'serverSideActions' => $actions,
        ),
      ),
    );
    acquia_lift_profiles_clear_server_side_actions();
  }
}
function acquia_lift_profiles_thumbnail_get_field($entity_type, $bundle_name, $reset = FALSE) {
  $fields =& drupal_static(__FUNCTION__);
  $fields = $reset || is_null($fields) ? array() : $fields;
  if (!array_key_exists($entity_type . '_' . $bundle_name, $fields)) {
    module_load_include('inc', 'acquia_lift_profiles', 'acquia_lift_profiles.admin');
    $field = variable_get('acquia_lift_profiles_thumbnail_field_' . $entity_type . '_' . $bundle_name, '');
    $fields[$entity_type . '_' . $bundle_name] = array_key_exists($field, acquia_lift_profiles_thumbnail_available_fields($entity_type, $bundle_name)) ? $field : NULL;
  }
  return $fields[$entity_type . '_' . $bundle_name];
}
function acquia_lift_profiles_thumbnail_set_field($entity_type, $bundle, $field) {
  variable_set('acquia_lift_profiles_thumbnail_field_' . $entity_type . '_' . $bundle, $field);
}
function acquia_lift_profiles_thumbnail_get_style_field($entity_type, $bundle_name) {
  return variable_get('acquia_lift_profiles_thumbnail_style_field_' . $entity_type . '_' . $bundle_name, '');
}
function acquia_lift_profiles_thumbnail_set_style_field($entity_type, $bundle_name, $field) {
  variable_set('acquia_lift_profiles_thumbnail_style_field_' . $entity_type . '_' . $bundle_name, $field);
}
function acquia_lift_profiles_thumbnail_image($entity_type, $entity, $bundle) {
  $image = '';
  $thumbnail = acquia_lift_profiles_thumbnail_get_field($entity_type, $bundle);
  if (empty($thumbnail)) {
    return $image;
  }
  list($field, $widget) = explode(ACQUIA_LIFT_PROFILES_THUMBNAIL_WIDGET_SEPARATOR, $thumbnail);
  $style = acquia_lift_profiles_thumbnail_get_style_field($entity_type, $bundle);
  if ($field) {
    $field_items = field_get_items($entity_type, $entity, $field);
    if (is_array($field_items) && count($field_items) && isset($field_items[0]['uri'])) {
      
      if ($style) {
        $image = image_style_url($style, $field_items[0]['uri']);
      }
      else {
        $image = file_create_url($field_items[0]['uri']);
      }
    }
  }
  return $image;
}
function acquia_lift_profiles_get_udfs() {
  $udfs = array(
    'person' => array(),
    'touch' => array(),
    'event' => array(),
  );
  $counts = array(
    'person' => 50,
    'touch' => 20,
    'event' => 20,
  );
  foreach ($counts as $type => $count) {
    for ($i = 1; $i <= $count; $i++) {
      $udfs[$type][] = $type . '_udf' . $i;
    }
  }
  return $udfs;
}