- <?php
- /**
- * This file contains the {@link ShoppingCart} class. As well as the {@link CartItem}
- * class which stores information on items contained in a ShoppingCart.
- *
- * @author Mauricio Diaz <madd0@users.sourceforge.net>
- * @copyright 2004 (c) Mauricio Diaz Orlich
- * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
- * @version 0.3
- *
- * @package amazOOP
- */
-
- /**
- * For security, only this file should be included in your scripts.
- * @access private
- */
- defined('_AMAZOOP') or die( 'Direct Access to this location is not allowed.' );
-
- /**
- * This is one of the classes returned when querying AWS.
- *
- * This class is returned when you create or modify a {@link ShoppingCart}.
- *
- * @since version 0.3
- *
- * @author Mauricio Diaz <madd0@users.sourceforge.net>
- * @copyright 2004 (c) Mauricio Diaz Orlich
- * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
- * @version 0.3RC2
- *
- * @package amazOOP
- */
- class ShoppingCart extends AmazonResponse {
-
- /**#@+
- * @access private
- */
-
- /**
- * The Cart ID is a unique identifier assigned to the Cart.
- * @var string
- */
- var $cartID;
- /**
- * The HMAC code is used as a security feature;
- * @var string
- */
- var $HMAC;
- /**
- * The Purchase URL points to the user's shopping basket at Amazon, where
- * he can directly check out.
- *
- * Once the purchase URL is accessed, the Cart's ID is no longer valid.
- *
- * Currently only locale US returns purchase URL.
- * @var string
- */
- var $purchaseURL;
- /**
- * This is an array of {@link CartItem}s stored in the Shopping Cart
- * @var array
- */
- var $items;
- /**
- * This is an array of ASINs of product's similar to those in the Cart.
- * @var array
- */
- var $similarProducts;
-
- /**#@-*/
-
- /**
- * Default constructor.
- *
- * Prepares the class.
- */
- function ShoppingCart(){
- $this->productInfo = false;
- $this->blendedSearch = false;
- $this->shoppingCart = true;
- }
-
- function getCartID($decoded = false){
- if($decoded)
- return rawurldecode($this->cartID);
- else
- return $this->cartID;
- }
-
- function getHMAC($decoded = false){
- if($decoded)
- return rawurldecode($this->HMAC);
- else
- return $this->HMAC;
- }
-
- function getItems($number = 0){
- if(isset($this->items) && sizeof($this->items) != 0 && $number <= sizeof($this->items)){
- if($number == 0){
- $number = rand(1, sizeof($this->items));
- return $this->items[$number-1];
- }
- else{
- if(isset($this->items[$number-1]))
- return $this->items[$number-1];
- else
- return false;
- }
- }
- else
- return false;
- }
-
- /**
- * Gets ASINs of other products sold at Amazon that are similar to the current product.
- *
- * @param integer $product the ASIN you want to get. If set to 0 (default),
- * a random ASIN from the list is returned.
- *
- * @return string|booleanthe ASIN of the requested product from the list,
- * or false if the requested position of the list does not contain an ASIN.
- */
- function getSimilarProducts($product = 0){
- if(isset($this->similarProducts) && sizeof($this->similarProducts) != 0 && $product <= sizeof($this->similarProducts)){
- if($product == 0){
- $number = rand(1, sizeof($this->similarProducts));
- return rawurldecode($this->similarProducts[$number-1]);
- }
- else{
- if(isset($this->similarProducts[$product-1]))
- return rawurldecode($this->similarProducts[$product-1]);
- else
- return false;
- }
- }
- else
- return false;
- }
-
- /**
- * Will generate either a link or a form to add the products in the cart to the final
- * Amazon shopping cart.
- *
- * For US shopping carts, a link will be created. If the <pre>$img</pre>
- * parameter is set to "true", the {@tutorial amazoop.configure.pkg#buynow more Buy Now image}
- * will be used as a link to the purchase URL, otherwise the <pre>$text</pre> parameter will
- * be used as the text for the link. Parameters <pre>$linkAttributes</pre> and <pre>$imageAttributes</pre>
- * can be used to specify additional attributes for the anchor and image tags. If no border is
- * specified in the <pre>$imageAttributes</pre>, amazOOP will set the image's border to 0.
- *
- * For all other shopping carts, the purchase URL is not available, therefore an "Add to Cart" form
- * is created. In this case, if <pre>$img</pre> is set to "true", the
- * {@tutorial amazoop.configure.pkg#buynow more Buy Now image} will be used as the submit button,
- * otherwise a submit button will be created with the text from the first parameter.
- * <pre>$linkAttributes</pre> and <pre>$imageAttributes</pre> are not used for these carts.
- *
- * @param string $text is the text that should be used in the link or button. If a blank
- * string is submitted, the default value is used.
- *
- * @param boolean $img if set to true, instead of a regular link or button an image is used.
- * To learn how to define the image you want to use, read the tutorial about
- * {@tutorial amazoop.configure.pkg Configuring amazOOP}. If this is set
- * to true, the <pre>$text</pre> parameter is used as the ALT text for the image.
- *
- * @param string $linkAttributes In US shopping carts, any additional attributes passed through
- * this parameter, will be added to the anchor tag of the purchase URL.
- *
- * @param string $imageAttributes In US shopping carts, any additional attributes passed through
- * this parameter will be added to the image tag.
- *
- * @return string For US shopping carts, a link to the purchase URL. For all other carts, an "Add
- * to Cart" form for all products in the shopping cart.
- *
- */
- function buyNow($text = "", $img = false, $linkAttributes = "", $imageAttributes = ""){
- global $_AMAZOOP_ASSOC_IDS, $_AMAZOOP_BUYNOW_BUTTONS;
-
- $res = "";
-
- if($item = $this->getItems()){
- $sites = array( "us" => "com",
- "uk" => "co.uk",
- "de" => "de",
- "fr" => "fr",
- "jp" => "co.jp");
-
- $locale = strtolower($this->requestArgument("locale"));
-
- $newWin = (getAmazoopConfig("open_links_in") == "new");
-
- if(getAmazoopConfig("close_tag_character") == "xhtml")
- $closeTag = " />";
- else
- $closeTag = ">";
-
- if($text == ""){
- if($locale == "us")
- $text = "buy now from Amazon.com";
- else if($locale == "uk")
- $text = "buy now from Amazon.co.uk";
- else if($locale == "de")
- $text = "Jetzt bei Amazon.de kaufen";
- else if($locale == "jp")
- //$text = "Amazon.co.jpからのこれを?。買いなさい";
- $text = "Buy now from Amazon.co.jp";
- }
-
- if($locale == "us"){
- if($linkAttributes != "")
- $linkAttributes = " " . $linkAttributes;
- $res = "<a href=\"" . rawurldecode($this->purchaseURL) . "\"";
- if($newWin)
- $res .= " target=\"_blank\"";
- $res .= $linkAttributes . ">\n";
- if(!$img)
- $res .= $text;
- else{
- if(strpos($imageAttributes, "border") === false)
- $imageAttributes = " border=\"0\" " . $imageAttributes;
- elseif($imageAttributes != "")
- $imageAttributes = " " . $imageAttributes;
-
- $res .= "<img src=\"" . $_AMAZOOP_BUYNOW_BUTTONS[$locale]['url'] . "\" width=\"" . $_AMAZOOP_BUYNOW_BUTTONS[$locale]['width'] . "\" height=\"" . $_AMAZOOP_BUYNOW_BUTTONS[$locale]['height'] . "\" alt=\"" . $text . "\"" . $imageAttributes . $closeTag;
- }
- $res .= "</a>\n";
- }
- else{
- $res = "<form method=\"post\"";
- if($newWin)
- $res .= " target=\"_blank\"";
- $res .= " action=\"http://www.amazon." . $sites[$locale] . "/exec/obidos/dt/assoc/handle-buy-box=" . $item->getASIN() . "\">\n";
- for($i = 1; $item = $this->getItems($i); $i++){
- $res .= "<input type=\"hidden\" name=\"asin.". $item->getASIN() . "\" value=\"1\"" . $closeTag . "\n";
- }
- $res .= "<input type=\"hidden\" name=\"tag-value\" value=\"" . $_AMAZOOP_ASSOC_IDS[$locale] . "\"" . $closeTag . "\n";
- $res .= "<input type=\"hidden\" name=\"tag_value\" value=\"" . $_AMAZOOP_ASSOC_IDS[$locale] . "\"" . $closeTag . "\n";
- $res .= "<input type=\"hidden\" name=\"dev-tag-value\" value=\"" . _AMAZOOP_DEV_TOKEN . "\"" . $closeTag . "\n";
-
- if(!$img)
- $res .= "<input type=\"submit\" name=\"submit.add-to-cart\" value=\"$text\"" . $closeTag . "\n";
- else{
- $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 . "";
- }
- $res .= "</form>\n";
- }
- }
-
- return $res;
- }
- }
-
- /**
- * Objects contained in a {@link ShoppingCart} are of made from this class.
- *
- * Each {@link CartItem} contains information about the items in a cart, such
- * as the product name, ASIN, quantity, price, and and item ID. When changing
- * quantities or removing an item from a shopping cart, it should be done by
- * changing the quantity or removing a specific item ID and not an ASIN.
- *
- * @since version 0.3RC2
- *
- * @author Mauricio Diaz <madd0@users.sourceforge.net>
- * @copyright 2004 (c) Mauricio Diaz Orlich
- * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
- * @version 0.3RC2
- *
- * @package amazOOP
- */
- class CartItem extends AmazonItem {
- /**#@+
- * @access private
- */
- /**
- * The ID of a specific item in a specific cart.
- * @var string
- */
- var $itemId;
- /**
- * The quantity of the item.
- * @var integer
- */
- var $qty;
- /**#@-*/
-
- /**
- * Decodes the ID of an item.
- *
- * @return string The Item's ID.
- */
- function getItemID(){
- return rawurldecode($itemId);
- }
-
- /**
- * The quantity of the item in the shopping cart.
- *
- * @return integer The number of items in the shopping cart.
- */
- function getQuantity(){
- return rawurldecode($qty);
- }
-
- }