| 1 |
<?php
|
| 2 |
// $Id: amazon_wrapper.inc,v 1.1 2006/06/27 05:38:46 eaton Exp $
|
| 3 |
//
|
| 4 |
// A simple wrapper for the complicated inner workings of Prometheus6's excellent amazontools.module.
|
| 5 |
// Rather than assuming that products are always associated with nodes, this set of functions treats
|
| 6 |
// the {amazonitem} like a cache.
|
| 7 |
//
|
| 8 |
// This approach makes it easier for helper modules to utilize the data even when full-fledged nodes
|
| 9 |
// are unecessary or unwanted. Since amazon_item and amazon node types are joined to it via asin
|
| 10 |
// rather than nid, it doesn't hurt to have extra {amazonitem} cache records around.
|
| 11 |
|
| 12 |
function amazon_get_for_asin($asin = '', $force_lookup = FALSE, $cache = TRUE) {
|
| 13 |
$amazon_items = amazon_get_for_asins(array($asin), $force_lookup, $cache);
|
| 14 |
if (count($amazon_items)) {
|
| 15 |
return $amazon_items[$asin];
|
| 16 |
}
|
| 17 |
return NULL;
|
| 18 |
}
|
| 19 |
|
| 20 |
function amazon_get_for_asins($asins = array(), $force_lookup = FALSE, $cache = TRUE) {
|
| 21 |
$amazon_items = array();
|
| 22 |
$to_look_up = $asins;
|
| 23 |
|
| 24 |
if ($force_lookup) {
|
| 25 |
$asins = array();
|
| 26 |
}
|
| 27 |
|
| 28 |
foreach(amazon_get_from_cache($asins) as $item) {
|
| 29 |
$amazon_items[$item->asin] = $item;
|
| 30 |
$to_look_up = array_diff($to_look_up, array_keys($amazon_items));
|
| 31 |
}
|
| 32 |
|
| 33 |
foreach(amazon_get_from_web($to_look_up, $cache) as $item) {
|
| 34 |
$amazon_items[$item->asin] = $item;
|
| 35 |
}
|
| 36 |
|
| 37 |
return $amazon_items;
|
| 38 |
}
|
| 39 |
|
| 40 |
function amazon_get_from_web($asins = array(), $cache = TRUE) {
|
| 41 |
$amazon_items = array();
|
| 42 |
if (count($asins)) {
|
| 43 |
$items = _amazon_product_data_from_Amazon(implode($asins, ','));
|
| 44 |
if ($cache) {
|
| 45 |
foreach($items as $item) {
|
| 46 |
$amazon_items[$amazon_item->asin] = $item;
|
| 47 |
amazon_cache_item($item);
|
| 48 |
}
|
| 49 |
}
|
| 50 |
}
|
| 51 |
return $amazon_items;
|
| 52 |
}
|
| 53 |
|
| 54 |
function amazon_get_from_cache($asins = array()) {
|
| 55 |
$amazon_items = array();
|
| 56 |
if (count($asins)) {
|
| 57 |
foreach($asins as $asin) {
|
| 58 |
$item = _amazon_product_db_data($asin);
|
| 59 |
$amazon_items[$item->asin] = $item;
|
| 60 |
}
|
| 61 |
}
|
| 62 |
return $amazon_items;
|
| 63 |
}
|
| 64 |
|
| 65 |
function amazon_cache_item($item) {
|
| 66 |
if ($item->asin != '') {
|
| 67 |
// first, wipe out any past cached amazon records.
|
| 68 |
db_query("DELETE FROM {amazonitem} WHERE asin='%s'", $item->asin);
|
| 69 |
|
| 70 |
// now, insert the new stuff.
|
| 71 |
db_query("INSERT INTO {amazonitem} (asin, detailpageurl,
|
| 72 |
smallimageurl, smallimageheight, smallimagewidth,
|
| 73 |
mediumimageurl, mediumimageheight, mediumimagewidth,
|
| 74 |
largeimageurl, largeimageheight, largeimagewidth,
|
| 75 |
author, editorialreview, binding, listamount,
|
| 76 |
listcurrencycode, listformattedprice, title,
|
| 77 |
amount, currencycode, formattedprice, pricedate, availability)
|
| 78 |
VALUES ('%s', '%s',
|
| 79 |
'%s', %d, %d,
|
| 80 |
'%s', %d, %d,
|
| 81 |
'%s', %d, %d,
|
| 82 |
'%s', '%s', '%s', %d,
|
| 83 |
'%s', '%s', '%s',
|
| 84 |
%d, '%s', '%s',
|
| 85 |
'%s', '%s')",
|
| 86 |
$item->asin, $item->detailpageurl,
|
| 87 |
db_escape_string($item->smallimageurl), $item->smallimageheight, $item->smallimagewidth,
|
| 88 |
db_escape_string($item->mediumimageurl), $item->mediumimageheight, $item->mediumimagewidth,
|
| 89 |
db_escape_string($item->largeimageurl), $item->largeimageheight, $item->largeimagewidth,
|
| 90 |
serialize($item->author), db_escape_string($item->editorialreview), $item->binding,
|
| 91 |
$item->listamount,
|
| 92 |
$item->listcurrencycode, $item->listformattedprice, $item->title,
|
| 93 |
$item->amount ? $item->amount: $item->listamount, $item->currencycode,
|
| 94 |
$item->formattedprice ? $item->formattedprice: $item->listformattedprice,
|
| 95 |
date('Y-m-d H:i:s'), $item->availability);
|
| 96 |
}
|
| 97 |
}
|