<?php

/**
 * Implements hook_schema().
 */
function tripal_fields_layout_schema() {
  $schema = array();

  $schema['tripal_panels'] = array(
    'description' => 'The list of panels into which fields can be placed.',
    'fields' => array(
      'panel_id' => array(
        'description' => 'The primary identifier for this table.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'bundle_id' => array(
        'description' => 'A bundle ID from the tripal_bundle table.',
        'type' => 'int',
        'not null' => TRUE,
      ),
      'name' => array(
        'description' => 'The computer readable name for the panel. This name must only have alphanumerical characters and underscores and must not begin with a number. ',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
      ),
      'label' => array(
        'description' => 'A human readable name for panel. This name will be shown to users in the sidebar menu.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
      ),
      'settings' => array(
        'description' => 'Contains a serialized array tripal_fields_layoutof settings for the panel.',
        'type' => 'text',
        'not null' => FALSE,
      ),
      'weight' => array(
        'type' => 'int',
        'not null' => FALSE,
        'default' => 0
      ),
    ),
    'indexes' => array(
      'bundle_id' => array('bundle_id'),
    ),
    'unique keys' => array(
      'bundle_panel' => array('bundle_id', 'name'),
    ),
    'foreign keys' => array(
      'tripal_bundle' => array(
        'table' => 'tripal_bundle',
        'columns' => array(
          'bundle_id' => 'id',
        ),
      ),
    ),
    'primary key' => array('panel_id'),
  );
  $schema['tripal_panel_fields'] = array(
    'description' => 'The list of panels into which fields can be placed.',
    'fields' => array(
      'panel_field_id' => array(
        'description' => 'The primary identifier for this table.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'panel_id' => array(
        'description' => 'The primary identifier for this table.',
        'type' => 'int',
        'not null' => TRUE,
      ),
      'field_id' => array(
        'description' => 'A bundle ID from the tripal_bundle table.',
        'type' => 'int',
        'not null' => TRUE,
      ),
    ),
    'indexes' => array(
      'panel_id' => array('panel_id'),
      'field_id' => array('field_id'),
    ),
    'unique keys' => array(
      'panel_field' => array('panel_id', 'field_id'),
    ),
    'foreign keys' => array(
      'tripal_panel' => array(
        'table' => 'tripal_panel',
        'columns' => array(
          'panel_id' => 'panel_id',
        ),
      ),
      'field_config' => array(
        'table' => 'field_config',
        'columns' => array(
          'field_id' => 'id',
        ),
      ),
    ),
    'primary key' => array('panel_field_id'),
  );
  
  return $schema;
}

/**
 *
 */
function tripal_entities_add_tripal_bundle_panes_table(){
  $schema = array (
    'table' => 'tripal_bundle_panes',
    'fields' => array (
      'pane_id' => array (
        'type' => 'serial',
        'not null' => TRUE
      ),
      'bundle_id' => array(
        'type' => 'int',
        'not null' => TRUE
      ),
      'name' => array (
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE
      ),
      'weight' => array (
        'type' => 'int',
        'not null' => TRUE
      ),
      'settings' => array(
        'type' => 'text'
      ),
    ),
    'primary key' => array (
      0 => 'pane_id'
    ),
    'foreign keys' => array (
      'tripal_bundle' => array (
        'table' => 'tripal_bundle',
        'columns' => array (
          'bundle_id' => 'bundle_id'
        ),
      ),
    ),
    'unique keys' => array (
      'tripal_bundle_panes_name' => array ('name'),
    ),
    'indexes' => array(
      'tripal_bundle_panes_bundle_id' => array('bundle_id'),
    ),
  );
  chado_create_custom_table('tripal_bundle_panes', $schema, TRUE);
}