You are here

public function AbjsTestForm::saveTest in A/B Test JS 2.0.x

Same name and namespace in other branches
  1. 8 src/Form/AbjsTestForm.php \Drupal\abjs\Form\AbjsTestForm::saveTest()

Save data.

Parameters

array $form: The form.

\Drupal\Core\Form\FormStateInterface $form_state: The state of forms.

File

src/Form/AbjsTestForm.php, line 405

Class

AbjsTestForm
Class for build form test.

Namespace

Drupal\abjs\Form

Code

public function saveTest(array &$form, FormStateInterface $form_state) {
  $user = $this
    ->currentUser();
  if ($form_state
    ->hasValue('tid')) {

    // This is an existing test, so update instead of insert.
    $tid = $form_state
      ->getValue('tid');
    $this->database
      ->update('abjs_test')
      ->fields([
      'name' => $form_state
        ->getValue('name'),
      'active' => $form_state
        ->getValue('active'),
      'changed' => $this->time
        ->getRequestTime(),
      'changed_by' => $user
        ->id(),
    ])
      ->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.
    $this->database
      ->delete('abjs_test_condition')
      ->condition('tid', $tid)
      ->execute();
    $this->database
      ->delete('abjs_test_experience')
      ->condition('tid', $tid)
      ->execute();
  }
  else {

    // This is a new test, so insert it.
    $tid = $this->database
      ->insert('abjs_test')
      ->fields([
      'name' => $form_state
        ->getValue('name'),
      'active' => $form_state
        ->getValue('active'),
      'created' => $this->time
        ->getRequestTime(),
      'created_by' => $user
        ->id(),
      'changed' => $this->time
        ->getRequestTime(),
      'changed_by' => $user
        ->id(),
    ])
      ->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.
  foreach ($form_state
    ->getValue([
    'conditions_fieldset',
    'conditions',
  ]) as $cid) {
    if ($cid > 0) {
      $this->database
        ->merge('abjs_test_condition')
        ->key([
        'tid' => $tid,
        'cid' => $cid,
      ])
        ->fields([
        'tid' => $tid,
        'cid' => $cid,
      ])
        ->execute();
    }
  }
  foreach ($form_state
    ->getValue([
    'experiences_fieldset',
    'experiences',
  ]) as $experience) {
    if (isset($experience['experience']) && $experience['experience'] > 0) {
      $this->database
        ->merge('abjs_test_experience')
        ->key([
        'tid' => $tid,
        'eid' => $experience['experience'],
      ])
        ->fields([
        'tid' => $tid,
        'eid' => $experience['experience'],
        'fraction' => $experience['fraction'],
      ])
        ->execute();
    }
  }
  $msg = $form_state
    ->hasValue('tid') ? $this
    ->t("Successfully updated test") : $this
    ->t("Successfully saved new test");
  $this
    ->messenger()
    ->addMessage($msg);
  $form_state
    ->setRedirect('abjs.test_admin');
}