<?php

/**
 * @file
 * Fieldgroup module install file.
 */

/**
 * Implements hook_schema().
 */
function field_group_schema() {
  $schema['field_group'] = array(
    'description' => t('Table that contains field group entries and settings.'),

    // CTools export definitions.
    'export' => array(
      'key' => 'identifier',
      'identifier' => 'field_group',
      'default hook' => 'field_group_info',
      'load callback' => 'field_group_group_load_by_identifier',
      'save callback' => 'field_group_group_save',
      'delete callback' => 'field_group_group_export_delete',
      'can disable' => TRUE,
      'api' => array(
        'owner' => 'field_group',
        'api' => 'field_group',
        'minimum_version' => 1,
        'current_version' => 1,
      ),
    ),

    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'The primary identifier for a group',
        'no export' => TRUE,
      ),
      'identifier' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The unique string identifier for a group.',
      ),
      'group_name' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The name of this group.',
      ),
      'entity_type' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'bundle' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => ''
        ),
      'mode' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => ''
      ),
      // @todo 'parent_name' is redundant with the data in the 'children'
      // entry, brings a risk of inconsistent data. This should be removed from
      // the schema and pre-computed it if needed in field_group_get_groups().
      'parent_name' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The parent name for a group',
      ),
      'data' => array(
        'type' => 'blob',
        'size' => 'big',
        'not null' => TRUE,
        'serialize' => TRUE,
        'description' => 'Serialized data containing the group properties that do not warrant a dedicated column.',
      ),
    ),
    'primary key' => array('id'),
    'indexes' => array(
      'group_name' => array('group_name'),
    ),
    'unique keys' => array(
      'identifier' => array('identifier'),
    ),
  );
  return $schema;
}

/**
 * Utility function: fetch all the field_group definitions from the database.
 */
function _field_group_install_read_groups() {
  $groups = array();
  if (db_table_exists('content_group')) {
    $query = db_select('content_group', 'cg', array('fetch' => PDO::FETCH_ASSOC))
      ->fields('cg')
      // We only want non-multigroups.
      ->condition('group_type', 'standard');
    foreach ($query->execute() as $record) {
      $record['settings'] = unserialize($record['settings']);
      $groups[$record['group_name'] . '-' . $record['type_name']] = $record;
    }
    foreach ($groups as $key => $group) {
      $query2 = db_select('content_group_fields', 'cgf', array('fetch' => PDO::FETCH_ASSOC))
        ->fields('cgf')
        ->condition('group_name', $group['group_name']);
      foreach ($query2->execute() as $field) {
        $groups[$field['group_name'] . '-' . $field['type_name']]['children'][] = $field;
      }
    }
  }
  return $groups;
}

/**
 * Implements of hook_install().
 *
 * Because this is a new module in D7, hook_update_N() doesn't help D6
 * users who upgrade to run the migration path. So, we try that here as
 * the module is being installed.
 */
function field_group_install() {

  $groups = _field_group_install_read_groups();
  module_load_include('module', 'field_group');

  if (!empty($groups)) {

    module_load_include('module', 'ctools');
    ctools_include('export');

    foreach ($groups as $group) {

      $group = (object) $group;

      $new = new stdClass();
      $new->group_name = $group->group_name;
      $new->entity_type = 'node';
      $new->bundle = $group->type_name;
      $new->label = $group->label;
      $new->parent_name = '';
      $new->children = array();
      foreach ($group->children as $child) {
        $new->children[] = $child['field_name'];
      }

      // The form.
      $new->id = NULL;
      $new->weight = $group->weight;
      $new->mode = 'form';
      $new->format_type = 'fieldset';
      $new->format_settings = array(
        'formatter' => preg_match("/fieldset/", $group->settings['form']['style']) ? 'collapsible' : 'collapsed',
        'instance_settings' => array(),
      );
      $new->identifier = $new->group_name . '|' . $new->entity_type . '|' . $new->bundle . '|' . $new->mode;
      ctools_export_crud_save('field_group', $new);

      // The full node.
      $new->id = NULL;
      $new->weight = $group->weight;
      $new->mode = 'default';
      $new->format_type = $group->settings['display']['full']['format'];
      $new->format_settings = array(
        'formatter' => 'collapsible',
        'instance_settings' => array(),
      );
      $new->identifier = $new->group_name . '|' . $new->entity_type . '|' . $new->bundle . '|' . $new->mode;
      ctools_export_crud_save('field_group', $new);

      // The teaser node.
      $new->id = NULL;
      $new->weight = $group->weight;
      $new->mode = 'teaser';
      $new->format_type = $group->settings['display']['teaser']['format'];
      $new->format_settings = array(
        'formatter' => 'collapsible',
        'instance_settings' => array(),
      );
      $new->identifier = $new->group_name . '|' . $new->entity_type . '|' . $new->bundle . '|' . $new->mode;
      ctools_export_crud_save('field_group', $new);

    }

  }

  // Set weight to 1.
  db_update('system')
    ->fields(array('weight' => 1))
    ->condition('name', 'field_group')
    ->execute();

}

/**
 * Update hook on the field_group table to add an unique identifier.
 */
function field_group_update_7001() {

  if (!db_field_exists('field_group', 'identifier')) {
    // Add the new string identifier field for ctools.
    db_add_field('field_group', 'identifier', array(
      'type' => 'varchar',
      'length' => 255,
      'not null' => TRUE,
      'default' => '',
      'description' => 'The unique string identifier for a group.',
    ));
    // Force drupal's schema to be rebuilt
    drupal_get_schema('field_group', TRUE);

    module_load_include('module', 'field_group');
    _field_group_recreate_identifiers();

  }

  db_update('system')
    ->fields(array('weight' => 1))
    ->condition('name', 'field_group')
    ->execute();

  // Clear drupal and static cache.
  field_group_info_groups(NULL, NULL, NULL, TRUE);
}

/**
 * Update hook to clear cache for new changes to take effect.
 */
function field_group_update_7002() {

  module_load_include('module', 'field_group');

  // This hook is called to satify people with older version of field_group.
  // This will recreate all identifiers for the field_groups known in database.
  // At the moment, we only trigger field_groups that are stored in the database, where
  // we should maybe get all field_groups as ctools has registered them.
  // See http://drupal.org/node/1169146.
  // See http://drupal.org/node/1018550.
  _field_group_recreate_identifiers();

  // Clear drupal and static cache.
  field_group_info_groups(NULL, NULL, NULL, TRUE);
}

/**
 * Update hook to recreate identifiers.
 * @see function field_group_update_7002.
 */
function field_group_update_7003() {

  module_load_include('module', 'field_group');
  _field_group_recreate_identifiers();

  // Clear drupal and static cache.
  field_group_info_groups(NULL, NULL, NULL, TRUE);
}

/**
 * Update hook to make sure identifier is set as unique key.
 */
function field_group_update_7004() {
  db_drop_unique_key('field_group', 'identifier');
  db_add_unique_key('field_group', 'identifier', array('identifier'));
}

/**
 * Checks all existing groups and removes optional HTML classes
 * while adding them as extra classes.
 */
function field_group_update_7005() {

  // Migrate the field groups so they have a unique identifier.
  $result = db_select('field_group', 'fg')
    ->fields('fg')
    ->execute();
  $rows = array();
  foreach($result as $row) {
    //$row->identifier = $row->group_name . '|' . $row->entity_type . '|' . $row->bundle . '|' . $row->mode;
    $row->data = unserialize($row->data);
    $classes = explode(" ", $row->data['format_settings']['instance_settings']['classes']);
    $optional_classes = array(str_replace("_", "-", $row->group_name), 'field-group-' . $row->data['format_type']);
    foreach ($optional_classes as $optional_class) {
      if (!in_array($optional_class, $classes)) {
        $classes[] = $optional_class;
      }
    }
    $row->data['format_settings']['instance_settings']['classes'] = implode(" ", $classes);
    $rows[] = $row;
  }
  foreach ($rows as $row) {
    drupal_write_record('field_group', $row, array('id'));
  }

  // Clear drupal and static cache.
  field_group_info_groups(NULL, NULL, NULL, TRUE);
}

/**
 * Save all optional HTML classes for fieldgroups located in features.
 * If you need the optional classes, recreate feature after this update.
 * If not, you can revert it.
 */
function field_group_update_7006() {
  ctools_include("export");
  // Migrate the field groups so they have a unique identifier.
  $field_groups = ctools_export_load_object("field_group");
  foreach ($field_groups as $row) {

    // Only update feature field_groups this time.
    // Don't touch the fieldgroups in db.

    if ($row->export_type == EXPORT_IN_CODE) {
      $classes = array();

      if (isset($row->data['format_settings'], $row->data['format_settings']['instance_settings'], $row->data['format_settings']['instance_settings']['classes'])) {
        $classes = explode(" ", $row->data['format_settings']['instance_settings']['classes']);
      }

      $optional_classes = array(str_replace("_", "-", $row->group_name), 'field-group-' . $row->data['format_type']);
      foreach ($optional_classes as $optional_class) {
        if (!in_array($optional_class, $classes)) {
          $classes[] = $optional_class;
        }
      }
      $row->data['format_settings']['instance_settings']['classes'] = implode(" ", $classes);
      unset($row->id);
      drupal_write_record('field_group', $row);
    }
  }

  // Clear drupal and static cache.
  field_group_info_groups(NULL, NULL, NULL, TRUE);
}

/**
 * Id attributes are now a setting. This update will insert the old id in the setting so old
 * markup doesn't get broken. If you don't want an attribute, you can delete
 * them on the fieldgroup settings.
 */
function field_group_update_7007() {

  ctools_include("export");

  // Migrate the field groups so they have a unique identifier.
  $field_groups = ctools_export_load_object("field_group");
  foreach ($field_groups as $row) {
    if ($row->data['format_type'] == 'div' || $row->data['format_type'] == 'html5' || $row->data['format_type'] == 'html-element') {

      // If mode is default, we don't know what view mode it was. Take full then.
      $view_mode = $row->mode == 'default' ? 'full' : $row->mode;
      $id = $row->entity_type . '_' . $row->bundle . '_' . $view_mode . '_' . $row->group_name;
      $row->data['format_settings']['instance_settings']['id'] = $id;
    }

    if ($row->export_type == EXPORT_IN_CODE) {
      unset($row->id);
      drupal_write_record('field_group', $row);
    }
    else {
      drupal_write_record('field_group', $row, array('id'));
    }
  }

  // Clear drupal and static cache.
  field_group_info_groups(NULL, NULL, NULL, TRUE);
}

/**
 * Clear cache to notice the CTools load callback.
 */
function field_group_update_7008() {
  drupal_flush_all_caches();
}