You are here

function abjs_condition_form in A/B Test JS 7

Generates a form for creating and editing conditions.

Arg. $cid will be NULL when adding a condition, and will be a number when editing conditions. $cid is checked throughout the function for determining if this is an add form or edit form.

1 string reference to 'abjs_condition_form'
abjs_menu in ./abjs.module
Implements hook_menu().

File

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

Code

function abjs_condition_form($form, &$form_state, $cid = NULL) {
  $form = array();
  $condition_name_default = "";
  $condition_script_default = "";
  if (!empty($cid)) {
    $condition_result = db_query('SELECT name, script FROM {abjs_condition} WHERE cid = :cid', array(
      ':cid' => $cid,
    ));
    if (empty($condition_result)) {
      drupal_set_message(t('The requested condition does not exist.'), 'error');
      return $form;
    }
    $condition = $condition_result
      ->fetchObject();
    $condition_name_default = $condition->name;
    $condition_script_default = $condition->script;
    $form['cid'] = array(
      '#type' => 'value',
      '#value' => $cid,
    );
  }
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Condition Name'),
    '#default_value' => $condition_name_default,
    '#size' => 30,
    '#maxlength' => 50,
    '#required' => TRUE,
  );
  $form['script'] = array(
    '#type' => 'textarea',
    '#title' => t('Condition Script'),
    '#default_value' => $condition_script_default,
    '#description' => t('Any valid javascript with a return statement at the end, returning true or false. Read the <a href="@documentation">documentation</a> for examples', array(
      '@documentation' => 'https://www.drupal.org/node/2716391#example-conditions',
    )),
    '#rows' => 3,
    '#required' => TRUE,
  );
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['save'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    '#weight' => 5,
    '#submit' => array(
      'abjs_condition_form_save_submit',
    ),
  );
  $form['actions']['cancel'] = array(
    '#type' => 'submit',
    '#value' => t('Cancel'),
    '#weight' => 10,
    '#submit' => array(
      'abjs_condition_form_cancel_submit',
    ),
    '#limit_validation_errors' => array(),
  );
  if (!empty($cid)) {
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
      '#weight' => 15,
      '#submit' => array(
        'abjs_condition_form_delete_submit',
      ),
    );
  }

  // Add ace code editor for syntax highlighting on the script field.
  if (variable_get('abjs_ace') == 1) {
    drupal_add_js('https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.0/ace.js', array(
      'type' => 'external',
      'scope' => 'header',
      'group' => JS_LIBRARY,
    ));
    drupal_add_js(drupal_get_path('module', 'abjs') . '/js/abjs-ace-edit.js');
  }
  return $form;
}