<?php

/**
 * @file
 * Create complex, repeating groups of CCK fields that work in unison.
 */

function content_multigroup_help($path, $arg) {
  switch ($path) {
    case 'admin/help#content_multigroup':
      return t('The fields in a Standard group are independent of each other and each can have either single or multiple values. The fields in a Multigroup are treated as a repeating collection of single value fields.');
  }
}

/**
 * Implementation of hook_views_api().
 */
function content_multigroup_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'content_multigroup') . '/views',
  );
}

/**
 * Implementation of hook_ctools_plugin_directory().
 */
function content_multigroup_ctools_plugin_directory($module, $plugin) {
  if ($module == 'ctools' && $plugin == 'content_types') {
    return 'panels/' . $plugin;
  }
}

/**
 * Implementation of hook_menu().
 */
function content_multigroup_menu() {
  $items = array();
  // Callback for AHAH add more buttons.
  $items['content_multigroup/js_add_more'] = array(
    'page callback' => 'content_multigroup_add_more_js',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
    'file' => 'content_multigroup.node_form.inc',
  );
  return $items;
}

/**
 * Implementation of hook_theme().
 */
function content_multigroup_theme() {
  return array(
    'content_multigroup_node_form' => array(
      'arguments' => array('element' => NULL),
      'file' => 'content_multigroup.node_form.inc',
    ),
    'content_multigroup_node_label' => array(
      'arguments' => array('text' => NULL),
      'file' => 'content_multigroup.node_form.inc',
    ),
    'content_multigroup_add_more_label' => array(
      'arguments' => array('group_name' => NULL),
      'file' => 'content_multigroup.node_form.inc',
    ),
    'content_multigroup_display_simple' => array(
      'arguments' => array('element' => NULL),
      'file' => 'content_multigroup.node_view.inc',
    ),
    'content_multigroup_display_fieldset' => array(
      'arguments' => array('element' => NULL),
      'file' => 'content_multigroup.node_view.inc',
    ),
    'content_multigroup_display_hr' => array(
      'arguments' => array('element' => NULL),
      'file' => 'content_multigroup.node_view.inc',
    ),
    'content_multigroup_display_table_single' => array(
      'arguments' => array('element' => NULL),
      'file' => 'content_multigroup.node_view.inc',
    ),
    'content_multigroup_display_table_multiple' => array(
      'arguments' => array('element' => NULL),
      'file' => 'content_multigroup.node_view.inc',
    ),
    'content_multigroup_display_ul' => array(
      'arguments' => array('element' => NULL),
      'file' => 'content_multigroup.node_view.inc',
    ),
  );
}

/**
 * Implementation of hook_elements().
 */
function content_multigroup_elements() {
  return array(
    'content_multigroup_display_fieldset' => array('#value' => NULL),
  );
}

/**
 * Implementation of hook_fieldgroup_types().
 */
function content_multigroup_fieldgroup_types() {
  return array('multigroup' => t('Multigroup'));
}

/**
 * Implementation of hook_fieldgroup_default_settings().
 */
function content_multigroup_fieldgroup_default_settings($group_type) {
  if ($group_type == 'multigroup') {
    module_load_include('inc', 'content', 'includes/content.admin');
    $settings = array('multigroup' => array('multiple' => 1));
    foreach (array_keys(content_build_modes()) as $key) {
      $settings['display'][$key]['format'] = 'fieldset';
    }
    return $settings;
  }
}

/**
 * Implementation of hook_form_alter().
 */
function content_multigroup_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'content_field_edit_form' && isset($form['widget'])) {
    // If this is a field edit form and the field is in a Multigroup,
    // override the multiple value settings.
    $content_type = content_types($form['type_name']['#value']);
    $groups = fieldgroup_groups($content_type['type']);
    $group_name = _fieldgroup_field_get_group($content_type['type'], $form['field_name']['#value']);
    $group = isset($groups[$group_name]) ? $groups[$group_name] : array();
    if (!empty($group) && $group['group_type'] == 'multigroup') {
      $form['field']['multiple']['#value'] = $group['settings']['multigroup']['multiple'];
      $form['field']['multiple']['#access'] = FALSE;
    }
  }
  elseif ($form_id == 'content_field_edit_form' && isset($form_state['change_basic'])) {
    // This is the basic field settings form. It should not be possible to
    // choose a widget type that is not compatible with multigroups.
    module_load_include('inc', 'content_multigroup', 'content_multigroup.admin');
    content_multigroup_field_basic_form($form, $form_state);
  }
  elseif ($form_id == 'content_field_overview_form') {
    // Validation for creating/moving fields and groups on the
    // Manage Fields screen.
    module_load_include('inc', 'content_multigroup', 'content_multigroup.admin');
    $form['#validate'][] = 'content_multigroup_field_overview_form_validate';
  }
  elseif ($form_id == 'content_display_overview_form' && !empty($form['#groups'])) {
    // Add an additional selector for setting multigroup field display
    // format to the Display Fields screen.
    module_load_include('inc', 'content_multigroup', 'content_multigroup.admin');
    content_multigroup_display_overview_form($form, $form_state);
  }
  elseif ($form_id == 'fieldgroup_group_edit_form') {
    // Alter the Fieldgroup edit form to add Multigroup settings.
    module_load_include('inc', 'content_multigroup', 'content_multigroup.admin');
    content_multigroup_group_edit_form($form, $form_state);
  }
}

/**
 * After build callback for multigroups in node form.
 *
 * This proxy function is necessary to prevent from breaking AHAH handlers.
 */
function content_multigroup_node_form_after_build($form, &$form_state) {
  module_load_include('inc', 'content_multigroup', 'content_multigroup.node_form');
  return _content_multigroup_node_form_after_build($form, $form_state);
}

/**
 * Implementation of hook_fieldgroup_form().
 */
function content_multigroup_fieldgroup_form(&$form, &$form_state, $form_id, $group) {
  $group_name = $group['group_name'];
  if ($group['group_type'] == 'multigroup' && !empty($form[$group_name])) {
    if (!isset($form[$group_name]['#access']) || $form[$group_name]['#access']) {
      module_load_include('inc', 'content_multigroup', 'content_multigroup.node_form');
      _content_multigroup_fieldgroup_form($form, $form_state, $form_id, $group);
    }
  }
}

/**
 * Implementation of hook_fieldgroup_view().
 */
function content_multigroup_fieldgroup_view(&$node, &$element, $group, $context) {
  if ($group['group_type'] == 'multigroup') {
    module_load_include('inc', 'content_multigroup', 'content_multigroup.node_view');
    _content_multigroup_fieldgroup_view($node, $element, $group, $context);
  }
}
