<?php

/**
 * @file
 * Implements three hooks. This allows for form elements provided by the taxonomy
 * module to be rearranged individually in node add/edit forms.
 */

/**
 * Implementation of hook_menu().
 */
function node_form_rearrange_menu() {
  $items = array();
  $items['admin/settings/node-form-rearrange'] = array(
    'title' => 'Node Form Rearrange settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('node_form_rearrange_settings'),
    'access arguments' => array('administer content types'),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implementation of hook_settings().
 */
function node_form_rearrange_settings() {
  $form = array();
  $types = node_get_types('names');
  $form['node_form_rearrange_content_types'] = array(
    '#type' => 'checkboxes',
    '#description' => 'Check the content types for which you want to use the Node Form Rearrange functions',
    '#title' => t('Content types'),
    '#options' => $types,
    '#default_value' => variable_get('node_form_rearrange_content_types', array(0)),
  );
  return system_settings_form($form);
}


/**
 * Implementation of hook_form_alter().
 */

function node_form_rearrange_form_alter(&$form, $form_state, $form_id) {
  if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) {
    $types = variable_get('node_form_rearrange_content_types', array(0));
    if (isset($types[$form['type']['#value']]) && ($types[$form['type']['#value']] != '0')) {
      //retrieve all vocabularies for this node's content type
      $vocabularies = db_query(db_rewrite_sql("SELECT v.* FROM {vocabulary} v INNER JOIN {vocabulary_node_types} n ON v.vid = n.vid WHERE n.type = '%s'
  ORDER BY v.weight, v.name", 'v', 'vid'), $form['type']['#value']);
      //loop through vocabularies, moving their form element out of the taxonomy fieldset
      while ($vocabulary = db_fetch_object($vocabularies)) {
      //check to see if non-tag vocabularies exist
      if (isset($form['taxonomy'][$vocabulary->vid])) {
          $field = $form['taxonomy'][$vocabulary->vid];
          unset($form['taxonomy'][$vocabulary->vid]);
          $form["taxonomy_field_$vocabulary->vid"] = $field;
        }
        //check to see if tag vocabularies exist
        if (isset($form['taxonomy']['tags'][$vocabulary->vid])) {
          $field = $form['taxonomy']['tags'][$vocabulary->vid];
          unset($form['taxonomy']['tags'][$vocabulary->vid]);
          $form["taxonomy_field_tags_$vocabulary->vid"] = $field;
        }
        // Direct copy from content.module, for setting the weight of fields based on arrangement in admin/content/node-type/[content type]/fields
        $type = content_types($form['type']['#value']);
        foreach ($type['extra'] as $key => $value) {
          if (isset($form[$key])) {
            $form[$key]['#weight'] = $value['weight'];
          }
        }
      }
      //Removing now empty taxonomy fieldset
      if (isset($form['taxonomy'])) {
        unset($form['taxonomy']);
      }
    }
  }
}

/**
 * Implementation of hook_nodeapi().
 */

function node_form_rearrange_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'presave':
      $types = variable_get('node_form_rearrange_content_types', array(0));
      if (isset($types[$node->type]) && ($types[$node->type] != '0')) {
        $vocabularies = db_query(db_rewrite_sql("SELECT v.* FROM {vocabulary} v INNER JOIN {vocabulary_node_types} n ON v.vid = n.vid WHERE n.type = '%s'
  ORDER BY v.weight, v.name", 'v', 'vid'), $node->type);
        //loop through vocabularies, resetting the original data arrangement (fields in taxonomy fieldset) so the taxonomy is saved
        while ($vocabulary = db_fetch_object($vocabularies)) {
          $fieldname = 'taxonomy_field_'. $vocabulary->vid;
          if (isset($node->$fieldname) && $node->$fieldname) {
            $node->taxonomy[$vocabulary->vid] = $node->$fieldname;
            unset($node->$fieldname);
          }
          $fieldname = 'taxonomy_field_tags_'. $vocabulary->vid;
          if (isset($node->$fieldname) && $node->$fieldname) {
            $node->taxonomy['tags'][$vocabulary->vid] = $node->$fieldname;
            unset($node->$fieldname);
          }
        }
      }
      break;
  }
}

/**
 * Implementation of hook_content_extra_fields().
 */

function node_form_rearrange_content_extra_fields($type_name) {
  $types = variable_get('node_form_rearrange_content_types', array(0));
  $extra = array();
  if (isset($types[$type_name]) && ($types[$type_name] != '0')) {
    $vocabularies = db_query(db_rewrite_sql("SELECT v.* FROM {vocabulary} v INNER JOIN {vocabulary_node_types} n ON v.vid = n.vid WHERE n.type = '%s'
  ORDER BY v.weight, v.name", 'v', 'vid'), $type_name);
    //add rows for each vocabulary
    while ($vocabulary = db_fetch_object($vocabularies)) {
      if ($vocabulary->tags) {
        $extra["taxonomy_field_tags_$vocabulary->vid"] = array(
        'label' => $vocabulary->name,
        'description' => t('Individual field for %vocabularyname vocabulary, disables Taxonomy group ', array('%vocabularyname' => $vocabulary->name)),
        'weight' => 0);
      }
      else {
        $extra["taxonomy_field_$vocabulary->vid"] = array(
          'label' => $vocabulary->name,
          'description' => t('Individual field for %vocabularyname vocabulary, disables Taxonomy group', array('%vocabularyname' => $vocabulary->name)),
          'weight' => 0);
      }
    }
  }
  return $extra;
}
