| 1 |
<?php
|
| 2 |
// $Id: amazon_items.module,v 1.12 2005/08/03 04:11:52 nsk Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Allows sites to display products within nodes via the web-services of Amazon.com.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/*
|
| 10 |
* Third-party includes
|
| 11 |
*/
|
| 12 |
require_once "includes/AmazonProduct.class.inc";
|
| 13 |
require_once "includes/AmazonSearchEngine.class.inc";
|
| 14 |
if (file_exists("includes/Snoopy.class.inc")) {
|
| 15 |
include_once "includes/Snoopy.class.inc";
|
| 16 |
}
|
| 17 |
|
| 18 |
/**
|
| 19 |
* Implementation of hook_perm()
|
| 20 |
*/
|
| 21 |
function amazon_items_perm() {
|
| 22 |
return array("access amazon items", "administer amazon items");
|
| 23 |
}
|
| 24 |
|
| 25 |
/**
|
| 26 |
* Implementation of hook_nodeapi().
|
| 27 |
*/
|
| 28 |
function amazon_items_nodeapi(&$node, $op, $arg = 0, $arg2 = 0) {
|
| 29 |
switch ($op) {
|
| 30 |
case "settings":
|
| 31 |
return array("amazon item" => form_checkbox("Amazon Items", "amazon_items_node_$node->type", 1, variable_get("amazon_items_node_$node->type", 0)));
|
| 32 |
|
| 33 |
case "load":
|
| 34 |
return _amazon_items_load($node);
|
| 35 |
|
| 36 |
case "form post":
|
| 37 |
if (user_access("administer amazon items") && (variable_get("amazon_items_node_$node->type", 0) == 1)) {
|
| 38 |
$form = amazon_items_form($node, $arg);
|
| 39 |
}
|
| 40 |
return $form;
|
| 41 |
|
| 42 |
case "view":
|
| 43 |
if (!$teaser && user_access("access amazon items") && variable_get("amazon_items_box", 1)) {
|
| 44 |
if ($node->amazon_items) {
|
| 45 |
$node->body .= '<br /><a name="amazon-items"></a>' . theme("amazon_items", $node);
|
| 46 |
}
|
| 47 |
}
|
| 48 |
break;
|
| 49 |
|
| 50 |
case "insert":
|
| 51 |
case "update":
|
| 52 |
foreach ((array)$node->amazon_items as $amazon_item) {
|
| 53 |
if (!$amazon_item["deleted"] && !$amazon_item["aid"]) {
|
| 54 |
$aid = db_next_id("amazon_items_aid");
|
| 55 |
db_query("INSERT INTO {amazon_items} (aid, asin, nid, name, url, imgsmall, imgmedium, imglarge)
|
| 56 |
VALUES (%d, '%s', %d, '%s', '%s', '%s', '%s', '%s')",
|
| 57 |
$aid, $amazon_item["asin"], $node->nid, $amazon_item["name"], $amazon_item["url"],
|
| 58 |
$amazon_item["imgsmall"], $amazon_item["imgmedium"], $amazon_item["imglarge"]);
|
| 59 |
}
|
| 60 |
elseif ($amazon_item["deleted"] && $amazon_item["aid"]) {
|
| 61 |
db_query("DELETE FROM {amazon_items} WHERE aid=%d", $amazon_item["aid"]);
|
| 62 |
}
|
| 63 |
}
|
| 64 |
break;
|
| 65 |
|
| 66 |
case "delete":
|
| 67 |
if (variable_get("amazon_items_node_$node->type", TRUE)) {
|
| 68 |
db_query("DELETE FROM {amazon_items} WHERE nid=%d", $node->nid);
|
| 69 |
}
|
| 70 |
break;
|
| 71 |
|
| 72 |
case "validate":
|
| 73 |
switch ($_POST["fileop"]) {
|
| 74 |
case t("Add item"):
|
| 75 |
return _amazon_items_add_item($node);
|
| 76 |
case t("Delete selected items"):
|
| 77 |
return _amazon_items_delete_item($node);
|
| 78 |
}
|
| 79 |
break;
|
| 80 |
|
| 81 |
default:
|
| 82 |
break;
|
| 83 |
}
|
| 84 |
return;
|
| 85 |
}
|
| 86 |
|
| 87 |
/**
|
| 88 |
* Implementation of hook_menu()
|
| 89 |
*/
|
| 90 |
function amazon_items_menu($may_cache) {
|
| 91 |
if (!$may_cache) {
|
| 92 |
drupal_set_html_head(_amazon_items_html_head());
|
| 93 |
}
|
| 94 |
return;
|
| 95 |
}
|
| 96 |
|
| 97 |
/**
|
| 98 |
* Implementation of hook_help().
|
| 99 |
*/
|
| 100 |
function amazon_items_help($section) {
|
| 101 |
switch ($section) {
|
| 102 |
case "admin/modules#description":
|
| 103 |
return t("Allows sites to display products within nodes via the web services of Amazon.com.");
|
| 104 |
|
| 105 |
case "admin/settings/amazon_items":
|
| 106 |
return t("<p>Amazon items are products added to nodes via ASIN. They may be displayed in a box below node content, or in a sidebar block, or both.</p>
|
| 107 |
<p>The intended usage is to list items related to the node so that a reader could buy products from Amazon using your Affiliate ID, thus giving you a kickback on the purchase.</p>
|
| 108 |
<p>There are two ways to display items within nodes:</p>
|
| 109 |
<ul><li>Box - enable the option on this page</li>
|
| 110 |
<li>Block - go to admin/block and enable the block</li></ul>");
|
| 111 |
|
| 112 |
default:
|
| 113 |
break;
|
| 114 |
}
|
| 115 |
}
|
| 116 |
|
| 117 |
/**
|
| 118 |
* Implementation of hook_settings().
|
| 119 |
*/
|
| 120 |
function amazon_items_settings() {
|
| 121 |
$group = form_textfield(t("Amazon XML server"), "amazon_items_server", variable_get("amazon_items_server", "xml.amazon.com"), 20, 40);
|
| 122 |
$group .= form_textfield(t("Affiliate ID"), "amazon_items_affid", variable_get("amazon_items_affid", "none"), 20, 40, "Your Amazon affiliate ID.");
|
| 123 |
$group .= form_textfield(t("Developer Token"), "amazon_items_devt", variable_get("amazon_items_devt", "none"), 20, 40, "Your Amazon Developer Token.");
|
| 124 |
$output = form_group(t("Amazon authentication"), $group);
|
| 125 |
|
| 126 |
$group = form_select(t("Box display"), "amazon_items_box", variable_get("amazon_items_box", 1), array("1" => "Enable", "0" => "Disable"));
|
| 127 |
$group .= form_textfield(t("Box title"), "amazon_items_boxtitle", variable_get("amazon_items_boxtitle", "Related items from Amazon.com"), 40, 255, "The label for the Amazon items box that will appear below the node content.");
|
| 128 |
$group .= form_textfield(t("Block title"), "amazon_items_blocktitle", variable_get("amazon_items_blocktitle", "Related items from Amazon.com"), 40, 255, "The title of the Amazon items block.");
|
| 129 |
$group .= form_select(t("Item format"), "amazon_items_format", variable_get("amazon_items_format", "iframe"), array("iframe" => "Amazon-served iFrame", "html" => "Standard HTML"));
|
| 130 |
$group .= form_select(t("Thumbnail size"), "amazon_items_imgsize", variable_get("amazon_items_imgsize", "imgmedium"), array("imgsmall" => "Small", "imgmedium" => "Medium", "imglarge" => "Large"), 'Enter thumbnail size. This applies to the "Standard HTML" format for items displayed in the amazon_items box, and for all items displayed in the amazon_items block.');
|
| 131 |
$output .= form_group(t("Node display settings"), $group);
|
| 132 |
|
| 133 |
return $output;
|
| 134 |
}
|
| 135 |
|
| 136 |
/**
|
| 137 |
* Implementation of hook_link()
|
| 138 |
*/
|
| 139 |
function amazon_items_link($type, $node = NULL, $teaser = FALSE) {
|
| 140 |
$links = array();
|
| 141 |
|
| 142 |
switch ($type) {
|
| 143 |
case "node":
|
| 144 |
if ($teaser && !empty($node->amazon_items)) {
|
| 145 |
$links[] = l(t("related items"), "node/$node->nid#amazon-items", array("title" => t("Related items from Amazon.com")));
|
| 146 |
}
|
| 147 |
break;
|
| 148 |
|
| 149 |
default:
|
| 150 |
break;
|
| 151 |
}
|
| 152 |
return $links;
|
| 153 |
}
|
| 154 |
|
| 155 |
/**
|
| 156 |
* Implementation of hook_block()
|
| 157 |
*/
|
| 158 |
function amazon_items_block($op = "list", $delta = 0) {
|
| 159 |
if ($op == "list") {
|
| 160 |
$blocks[0]["info"] = variable_get("amazon_items_blocktitle", "Related items from Amazon.com");
|
| 161 |
return $blocks;
|
| 162 |
} else {
|
| 163 |
switch ($delta) {
|
| 164 |
case 0:
|
| 165 |
if (user_access("access amazon items")) {
|
| 166 |
$block["subject"] = variable_get("amazon_items_blocktitle", "Related items from Amazon.com");
|
| 167 |
$block["content"] = _amazon_items_block_show();
|
| 168 |
}
|
| 169 |
break;
|
| 170 |
}
|
| 171 |
return $block;
|
| 172 |
}
|
| 173 |
}
|
| 174 |
|
| 175 |
/**
|
| 176 |
* Implementation of hook_form().
|
| 177 |
*/
|
| 178 |
function amazon_items_form(&$node, &$param) {
|
| 179 |
$output = '<div class="amazon-item-form">';
|
| 180 |
|
| 181 |
$header = array("", t("Title"), t("Asin"));
|
| 182 |
|
| 183 |
foreach ((array)$node->amazon_items as $key => $amazon_item) {
|
| 184 |
$fields = array("aid", "asin", "name", "url", "imgsmall", "imgmedium", "imglarge", "deleted");
|
| 185 |
foreach ($fields as $field) {
|
| 186 |
$output .= form_hidden("amazon_items][$key][$field", $amazon_item[$field]);
|
| 187 |
}
|
| 188 |
if ($amazon_item["deleted"]) {
|
| 189 |
$output .= form_hidden("amazon_items][$key][selected", false);
|
| 190 |
}
|
| 191 |
else {
|
| 192 |
$rows[] = array(
|
| 193 |
form_checkbox("", "amazon_items][$key][selected"),
|
| 194 |
l(stripslashes($amazon_item["name"]), $amazon_item["url"]),
|
| 195 |
$amazon_item["asin"]);
|
| 196 |
}
|
| 197 |
}
|
| 198 |
if (count($rows) > 0) {
|
| 199 |
$attributes["class"] = "amazon-item-list";
|
| 200 |
$group .= theme("table", $header, $rows, $attributes);
|
| 201 |
$group .= form_submit(t("Delete selected items"), "fileop");
|
| 202 |
}
|
| 203 |
$group .= form_textfield(t("New item"), "amazon_items_asin", NULL, 20, 20, t("Enter the Asin for the item."));
|
| 204 |
$group .= form_submit(t("Add item"), "fileop");
|
| 205 |
|
| 206 |
$output .= form_group(variable_get("amazon_items_boxtitle", "Related items from Amazon.com"), $group);
|
| 207 |
$output .= "</div>";
|
| 208 |
|
| 209 |
return $output;
|
| 210 |
}
|
| 211 |
|
| 212 |
/*
|
| 213 |
* Functions specific to amazon_items
|
| 214 |
*/
|
| 215 |
|
| 216 |
/**
|
| 217 |
* Provides a link to the CSS stylesheet associated with this module.
|
| 218 |
*
|
| 219 |
* @return
|
| 220 |
* An HTML string of a style declaration to import the custom stylesheet
|
| 221 |
*/
|
| 222 |
function _amazon_items_html_head() {
|
| 223 |
return '<style type="text/css" media="all">@import url(modules/amazon_items/amazon_items.css);</style>';
|
| 224 |
}
|
| 225 |
|
| 226 |
/**
|
| 227 |
* Callback function to add an item to a node being edited.
|
| 228 |
*
|
| 229 |
* @param
|
| 230 |
* The node being edited
|
| 231 |
*/
|
| 232 |
function _amazon_items_add_item(&$node) {
|
| 233 |
if (!$node->amazon_items_asin) {
|
| 234 |
form_set_error("amazon_items_asin", t("No Asin was entered."));
|
| 235 |
return $node;
|
| 236 |
}
|
| 237 |
$am_affid = variable_get("amazon_items_affid", "none");
|
| 238 |
$am_devt = variable_get("amazon_items_devt", "none");
|
| 239 |
$amazonSE = new AmazonSearchEngine($am_devt, $am_affid, "2.0");
|
| 240 |
$amazonSE->server = variable_get("amazon_items_server", "xml.amazon.com");
|
| 241 |
$amazonSE->searchAsin(db_escape_string(trim($node->amazon_items_asin)));
|
| 242 |
|
| 243 |
if (count($amazonSE->results) == 1) {
|
| 244 |
$new_amazon_item = array(
|
| 245 |
"asin" => trim($node->amazon_items_asin),
|
| 246 |
"name" => db_escape_string($amazonSE->results[0]->ProductName),
|
| 247 |
"url" => $amazonSE->results[0]->url,
|
| 248 |
"imgsmall" => $amazonSE->results[0]->ImageUrlSmall,
|
| 249 |
"imgmedium" => $amazonSE->results[0]->ImageUrlMedium,
|
| 250 |
"imglarge" => $amazonSE->results[0]->ImageUrlLarge,
|
| 251 |
"deleted" => FALSE);
|
| 252 |
$node->amazon_items[] = $new_amazon_item;
|
| 253 |
}
|
| 254 |
elseif (count($amazonSE->results) == 0) {
|
| 255 |
form_set_error("amazon_items_asin", t("No records matched that ASIN, or API changes prohibit this module from working ok, or your Developer Token in the settings page is wrong."));
|
| 256 |
return $node;
|
| 257 |
}
|
| 258 |
drupal_set_message(t("The Amazon item has been added. Remember to press Submit at the bottom of this form to save your changes."));
|
| 259 |
}
|
| 260 |
|
| 261 |
/**
|
| 262 |
* Callback function to delete an item from a node being edited.
|
| 263 |
*
|
| 264 |
* @param
|
| 265 |
* A reference to the node object being edited
|
| 266 |
*/
|
| 267 |
function _amazon_items_delete_item(&$node) {
|
| 268 |
for ($i = 0; $i < count($node->amazon_items); $i++) {
|
| 269 |
if ($node->amazon_items[$i]["selected"]) {
|
| 270 |
$node->amazon_items[$i]["deleted"] = TRUE;
|
| 271 |
$report = TRUE;
|
| 272 |
}
|
| 273 |
}
|
| 274 |
if ($report) {
|
| 275 |
drupal_set_message(t("The Amazon item(s) have been deleted. Remember to press Submit at the bottom of this form to save your changes."));
|
| 276 |
}
|
| 277 |
else {
|
| 278 |
form_set_error("amazon_items", t("No Amazon items were selected for deletion."));
|
| 279 |
}
|
| 280 |
}
|
| 281 |
|
| 282 |
/**
|
| 283 |
* Retrieve Amazon items from database for a given node.
|
| 284 |
*
|
| 285 |
* @param
|
| 286 |
* The node object being viewed
|
| 287 |
* @return
|
| 288 |
* An associative array of Amazon item data for the node
|
| 289 |
*/
|
| 290 |
function _amazon_items_load($node) {
|
| 291 |
$result = db_query("SELECT aid, asin, nid, name, url, imgsmall, imgmedium, imglarge ".
|
| 292 |
"FROM {amazon_items} WHERE nid = %d", $node->nid);
|
| 293 |
while ($amazon_item = db_fetch_array($result)) {
|
| 294 |
$amazon_item["deleted"] = FALSE;
|
| 295 |
$amazon_items[] = $amazon_item;
|
| 296 |
}
|
| 297 |
$fields["amazon_items"] = $amazon_items;
|
| 298 |
return $fields;
|
| 299 |
}
|
| 300 |
|
| 301 |
/**
|
| 302 |
* Retrieve Amazon items for a node and generate an HTML-formatted block
|
| 303 |
*
|
| 304 |
* @return
|
| 305 |
* An HTML-formatted block with associated Amazon items
|
| 306 |
*/
|
| 307 |
function _amazon_items_block_show() {
|
| 308 |
$output = "";
|
| 309 |
|
| 310 |
$result = db_query("SELECT * FROM {amazon_items} WHERE nid=%d", arg(1));
|
| 311 |
while ($amazon_item = db_fetch_array($result)) {
|
| 312 |
$output .= '<div class="amazon-items-item">'
|
| 313 |
. '<a href="'.$amazon_item['url'].'">'
|
| 314 |
. '<img src="'.$amazon_item[variable_get("amazon_items_imgsize", "imgmedium")].'" alt="'.stripslashes($amazon_item['name']).'" /></a><br />'
|
| 315 |
. '<a href="'.$amazon_item['url'].'">'.stripslashes($amazon_item['name']).'</a></div>'. "\n";
|
| 316 |
}
|
| 317 |
return $output;
|
| 318 |
}
|
| 319 |
|
| 320 |
/**
|
| 321 |
* @addtogroup themeable
|
| 322 |
* @{
|
| 323 |
*/
|
| 324 |
|
| 325 |
/**
|
| 326 |
* Format a list of Amazon items for display in a given node
|
| 327 |
*
|
| 328 |
* @param $node
|
| 329 |
* The node being viewed
|
| 330 |
* @return
|
| 331 |
* An HTML-formatted container with Amazon items for the node
|
| 332 |
*/
|
| 333 |
function theme_amazon_items($node) {
|
| 334 |
$output = '<fieldset class="amazon-items-fieldset"><legend>'.variable_get("amazon_items_boxtitle", "Related items from Amazon.com")."</legend>\n";
|
| 335 |
$output .= '<div class="amazon-items">' . "\n";
|
| 336 |
|
| 337 |
foreach ((array)$node->amazon_items as $amazon_item) {
|
| 338 |
if (!$amazon_item["deleted"]) {
|
| 339 |
$display = TRUE;
|
| 340 |
$format = variable_get("amazon_items_format", "iframe");
|
| 341 |
if ($format == "iframe") {
|
| 342 |
// Amazon-served iFrame format
|
| 343 |
$amazon_affid = variable_get("amazon_items_affid", "none");
|
| 344 |
// Asin must be 10 characters, so pad if necessary
|
| 345 |
$url = "http://rcm.amazon.com/e/cm?t=$amazon_affid&o=1&p=8&l=as1&asins="
|
| 346 |
. str_pad($amazon_item["asin"], 10, "0", STR_PAD_LEFT)."&fc1=000000&lc1=0000ff&bc1=<1=_blank&IS2=1&bg1=ffffff&f=ifr";
|
| 347 |
$output .= '<iframe src="'.$url.'" width="120" height="240" scrolling="no" marginwidth="0" marginheight="0" frameborder="0">'
|
| 348 |
. '<a href="'.$url.'">'.$amazon_item["name"]."</a>"
|
| 349 |
. "</iframe>\n";
|
| 350 |
}
|
| 351 |
elseif ($format == "html") {
|
| 352 |
// Standard HTML format
|
| 353 |
$imgsize = variable_get("amazon_items_imgsize", "imgsmall");
|
| 354 |
$output .= '<dl class="'.$imgsize.'">' . "\n";
|
| 355 |
$output .= '<dt><a href="'.$amazon_item['url'].'" title="'.stripslashes($amazon_item['name']).'"><img src="'.$amazon_item[$imgsize].'" /></a></dt>' . "\n";
|
| 356 |
$output .= '<dd><a href="'.$amazon_item['url'].'" title="'.stripslashes($amazon_item['name']).'">'.stripslashes($amazon_item['name'])."</a></dd>\n";
|
| 357 |
$output .= "</dl>\n";
|
| 358 |
}
|
| 359 |
}
|
| 360 |
}
|
| 361 |
$output .= "</div>\n";
|
| 362 |
$output .= "</fieldset>\n";
|
| 363 |
|
| 364 |
return $display ? $output : FALSE;
|
| 365 |
}
|
| 366 |
/**
|
| 367 |
* @} end of addtogroup themeable
|
| 368 |
*/
|
| 369 |
?>
|