<?php
/**
 * @file
 * The Serial module main file.
 */

/**
 * Implements hook_field_info().
 */
function serial_field_info() {
  return array(
    'serial' => array(
      'label' => t('Serial'),
      'description' => t('Auto increment serial field type.'),
      // The "property_type" should be defined for accessing the
      // field by entity metadata wrapper.
      'property_type' => 'serial',
      'default_widget' => 'serial',
      'default_formatter' => 'serial_formatter_default',
    ),
  );
}

/**
 * Implements hook_field_create_instance().
 */
function serial_field_create_instance($instance) {
  $field = field_read_field($instance['field_name']);

  if ('serial' == $field['type']) {
    // Create the assistant table:
    module_load_include('inc', 'serial');
    _serial_create_table($field, $instance);

    // Set serial values for old objects.
    $old_count = _serial_init_old_nodes($instance['bundle'], $field['field_name']);

    if ($old_count > 0) {
      drupal_set_message(t('Serial values have been automatically set for %count existing nodes.', array(
        '%count' => $old_count,
      )));
    }
  }
}

/**
 * Implements hook_field_delete_instance().
 */
function serial_field_delete_instance($instance) {
  $field = field_read_field($instance['field_name']);

  if ('serial' == $field['type']) {
    // Drop the assistant table.
    module_load_include('inc', 'serial');
    _serial_drop_table($field, $instance);
  }
}

/**
 * Implements hook_form_alter().
 */
function serial_form_alter(&$form, $form_state, $form_id) {
  if ('field_ui_field_settings_form' == $form_id && 'serial' == $form['field']['type']['#value']) {
    drupal_set_message(t('Serial field %field has been created.', array(
      '%field' => $form['field']['field_name']['#value'],
    )));

    drupal_goto("admin/structure/types/manage/{$form['#bundle']}/fields");
  }
}

/**
 * Implements hook_field_presave().
 */
function serial_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  if (empty($items)) {
    module_load_include('inc', 'serial');

    $items = array(array('value' => _serial_generate_value($instance['bundle'], $field['field_name'])));
  }
}

/**
 * Implements hook_field_is_empty().
 */
function serial_field_is_empty($item, $field) {
  // Never should be treated as empty.
  return FALSE;
}

/**
 * Implements hook_node_presave().
 */
function serial_node_presave($node) {
  if (module_exists('auto_nodetitle')) {
    if (auto_nodetitle_get_setting($node->type)) {
      auto_nodetitle_set_title($node);
    }
  }
}

/**
 * Implements hook_node_type_update().
 */
function serial_node_type_update($info) {
  // Handle content type rename:
  if (isset($info->old_type) && ($info->old_type != $info->type)) {
    module_load_include('inc', 'serial');
    _serial_rename_tables($info->old_type, $info->type);
  }
}

/**
 * Implements hook_field_formatter_info().
 */
function serial_field_formatter_info() {
  return array(
    'serial_formatter_default' => array(
      'label' => t('Default'),
      'field types' => array('serial'),
    ),
  );
}

/**
 * Implements hook_field_formatter_view().
 */
function serial_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();

  // Define the field contents for the single default formatter.
  foreach ($items as $delta => $item) {
    $element[$delta] = array(
      '#markup' => theme('serial_formatter_default', array(
        'serial_id' => $item['value'],
      )),
    );
  }

  return $element;
}

/**
 * Implements hook_theme().
 */
function serial_theme() {
  // Register the theme for the default formatter.
  return array(
    'serial_formatter_default' => array(
      'variables' => array(
        'serial_id' => NULL,
      ),
    ),
  );
}

/**
 * Theme function for the default formatter.
 */
function theme_serial_formatter_default($variables) {
  return $variables['serial_id'];
}

/**
 * Implements hook_field_widget_info().
 */
function serial_field_widget_info() {
  return array(
    'serial' => array(
      'label' => t('Hidden (Automatic)'),
      'field types' => array('serial'),
    ),
  );
}

/**
 * Implements hook_field_widget().
 */
function serial_field_widget(&$form, &$form_state, $field, $instance, $items, $delta = 0) {
  return array(
    'value' => array(
      '#type' => 'hidden',
      '#default_value' => $items[$delta]['value'],
    ),
  );
}
