You are here

codeless_discounts_field.module in Ubercart Discounts (Alternative) 7.2

Same filename and directory in other branches
  1. 6.2 codeless_discounts_field/codeless_discounts_field.module

Attach automatic (codeless) discounts to products using fields.

File

codeless_discounts_field/codeless_discounts_field.module
View source
<?php

/**
 * @file
 * Attach automatic (codeless) discounts to products using fields.
 */

/**
 * 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 = uc_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);
}

Functions

Namesort descending Description
codeless_discounts_field_content_is_empty Implementation of hook_content_is_empty().
codeless_discounts_field_field Implementation of hook_field()
codeless_discounts_field_field_formatter_info Implementation of hook_field_formatter_info().
codeless_discounts_field_field_info Implementation of hook_field_info().
codeless_discounts_field_theme Implementation of hook_theme().
codeless_discounts_field_widget Implementation of hook_widget().
codeless_discounts_field_widget_info Implementation of hook_widget_info().
theme_codeless_discounts_field_formatter_default Theme function for codeless discounts element.
theme_codeless_discounts_field_get_codeless_discount_html_for_product