You are here

function abjs_test_form_save_submit in A/B Test JS 7

Save function for new and edited tests. Check for new vs. edit by using tid.

1 string reference to 'abjs_test_form_save_submit'
abjs_test_form in ./abjs.admin.inc
Generates a form for adding and editing tests.

File

./abjs.admin.inc, line 440
Admin forms to view/add/edit/delete tests, conditions, experiences.

Code

function abjs_test_form_save_submit($form, &$form_state) {
  global $user;
  if (!empty($form_state['values']['tid'])) {

    // This is an existing test, so update instead of insert.
    $tid = $form_state['values']['tid'];
    db_update('abjs_test')
      ->fields(array(
      'name' => $form_state['values']['name'],
      'active' => $form_state['values']['active'],
      'changed' => REQUEST_TIME,
      'changed_by' => $user->uid,
    ))
      ->condition('tid', $tid, '=')
      ->execute();

    // Delete all entries in the test-condition and test-experience tables to
    // make life easy. Re-insert them later in this function.
    db_delete('abjs_test_condition')
      ->condition('tid', $tid)
      ->execute();
    db_delete('abjs_test_experience')
      ->condition('tid', $tid)
      ->execute();
  }
  else {

    // This is a new test, so insert it.
    $tid = db_insert('abjs_test')
      ->fields(array(
      'name' => $form_state['values']['name'],
      'active' => $form_state['values']['active'],
      'created' => REQUEST_TIME,
      'created_by' => $user->uid,
      'changed' => REQUEST_TIME,
      'changed_by' => $user->uid,
    ))
      ->execute();
  }

  // Whether new or existing test, insert conditions and experiences for this
  // test into the test-condition and test-experience tables. If this is an
  // existing test, an earlier step deleted all the existing rows for this
  // test in these tables. Using db_merge instead of db_insert in case
  // multiple of the same condition or experience were entered, in which case
  // this will collapse them to one.
  if (isset($form_state['values']['conditions_fieldset']['conditions'])) {
    foreach ($form_state['values']['conditions_fieldset']['conditions'] as $cid) {
      if ($cid > 0) {
        db_merge('abjs_test_condition')
          ->key(array(
          'tid' => $tid,
          'cid' => $cid,
        ))
          ->fields(array(
          'tid' => $tid,
          'cid' => $cid,
        ))
          ->execute();
      }
    }
  }
  foreach ($form_state['values']['experiences_fieldset'] as $fieldset) {
    if (isset($fieldset['experience']) && $fieldset['experience'] > 0) {
      db_merge('abjs_test_experience')
        ->key(array(
        'tid' => $tid,
        'eid' => $fieldset['experience'],
      ))
        ->fields(array(
        'tid' => $tid,
        'eid' => $fieldset['experience'],
        'fraction' => $fieldset['experience_fraction'],
      ))
        ->execute();
    }
  }
  $msg = !empty($form_state['values']['tid']) ? t("Successfully updated test") : t("Successfully saved new test");
  drupal_set_message($msg);
  $form_state['redirect'] = array(
    '/admin/config/user-interface/abjs/tests',
  );
}