amazOOP amazOOP
[ class tree: amazOOP ] [ index: amazOOP ] [ all elements ]

Source for file cart.inc.php

Documentation is available at cart.inc.php

  1. <?php
  2. /**
  3. * This file contains the {@link ShoppingCart} class. As well as the {@link CartItem}
  4. * class which stores information on items contained in a ShoppingCart.
  5. *
  6. * @author Mauricio Diaz <madd0@users.sourceforge.net>
  7. * @copyright 2004 (c) Mauricio Diaz Orlich
  8. * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
  9. * @version 0.3
  10. *
  11. * @package amazOOP
  12. */
  13.  
  14. /**
  15. * For security, only this file should be included in your scripts.
  16. * @access private
  17. */
  18. defined('_AMAZOOP') or die( 'Direct Access to this location is not allowed.' );
  19.  
  20. /**
  21. * This is one of the classes returned when querying AWS.
  22. *
  23. * This class is returned when you create or modify a {@link ShoppingCart}.
  24. *
  25. * @since version 0.3
  26. *
  27. * @author Mauricio Diaz <madd0@users.sourceforge.net>
  28. * @copyright 2004 (c) Mauricio Diaz Orlich
  29. * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
  30. * @version 0.3RC2
  31. *
  32. * @package amazOOP
  33. */
  34. class ShoppingCart extends AmazonResponse {
  35.  
  36. /**#@+
  37. * @access private
  38. */
  39.  
  40. /**
  41. * The Cart ID is a unique identifier assigned to the Cart.
  42. * @var string
  43. */
  44. var $cartID;
  45. /**
  46. * The HMAC code is used as a security feature;
  47. * @var string
  48. */
  49. var $HMAC;
  50. /**
  51. * The Purchase URL points to the user's shopping basket at Amazon, where
  52. * he can directly check out.
  53. *
  54. * Once the purchase URL is accessed, the Cart's ID is no longer valid.
  55. *
  56. * Currently only locale US returns purchase URL.
  57. * @var string
  58. */
  59. var $purchaseURL;
  60. /**
  61. * This is an array of {@link CartItem}s stored in the Shopping Cart
  62. * @var array
  63. */
  64. var $items;
  65. /**
  66. * This is an array of ASINs of product's similar to those in the Cart.
  67. * @var array
  68. */
  69. var $similarProducts;
  70.  
  71. /**#@-*/
  72.  
  73. /**
  74. * Default constructor.
  75. *
  76. * Prepares the class.
  77. */
  78. function ShoppingCart(){
  79. $this->productInfo = false;
  80. $this->blendedSearch = false;
  81. $this->shoppingCart = true;
  82. }
  83.  
  84. function getCartID($decoded = false){
  85. if($decoded)
  86. return rawurldecode($this->cartID);
  87. else
  88. return $this->cartID;
  89. }
  90.  
  91. function getHMAC($decoded = false){
  92. if($decoded)
  93. return rawurldecode($this->HMAC);
  94. else
  95. return $this->HMAC;
  96. }
  97.  
  98. function getItems($number = 0){
  99. if(isset($this->items) && sizeof($this->items) != 0 && $number <= sizeof($this->items)){
  100. if($number == 0){
  101. $number = rand(1, sizeof($this->items));
  102. return $this->items[$number-1];
  103. }
  104. else{
  105. if(isset($this->items[$number-1]))
  106. return $this->items[$number-1];
  107. else
  108. return false;
  109. }
  110. }
  111. else
  112. return false;
  113. }
  114.  
  115. /**
  116. * Gets ASINs of other products sold at Amazon that are similar to the current product.
  117. *
  118. * @param integer $product the ASIN you want to get. If set to 0 (default),
  119. * a random ASIN from the list is returned.
  120. *
  121. * @return string|booleanthe ASIN of the requested product from the list,
  122. * or false if the requested position of the list does not contain an ASIN.
  123. */
  124. function getSimilarProducts($product = 0){
  125. if(isset($this->similarProducts) && sizeof($this->similarProducts) != 0 && $product <= sizeof($this->similarProducts)){
  126. if($product == 0){
  127. $number = rand(1, sizeof($this->similarProducts));
  128. return rawurldecode($this->similarProducts[$number-1]);
  129. }
  130. else{
  131. if(isset($this->similarProducts[$product-1]))
  132. return rawurldecode($this->similarProducts[$product-1]);
  133. else
  134. return false;
  135. }
  136. }
  137. else
  138. return false;
  139. }
  140.  
  141. /**
  142. * Will generate either a link or a form to add the products in the cart to the final
  143. * Amazon shopping cart.
  144. *
  145. * For US shopping carts, a link will be created. If the <pre>$img</pre>
  146. * parameter is set to "true", the {@tutorial amazoop.configure.pkg#buynow more Buy Now image}
  147. * will be used as a link to the purchase URL, otherwise the <pre>$text</pre> parameter will
  148. * be used as the text for the link. Parameters <pre>$linkAttributes</pre> and <pre>$imageAttributes</pre>
  149. * can be used to specify additional attributes for the anchor and image tags. If no border is
  150. * specified in the <pre>$imageAttributes</pre>, amazOOP will set the image's border to 0.
  151. *
  152. * For all other shopping carts, the purchase URL is not available, therefore an "Add to Cart" form
  153. * is created. In this case, if <pre>$img</pre> is set to "true", the
  154. * {@tutorial amazoop.configure.pkg#buynow more Buy Now image} will be used as the submit button,
  155. * otherwise a submit button will be created with the text from the first parameter.
  156. * <pre>$linkAttributes</pre> and <pre>$imageAttributes</pre> are not used for these carts.
  157. *
  158. * @param string $text is the text that should be used in the link or button. If a blank
  159. * string is submitted, the default value is used.
  160. *
  161. * @param boolean $img if set to true, instead of a regular link or button an image is used.
  162. * To learn how to define the image you want to use, read the tutorial about
  163. * {@tutorial amazoop.configure.pkg Configuring amazOOP}. If this is set
  164. * to true, the <pre>$text</pre> parameter is used as the ALT text for the image.
  165. *
  166. * @param string $linkAttributes In US shopping carts, any additional attributes passed through
  167. * this parameter, will be added to the anchor tag of the purchase URL.
  168. *
  169. * @param string $imageAttributes In US shopping carts, any additional attributes passed through
  170. * this parameter will be added to the image tag.
  171. *
  172. * @return string For US shopping carts, a link to the purchase URL. For all other carts, an "Add
  173. * to Cart" form for all products in the shopping cart.
  174. *
  175. */
  176. function buyNow($text = "", $img = false, $linkAttributes = "", $imageAttributes = ""){
  177. global $_AMAZOOP_ASSOC_IDS, $_AMAZOOP_BUYNOW_BUTTONS;
  178.  
  179. $res = "";
  180.  
  181. if($item = $this->getItems()){
  182. $sites = array( "us" => "com",
  183. "uk" => "co.uk",
  184. "de" => "de",
  185. "fr" => "fr",
  186. "jp" => "co.jp");
  187.  
  188. $locale = strtolower($this->requestArgument("locale"));
  189.  
  190. $newWin = (getAmazoopConfig("open_links_in") == "new");
  191.  
  192. if(getAmazoopConfig("close_tag_character") == "xhtml")
  193. $closeTag = " />";
  194. else
  195. $closeTag = ">";
  196.  
  197. if($text == ""){
  198. if($locale == "us")
  199. $text = "buy now from Amazon.com";
  200. else if($locale == "uk")
  201. $text = "buy now from Amazon.co.uk";
  202. else if($locale == "de")
  203. $text = "Jetzt bei Amazon.de kaufen";
  204. else if($locale == "jp")
  205. //$text = "Amazon.co.jpからのこれを?。買いなさい";
  206. $text = "Buy now from Amazon.co.jp";
  207. }
  208.  
  209. if($locale == "us"){
  210. if($linkAttributes != "")
  211. $linkAttributes = " " . $linkAttributes;
  212. $res = "<a href=\"" . rawurldecode($this->purchaseURL) . "\"";
  213. if($newWin)
  214. $res .= " target=\"_blank\"";
  215. $res .= $linkAttributes . ">\n";
  216. if(!$img)
  217. $res .= $text;
  218. else{
  219. if(strpos($imageAttributes, "border") === false)
  220. $imageAttributes = " border=\"0\" " . $imageAttributes;
  221. elseif($imageAttributes != "")
  222. $imageAttributes = " " . $imageAttributes;
  223.  
  224. $res .= "<img src=\"" . $_AMAZOOP_BUYNOW_BUTTONS[$locale]['url'] . "\" width=\"" . $_AMAZOOP_BUYNOW_BUTTONS[$locale]['width'] . "\" height=\"" . $_AMAZOOP_BUYNOW_BUTTONS[$locale]['height'] . "\" alt=\"" . $text . "\"" . $imageAttributes . $closeTag;
  225. }
  226. $res .= "</a>\n";
  227. }
  228. else{
  229. $res = "<form method=\"post\"";
  230. if($newWin)
  231. $res .= " target=\"_blank\"";
  232. $res .= " action=\"http://www.amazon." . $sites[$locale] . "/exec/obidos/dt/assoc/handle-buy-box=" . $item->getASIN() . "\">\n";
  233. for($i = 1; $item = $this->getItems($i); $i++){
  234. $res .= "<input type=\"hidden\" name=\"asin.". $item->getASIN() . "\" value=\"1\"" . $closeTag . "\n";
  235. }
  236. $res .= "<input type=\"hidden\" name=\"tag-value\" value=\"" . $_AMAZOOP_ASSOC_IDS[$locale] . "\"" . $closeTag . "\n";
  237. $res .= "<input type=\"hidden\" name=\"tag_value\" value=\"" . $_AMAZOOP_ASSOC_IDS[$locale] . "\"" . $closeTag . "\n";
  238. $res .= "<input type=\"hidden\" name=\"dev-tag-value\" value=\"" . _AMAZOOP_DEV_TOKEN . "\"" . $closeTag . "\n";
  239.  
  240. if(!$img)
  241. $res .= "<input type=\"submit\" name=\"submit.add-to-cart\" value=\"$text\"" . $closeTag . "\n";
  242. else{
  243. $res .= "<input type=\"image\" name=\"submit.add-to-cart\" src=\"" . $_AMAZOOP_BUYNOW_BUTTONS[$locale]['url'] . "\" width=\"" . $_AMAZOOP_BUYNOW_BUTTONS[$locale]['width'] . "\" height=\"" . $_AMAZOOP_BUYNOW_BUTTONS[$locale]['height'] . "\" border=\"0\" alt=\"" . $text . "\"" . $closeTag . "";
  244. }
  245. $res .= "</form>\n";
  246. }
  247. }
  248.  
  249. return $res;
  250. }
  251. }
  252.  
  253. /**
  254. * Objects contained in a {@link ShoppingCart} are of made from this class.
  255. *
  256. * Each {@link CartItem} contains information about the items in a cart, such
  257. * as the product name, ASIN, quantity, price, and and item ID. When changing
  258. * quantities or removing an item from a shopping cart, it should be done by
  259. * changing the quantity or removing a specific item ID and not an ASIN.
  260. *
  261. * @since version 0.3RC2
  262. *
  263. * @author Mauricio Diaz <madd0@users.sourceforge.net>
  264. * @copyright 2004 (c) Mauricio Diaz Orlich
  265. * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
  266. * @version 0.3RC2
  267. *
  268. * @package amazOOP
  269. */
  270. class CartItem extends AmazonItem {
  271. /**#@+
  272. * @access private
  273. */
  274. /**
  275. * The ID of a specific item in a specific cart.
  276. * @var string
  277. */
  278. var $itemId;
  279. /**
  280. * The quantity of the item.
  281. * @var integer
  282. */
  283. var $qty;
  284. /**#@-*/
  285.  
  286. /**
  287. * Decodes the ID of an item.
  288. *
  289. * @return string The Item's ID.
  290. */
  291. function getItemID(){
  292. return rawurldecode($itemId);
  293. }
  294.  
  295. /**
  296. * The quantity of the item in the shopping cart.
  297. *
  298. * @return integer The number of items in the shopping cart.
  299. */
  300. function getQuantity(){
  301. return rawurldecode($qty);
  302. }
  303.  
  304. }
 
Documentation generated on Sat, 21 Aug 2004 17:40:24 +0200 by phpDocumentor 1.3.0RC3
hosted by
SourceForge.net Logo