1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Log_Model_Resource_Aggregation extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('log/summary_table', 'log_summary_id');
44: }
45:
46: 47: 48: 49: 50:
51: public function getLastRecordDate()
52: {
53: $adapter = $this->_getReadAdapter();
54: $select = $adapter->select()
55: ->from($this->getTable('log/summary_table'),
56: array($adapter->quoteIdentifier('date')=>'MAX(add_date)'));
57:
58: return $adapter->fetchOne($select);
59: }
60:
61: 62: 63: 64: 65: 66: 67: 68:
69: public function getCounts($from, $to, $store)
70: {
71: $adapter = $this->_getReadAdapter();
72: $result = array('customers'=>0, 'visitors'=>0);
73: $select = $adapter->select()
74: ->from($this->getTable('log/customer'), 'visitor_id')
75: ->where('login_at >= ?', $from)
76: ->where('login_at <= ?', $to);
77: if ($store) {
78: $select->where('store_id = ?', $store);
79: }
80:
81: $customers = $adapter->fetchCol($select);
82: $result['customers'] = count($customers);
83:
84:
85: $select = $adapter->select();
86: $select->from($this->getTable('log/visitor'), 'COUNT(*)')
87: ->where('first_visit_at >= ?', $from)
88: ->where('first_visit_at <= ?', $to);
89:
90: if ($store) {
91: $select->where('store_id = ?', $store);
92: }
93: if ($result['customers']) {
94: $select->where('visitor_id NOT IN(?)', $customers);
95: }
96:
97: $result['visitors'] = $adapter->fetchOne($select);
98:
99:
100: return $result;
101: }
102:
103: 104: 105: 106: 107: 108:
109: public function saveLog($data, $id = null)
110: {
111: $adapter = $this->_getWriteAdapter();
112: if (is_null($id)) {
113: $adapter->insert($this->getTable('log/summary_table'), $data);
114: } else {
115: $condition = $adapter->quoteInto('summary_id = ?', $id);
116: $adapter->update($this->getTable('log/summary_table'), $data, $condition);
117: }
118: }
119:
120: 121: 122: 123: 124:
125: public function removeEmpty($date)
126: {
127: $adapter = $this->_getWriteAdapter();
128: $condition = array(
129: 'add_date < ?' => $date,
130: 'customer_count = 0',
131: 'visitor_count = 0'
132: );
133: $adapter->delete($this->getTable('log/summary_table'), $condition);
134: }
135:
136: 137: 138: 139: 140: 141: 142:
143: public function getLogId($from, $to)
144: {
145: $adapter = $this->_getReadAdapter();
146: $select = $adapter->select()
147: ->from($this->getTable('log/summary_table'), 'summary_id')
148: ->where('add_date >= ?', $from)
149: ->where('add_date <= ?', $to);
150:
151: return $adapter->fetchOne($select);
152: }
153: }
154: