<?php

require_once "includes/tripal_ds.inc";
require_once "includes/tripal_ds.ds.inc";
require_once "includes/tripal_ds.field_group.inc";
require_once "includes/tripal_ds.field_formatter.inc";

function tripal_ds_init() {
  drupal_add_css(drupal_get_path('module', 'tripal_ds') . '/theme/css/tripaldsfeature.css');
  drupal_add_js(drupal_get_path('module', 'tripal_ds') . '/theme/js/tripal_ds.js');

  $theme_dir = url(drupal_get_path('module', 'tripal_ds') . '/theme');
  drupal_add_js("var ds_theme_dir  = '$theme_dir';", 'inline', 'header');

}

/**
 * Implements hook_views_api().
 */
function tripal_ds_views_api() {
  return array(
     'api' => 3,
     'path' => drupal_get_path('module', 'tripal_ds') . '/includes/views',
  );
}
/**
 * Implements hook_menu().
 * Defines all menu items needed by Tripal DS
 *
 */
function tripal_ds_menu() {
  $items = array();
  // Adds a +Apply Tripal Display Suite option to 'Tripal Content Types' page.
  $items['admin/structure/bio_data/manage/%/display/apply'] = array(
    'title' => 'Apply Default Tripal Layout (will reset current layout)',
    'description' => t('Apply the Tripal Display Suite settings to this content type.'),
    'page callback' => 'drupal_get_form',
    'access arguments' => array('administer tripal'),
    'page arguments' => array('tripal_ds_update_layout_form', 4),
    'type' => MENU_LOCAL_ACTION,
  );
  return $items;
}
/**
 * Implements hook_bundle_postcreate().
 *
 * This is a Triapl defined hook and is called in the TripalBundle::create()
 * function to allow modules to perform tasks when a bundle is created.
 */
function tripal_ds_bundle_postcreate($bundle) {
  $bundle_name = $bundle->name;
  $bundle_data_table = $bundle->data_table;
  if($bundle_data_table == 'pub'){
    $instances = field_info_instances('TripalEntity', $bundle_name);
    _ds_layout_pub_settings_info($bundle_name, $instances);
  }
  else {
    $instances = field_info_instances('TripalEntity', $bundle_name);
    _ds_layout_settings_info($bundle_name, $instances);
  }

}

function tripal_ds_table_column_delete($bundle){
    $bundle_name = $bundle->name;
    db_delete('tripal_ds')
      ->condition('bundle', $bundle_name, '=')
      ->execute();
}

function tripal_ds_bundle_delete($bundle){
  tripal_ds_table_column_delete($bundle);
}
/*
 * Implements hook_ds_layout_info() to define layouts from code in a module for
 * display suite
 */
function tripal_ds_ds_layout_info() {
  $path = drupal_get_path('module', 'tripal_ds');

  $layouts = array(
    'tripal_ds_feature' => array(
      'label' => t('Tripal Feature Layout'),
      'path' => $path . '/theme/templates',
      'regions' => array(
        'left' => t('Left'),
        'right' => t('Right'),
      ),
      'css' => TRUE,
    ),
  );

  return $layouts;
}

function tripal_ds_update_layout_form($form, &$form_state, $bundle) {
  $form = array();
  $form['bundle_name'] = array(
    '#type' => 'value',
    '#value' => $bundle,
  );

  return confirm_form($form,
    t('Please confirm you would like to update this layout: '.$bundle),
    'admin/structure/bio_data/manage/'.$bundle.'/display',
    t('This action cannot be undone.'),
    t('Yes, apply layout'),
    t('No, cancel')
  );
}

/**
 *
 * @param $bundle_name
 */
function tripal_ds_update_layout_form_submit($form, &$form_state) {
  $bundle_name = $form_state['build_info']['args'][0];
  //Build the identifier to check against ds_layout_settings.
  $ds_identifier = 'TripalEntity|'.$bundle_name.'|default';

  //Check to see if the layout already exists.
  $result = db_select('ds_layout_settings', 'ds')
    ->fields('ds')
    ->condition('id', $ds_identifier, '=')
    ->execute()
    ->fetchField();
  //Check to see if there are any field groups associated with the bundle.
  $result_fg = db_select('field_group', 'fg')
    ->fields('fg')
    ->condition('bundle', $bundle_name, '=')
    ->execute()
    ->fetchField();
  //Check to see if there are any tripal ds fields associated with the bundle.
  $result_tds = db_select('tripal_ds', 'tds')
    ->fields('tds')
    ->condition('bundle', $bundle_name, '=')
    ->execute();

  //Check to see if there are any field settings associated with the bundle.
  $result_fs = db_select('ds_field_settings', 'fs')
    ->fields('fs')
    ->condition('bundle', $bundle_name, '=')
    ->execute();

  //If the layout exists, delete it.
  if(!empty($result)) {
    db_delete('ds_layout_settings')
      ->condition('id', $ds_identifier, '=')
      ->execute();
  }
  //Then delete the field_group_fields associated with the identifier.
  if(!empty($result_fg)) {
    db_delete('field_group')
      ->condition('bundle', $bundle_name, '=')
      ->execute();
  }
  //Then delete the ds_field_settings associated with the identifier.
  if(!empty($result_tds)) {
    db_delete('ds_field_settings')
      ->condition('bundle', $bundle_name, '=')
      ->execute();
  }
  //Then delete the tripal_ds menu item.
  if(!empty($result_fs)) {
    db_delete('tripal_ds')
      ->condition('bundle', $bundle_name, '=')
      ->execute();
  }

  //Now you can build the layout fresh.
  $instances = field_info_instances('TripalEntity', $bundle_name);
  $success = _ds_layout_settings_info($bundle_name, $instances);
  if ($success) {
    drupal_set_message("Layout applied successfully and saved.");
  }
  else {
    drupal_set_message("Could not apply layout.", 'error');
  }

  drupal_goto("admin/structure/bio_data/manage/$bundle_name/display");
}


/*
 * Code for the view of the menu items

    //get tripal entity id from url then run it against tripal entity db
    //and grab the bundle id, then pass bundle id to view
    $url = current_path();
    $url_exploded = explode("/", $url);
    $tripal_entity_id = (int)$url_exploded[1];

    $result = db_select('tripal_entity', 'te')
      ->fields('te', array('bundle'))
      ->condition('id', $tripal_entity_id, '=')
      ->execute()
      ->fetchField();
*/