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_Entity_Customer_Collection extends Mage_Customer_Model_Entity_Customer_Collection
00036 {
00037 protected $_tagTable;
00038 protected $_tagRelTable;
00039
00040 public function __construct()
00041 {
00042 $resource = Mage::getSingleton('core/resource');
00043 parent::__construct();
00044 $this->_tagTable = $resource->getTableName('tag/tag');
00045 $this->_tagRelTable = $resource->getTableName('tag/tag_relation');
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 }
00057
00058 public function addTagFilter($tagId)
00059 {
00060 $this->joinField('tag_tag_id', $this->_tagRelTable, 'tag_id', 'customer_id=entity_id');
00061 $this->getSelect()->where($this->_getAttributeTableAlias('tag_tag_id') . '.tag_id=?', $tagId);
00062 return $this;
00063 }
00064
00065 public function addProductFilter($productId)
00066 {
00067 $this->joinField('tag_product_id', $this->_tagRelTable, 'product_id', 'customer_id=entity_id');
00068 $this->getSelect()->where($this->_getAttributeTableAlias('tag_product_id') . '.product_id=?', $productId);
00069 return $this;
00070 }
00071
00072 public function load($printQuery = false, $logQuery = false)
00073 {
00074 parent::load($printQuery, $logQuery);
00075 $this->_loadTags($printQuery, $logQuery);
00076 return $this;
00077 }
00078
00079 protected function _loadTags($printQuery = false, $logQuery = false)
00080 {
00081 if (empty($this->_items)) {
00082 return $this;
00083 }
00084 $customerIds = array();
00085 foreach ($this->getItems() as $item) {
00086 $customerIds[] = $item->getId();
00087 }
00088 $this->getSelect()->reset()
00089 ->from(array('tr' => $this->_tagRelTable), array('*','total_used' => 'count(tr.tag_relation_id)'))
00090 ->joinLeft(array('t' => $this->_tagTable),'t.tag_id=tr.tag_id')
00091 ->group(array('tr.customer_id', 't.tag_id'))
00092 ->where('tr.customer_id in (?)',$customerIds)
00093 ;
00094 $this->printLogQuery($printQuery, $logQuery);
00095
00096 $tags = array();
00097 $data = $this->_read->fetchAll($this->getSelect());
00098 foreach ($data as $row) {
00099 if (!isset($tags[ $row['customer_id'] ])) {
00100 $tags[ $row['customer_id'] ] = array();
00101 }
00102 $tags[ $row['customer_id'] ][] = $row;
00103 }
00104 foreach ($this->getItems() as $item) {
00105 if (isset($tags[$item->getId()])) {
00106 $item->setData('tags', $tags[$item->getId()]);
00107 }
00108 }
00109 return $this;
00110 }
00111
00112 }