00001 <?php 00002 /** 00003 * Magento 00004 * 00005 * NOTICE OF LICENSE 00006 * 00007 * This source file is subject to the Open Software License (OSL 3.0) 00008 * that is bundled with this package in the file LICENSE.txt. 00009 * It is also available through the world-wide-web at this URL: 00010 * http://opensource.org/licenses/osl-3.0.php 00011 * If you did not receive a copy of the license and are unable to 00012 * obtain it through the world-wide-web, please send an email 00013 * to license@magentocommerce.com so we can send you a copy immediately. 00014 * 00015 * DISCLAIMER 00016 * 00017 * Do not edit or add to this file if you wish to upgrade Magento to newer 00018 * versions in the future. If you wish to customize Magento for your 00019 * needs please refer to http://www.magentocommerce.com for more information. 00020 * 00021 * @category Mage 00022 * @package Mage_Catalog 00023 * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com) 00024 * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 00025 */ 00026 00027 00028 /** 00029 * Catalog product link model 00030 * 00031 * @category Mage 00032 * @package Mage_Catalog 00033 * @author Magento Core Team <core@magentocommerce.com> 00034 */ 00035 class Mage_Catalog_Model_Product_Link extends Mage_Core_Model_Abstract 00036 { 00037 const LINK_TYPE_RELATED = 1; 00038 const LINK_TYPE_GROUPED = 3; 00039 const LINK_TYPE_UPSELL = 4; 00040 const LINK_TYPE_CROSSSELL = 5; 00041 00042 protected $_attributeCollection = null; 00043 00044 /** 00045 * Initialize resource 00046 */ 00047 protected function _construct() 00048 { 00049 $this->_init('catalog/product_link'); 00050 } 00051 00052 public function useRelatedLinks() 00053 { 00054 $this->setLinkTypeId(self::LINK_TYPE_RELATED); 00055 return $this; 00056 } 00057 00058 public function useGroupedLinks() 00059 { 00060 $this->setLinkTypeId(self::LINK_TYPE_GROUPED); 00061 return $this; 00062 } 00063 00064 public function useUpSellLinks() 00065 { 00066 $this->setLinkTypeId(self::LINK_TYPE_UPSELL); 00067 return $this; 00068 } 00069 00070 /** 00071 * @return Mage_Catalog_Model_Product_Link 00072 */ 00073 public function useCrossSellLinks() 00074 { 00075 $this->setLinkTypeId(self::LINK_TYPE_CROSSSELL); 00076 return $this; 00077 } 00078 00079 /** 00080 * Retrieve table name for attribute type 00081 * 00082 * @param string $type 00083 * @return string 00084 */ 00085 public function getAttributeTypeTable($type) 00086 { 00087 return $this->_getResource()->getAttributeTypeTable($type); 00088 } 00089 00090 /** 00091 * Retrieve linked product collection 00092 */ 00093 public function getProductCollection() 00094 { 00095 $collection = Mage::getResourceModel('catalog/product_link_product_collection') 00096 ->setLinkModel($this); 00097 return $collection; 00098 } 00099 00100 /** 00101 * Retrieve link collection 00102 */ 00103 public function getLinkCollection() 00104 { 00105 $collection = Mage::getResourceModel('catalog/product_link_collection') 00106 ->setLinkModel($this); 00107 return $collection; 00108 } 00109 00110 public function getAttributes($type=null) 00111 { 00112 if (is_null($type)) { 00113 $type = $this->getLinkTypeId(); 00114 } 00115 return $this->_getResource()->getAttributesByType($type); 00116 } 00117 00118 /** 00119 * Save data for product relations 00120 * 00121 * @param Mage_Catalog_Model_Product $product 00122 * @return Mage_Catalog_Model_Product_Link 00123 */ 00124 public function saveProductRelations($product) 00125 { 00126 $data = $product->getRelatedLinkData(); 00127 if (!is_null($data)) { 00128 $this->_getResource()->saveProductLinks($product, $data, self::LINK_TYPE_RELATED); 00129 } 00130 $data = $product->getUpSellLinkData(); 00131 if (!is_null($data)) { 00132 $this->_getResource()->saveProductLinks($product, $data, self::LINK_TYPE_UPSELL); 00133 } 00134 $data = $product->getCrossSellLinkData(); 00135 if (!is_null($data)) { 00136 $this->_getResource()->saveProductLinks($product, $data, self::LINK_TYPE_CROSSSELL); 00137 } 00138 return $this; 00139 } 00140 00141 public function saveGroupedLinks($product) 00142 { 00143 $data = $product->getGroupedLinkData(); 00144 if (!is_null($data)) { 00145 $this->_getResource()->saveProductLinks($product, $data, self::LINK_TYPE_GROUPED); 00146 } 00147 return $this; 00148 } 00149 }