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 class Mage_Reports_Model_Mysql4_Event extends Mage_Core_Model_Mysql4_Abstract
00035 {
00036
00037
00038
00039
00040 protected function _construct()
00041 {
00042 $this->_init('reports/event', 'event_id');
00043 }
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 public function updateCustomerType(Mage_Reports_Model_Event $model, $visitorId, $customerId, $types = array())
00055 {
00056 if ($types) {
00057 $this->_getWriteAdapter()->update($this->getMainTable(),
00058 array('subject_id' => $customerId, 'subtype' => 0),
00059 array(
00060 $this->_getWriteAdapter()->quoteInto('subject_id=?', $visitorId),
00061 $this->_getWriteAdapter()->quoteInto('subtype=?', 1),
00062 $this->_getWriteAdapter()->quoteInto('event_type_id IN(?)', $types)
00063 )
00064 );
00065 }
00066 return $this;
00067 }
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 public function applyLogToCollection(Varien_Data_Collection_Db $collection, $eventTypeId, $eventSubjectId, $subtype, $skipIds = array())
00081 {
00082 $idFieldName = $collection->getResource()->getIdFieldName();
00083
00084 $derivedSelect = $this->getReadConnection()->select()
00085 ->from($this->getTable('reports/event'), array('event_id' => new Zend_Db_Expr('MAX(event_id)'), 'object_id'))
00086 ->where('event_type_id=?', (int)$eventTypeId)
00087 ->where('subject_id=?', (int)$eventSubjectId)
00088 ->where('subtype=?', (int)$subtype)
00089 ->where('store_id IN(?)', $this->getCurrentStoreIds())
00090 ->group('object_id')
00091 ;
00092 if ($skipIds) {
00093 if (!is_array($skipIds)) {
00094 $skipIds = array((int)$skipIds);
00095 }
00096 $derivedSelect->where('object_id NOT IN(?)', $skipIds);
00097 }
00098
00099 $collection->getSelect()
00100 ->joinInner(array('evt' => new Zend_Db_Expr("({$derivedSelect})")), "`{$idFieldName}`=evt.object_id", array())
00101 ->order('evt.event_id DESC');
00102 }
00103
00104
00105
00106
00107
00108
00109
00110 public function getCurrentStoreIds(array $predefinedStoreIds = null)
00111 {
00112 $stores = array();
00113
00114 if (Mage::app()->getStore()->getId() == 0) {
00115 if (null !== $predefinedStoreIds) {
00116 $stores = $predefinedStoreIds;
00117 }
00118 else {
00119 foreach (Mage::app()->getStores() as $store) {
00120 $stores[] = $store->getId();
00121 }
00122 }
00123 }
00124
00125 else {
00126 switch (Mage::getStoreConfig('catalog/recently_products/scope')) {
00127 case 'website':
00128 $resourceStore = Mage::app()->getStore()->getWebsite()->getStores();
00129 break;
00130 case 'group':
00131 $resourceStore = Mage::app()->getStore()->getGroup()->getStores();
00132 break;
00133 default:
00134 $resourceStore = array(Mage::app()->getStore());
00135 break;
00136 }
00137
00138 foreach ($resourceStore as $store) {
00139 $stores[] = $store->getId();
00140 }
00141 }
00142 foreach ($stores as $key => $store) {
00143 $stores[$key] = (int)$store;
00144 }
00145 return $stores;
00146 }
00147
00148
00149
00150
00151
00152
00153
00154 public function clean(Mage_Reports_Model_Event $object)
00155 {
00156 while (true) {
00157 $select = $this->_getReadAdapter()->select()
00158 ->from(array('event_table' => $this->getMainTable()), array('event_id'))
00159 ->joinLeft(
00160 array('visitor_table' => $this->getTable('log/visitor')),
00161 'event_table.subject_id = visitor_table.visitor_id',
00162 array())
00163 ->where('visitor_table.visitor_id IS NULL')
00164 ->where('event_table.subtype=?', 1)
00165 ->limit(1000);
00166 $eventIds = $this->_getReadAdapter()->fetchCol($select);
00167
00168 if (!$eventIds) {
00169 break;
00170 }
00171
00172 $this->_getWriteAdapter()->delete(
00173 $this->getMainTable(),
00174 $this->_getWriteAdapter()->quoteInto('event_id IN(?)', $eventIds)
00175 );
00176 }
00177 return $this;
00178 }
00179 }