00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Link_Product_Collection
00036 extends Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
00037 {
00038 protected $_product;
00039 protected $_linkModel;
00040 protected $_linkTypeId;
00041 protected $_isStrongMode;
00042 protected $_hasLinkFilter = false;
00043
00044
00045
00046
00047
00048
00049
00050 public function setLinkModel($linkModel)
00051 {
00052 $this->_linkModel = $linkModel;
00053 if ($linkModel->getLinkTypeId()) {
00054 $this->_linkTypeId = $linkModel->getLinkTypeId();
00055 }
00056 return $this;
00057 }
00058
00059 public function setIsStrongMode()
00060 {
00061 $this->_isStrongMode = true;
00062 return $this;
00063 }
00064
00065
00066
00067
00068
00069
00070 public function getLinkModel()
00071 {
00072 return $this->_linkModel;
00073 }
00074
00075
00076
00077
00078
00079
00080
00081 public function setProduct($product)
00082 {
00083 $this->_product = $product;
00084 return $this;
00085 }
00086
00087
00088
00089
00090
00091
00092 public function getProduct()
00093 {
00094 return $this->_product;
00095 }
00096
00097 public function addExcludeProductFilter($products)
00098 {
00099 if (is_array($products) && !empty($products)) {
00100 $this->_hasLinkFilter = true;
00101 $this->getSelect()->where('links.linked_product_id NOT IN (?)', $products);
00102 }
00103 return $this;
00104 }
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 public function addAttributeToSort($attribute, $dir='asc')
00115 {
00116
00117
00118
00119 if ($attribute == 'position') {
00120
00121
00122 if (!is_object($this->getProduct())) {
00123 return $this;
00124 }
00125 if (!$this->getProduct()->getId()) {
00126 return $this;
00127 }
00128
00129 $this->getSelect()->order($attribute.' '.$dir);
00130 }
00131 else {
00132 parent::addAttributeToSort($attribute, $dir);
00133 }
00134 return $this;
00135 }
00136
00137 public function addProductFilter($products)
00138 {
00139 $this->_hasLinkFilter = true;
00140 if (is_array($products) && !empty($products)) {
00141 $this->getSelect()->where('links.product_id IN (?)', $products);
00142 }
00143 elseif (is_string($products) || is_numeric($products)) {
00144 $this->getSelect()->where('links.product_id=?', $products);
00145 }
00146 return $this;
00147 }
00148
00149 public function setRandomOrder()
00150 {
00151 $this->getSelect()->order(new Zend_Db_Expr('RAND()'));
00152 return $this;
00153 }
00154
00155
00156
00157
00158
00159
00160
00161 public function setGroupBy($groupBy = 'e.entity_id')
00162 {
00163 $this->getSelect()->group($groupBy);
00164 return $this;
00165 }
00166
00167 protected function _beforeLoad()
00168 {
00169 if ($this->getLinkModel()) {
00170 $this->_joinLinks();
00171 }
00172 return parent::_beforeLoad();
00173 }
00174
00175 protected function _joinLinks()
00176 {
00177 $joinCondition = 'links.linked_product_id=e.entity_id AND links.link_type_id='.$this->_linkTypeId;
00178
00179 if ($this->getProduct() && $this->getProduct()->getId()) {
00180 if ($this->_isStrongMode) {
00181 $joinType = 'join';
00182 $this->getSelect()->where('links.product_id=?', $this->getProduct()->getId());
00183 }
00184 else {
00185 $joinType = 'joinLeft';
00186 $joinCondition.= ' AND links.product_id='. $this->getProduct()->getId();
00187 }
00188 $this->getSelect()->where('e.entity_id!=?', $this->getProduct()->getId());
00189
00190 $this->getSelect()->$joinType(
00191 array('links'=>$this->getTable('catalog/product_link')),
00192 $joinCondition,
00193 array('link_id')
00194 );
00195
00196 $attributes = $this->getLinkModel()->getAttributes();
00197 $attributesByType = array();
00198 foreach ($attributes as $attribute) {
00199 $table = $this->getLinkModel()->getAttributeTypeTable($attribute['type']);
00200 $alias = 'link_attribute_'.$attribute['code'].'_'.$attribute['type'];
00201 $this->getSelect()->joinLeft(
00202 array($alias => $table),
00203 $alias.'.link_id=links.link_id AND '.$alias.'.product_link_attribute_id='.$attribute['id'],
00204 array($attribute['code'] => 'value')
00205 );
00206 }
00207 }
00208 else {
00209 if ($this->_isStrongMode) {
00210 $this->getSelect()->where('e.entity_id=-1');
00211 }
00212 elseif($this->_hasLinkFilter) {
00213 $this->getSelect()->join(
00214 array('links'=>$this->getTable('catalog/product_link')),
00215 'links.linked_product_id=e.entity_id AND links.link_type_id='.$this->_linkTypeId,
00216 array('link_id')
00217 );
00218 }
00219 }
00220 return $this;
00221 }
00222
00223 public function setPositionOrder($dir='asc')
00224 {
00225 if ($this->getProduct() && $this->getProduct()->getId()) {
00226 $this->setOrder('position', $dir);
00227 }
00228 return $this;
00229 }
00230 }