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_Tag extends Mage_Core_Model_Mysql4_Abstract
00036 {
00037 protected function _construct()
00038 {
00039 $this->_init('tag/tag', 'tag_id');
00040 }
00041
00042
00043
00044
00045
00046
00047 protected function _initUniqueFields()
00048 {
00049 $this->_uniqueFields = array(array(
00050 'field' => 'name',
00051 'title' => Mage::helper('tag')->__('Tag')
00052 ));
00053 return $this;
00054 }
00055
00056 public function loadByName($model, $name)
00057 {
00058 if( $name ) {
00059 $read = $this->_getReadAdapter();
00060 $select = $read->select();
00061 if (Mage::helper('core/string')->strlen($name) > 255) {
00062 $name = Mage::helper('core/string')->substr($name, 0, 255);
00063 }
00064
00065 $select->from($this->getMainTable())
00066 ->where('name = ?', $name);
00067 $data = $read->fetchRow($select);
00068
00069 $model->setData( ( is_array($data) ) ? $data : array() );
00070 } else {
00071 return false;
00072 }
00073 }
00074
00075 protected function _beforeSave(Mage_Core_Model_Abstract $object)
00076 {
00077 if (!$object->getId() && $object->getStatus()==$object->getApprovedStatus()) {
00078 $searchTag = new Varien_Object();
00079 $this->loadByName($searchTag, $object->getName());
00080 if($searchTag->getData($this->getIdFieldName()) && $searchTag->getStatus()==$object->getPendingStatus()) {
00081 $object->setId($searchTag->getData($this->getIdFieldName()));
00082 }
00083 }
00084
00085 if (Mage::helper('core/string')->strlen($object->getName()) > 255) {
00086 $object->setName(Mage::helper('core/string')->substr($object->getName(), 0, 255));
00087 }
00088
00089 return parent::_beforeSave($object);
00090 }
00091
00092 public function aggregate($object)
00093 {
00094 $selectLocal = $this->_getReadAdapter()->select()
00095 ->from(
00096 array('main' => $this->getTable('relation')),
00097 array(
00098 'customers'=>'COUNT(DISTINCT main.customer_id)',
00099 'products'=>'COUNT(DISTINCT main.product_id)',
00100 'store_id',
00101 'uses'=>'COUNT(main.tag_relation_id)'
00102 )
00103 )
00104 ->join(array('store' => $this->getTable('core/store')),
00105 'store.store_id=main.store_id AND store.store_id>0',
00106 array()
00107 )
00108 ->join(array('product_website' => $this->getTable('catalog/product_website')),
00109 'product_website.website_id=store.website_id AND product_website.product_id=main.product_id',
00110 array()
00111 )
00112 ->where('main.tag_id = ?', $object->getId())
00113 ->where('main.active')
00114 ->group('main.store_id');
00115
00116 $selectGlobal = $this->_getReadAdapter()->select()
00117 ->from(
00118 array('main'=>$this->getTable('relation')),
00119 array(
00120 'customers'=>'COUNT(DISTINCT main.customer_id)',
00121 'products'=>'COUNT(DISTINCT main.product_id)',
00122 'store_id'=>'( 0 )' ,
00123 'uses'=>'COUNT(main.tag_relation_id)'
00124 )
00125 )
00126 ->join(array('store' => $this->getTable('core/store')),
00127 'store.store_id=main.store_id AND store.store_id>0',
00128 array()
00129 )
00130 ->join(array('product_website' => $this->getTable('catalog/product_website')),
00131 'product_website.website_id=store.website_id AND product_website.product_id=main.product_id',
00132 array()
00133 )
00134 ->where('main.tag_id = ?', $object->getId())
00135 ->where('main.active');
00136
00137 $selectHistorical = $this->_getReadAdapter()->select()
00138 ->from(
00139 array('main'=>$this->getTable('relation')),
00140 array('historical_uses'=>'COUNT(main.tag_relation_id)',
00141 'store_id')
00142 )
00143 ->join(array('store' => $this->getTable('core/store')),
00144 'store.store_id=main.store_id AND store.store_id>0',
00145 array()
00146 )
00147 ->join(array('product_website' => $this->getTable('catalog/product_website')),
00148 'product_website.website_id=store.website_id AND product_website.product_id=main.product_id',
00149 array()
00150 )
00151 ->group('main.store_id')
00152 ->where('main.tag_id = ?', $object->getId());
00153
00154 $selectHistoricalGlobal = $this->_getReadAdapter()->select()
00155 ->from(
00156 array('main'=>$this->getTable('relation')),
00157 array('historical_uses'=>'COUNT(main.tag_relation_id)')
00158 )
00159 ->join(array('store' => $this->getTable('core/store')),
00160 'store.store_id=main.store_id AND store.store_id>0',
00161 array()
00162 )
00163 ->join(array('product_website' => $this->getTable('catalog/product_website')),
00164 'product_website.website_id=store.website_id AND product_website.product_id=main.product_id',
00165 array()
00166 )
00167 ->where('main.tag_id = ?', $object->getId());
00168
00169 $historicalAll = $this->_getReadAdapter()->fetchAll($selectHistorical);
00170 $historicalCache = array();
00171 foreach ($historicalAll as $historical) {
00172 $historicalCache[$historical['store_id']] = $historical['historical_uses'];
00173 }
00174
00175 $summaries = $this->_getReadAdapter()->fetchAll($selectLocal);
00176 if ($row = $this->_getReadAdapter()->fetchRow($selectGlobal)) {
00177 $historical = $this->_getReadAdapter()->fetchOne($selectHistoricalGlobal);
00178
00179 if($historical) {
00180 $row['historical_uses'] = $historical;
00181 }
00182
00183 $summaries[] = $row;
00184 }
00185
00186 $this->_getReadAdapter()->delete($this->getTable('summary'), $this->_getReadAdapter()->quoteInto('tag_id = ?', $object->getId()));
00187
00188 foreach ($summaries as $summary) {
00189 if(!isset($summary['historical_uses'])) {
00190 $summary['historical_uses'] = isset($historicalCache[$summary['store_id']]) ? $historicalCache[$summary['store_id']] : 0;
00191 }
00192 $summary['tag_id'] = $object->getId();
00193 $summary['popularity'] = $summary['historical_uses'];
00194 if (is_null($summary['uses'])) {
00195 $summary['uses'] = 0;
00196 }
00197
00198 $this->_getReadAdapter()->insert($this->getTable('summary'), $summary);
00199 }
00200
00201 return $object;
00202 }
00203
00204 public function addSummary($object)
00205 {
00206 $select = $this->_getReadAdapter()->select()
00207 ->from($this->getTable('summary'))
00208 ->where('tag_id = ?', $object->getId())
00209 ->where('store_id = ?', $object->getStoreId());
00210
00211 $row = $this->_getReadAdapter()->fetchAll($select);
00212
00213 $object->addData($row);
00214 return $object;
00215 }
00216 }