You are here

function biblio_admin_types_edit_form_submit in Bibliography Module 7

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

Form handler for biblio_admin_types_edit_form.

File

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

Code

function biblio_admin_types_edit_form_submit($form, &$form_state) {
  if ($form_state['triggering_element']['#value'] == t('Change Publication Type')) {
    $form_state['rebuild'] = TRUE;
    $form_state['pub_type'] = $form_state['values']['pub_type'];
    $form_state['input'] = array();
    return;
  }
  $tid = isset($form_state['values']['type_id']) ? $form_state['values']['type_id'] : 0;
  if ($tid) {

    // Save publication type name.
    $type_name_record = array(
      'tid' => $tid,
      'name' => $form_state['values']['type_name'],
    );
    db_update('biblio_types')
      ->fields($type_name_record)
      ->condition('tid', $tid)
      ->execute();
    biblio_locale_refresh_types($tid);
  }
  $hide_other_fields = isset($form_state['values']['hide_other_fields']) ? $form_state['values']['hide_other_fields'] : 0;
  variable_set('biblio_hide_other_fields', $hide_other_fields);

  // Save other field data.
  foreach ($form_state['values']['biblio_tabs'] as $fid => $values) {
    $update = FALSE;
    $val = array();
    $link = array();
    if (is_int($fid)) {
      $common = $form_state['tab_defaults'][$fid]['common'];
      $link['fid'] = $fid;
      $link['tid'] = $tid;
      foreach ($values as $name => $value) {
        $disabled = !empty($form_state['tab_defaults'][$fid][$name]['disabled']) ? TRUE : FALSE;
        if (!$disabled) {
          $default_value = isset($form_state['tab_defaults'][$fid][$name]['default']) ? $form_state['tab_defaults'][$fid][$name]['default'] : '';
          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] = $common ? $default_value : $value;
            if (!$update) {
              $update = $default_value != $val[$name] ? $form_state['values']['biblio_tabs'][$fid]['ftdid'] : FALSE;
            }
          }
        }
      }

      // We just changed a default value, so create a new entry in biblio_field_type_data.
      if ($update == $fid && $link['tid']) {
        $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']) {
        $val['ftdid'] = $form_state['values']['biblio_tabs'][$fid]['ftdid'];
        drupal_write_record('biblio_field_type_data', $val, 'ftdid');
      }
      elseif ($update == $fid) {
        $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));.
          db_update('biblio_field_type')
            ->fields(array(
            'ftdid' => $fid,
            'common' => 1,
            'visible' => 1,
          ))
            ->condition('fid', $fid)
            ->execute();
        }
        else {
          $query = "UPDATE {biblio_field_type} SET ftdid = cust_tdid, common = :comm WHERE fid = :fid";
          db_query($query, array(
            ':comm' => 0,
            ':fid' => $fid,
          ));

          // db_update('biblio_field_type')
          //          ->fields(array('ftdid' => $fid, 'common' => 0, 'visible' => 1))
          //          ->condition('fid', $fid)
          //          ->execute();
        }

        // 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));.
        db_update('biblio_field_type')
          ->fields(array(
          'autocomplete' => $link['autocomplete'],
        ))
          ->condition('fid', $fid)
          ->execute();
      }
    }
  }
  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);
}