View source
<?php
namespace Drupal\optimizely\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Link;
use Drupal\optimizely\Util\AccountId;
use Drupal\optimizely\Util\PathChecker;
use Drupal\optimizely\Util\CacheRefresher;
class AddUpdateForm extends FormBase {
public function getFormID() {
return 'optimizely-add-update';
}
public function buildForm(array $form, FormStateInterface $form_state, $target_oid = NULL) {
$addupdate_form = [];
$addupdate_form['#theme'] = 'optimizely_add_update_form';
$form['#attached']['library'][] = 'optimizely/optimizely.forms';
if ($target_oid == NULL) {
$form_action = 'Add';
$intro_message = '';
$addupdate_form['optimizely_oid'] = [
'#type' => 'value',
'#value' => NULL,
];
$enabled = FALSE;
$project_code = '';
}
else {
$form_action = 'Update';
$query = \Drupal::database()
->select('optimizely', 'o', [
'target' => 'slave',
])
->fields('o')
->condition('o.oid', $target_oid, '=');
$record = $query
->execute()
->fetchObject();
$addupdate_form['optimizely_oid'] = [
'#type' => 'value',
'#value' => $target_oid,
];
$addupdate_form['optimizely_original_path'] = [
'#type' => 'value',
'#value' => implode("\n", unserialize($record->path)),
];
$enabled = $record->enabled;
$project_code = $record->project_code == 0 ? 'Undefined' : $record->project_code;
}
$addupdate_form['optimizely_project_title'] = [
'#type' => 'textfield',
'#disabled' => $target_oid == 1 ? TRUE : FALSE,
'#title' => $this
->t('Project Title'),
'#default_value' => $target_oid ? $record->project_title : '',
'#description' => $target_oid == 1 ? $this
->t('Default project, this field can not be changed.') : $this
->t('Descriptive name for the project entry.'),
'#size' => 60,
'#maxlength' => 256,
'#required' => TRUE,
'#weight' => 10,
];
$account_id = AccountId::getId();
$addupdate_form['optimizely_project_code'] = [
'#type' => 'textfield',
'#disabled' => $target_oid == 1 ? TRUE : FALSE,
'#title' => $this
->t('Optimizely Project Code'),
'#default_value' => $project_code,
'#description' => $account_id == 0 ? $this
->t('The Optimizely account value has not been set in the
<a href="@url">Account Info</a> settings form.
The Optimizely account value is used as
the project ID for this "default" project entry.', [
'@url' => Url::fromRoute('optimizely.settings')
->toString(),
]) : $this
->t('The Optimizely javascript file name used in the snippet
as provided by the Optimizely website for the project.'),
'#size' => 30,
'#maxlength' => 100,
'#required' => TRUE,
'#weight' => 20,
];
$addupdate_form['optimizely_path'] = [
'#type' => 'textarea',
'#title' => $this
->t('Set Path Where Optimizely Code Snippet Appears'),
'#default_value' => $target_oid ? implode("\n", unserialize($record->path)) : '',
'#description' => $this
->t('Enter the path where you want to insert the Optimizely
Snippet. For Example: "/clubs/*" causes the snippet to appear on all pages
below "/clubs" in the URL but not on the actual "/clubs" page itself.'),
'#cols' => 100,
'#rows' => 6,
'#resizable' => FALSE,
'#required' => FALSE,
'#weight' => 40,
];
$addupdate_form['optimizely_enabled'] = [
'#type' => 'radios',
'#title' => $this
->t('Enable/Disable Project'),
'#default_value' => $target_oid ? $record->enabled : 0,
'#options' => [
1 => 'Enable project',
0 => 'Disable project',
],
'#weight' => 25,
'#attributes' => $enabled ? [
'class' => [
'enabled',
],
] : [
'class' => [
'disabled',
],
],
];
$addupdate_form['submit'] = [
'#type' => 'submit',
'#value' => $form_action,
'#weight' => 100,
];
$addupdate_form['cancel'] = [
'#markup' => Link::fromTextAndUrl(t('Cancel'), new Url('optimizely.settings'))
->toString(),
'#weight' => 101,
];
return $addupdate_form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$proj_code = $form_state
->getValue('optimizely_project_code');
$op = $form_state
->getValue('op');
if ($proj_code == "Undefined") {
$form_state
->setErrorByName('optimizely_project_code', $this
->t('The Optimizely Account ID must be set in the
<a href="@url">Account Info</a> page.
The account ID is used as the default Optimizely Project Code.', [
'@url' => Url::fromRoute('optimizely.settings')
->toString(),
]));
}
elseif (!ctype_digit($proj_code)) {
$form_state
->setErrorByName('optimizely_project_code', $this
->t("The project code %code must only contain digits.", [
'%code' => $proj_code,
]));
}
elseif ($op == 'Add') {
$query = \Drupal::database()
->query('SELECT project_title FROM {optimizely}
WHERE project_code = :project_code ORDER BY oid DESC', [
':project_code' => $proj_code,
]);
$results = $query
->fetchCol(0);
$query_count = count($results);
if ($query_count > 0 || $proj_code != AccountId::getId() && $query_count >= 2) {
$found_entry_title = $results[0];
$form_state
->setErrorByName('optimizely_project_code', $this
->t('The project code (%project_code) already has an entry
in the "%found_entry_title" project.', [
'%project_code' => $proj_code,
'%found_entry_title' => $found_entry_title,
]));
}
}
$enabled = $form_state
->getValue('optimizely_enabled');
$paths = $form_state
->getValue('optimizely_path');
$oid = $form_state
->getValue('optimizely_oid');
if ($enabled) {
$target_paths = preg_split('/[\\r\\n]+/', $paths, -1, PREG_SPLIT_NO_EMPTY);
self::checkPaths($target_paths);
$valid_path = PathChecker::validatePaths($target_paths);
if (!is_bool($valid_path)) {
$form_state
->setErrorByName('optimizely_path', t('The project path "%project_path" could not be resolved as a valid URL for the site,
or it contains a wildcard * that cannot be handled by this module.', [
'%project_path' => $valid_path,
]));
}
list($error_title, $error_path) = PathChecker::uniquePaths($target_paths, $oid);
if (!is_bool($error_title)) {
$form_state
->setErrorByName('optimizely_path', t('The path "%error_path" will result in a duplicate entry based on
the other project path settings. Optimizely does not allow more
than one project to be run on a page.', [
'%error_path' => $error_path,
]));
}
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$oid = $form_state
->getValue('optimizely_oid');
$project_title = $form_state
->getValue('optimizely_project_title');
$project_code = $form_state
->getValue('optimizely_project_code');
$path_array = preg_split('/[\\r\\n]+/', $form_state
->getValue('optimizely_path'), -1, PREG_SPLIT_NO_EMPTY);
self::checkPaths($path_array);
$enabled = $form_state
->getValue('optimizely_enabled');
if (!isset($oid)) {
\Drupal::database()
->insert('optimizely')
->fields([
'project_title' => $project_title,
'path' => serialize($path_array),
'project_code' => $project_code,
'enabled' => $enabled,
])
->execute();
drupal_set_message(t('The project entry has been created.'), 'status');
if ($enabled) {
CacheRefresher::doRefresh($path_array);
}
}
else {
\Drupal::database()
->update('optimizely')
->fields([
'project_title' => $project_title,
'path' => serialize($path_array),
'project_code' => $project_code,
'enabled' => $enabled,
])
->condition('oid', $oid)
->execute();
drupal_set_message(t('The project entry has been updated.'), 'status');
$original_path_array = preg_split('/[\\r\\n]+/', $form_state
->getValue('optimizely_original_path'), -1, PREG_SPLIT_NO_EMPTY);
CacheRefresher::doRefresh($path_array, $original_path_array);
}
$form_state
->setRedirect('optimizely.listing');
}
private static function checkPaths(&$path_array) {
foreach ($path_array as &$path) {
$path = self::checkPathLeadingSlash($path);
}
}
private static function checkPathLeadingSlash($path) {
return ctype_alnum($path[0]) ? '/' . $path : $path;
}
}