<?php

/**
 * @file
 * Installation file for Scheduler module.
 */

/**
 * Implements hook_schema().
 */
function scheduler_schema() {
  return array(
    'scheduler' => array(
      'description' => 'The main table to hold the scheduler data.',
      'fields' => array(
        'nid' => array(
          'description' => 'The foreign key to node.nid',
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
        ),
        'publish_on' => array(
          'description' => 'The UNIX UTC timestamp when to publish',
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
          'default' => 0,
        ),
        'unpublish_on' => array(
          'description' => 'The UNIX UTC timestamp when to unpublish',
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
          'default' => 0,
        ),
      ),
      'indexes' => array(
        'scheduler_publish_on' => array('publish_on'),
        'scheduler_unpublish_on' => array('unpublish_on'),
      ),
      'primary key' => array('nid'),
    ),
  );
}

/**
 * Implements hook_uninstall().
 */
function scheduler_uninstall() {
  $variables = array(
    'scheduler_date_format',
    'scheduler_field_type',
    'scheduler_extra_info',
  );

  $types = node_type_get_types();
  foreach ($types as $type) {
    $type_name = $type->type;
    $variables[] = "scheduler_publish_enable_" . $type_name;
    $variables[] = "scheduler_publish_touch_" . $type_name;
    $variables[] = "scheduler_publish_required_" . $type_name;
    $variables[] = "scheduler_publish_revision_" . $type_name;
    $variables[] = "scheduler_unpublish_enable_" . $type_name;
    $variables[] = "scheduler_unpublish_required_" . $type_name;
    $variables[] = "scheduler_unpublish_revision_" . $type_name;
  }

  foreach ($variables as $variable) {
    variable_del($variable);
  }
}

function scheduler_update_6100() {
  if (db_field_exists('scheduler', 'timezone')) {
    $entries = db_select('scheduler', 's')
      ->fields('s', array('nid', 'publish_on', 'unpublish_on', 'timezone'))
      ->execute();

    foreach ($entries as $entry) {
      if ($entry->publish_on != 0) {
        db_merge('scheduler')
          ->key(array('nid' => $entry->nid))
          ->fields(array('publish_on' => $entry->publish_on - $entry->timezone))
          ->execute();
      }
      if ($entry->unpublish_on != 0) {
        db_merge('scheduler')
          ->key(array('nid' => $entry->nid))
          ->fields(array('unpublish_on' => $entry->unpublish_on - $entry->timezone))
          ->execute();
      }
    }
    db_drop_field('scheduler', 'timezone');
  }
}

function scheduler_update_6101() {
  $ret = array();
  $types = node_type_get_types();
  foreach ($types as $type) {
    $type_name = $type->type;
  	$publish_enable = variable_get('scheduler_'. $type_name, 0);
  	$publish_touch = variable_get('scheduler_touch_'. $type_name, 0);

  	variable_set('scheduler_publish_enable_'. $type_name, $publish_enable);
  	variable_set('scheduler_unpublish_enable_'. $type_name, $publish_enable);
   	variable_set('scheduler_publish_touch_'. $type_name, $publish_touch);

  	variable_del('scheduler_touch_'. $type_name);
  	variable_del('scheduler_'. $type_name);
  }
  return $ret;
}
