You are here

function optimizely_ajax_enable in Optimizely 7.2

Same name and namespace in other branches
  1. 7.3 optimizely.admin.inc \optimizely_ajax_enable()

AJAX callback for click event on project enable checkbox.

Return value

json response details for AJAX call.

1 string reference to 'optimizely_ajax_enable'
optimizely_menu in ./optimizely.module
Implements hook_menu().

File

./optimizely.admin.inc, line 581
Admin page callback for the Optimizely module.

Code

function optimizely_ajax_enable() {

  // Default
  $unique_path = FALSE;
  $default_project = FALSE;
  $message = '';

  // Retrieve the json POST values
  $target_oid = $_POST['target_oid'];
  $target_enable = $_POST['target_enable'];

  // Only check path values if project is being enabled,
  // Project is currently disabled (FALSE) and is now being enabled (TRUE)
  if ($target_enable == TRUE) {

    // Lookup the current project settings
    $query = db_select('optimizely', 'o', array(
      'target' => 'slave',
    ))
      ->fields('o', array(
      'path',
      'project_code',
    ))
      ->condition('o.oid', $target_oid, '=');
    $result = $query
      ->execute()
      ->fetchObject();

    // Prevent the Default project from being enabled when the project code is not set
    if (!($target_oid == 1 && $result->project_code == 0)) {

      // Check that the paths are valid for the newly enabled project
      $target_paths = unserialize($result->path);
      $valid_paths = _optimizely_valid_paths($target_paths);

      // Check to see if the enable project has path entries that will result in
      // duplicates with other enable projects
      if ($valid_paths === TRUE) {
        list($unique_path, $target_path) = _optimizely_unique_paths($target_paths, $target_oid);
        if ($unique_path !== TRUE) {
          $message = t('Project was not enabled due to path setting resulting in duplicate path entries between enabled projects.');
        }
      }
      else {
        $message = t('Project was not enabled due to path setting: ' . $valid_paths . ' resulting in an invalid path.');
      }
    }
    else {
      $default_project = TRUE;
      $message = t('Default project not enabled. Enter Optimizely ID in Account Info page.');
    }
  }

  // The newly enabled project has unique paths OR the target project is
  // currently enabled (TRUE) and will now be disbaled
  if (($target_enable == FALSE || $unique_path === TRUE) && $default_project == FALSE) {

    // Toggle $target_enable
    $target_enable ? $target_enable = 1 : ($target_enable = 0);

    // Update database with new enable setting for project entry
    $results = db_update('optimizely')
      ->fields(array(
      'enabled' => (int) $target_enable,
    ))
      ->condition('oid', $target_oid)
      ->execute();

    // Refresh cache on project paths, this includes both enable and disabled
    // projects as there will be a need to clear the js calls in both cases
    $query = db_select('optimizely', 'o', array(
      'target' => 'slave',
    ))
      ->fields('o', array(
      'path',
    ))
      ->condition('o.oid', $target_oid, '=');
    $result = $query
      ->execute()
      ->fetchObject();
    $target_paths = unserialize($result->path);
    optimizely_refresh_cache($target_paths);

    // Tell AJAX request of status to trigger jquery
    drupal_json_output(array(
      'status' => 'updated',
      'oid' => $target_oid,
      'target_enable' => $target_enable,
      'message' => $message,
    ));
  }
  else {
    drupal_json_output(array(
      'status' => 'rejected',
      'oid' => $target_oid,
      'issue_path' => $target_path,
      'message' => $message,
    ));
  }
}