AjaxEnable.php in Optimizely 8.3
File
src/Controller/AjaxEnable.php
View source
<?php
namespace Drupal\optimizely\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\optimizely\Util\PathChecker;
use Drupal\optimizely\Util\CacheRefresher;
class AjaxEnable {
public static function enableDisable() {
$unique_path = FALSE;
$default_project = FALSE;
$message = '';
$target_oid = $_POST['target_oid'];
$target_enable = $_POST['target_enable'];
$query = \Drupal::database()
->select('optimizely', 'o', [
'target' => 'slave',
])
->fields('o', [
'path',
'project_code',
])
->condition('o.oid', $target_oid, '=');
$result = $query
->execute()
->fetchObject();
$target_paths = unserialize($result->path);
if ($target_enable == TRUE) {
if (!($target_oid == 1 && $result->project_code == 0)) {
$valid_paths = PathChecker::validatePaths($target_paths);
if ($valid_paths === TRUE) {
list($unique_path, $target_path) = PathChecker::uniquePaths($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.', [
'@valid_paths' => $valid_paths,
]);
}
}
else {
$default_project = TRUE;
$message = t('Default project not enabled.
Enter Optimizely ID in Account Info page.');
}
}
if (($target_enable == FALSE || $unique_path === TRUE) && $default_project == FALSE) {
$target_enable ? $target_enable = 1 : ($target_enable = 0);
$results = \Drupal::database()
->update('optimizely')
->fields([
'enabled' => (int) $target_enable,
])
->condition('oid', $target_oid)
->execute();
CacheRefresher::doRefresh($target_paths);
$options = [
'status' => 'updated',
'oid' => $target_oid,
'target_enable' => $target_enable,
'message' => $message,
];
return new JsonResponse($options);
}
else {
$options = [
'status' => 'rejected',
'oid' => $target_oid,
'issue_path' => $target_path,
'message' => $message,
];
return new JsonResponse($options);
}
}
}
Classes
Name |
Description |
AjaxEnable |
Respond to ajax requests when Enable/Disable checkbox is clicked. |