You are here

function optimizely_add_update_form_submit in Optimizely 7.2

Same name and namespace in other branches
  1. 7.3 optimizely.admin.inc \optimizely_add_update_form_submit()

Process form submissions from optimizely_add_update_form().

Either "Add"s or "Update"s a record from the optimizely_add_update_form() form.

File

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

Code

function optimizely_add_update_form_submit($form, &$form_state) {

  // Catch form submitted values and prep for processing
  $oid = $form_state['values']['optimizely_oid'];
  $project_title = check_plain($form_state['values']['optimizely_project_title']);
  $project_code = check_plain($form_state['values']['optimizely_project_code']);

  // @totdo - Add support for "<front>" to allow use of check_plain() on ['optimizely_path']
  $path_array = preg_split('/[\\r\\n]+/', $form_state['values']['optimizely_path'], -1, PREG_SPLIT_NO_EMPTY);
  $enabled = check_plain($form_state['values']['optimizely_enabled']);

  // Process add or edit submission
  // No ID value included in submission - add new entry
  if (!isset($oid)) {
    db_insert('optimizely')
      ->fields(array(
      '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');

    // Rebuild the provided paths to ensure Optimizely javascript is now included on paths
    if ($enabled) {
      optimizely_refresh_cache($path_array);
    }
  }
  else {
    db_update('optimizely')
      ->fields(array(
      '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');

    // Path originally set for project - to be compaired to the updated value to determine what cache paths needs to be refreshed
    $original_path_array = preg_split('/[\\r\\n]+/', $form_state['values']['optimizely_original_path'], -1, PREG_SPLIT_NO_EMPTY);
    optimizely_refresh_cache($path_array, $original_path_array);
  }

  // Return to project listing page
  drupal_goto('admin/config/system/optimizely');
}