You are here

function acquia_lift_target_audience_save in Acquia Lift Connector 7

Same name and namespace in other branches
  1. 7.2 acquia_lift.admin.inc \acquia_lift_target_audience_save()

Saves a new target audience for an agent.

Parameters

$label: The human-readable name of the audience to create.

$agent_name: THe name of the agent to add the audience to.

$contexts: The contexts that make up the audience definition.

$strategy: The strategy to use for multiple contexts, i.e. 'AND' or 'OR'

3 calls to acquia_lift_target_audience_save()
AcquiaLiftWebTestTarget::createTargetAudience in tests/acquia_lift.test
Helper that adds a target audience using two contexts AND'd together.
AcquiaLiftWebTestTarget::testCreateTargetAudiences in tests/acquia_lift.test
acquia_lift_new_target_audience_form_submit in ./acquia_lift.admin.inc
Submit callback for the new target audience form.

File

./acquia_lift.admin.inc, line 1213
acquia_lift.admin.inc Provides functions needed for the admin UI.

Code

function acquia_lift_target_audience_save($label, $agent_name, $contexts, $strategy) {
  module_load_include('inc', 'personalize', 'personalize.admin');
  $machine_name = personalize_generate_machine_name($label, NULL, '-');

  // Find the option set to use for targeting and add the audience.
  $option_set = acquia_lift_get_option_set_for_targeting($agent_name);
  if (empty($option_set)) {
    return FALSE;
  }
  $option_set->targeting[$machine_name] = array(
    'label' => $label,
    'weight' => count($option_set->targeting),
  );

  // Generate the feature strings and rules for the contexts.
  $agent = personalize_agent_load_agent($agent_name);
  $feature_strings = $feature_rules = array();
  foreach ($contexts as $context_values) {
    list($plugin_name, $context_name) = explode(PERSONALIZE_TARGETING_ADMIN_SEPARATOR, $context_values['context']);

    // Generate a value code based on the operator used.
    $value = personalize_targeting_generate_value_code($context_values['match'], $context_values['operator']);

    // Create a feature string for this context value that can be consumed
    // by the agent that will be using it.
    $feature_string = $agent
      ->convertContextToFeatureString($context_name, $value);
    $feature_strings[] = $feature_string;

    // Save the actual rule information as this is what will be used
    // for evaluating it.
    $feature_rules[$feature_string] = $context_values;

    // Override the context to split it into plugin and context parts.
    $feature_rules[$feature_string]['context'] = $context_name;
    $feature_rules[$feature_string]['plugin'] = $plugin_name;
  }
  $option_set->targeting[$machine_name]['targeting_features'] = $feature_strings;
  $option_set->targeting[$machine_name]['targeting_rules'] = $feature_rules;
  $option_set->targeting[$machine_name]['targeting_strategy'] = $strategy;
  try {
    personalize_option_set_save($option_set);
    return TRUE;
  } catch (PersonalizeException $e) {
    return FALSE;
  }
}