<?php
//$Id: codeless_discounts_field.module,v 1.9 2009/08/23 01:21:28 ryangroe Exp $


/**
 * See http://drupal.org/node/106716 for more information.
 */

/**
 * Implementation of hook_field_info().
 */
function codeless_discounts_field_field_info() {
  return array(
    "codeless_discounts" => array(
      "label" => t("Codeless Discounts"),
      "description" => t("Displays uc_discounts_alt discounts for node without codes"),
    ),
  );
}

/**
 * Implementation of hook_content_is_empty().
 */
function codeless_discounts_field_content_is_empty($item, $field) {
  return FALSE;
}

/**
 * Implementation of hook_widget_info().
 */
function codeless_discounts_field_widget_info() {
  return array(
    "codeless_discounts" => array(
      "label" => "Default Display",
      "field types" => array("codeless_discounts"),
      "multiple values" => CONTENT_HANDLE_CORE,
      "callbacks" => array(
        "default value" => CONTENT_CALLBACK_DEFAULT,
      ),
    ),
  );
}

/**
 * Implementation of hook_widget().
 */
function codeless_discounts_field_widget(&$form, &$form_state, $field, $items, $delta = 0) {
  $element = array(
    "#type" => $field["widget"]["type"],
    "#default_value" => isset($items[$delta]) ? $items[$delta] : NULL,
  );
  return $element;
}

/**
 * Implementation of hook_field_formatter_info().
 */
function codeless_discounts_field_field_formatter_info() {
  return array(
    "default" => array(
      "label" => "Discount Description",
      "field types" => array("codeless_discounts"),
    ),
  );
}

/**
 * Implementation of hook_field()
 */
function codeless_discounts_field_field($op, &$node, $field, &$items, $teaser, $page) {
  switch ($op) {
    case "sanitize":
    case "view":
      //If items is empty, generate value by getting discounts that apply to this product
      if (empty($items)) {
        $item = array();
        $item["codeless_discounts"] = theme("codeless_discounts_field_get_codeless_discount_html_for_product", $node);
        $items[] = $item;
      }
      break;
  }
}

/**
 * Implementation of hook_theme().
 */
function codeless_discounts_field_theme() {
  return array(
    "codeless_discounts_field_formatter_default" => array(
      "arguments" => array("element"),
    ),
    "codeless_discounts_field_get_codeless_discount_html_for_product" => array(
      "arguments" => array("product_id"),
    ),
  );
}

/**
 * Theme function for codeless discounts element.
 */
function theme_codeless_discounts_field_formatter_default($element) {
  return $element["#item"]["codeless_discounts"];
}

function theme_codeless_discounts_field_get_codeless_discount_html_for_product($product) {
  $discounts = get_codeless_discounts_for_product($product);
  if (empty($discounts)) {
    return "";
  }

  $descriptions = array();
  foreach ($discounts as $discount) {
    $description = $discount->short_description;
    if ($discount->has_expiration) {
      $description .= " (". t("Expires:") ." ". date("Y-m-d H:i", $discount->expiration) .")";
    }
    $descriptions[] = $description;
  }

  return join("<br/>", $descriptions);
}

