public static function PathChecker::uniquePaths in Optimizely 8
Same name and namespace in other branches
- 8.0 src/PathChecker.php \Drupal\optimizely\PathChecker::uniquePaths()
Compare target path against the project paths to confirm they're unique.
Parameters
array $target_paths: The paths entered for a new project entry, OR the paths of an existing project entry that has been enabled.
int|null $target_oid: The oid of the project entry that has been enabled, or NULL.
Return value
bool| $target_path: the path that is a duplicate that must be addressed to enable or create the new project entry, or TRUE if unique paths.
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 - Enable or disable the project.
File
- src/
PathChecker.php, line 104
Class
- PathChecker
- Provides static methods to check path validity, etc.
Namespace
Drupal\optimizelyCode
public static function uniquePaths(array $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 = \Drupal::database()
->select('optimizely', 'o', [
'target' => 'slave',
])
->fields('o', [
'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 [
TRUE,
NULL,
];
}
$all_project_paths = [];
// 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 [
$project->project_title,
$target_path,
];
}
}
}
elseif (strpos($target_path, '*') === 0 && (count($target_paths) > 1 || count($all_project_paths) > 0)) {
return [
$project->project_title,
$target_path,
];
}
// Look for sitewide wild card in target project paths.
if (in_array('*', $all_project_paths)) {
return [
$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 [
$project->project_title,
$target_path,
];
}
}
return [
TRUE,
NULL,
];
}
else {
return [
NULL,
$duplicate_target_path,
];
}
}