<?php
/**
 * @file
 * Provides install, update, schema and uninstall hooks for the module
 * @copyright Copyright(c) 2011 Rowlands Group
 * @license GPL v2 http://www.fsf.org/licensing/licenses/gpl.html
 * @author Lee Rowlands leerowlands at rowlandsgroup dot com
 */

/**
 * Implements hook_install().
 */
function commerce_node_checkout_expire_install() {

  // If a field type we know should exist isn't found, clear the Field cache.
  if (!field_info_field_types('interval') ||
    !field_info_field_types('datestamp')) {
    field_cache_clear();
  }

  // Make sure our entity info exists.
  entity_info_cache_clear();

  // Create our fields.
  foreach (_commerce_node_checkout_expire_installed_fields() as $field_name => $field_detail) {
    // Look for existing field.
    $field = field_info_field($field_name);

    if (empty($field)) {
      $field = field_create_field($field_detail);
    }
  }

  // And their instances.
  foreach (_commerce_node_checkout_expire_installed_instances() as $field_name => $instance_detail) {
    // Look for existing instance.
    $instance = field_info_instance($instance_detail['entity_type'], $instance_detail['bundle'], $field_name, $type);

    if (empty($instance)) {
      field_create_instance($instance_detail);
    }
  }
}


/**
 * Implements hook_uninstall().
 */
function commerce_node_checkout_expire_uninstall() {
  $variables = array(
    'commerce_node_checkout_expire_expire_subject',
    'commerce_node_checkout_expire_expire_body',
    'commerce_node_checkout_expire_expire_reminder'
  );
  foreach ($variables as $variable) {
    variable_del($variable);
  }
}

/**
 * Returns a structured array defining the fields created by this module.
 *
 * @return array
 *   An associative array specifying the fields we wish to add to our
 *   entities
 */
function _commerce_node_checkout_expire_installed_fields() {
  $t = get_t();
  return array(
    'commerce_node_checkout_expire' => array(
      'field_name' => 'commerce_node_checkout_expire',
      'cardinality' => 1,
      'type'        => 'interval',
      'settings'    => array(
        'allowed_periods' => array(
          'day' => 'day',
          'week' => 'week',
          'month' => 'month'
        ),
      ),
    ),
    'commerce_node_checkout_expires' => array(
      'field_name' => 'commerce_node_checkout_expires',
      'cardinality' => 1,
      'type'        => 'datestamp',
      'settings'    => array(
        'granularity' => array(
          'day' => 'day',
          'hour' => 0,
          'minute' => 0,
          'month' => 'month',
          'second' => 0,
          'year' => 'year',
        ),
        'repeat' => 0,
        'timezone_db' => 'UTC',
        'todate' => '',
        'tz_handling' => 'none',
      ),
    ),
  );
}

/**
 * Returns a structured array defining the instances for this module
 *
 * @return array
 *   An associative array specifying the instances we wish to add to our entities
 */
function _commerce_node_checkout_expire_installed_instances() {
  $t = get_t();
  return array(
    'commerce_node_checkout_expire' => array(
      'entity_type' => 'commerce_product',
      'bundle' => 'commerce_node_checkout',
      'field_name' => 'commerce_node_checkout_expire',
      'label'       => $t('Unpublish after'),
      'widget'      => array(
        'type'    => 'interval_default',
      ),
      'display' => array(
        'default' => array(
          'label' => 'hidden',
          'type' => 'interval_default',
        ),
      ),
    ),
    'commerce_node_checkout_expires' => array(
      'entity_type' => 'commerce_line_item',
      'bundle' => 'commerce_node_checkout',
      'field_name' => 'commerce_node_checkout_expires',
      'label'       => $t('Unpublish on'),
      'widget'      => array(
        'settings' => array(
          'increment' => '15',
          'input_format' => 'm/d/Y',
          'default_value' => 'blank',
          'input_format_custom' => '',
          'label_position' => 'above',
          'repeat_collapsed' => 0,
          'text_parts' => array(),
          'year_range' => '-3:+3',
        ),
        'type' => 'date_popup',
      ),
      'display' => array(
        'default' => array(
          'label' => 'above',
          'module' => 'date',
          'settings' => array(
            'format_type' => 'long',
            'fromto' => 'both',
            'multiple_from' => '',
            'multiple_number' => '',
            'multiple_to' => '',
            'show_repeat_rule' => 'show',
          ),
          'type' => 'date_default',
        ),
      ),
    ),
  );
}
