You are here

function acquia_lift_personalize_option_set_save in Acquia Lift Connector 7

Same name and namespace in other branches
  1. 7.2 acquia_lift.module \acquia_lift_personalize_option_set_save()

Implements hook_personalize_option_set_save().

File

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

Code

function acquia_lift_personalize_option_set_save($option_set) {
  $agent = personalize_agent_load($option_set->agent);
  if (!acquia_lift_is_testing_agent($agent)) {
    return;
  }
  $new_os = FALSE;
  if (isset($option_set->is_new)) {

    // We don't want to save the "is_new" property but we do need to know later on
    // if we're dealing a new option set. We'll put the property back when we're
    // done in case any other code needs it.
    $new_os = TRUE;
    unset($option_set->is_new);
  }

  // Acquia Lift agents store their option set info in the db so that they can
  // sync any changes with Acquia Lift.
  $old_option_sets = isset($agent->data['decisions']) ? $agent->data['decisions'] : array();

  // We can't rely on the fact that the only difference between what we previously
  // stored and the current set of option sets is the option set now being saved,
  // as option sets may have been altered outside of the save/delete api calls.
  // Reload the option sets for this agent.
  $new_option_sets = personalize_option_set_load_by_agent($agent->machine_name, TRUE);

  // Take the data on this option set from what was passed in, rather than from the
  // db.
  $new_option_sets[$option_set->osid] = $option_set;
  if ($old_option_sets == $new_option_sets) {
    return;
  }
  acquia_lift_sync_option_sets($agent, $old_option_sets, $new_option_sets, TRUE);

  // This agent will need to be re-verified as the changes may have made it changed
  // whether or not it is valid.
  acquia_lift_agent_clear_verified_status($agent->machine_name);

  // The only reason that a change to this option set should cause the agent to be
  // paused, is if we now have fewer than two options in the option set.
  if (count($option_set->options) < 2) {
    personalize_pause_if_running($agent->machine_name);
  }
  acquia_lift_sync_fixed_targeting($agent, $new_option_sets);

  // Put back the is_new property if it was there.
  if ($new_os) {
    $option_set->is_new = TRUE;
  }

  // Unless this is a fields-based option set, we're done.
  if ($option_set->plugin !== 'fields') {
    return;
  }

  // For fields-based option sets, we need to do some more set-up, depending on the
  // field settings.
  $field = field_info_field($option_set->data['personalize_fields_field_name']);
  if (!isset($field['settings']['personalize']) || !$field['settings']['personalize']['enabled']) {
    return;
  }
  $needs_starting = FALSE;
  if (isset($option_set->is_new)) {
    if (isset($field['settings']['personalize']['auto_stop']) && $field['settings']['personalize']['auto_stop']) {

      // We need to set the 'auto_stop' property on the newly created agent.
      $agent->data['auto_stop'] = 1;
      personalize_agent_save($agent);
    }
    if (isset($field['settings']['personalize']['create_goal']) && $field['settings']['personalize']['create_goal']) {
      $goals = personalize_goal_load_by_conditions(array(
        'agent' => $agent->machine_name,
      ));
      if (empty($goals)) {
        $js_id = personalize_stringify_osid($option_set->osid);
        $plugin = 'link';
        $action_label = t('Clicks @option_set', array(
          '@option_set' => $option_set->label,
        ));
        $action_name = personalize_generate_machine_name($action_label, 'visitor_actions_machine_name_exists', '_');
        $pages = empty($field['settings']['personalize']['goal_pages']) ? '' : $field['settings']['personalize']['goal_pages'];
        $action = array(
          'label' => $action_label,
          'machine_name' => $action_name,
          'plugin' => $plugin,
          'client_side' => 1,
          'identifier' => '[data-personalize=' . $js_id . ']',
          'event' => 'click',
          'pages' => $pages,
          'data' => array(),
          'limited_use' => 1,
        );

        // Allow the plugin to modify the action before saving.
        if ($class = ctools_plugin_load_class('visitor_actions', 'actionable_element', $plugin, 'handler')) {
          $action = call_user_func_array(array(
            $class,
            'actionPresave',
          ), array(
            $action,
          ));
        }
        if (visitor_actions_save_action($action)) {
          personalize_goal_save($option_set->agent, $action_name, 1);
          $needs_starting = TRUE;
        }
      }
    }
  }
  if ($needs_starting && $field['settings']['personalize']['auto_start']) {

    // We also need to make sure the agent is set to running after everything
    // has been sync'd up.
    $queue = DrupalQueue::get('acquia_lift_sync');
    $queue
      ->createItem(array(
      'callback' => 'personalize_agent_set_status',
      'args' => array(
        $agent->machine_name,
        PERSONALIZE_STATUS_RUNNING,
      ),
    ));

    // Mark this agent as "pending" as it will be started once the queue runs.
    acquia_lift_set_pending_status($agent->machine_name, $agent->label);
  }
}