You are here

function patron_form in Library 6

Same name and namespace in other branches
  1. 5.2 patron/patron.module \patron_form()

Implementation of hook_form().

File

patron/patron.module, line 219

Code

function patron_form(&$node) {
  $type = node_get_types('type', $node);
  $patrons_are_users = variable_get('patron_is_user', PATRON_NOT_USER) == PATRON_USER;
  $patron_uid = NULL;
  if ($patrons_are_users && !empty($node->email)) {
    $exists = db_result(db_query_range("SELECT uid from {users} where mail= '%s'", $node->email, 0, 1));
    if ($exists) {
      $patron_uid = $exists;
    }
  }

  // We need to define form elements for the node's title and body.
  if ($type->has_title) {
    $form['title'] = array(
      '#type' => 'textfield',
      '#title' => 'Patron Identifier (ID or Complete Name)',
      '#required' => TRUE,
      '#default_value' => $node->title,
      '#description' => t('This must be unique.'),
      '#weight' => -5,
    );
  }
  if ($type->has_body) {
    $form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count);
  }
  $form['required_for_library'] = array(
    '#type' => 'fieldset',
    '#title' => t('Required Library Fields'),
    '#collapsed' => FALSE,
    '#weight' => -3,
  );

  // Now we define the form elements specific to our node type.
  $form['required_for_library']['name_first'] = array(
    '#type' => 'textfield',
    '#title' => t('First Name'),
    '#default_value' => $node->name_first,
    '#size' => 10,
    '#maxlength' => 30,
    '#weight' => -4,
    '#required' => TRUE,
  );
  $form['required_for_library']['name_last'] = array(
    '#type' => 'textfield',
    '#title' => t('Last Name'),
    '#default_value' => $node->name_last,
    '#size' => 10,
    '#maxlength' => 30,
    '#weight' => -3,
    '#required' => TRUE,
  );
  $form['required_for_library']['email'] = array(
    '#type' => 'textfield',
    '#title' => t('E-mail'),
    '#default_value' => $node->email,
    '#size' => 20,
    '#maxlength' => 30,
    '#weight' => -1,
    '#required' => TRUE,
  );
  if ($patrons_are_users) {
    $form['required_for_library']['email']['#autocomplete_path'] = 'patrons/js/email';
    if (!user_access('administer patrons') || !user_access('access user profiles')) {
      $form['required_for_library']['email']['#access'] = FALSE;
    }
    $form['required_for_library']['email']['#description'] = t('If this email address matches an existing Drupal user, that user will be associated with this patron. Similar user emails will be suggested when you begin entering a value.');
    $form['required_for_library']['patron_uid'] = array(
      '#type' => 'value',
      '#value' => $patron_uid,
    );
  }
  if (variable_get('patron_use_barcodes', PATRON_NO_BARCODES) == PATRON_BARCODES) {
    $form['required_for_library']['barcode'] = array(
      '#type' => 'textfield',
      '#size' => 30,
      '#maxlength' => 60,
      '#title' => t('Patron Card Number/Barcode'),
      '#default_value' => $node->barcode,
      '#weight' => 0,
      '#required' => TRUE,
    );
  }
  $form['required_for_library']['disabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('Disabled'),
    '#default_value' => $node->disabled,
    '#return_value' => PATRON_DISABLED,
    '#description' => t('Disabled patrons will not appear in patron lists and may not use the library'),
    '#weight' => 6,
  );
  return $form;
}