<?php

/**
 * @file
 * Install logic.
 */

/**
 * Implements hook_install().
 */
function archibald_proposals_install() {
  module_load_include('inc', 'archibald', 'includes/archibald.entities');

  $entity = 'node';
  $bundle = 'archibald_lomch_description';
  $children = array();
  foreach (archibald_proposals_archibald_lomch_description_fields() as $field_name => $field_options) {
    archibald_add_field_to_entity($entity, $bundle, $field_name, $field_options);

    // Register custom permissions.
    if ($field = field_info_field($field_name)) {
      $field['field_permissions']['type'] = FIELD_PERMISSIONS_CUSTOM;
      field_update_field($field);

      // Register the field for the group below.
      $children[] = $field_name;
    }
  }

  // Create a group for the form, but also for the node default view.
  $group_name = 'group_proposal_contact';
  foreach (array(
    'form' => 'tab',
    'default' => 'fieldset',
  ) as $display => $type) {
    $group = (object) array(
      'identifier' => "{$group_name}|{$entity}|{$bundle}|{$display}",
      'group_name' => $group_name,
      'entity_type' => $entity,
      'bundle' => $bundle,
      'mode' => $display,
      'label' => "Contact information",
      'children' => $children,
      'weight' => 20,
      'format_type' => $type,
      'format_settings' => array(
        'formatter' => $type == 'tab' ? 'closed' : 'collapsed',
        'instance_settings' => array(
          'description' => "Provide contact information so the people in charge can get in touch.",
          'classes' => str_replace('_', '-', $group_name) . ' field-group-tab',
          'required_fields' => TRUE,
        ),
      ),
    );

    field_group_group_save($group);

    // With newer versions of Ctools, groups are not enabled by default.
    // Enable it.
    ctools_include('export');
    ctools_export_crud_enable('field_group', $group->identifier);
  }
}

/**
 * Implements hook_uninstall().
 */
function archibald_proposals_uninstall() {
  variable_del('archibald_proposals_set_permissions');
}

/**
 * Implements hook_update_N().
 *
 * - Correct the default field permissions.
 * - Add a group on the node page which shows the contact information.
 */
function archibald_proposals_update_7301() {
  $children = array();
  $entity = 'node';
  $bundle = 'archibald_lomch_description';

  foreach (archibald_proposals_archibald_lomch_description_fields() as $field_name => $field_options) {
    // In order for users to see the fields when editing, we need to give "edit"
    // permissions as well.
    user_role_change_permissions(DRUPAL_AUTHENTICATED_RID, array(
      "create {$field_name}" => FALSE,
      "edit {$field_name}" => TRUE,
      "view {$field_name}" => TRUE,
    ));

    // Register the field for the group below.
    $children[] = $field_name;

    // Make it visible by default.
    $instance = field_info_instance($entity, $field_name, $bundle);
    $instance['display']['default']['type'] = $field_options['type'] == 'email' ?
      'email_plain' :
      'text_default';
    field_update_instance($instance);
  }

  // Make sure the cache is cleared. If the fields are still "hidden" in the
  // cache, there is no way to enable the group below, even if we explicitly
  // call the Ctools function.
  field_cache_clear();

  $display = 'default';
  $group_name = 'group_proposal_contact';
  $group = (object) array(
    'identifier' => "{$group_name}|{$entity}|{$bundle}|{$display}",
    'group_name' => $group_name,
    'entity_type' => $entity,
    'bundle' => $bundle,
    'mode' => $display,
    'label' => "Contact information",
    'children' => $children,
    'weight' => 20,
    'format_type' => 'fieldset',
    'format_settings' => array(
      'formatter' => 'collapsed',
      'instance_settings' => array(
        'classes' => str_replace('_', '-', $group_name) . ' field-group-tab',
      ),
    ),
  );
  field_group_group_save($group);

  // With newer versions of Ctools, groups are not enabled by default.
  // Enable it.
  ctools_include('export');
  ctools_export_crud_enable('field_group', $group->identifier);

  return array();
}
