You are here

function biblio_admin_types_edit_form_submit in Bibliography Module 6.2

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

File

includes/biblio.admin.inc, line 924
Administrative files for the biblio module.

Code

function biblio_admin_types_edit_form_submit($form, &$form_state) {
  $tid = $form_state['values']['type_id'];
  if (empty($tid)) {
    $tid = 0;
  }
  if ($tid) {

    // save publication type name
    $type_name_record = array(
      'tid' => $tid,
      'name' => $form_state['values']['type_name'],
    );
    drupal_write_record('biblio_types', $type_name_record, 'tid');
    biblio_locale_refresh_types($tid);
  }

  // save author types
  $link['biblio_type'] = $tid;
  db_query('DELETE FROM {biblio_contributor_type} WHERE biblio_type=%d', $link['biblio_type']);
  for ($i = 1; $i <= 5; $i++) {
    $link['auth_category'] = $i;
    $auth_types = (array) $form_state['values']['configured_flds'][$i]['auth_type'];

    // if default values have not changed, do not store to the database explicitly
    if ($link['biblio_type'] && array_values($auth_types) == array_values((array) $form['configured_flds'][$i]['auth_type']['#default_value'])) {
      continue;
    }
    foreach ($auth_types as $type) {
      $link['auth_type'] = $type;
      drupal_write_record('biblio_contributor_type', $link);
    }
  }
  variable_set('biblio_hide_other_fields', $form_state['values']['hide_other_fields']);

  // save other field data
  foreach ($form_state['values']['configured_flds'] as $fid => $values) {
    $update = FALSE;
    $val = array();
    $link = array();
    $link['fid'] = $fid;
    $link['tid'] = $tid;
    foreach ($values as $name => $value) {
      if (!$form['configured_flds'][$fid][$name]['#disabled']) {
        $default_value = $form['configured_flds'][$fid][$name]['#default_value'];
        if ($name == 'checkboxes') {
          $link['visible'] = !empty($value['visible']) ? 1 : 0;
          $link['required'] = !empty($value['required']) ? 1 : 0;
          if ($tid == 0) {
            $link['common'] = !empty($value['common']) ? 1 : 0;
            $link['autocomplete'] = !empty($value['autocomplete']) ? 1 : 0;
          }
        }
        elseif ($name == weight) {
          $link['weight'] = $value;
        }
        else {
          $val[$name] = $value;
          if (!$update) {
            $update = $default_value != $val[$name] ? $form['configured_flds'][$fid]['ftdid']['#value'] : FALSE;
          }
        }
      }
    }
    if ($update == $fid && $link['tid']) {

      // we just changed a default value, so create a new entry in biblio_field_type_data
      $new_ftdid = variable_get('biblio_last_ftdid', 100);
      variable_set('biblio_last_ftdid', $new_ftdid + 1);
      $val['ftdid'] = $new_ftdid;
      $link['ftdid'] = $new_ftdid;
      $link['cust_tdid'] = $new_ftdid;
      drupal_write_record('biblio_field_type_data', $val);
    }
    elseif ($update >= 100 && $link['tid']) {

      // we are updating an existing entry
      $val['ftdid'] = $form['configured_flds'][$fid]['ftdid']['#value'];
      drupal_write_record('biblio_field_type_data', $val, 'ftdid');
    }
    elseif ($update == $fid) {

      // changing the defaults
      $val['ftdid'] = $fid;
      drupal_write_record('biblio_field_type_data', $val, 'ftdid');
    }
    drupal_write_record('biblio_field_type', $link, array(
      'fid',
      'tid',
    ));
    if ($tid == 0) {
      if ($link['common']) {
        $query = "UPDATE {biblio_field_type} SET ftdid = %d, common = %d, visible = %d WHERE fid = %d";
        db_query($query, array(
          $fid,
          1,
          1,
          $fid,
        ));
      }
      else {

        // not common, so change pointer back to customizations if available
        $query = "UPDATE {biblio_field_type} SET ftdid = cust_tdid, common = %d WHERE fid = %d";
        db_query($query, array(
          0,
          $fid,
        ));
      }

      // set the autocomplete bit on this field for all the types
      $query = "UPDATE {biblio_field_type} SET autocomplete = %d WHERE fid = %d";
      db_query($query, array(
        $link['autocomplete'],
        $fid,
      ));
    }
  }
  drupal_set_message(t("The changes have been saved."));

  // Clear the cached pages and menus:
  menu_rebuild();

  // Refresh translatable field strings.
  biblio_locale_refresh_fields($tid);
}