You are here

function biblio_admin_types_edit_form in Bibliography Module 7

Same name and namespace in other branches
  1. 6.2 includes/biblio.admin.inc \biblio_admin_types_edit_form()
  2. 6 biblio.admin.inc \biblio_admin_types_edit_form()
  3. 7.2 includes/biblio.admin.inc \biblio_admin_types_edit_form()

Form constructor for the Publication type edit form.

Parameters

int $tid: The publication type.

Return value

array $form

1 string reference to 'biblio_admin_types_edit_form'
biblio_menu in ./biblio.module
Implements hook_menu().

File

includes/biblio.admin.inc, line 763
biblio.admin.inc

Code

function biblio_admin_types_edit_form($form, &$form_state, $tid = 0) {

  // $form['#theme'] = 'biblio_admin_types_edit_form';
  // $form['#tree'] = TRUE;.
  $form['#cache'] = TRUE;
  $tid = isset($form_state['pub_type']) ? $form_state['pub_type'] : (int) $tid;
  $msg = '<div>' . t('On this page you can set type specific "Titles" and "Hints" which will display on the input form.');
  if ($tid) {
    $msg .= ' ' . t('Checking the "Visible" box will add the field to the input form, checking "Required" will force the user to supply a value for this field and the weight value changes the order which it is rendered on the form with smaller values floating to the top of the form.
    <p> Fields which are grayed out on this page have been set to "common" on the !linktobiblioadmin page.</p>', array(
      '!linktobiblioadmin' => l("admin/config/content/biblio/fields", "admin/config/content/biblio/fields"),
    ));
  }
  else {
    $msg .= ' ' . t('Checking the "Common" box will add the field to all the different publication types. Checking "Required" will force the user to supply a value for the field, checking "Autocomplete" will enable AJAX type auto complete look up for the field when the user is entering data and the weight value changes the order which it is rendered on the form with smaller values floating to the top of the form.');
  }
  $msg .= t('Finally, for each author field you can choose a set of author roles. Assigning different roles to authors within the same field, e.g. primary and secondary authors within the authors field, allows to theme them differently.');
  $msg .= '</div>';
  $form['#redirect'] = 'admin/config/content/biblio/fields';
  $form['help'] = array(
    '#type' => 'fieldset',
    '#title' => t('Help'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['help']['message'] = array(
    '#markup' => $msg,
  );

  // Get author types.
  $result = db_query("SELECT * FROM {biblio_contributor_type_data} b");
  $contrib_options = array();
  foreach ($result as $contrib_type) {
    $contrib_options[$contrib_type->auth_type] = $contrib_type->title;
    $contrib_hints[$contrib_type->auth_type] = $contrib_type->hint;
  }

  // First get all of the field info.
  if ($tid) {
    $result = db_query("SELECT bf.*, bftd.*,  bft.vtab, bft.common, bft.visible, bft.autocomplete, bft.weight, bft.required, bt.name AS type_name\n                        FROM {biblio_fields} AS bf\n                        INNER JOIN {biblio_field_type} AS bft ON bft.fid=bf.fid\n                        INNER JOIN {biblio_field_type_data} AS bftd ON bftd.ftdid=bft.ftdid\n                        INNER JOIN {biblio_types} AS bt ON bt.tid=bft.tid\n                        WHERE bft.tid=:tid", array(
      ':tid' => $tid,
    ), array(
      'fetch' => PDO::FETCH_ASSOC,
    ));
  }
  else {
    $result = db_query("SELECT bf.*, bftd.*, bft.vtab, bft.common, bft.visible, bft.autocomplete, bft.weight, bft.required\n                        FROM {biblio_fields} AS bf\n                        INNER JOIN {biblio_field_type} AS bft ON bft.fid=bf.fid\n                        INNER JOIN {biblio_field_type_data} AS bftd ON bftd.ftdid=bft.ftdid\n                        WHERE bft.tid=:tid", array(
      ':tid' => $tid,
    ), array(
      'fetch' => PDO::FETCH_ASSOC,
    ));
  }
  foreach ($result as $row) {
    $fields[$row['fid']] = $row;
  }
  $types = db_query("SELECT * from {biblio_types} WHERE visible=1 ORDER BY name ASC");
  $types_options[0] = t('Common');
  foreach ($types as $type) {
    $types_options[$type->tid] = $type->name;
  }
  $form['pub_type'] = array(
    '#title' => t('Publication type'),
    '#type' => 'select',
    '#options' => $types_options,
    '#attributes' => array(
      'onchange' => 'document.getElementById(\'edit-change-type\').click()',
    ),
    '#default_value' => $tid,
  );
  $no_js = !isset($_COOKIE['has_js']) || empty($_COOKIE['has_js']);
  $form['change_type'] = array(
    '#type' => 'submit',
    '#value' => t('Change Publication Type'),
    '#weight' => -10,
    '#prefix' => $no_js ? '' : '<div style="display:none;">',
    '#suffix' => $no_js ? '' : '</div>',
  );
  $form['biblio_tabs'] = array(
    '#type' => 'vertical_tabs',
    '#weight' => 0,
  );
  $form['biblio_tabs'] += biblio_node_form_vtabs();

  // $form['configured_flds'] = array('#tree' => 1);
  // show an existing type.
  if ($tid) {
    $typename = $fields[1]['type_name'];
    $form['type_name'] = array(
      '#title' => t('Publication type name'),
      '#type' => 'textfield',
      '#maxlength' => 64,
      '#default_value' => $typename,
      '#required' => TRUE,
    );
    $form['top_message'] = array(
      '#value' => t('Field settings related to @type publications', array(
        '@type' => $typename,
      )),
    );
    $form['type_id'] = array(
      '#type' => 'value',
      '#title' => 'tid',
      '#value' => $tid,
    );
  }
  else {
    $form['top_message'] = array(
      '#value' => t('Field settings common to all publication types'),
    );
  }

  // Resort the fields since the weight may have changed.
  uasort($fields, "biblio_form_sort");
  $vis_comm = $tid ? 'visible' : 'common';
  $options["{$vis_comm}"] = '';
  $options['required'] = '';
  if ($tid == 0) {
    $options['autocomplete'] = '';
  }
  foreach ($fields as $key => $fld) {
    if ($fld['type'] == 'contrib_widget') {
      continue;
    }
    $def_values[$fld['name']] = array();
    if ($tid) {
      if ($fld['visible']) {
        array_push($def_values[$fld['name']], 'visible');
      }
      if ($fld['required']) {
        array_push($def_values[$fld['name']], 'required');
      }
    }
    else {
      if ($fld['common']) {
        array_push($def_values[$fld['name']], 'common');
      }
      if ($fld['required']) {
        array_push($def_values[$fld['name']], 'required');
      }
      if ($fld['autocomplete']) {
        array_push($def_values[$fld['name']], 'autocomplete');
      }
    }
    $disabled = $tid ? $fld['common'] ? 1 : 0 : 0;
    $form_state['tab_defaults'][$key]['common'] = $disabled;
    $tab_data[$fld['vtab']][$key]['#parents'] = array(
      'biblio_tabs',
      $key,
    );
    $tab_data[$fld['vtab']][$key]['name'] = array(
      '#type' => 'markup',
      '#markup' => check_plain($fld['name']),
      '#weight' => $fld['weight'],
    );
    $tab_data[$fld['vtab']][$key]['title'] = array(
      '#type' => 'textfield',
      '#default_value' => $fld['title'],
      '#size' => 15,
      '#weight' => $fld['weight'],
      '#disabled' => $disabled,
    );

    // We need store the default to see if it's changed later.
    $form_state['tab_defaults'][$key]['title']['default'] = $fld['title'];
    $form_state['tab_defaults'][$key]['title']['disabled'] = $disabled;
    $tab_data[$fld['vtab']][$key]['hint'] = array(
      '#type' => 'textfield',
      '#default_value' => $fld['hint'],
      '#size' => 20,
      '#weight' => $fld['weight'],
      '#disabled' => $disabled,
    );
    $form_state['tab_defaults'][$key]['hint']['default'] = $fld['hint'];
    $form_state['tab_defaults'][$key]['hint']['disabled'] = $disabled;
    $tab_data[$fld['vtab']][$key]['weight'] = array(
      '#type' => 'textfield',
      '#default_value' => $fld['weight'],
      '#size' => 2,
      '#weight' => $fld['weight'],
    );
    $tab_data[$fld['vtab']][$key]['checkboxes'] = array(
      '#type' => 'checkboxes',
      '#options' => $options,
      '#default_value' => $def_values[$fld['name']],
      '#weight' => $fld['weight'],
    );
    $tab_data[$fld['vtab']][$key]['ftdid'] = array(
      '#type' => 'value',
      '#value' => $fld['ftdid'],
    );
  }
  if ($tid) {
    $header = array(
      '',
      t('Field Name'),
      t('Title'),
      t('Hint'),
      t('Visible'),
      t('Required'),
      '',
    );
  }
  else {
    $header = array(
      '',
      t('Field Name'),
      t('Default Title'),
      t('Hint'),
      t('Common'),
      t('Required'),
      t('Autocomplete'),
      '',
    );
  }
  foreach ($tab_data as $key => $rows) {
    $form['biblio_tabs'][$key][] = array(
      '#theme' => 'biblio_field_tab',
      '#header' => $header,
      'rows' => $rows,
    );
  }
  $form['biblio_tabs']['#tree'] = TRUE;
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  return $form;
}