You are here

function optimizely_add_update_form_validate in Optimizely 7.3

Same name and namespace in other branches
  1. 7.2 optimizely.admin.inc \optimizely_add_update_form_validate()

Validate form submissions from optimizely_add_update_form().

Check to make sute 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.

File

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

Code

function optimizely_add_update_form_validate($form, &$form_state) {

  // Ensure at least one role is selected, don't allow empty selection
  if (count($form_state['values']['optimizely_roles']) == 0) {
    form_set_error('optimizely_roles', t('At least one user role must be selected. Hold the Command / Alt key to enable / disable  multiple selections.'));
  }

  // Watch for "Undefined" value in Project Code, Account ID needed in Settings page
  if ($form_state['values']['optimizely_project_code'] == "Undefined") {
    form_set_error('optimizely_project_code', t('The Optimizely Account ID must be set in the <a href="/admin/config/system/optimizely/settings">Account Info</a> page. The account ID is used as the default Optimizely Project Code.'));
  }
  elseif (!ctype_digit($form_state['values']['optimizely_project_code'])) {
    form_set_error('optimizely_project_code', t('The project code !code must only contain digits.', array(
      '!code' => $form_state['values']['optimizely_project_code'],
    )));
  }
  elseif ($form_state['values']['op'] == 'Add') {

    // Confirm project_code is unique or the entered project code is also the account ID - SELECT the project title in prep for related form error message
    $query = db_query('SELECT project_title FROM {optimizely} WHERE
      project_code = :project_code ORDER BY oid DESC', array(
      ':project_code' => $form_state['values']['optimizely_project_code'],
    ));
    $query_count = $query
      ->rowCount();

    // Flag submission if existing entry is found with the same project code value AND it's not an SINGLE entry to replace the "default" entry.
    if ($query_count > 0 || $form_state['values']['optimizely_project_code'] != variable_get('optimizely_id', FALSE) && $query_count >= 2) {

      // Get the title of the project that already had the propject code
      $found_entry_title = $query
        ->fetchField();

      // Flag the project code form field
      form_set_error('optimizely_project_code', t('The project code (!project_code) already has an entry in the "!found_entry_title" project.', array(
        '!project_code' => $form_state['values']['optimizely_project_code'],
        '!found_entry_title' => $found_entry_title,
      )));
    }
  }

  // Skip if disabled entry
  if ($form_state['values']['optimizely_enabled']) {

    // Confirm that the project paths point to valid site URLs
    $target_paths = preg_split('/[\\r\\n]+/', $form_state['values']['optimizely_path'], -1, PREG_SPLIT_NO_EMPTY);
    $valid_path = _optimizely_valid_paths($target_paths);
    if (!is_bool($valid_path)) {
      $valid_path = htmlentities($valid_path);
      form_set_error('optimizely_path', t('The project path "!project_path" is not a valid path. The path or alias could not be resolved as a valid URL that will result in content on the site.', array(
        '!project_path' => $valid_path,
      )));
    }

    // There must be only one Optimizely javascript call on a page. Check paths to ensure there are no duplicates
    // http://support.optimizely.com/customer/portal/questions/893051-multiple-code-snippets-on-same-page-ok-
    list($error_title, $error_path) = _optimizely_unique_paths($target_paths, $form_state['values']['optimizely_oid']);
    if (!is_bool($error_title)) {
      form_set_error('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.', array(
        '!error_path' => $error_path,
      )));
    }
  }
}