<?php
// $Id: og_panels.install,v 1.2 2009/08/15 19:17:31 populist Exp $

/**
 * Implementations of hook_install/uninstall
 *
 * To go with our lovely new hook_schema()
 */
function og_panels_install() {
  drupal_install_schema('og_panels');
}

function og_panels_uninstall() {
  drupal_uninstall_schema('og_panels');
}


/**
* Implementation of hook_enable()
*/
function og_panels_enable() {
  variable_set('page_manager_node_view_disabled', FALSE);
}


/**
 * Implementation of hook_schema()
 *
 * Create schemas for handling og_panels and also the tab router, as per
 * http://drupal.org/node/362031
 *  
 */
function og_panels_schema() {
  $schema['og_panels'] = array(
    'description' => 'Keeps track of panel dids per-group.',
    'fields' => array(
      'did' => array(
        'description' => 'A panels display id.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0),
      'nid' => array(
        'description' => 'The organic group node id.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0),
      'published' => array(
        'description' => 'Boolean: is this panels page published?',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0),
      'page_title' => array(
        'description' => 'The tab/title for this panels page.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
      'path' => array(
        'description' => 'The url alias for this panel.',
        'type' => 'varchar',
        'length' => 100,
        'not null' => TRUE,
        'default' => ''),
      'default_page' => array(
        'description' => 'Boolean: is this panels page the default home for the group?',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0),
      'show_blocks' => array(
        'description' => 'Boolean: should this panels page hide the normal block regions?',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0),
      'weight' => array(
        'description' => 'Weighting for tab order',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0),
      'tab_num' => array(
        'description' => 'Tab number for this node_tab.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0),
      ),
    'indexes' => array(
      'nid' => array('nid'),
      'page_title' => array('page_title'),
      'tab_num' => array('tab_num'),
      'did' => array('did'),
     ),
    'primary key' => array('nid', 'tab_num'),
  );
  return $schema;    
}

function og_panels_update_5001() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {og_panels} ADD COLUMN weight int(4) NOT NULL DEFAULT 0");
      break;
    case 'pgsql':
      db_add_column($ret, 'og_panels', 'weight', 'integer', array('not null' => TRUE, 'default' => 0));
      break;
  }
  return $ret ? $ret : array();
}

/**
* Update og_panels table and data from Drupal 5 site to Drupal 6.
*
* Add the tab_num field, plus add as index, and a primary key
* Update all existing records in the table with a tab_num value
* To get the tab_num value we just need to select the records in the
* correct order, then run through them, resetting the tab_num when we get
* to the next nid.
*/
function og_panels_update_6000() {
  $ret = array();

// Add the tab_num field
  $new_field = array(
   'description' => 'Tab number for this node_tab.',
   'type' => 'int',
   'unsigned' => TRUE,
   'not null' => TRUE,
   'default' => 0,
  );

// Setup the indexes
  $keys_new = array(
    'indexes' => array(
      'nid' => array('nid'),
      'page_title' => array('page_title'),
      'tab_num' => array('tab_num'),
      'did' => array('did'),
     ),
  );
  // Don't want did to be primary key anymore - will use nid and tab_num

  db_add_field($ret, 'og_panels', 'tab_num', $new_field, $keys_new);
 
  // Select all records in the table, order by nid, default_page and weight
  $result = db_query("SELECT * from {og_panels} ORDER BY nid, default_page DESC, WEIGHT ASC");
  $old_nid = 0;
  $tab_num = 0;
  while ($row = db_fetch_object($result)) {
   $tab_num ++;
   $current_nid = $row->nid;
   $did = $row->did;
   // Reset the tab_num value to 1 when we move to the next node
   if ($old_nid != 0 && ($old_nid != $current_nid)) {
    $tab_num = 1;
   }
   $ret[] = update_sql("UPDATE {og_panels} SET tab_num = $tab_num WHERE nid = $current_nid AND did = $did");
   $old_nid = $current_nid;
  }
 
  // Can only add the new primary key here because there are were no values in
  // tab_num before, so would cause "duplicate entry for key 1" errors 
  db_drop_primary_key($ret, 'og_panels');
  db_add_primary_key($ret, 'og_panels', array('nid', 'tab_num'));
 
  return $ret;
}

/**
* This will make sure that your page manager node_view is enabled. This should fire with hook_enable() but if you just copy over the new code Drupal will think the module is already installed.
*/
function og_panels_update_60001() {
  variable_set('page_manager_node_view_disabled', FALSE);
}
