You are here

function _optimizely_unique_paths in Optimizely 7.2

Same name and namespace in other branches
  1. 7.3 optimizely.admin.inc \_optimizely_unique_paths()
2 calls to _optimizely_unique_paths()
optimizely_add_update_form_validate in ./optimizely.admin.inc
Validate form submissions from optimizely_add_update_form().
optimizely_ajax_enable in ./optimizely.admin.inc
AJAX callback for click event on project enable checkbox.

File

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

Code

function _optimizely_unique_paths($target_paths, $target_oid = NULL) {

  // Look up alternative paths
  $target_paths = _optimizely_collect_alias($target_paths);

  // Look for duplicate paths in submitted $target_paths
  $duplicate_target_path = _optimizely_duplicate_check($target_paths);

  // Look for duplicate paths within target paths
  if (!$duplicate_target_path) {

    // Collect all of the exsisting project paths that are enabled,
    $query = db_select('optimizely', 'o', array(
      'target' => 'slave',
    ))
      ->fields('o', array(
      'oid',
      'project_title',
      'path',
    ))
      ->condition('o.enabled', 1, '=');

    // Add target_oid to query when it's an update, $target_oid is will be defined
    if ($target_oid != NULL) {
      $query = $query
        ->condition('o.oid', $target_oid, '<>');
    }
    $projects = $query
      ->execute();

    // No other enabled projects
    if ($query
      ->countQuery()
      ->execute()
      ->fetchField() == 0) {
      return array(
        TRUE,
        NULL,
      );
    }
    $all_project_paths = array();

    // Build array of all the project entry paths
    foreach ($projects as $project) {

      // Collect all of the path values and merge into collective array
      $project_paths = unserialize($project->path);
      $all_project_paths = array_merge($all_project_paths, $project_paths);
    }

    // Add any additional aliases to catch all match possiblities
    $all_project_paths = _optimizely_collect_alias($all_project_paths);

    // Convert array into string for drupal_match_path()
    $all_project_paths_string = implode("\n", $all_project_paths);

    // Check all of the paths for all of the active project entries to make sure
    // the paths are unique
    foreach ($target_paths as $target_path) {

      // "*" found in path
      if (strpos($target_path, '*') !== FALSE) {

        // Look for wild card match if not site wide
        if (strpos($target_path, '*') !== 0) {
          $target_path = substr($target_path, 0, -2);

          // Look for duplicate path due to wild card
          foreach ($all_project_paths as $all_project_path) {
            if (strpos($all_project_path, $target_path) === 0 && $all_project_path != $target_path) {
              return array(
                $project->project_title,
                $target_path,
              );
            }
          }
        }
        elseif (strpos($target_path, '*') === 0 && (count($target_paths) > 1 || count($all_project_paths) > 0)) {
          return array(
            $project->project_title,
            $target_path,
          );
        }

        // Look for site wide wild card in target project paths
        if (in_array('*', $all_project_paths)) {
          return array(
            $project->project_title,
            $target_path,
          );
        }
      }
      elseif (strpos($target_path, '?') !== FALSE) {
        $target_path = substr($target_path, 0, strpos($target_path, '?'));
      }

      // Look for duplicates
      if (drupal_match_path($target_path, $all_project_paths_string)) {
        return array(
          $project->project_title,
          $target_path,
        );
      }
    }
    return array(
      TRUE,
      NULL,
    );
  }
  else {
    return array(
      NULL,
      $duplicate_target_path,
    );
  }
}