You are here

public static function PathChecker::uniquePaths in Optimizely 8.0

Same name and namespace in other branches
  1. 8 src/PathChecker.php \Drupal\optimizely\PathChecker::uniquePaths()
2 calls to PathChecker::uniquePaths()
AddUpdateForm::validateForm in src/AddUpdateForm.php
Check to make sure the project code is unique except for the default entry which uses the account ID but should support an additional entry to allow for custom settings.
AjaxEnable::enableDisable in src/AjaxEnable.php

File

src/PathChecker.php, line 109
Contains \Drupal\optimizely\src\PathChecker

Class

PathChecker
Provides static methods to check path validity, etc.

Namespace

Drupal\optimizely

Code

public static function uniquePaths($target_paths, $target_oid = NULL) {

  // Look up alternative paths
  $target_paths = self::collectAlias($target_paths);

  // Look for duplicate paths in submitted $target_paths
  $duplicate_target_path = self::duplicateCheck($target_paths);

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

    // Collect all of the existing 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 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 = self::collectAlias($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 sitewide
        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 sitewide 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::service('path.matcher')
        ->matchPath($target_path, $all_project_paths_string)) {
        return array(
          $project->project_title,
          $target_path,
        );
      }
    }
    return array(
      TRUE,
      NULL,
    );
  }
  else {
    return array(
      NULL,
      $duplicate_target_path,
    );
  }
}