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_Tag_Model_Mysql4_Customer_Collection extends Mage_Customer_Model_Entity_Customer_Collection
00036 {
00037 protected $_allowDisableGrouping = true;
00038 protected $_countAttribute = 'tr.tag_relation_id';
00039 protected $_joinFlags = array();
00040
00041 public function _initSelect()
00042 {
00043 parent::_initSelect();
00044 $this->_joinFields();
00045 $this->_setIdFieldName('tag_relation_id');
00046 return $this;
00047 }
00048
00049 public function setJoinFlag($table)
00050 {
00051 $this->_joinFlags[$table] = true;
00052 return $this;
00053 }
00054
00055 public function getJoinFlag($table)
00056 {
00057 return isset($this->_joinFlags[$table]);
00058 }
00059
00060 public function unsetJoinFlag($table=null)
00061 {
00062 if (is_null($table)) {
00063 $this->_joinFlags = array();
00064 } elseif ($this->getJoinFlag($table)) {
00065 unset($this->_joinFlags[$table]);
00066 }
00067
00068 return $this;
00069 }
00070
00071 public function addTagFilter($tagId)
00072 {
00073 $this->getSelect()
00074 ->where('tr.tag_id = ?', $tagId);
00075 return $this;
00076 }
00077
00078 public function addProductFilter($productId)
00079 {
00080 $this->getSelect()
00081 ->where('tr.product_id = ?', $productId);
00082 return $this;
00083 }
00084
00085 public function addStatusFilter($status)
00086 {
00087 $this->getSelect()
00088 ->where('t.status = ?', $status);
00089 return $this;
00090 }
00091
00092 public function addDescOrder()
00093 {
00094 $this->getSelect()
00095 ->order('tr.tag_relation_id desc');
00096 return $this;
00097 }
00098
00099 public function addGroupByTag()
00100 {
00101 $this->getSelect()
00102 ->group('tr.tag_relation_id');
00103
00104 $this->_allowDisableGrouping = false;
00105 return $this;
00106 }
00107
00108 public function addGroupByCustomer()
00109 {
00110 $this->getSelect()
00111 ->group('tr.customer_id');
00112
00113 $this->_allowDisableGrouping = false;
00114 return $this;
00115 }
00116
00117 public function addGroupByCustomerProduct()
00118 {
00119
00120 $this->_allowDisableGrouping = false;
00121 return $this;
00122 }
00123
00124 public function addCustomerFilter($customerId)
00125 {
00126 $this->getSelect()->where('tr.customer_id = ?', $customerId);
00127 return $this;
00128 }
00129
00130 protected function _joinFields()
00131 {
00132 $tagRelationTable = $this->getTable('tag/relation');
00133 $tagTable = $this->getTable('tag/tag');
00134
00135
00136 $this->addAttributeToSelect('firstname')
00137 ->addAttributeToSelect('lastname')
00138 ->addAttributeToSelect('email');
00139
00140 $this->getSelect()
00141 ->join(array('tr' => $tagRelationTable), 'tr.customer_id = e.entity_id')
00142 ->join(array('t' => $tagTable), 't.tag_id = tr.tag_id');
00143 }
00144
00145 public function getSelectCountSql()
00146 {
00147 $countSelect = clone $this->getSelect();
00148 $countSelect->reset(Zend_Db_Select::ORDER);
00149 $countSelect->reset(Zend_Db_Select::LIMIT_COUNT);
00150 $countSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
00151
00152 if( $this->_allowDisableGrouping ) {
00153 $countSelect->reset(Zend_Db_Select::GROUP);
00154 }
00155
00156 $sql = $countSelect->__toString();
00157 $sql = preg_replace('/^select\s+.+?\s+from\s+/is', "select count({$this->getCountAttribute()}) from ", $sql);
00158 return $sql;
00159 }
00160
00161 public function addProductName()
00162 {
00163 $productsId = array();
00164 $productsData = array();
00165
00166 foreach ($this->getItems() as $item)
00167 {
00168 $productsId[] = $item->getProductId();
00169 }
00170
00171 $productsId = array_unique($productsId);
00172
00173
00174 if( sizeof($productsId) == 0 ) {
00175 return;
00176 }
00177
00178 $collection = Mage::getModel('catalog/product')->getCollection()
00179 ->addAttributeToSelect('name')
00180 ->addAttributeToSelect('sku')
00181 ->addIdFilter($productsId);
00182
00183 $collection->load();
00184
00185 foreach ($collection->getItems() as $item)
00186 {
00187 $productsData[$item->getId()] = $item->getName();
00188 $productsSku[$item->getId()] = $item->getSku();
00189 }
00190
00191 foreach ($this->getItems() as $item)
00192 {
00193 $item->setProduct($productsData[$item->getProductId()]);
00194 $item->setProductSku($productsSku[$item->getProductId()]);
00195 }
00196 return $this;
00197 }
00198
00199 public function setOrder($attribute, $dir='desc')
00200 {
00201 switch( $attribute ) {
00202 case 'name':
00203 case 'status':
00204 $this->getSelect()->order($attribute . ' ' . $dir);
00205 break;
00206
00207 default:
00208 parent::setOrder($attribute, $dir);
00209 }
00210 return $this;
00211 }
00212
00213 public function setCountAttribute($value)
00214 {
00215 $this->_countAttribute = $value;
00216 return $this;
00217 }
00218
00219 public function getCountAttribute()
00220 {
00221 return $this->_countAttribute;
00222 }
00223
00224 public function addFieldToFilter($attribute, $condition=null){
00225 if ($attribute == 'name') {
00226 $this->getSelect()->where($this->_getConditionSql('t.name', $condition));
00227 return $this;
00228 } else {
00229 return parent::addFieldToFilter($attribute, $condition);
00230 }
00231 }
00232 }