You are here

function acquia_lift_personalize_option_set_presave in Acquia Lift Connector 7.2

Same name and namespace in other branches
  1. 7 acquia_lift.module \acquia_lift_personalize_option_set_presave()

Implements hook_personalize_option_set_presave().

File

./acquia_lift.module, line 836
acquia_lift.module Provides Acquia Lift-specific personalization functionality.

Code

function acquia_lift_personalize_option_set_presave($option_set) {

  // If the everyone-else audience is the only audience, name it "Everyone";
  // otherwise name it "Everyone else".
  if (!empty($option_set->targeting) && ($fallback_audience_id = acquia_lift_get_fallback_audience_name(array_keys($option_set->targeting)))) {
    $fallback_audience_name = count($option_set->targeting) > 1 ? t('Everyone else') : t("Everyone");
    $option_set->targeting[$fallback_audience_id]['label'] = $fallback_audience_name;
  }

  // The rest only applies to saving of existing option sets.
  if (empty($option_set->osid)) {
    return;
  }
  module_load_include('inc', 'acquia_lift', 'acquia_lift.admin');
  $agent_data = personalize_agent_load($option_set->agent);
  if (acquia_lift_target_definition_changes_allowed($agent_data)) {
    return;
  }
  $original_option_set = personalize_option_set_load($option_set->osid);
  $new_option_ids = array_map(function ($option) {
    return $option['option_id'];
  }, $option_set->options);
  $original_option_ids = array_map(function ($option) {
    return $option['option_id'];
  }, $original_option_set->options);
  $removed = array_diff($original_option_ids, $new_option_ids);
  foreach ($removed as $option_id) {
    if (acquia_lift_target_option_targeted($agent_data->machine_name, $option_id)) {
      throw new PersonalizeException(t('Variations used in targeting cannot be removed until the personalization is paused.'));
    }
  }
}