Overview

Packages

  • currencysymbol
  • MAbout
  • Mage
    • Admin
    • Adminhtml
    • AdminNotification
    • Api
    • Api2
    • Authorizenet
    • Backup
    • Bundle
    • Captcha
    • Catalog
    • CatalogIndex
    • CatalogInventory
    • CatalogRule
    • CatalogSearch
    • Centinel
    • Checkout
    • Cms
    • Compiler
    • Connect
    • Contacts
    • Core
    • Cron
    • CurrencySymbol
    • Customer
    • Dataflow
    • Directory
    • DirtectPost
    • Downloadable
    • Eav
    • GiftMessage
    • GoogleAnalytics
    • GoogleBase
    • GoogleCheckout
    • ImportExport
    • Index
    • Install
    • Log
    • Media
    • Newsletter
    • Oauth
    • Page
    • PageCache
    • Paygate
    • Payment
    • Paypal
    • PaypalUk
    • Persistent
    • Poll
    • ProductAlert
    • Rating
    • Reports
    • Review
    • Rss
    • Rule
    • Sales
    • SalesRule
    • Sedfriend
    • Sendfriend
    • Shipping
    • Sitemap
    • Tag
    • Tax
    • Usa
    • Weee
    • Widget
    • Wishlist
    • XmlConnect
  • None
  • Phoenix
    • Moneybookers
  • PHP
  • Zend
    • Date
    • Mime
    • XmlRpc

Classes

  • Mage_Log_Helper_Data
  • Mage_Log_Model_Aggregation
  • Mage_Log_Model_Cron
  • Mage_Log_Model_Customer
  • Mage_Log_Model_Log
  • Mage_Log_Model_Mysql4_Aggregation
  • Mage_Log_Model_Mysql4_Customer
  • Mage_Log_Model_Mysql4_Log
  • Mage_Log_Model_Mysql4_Visitor
  • Mage_Log_Model_Mysql4_Visitor_Collection
  • Mage_Log_Model_Mysql4_Visitor_Online
  • Mage_Log_Model_Mysql4_Visitor_Online_Collection
  • Mage_Log_Model_Resource_Aggregation
  • Mage_Log_Model_Resource_Customer
  • Mage_Log_Model_Resource_Log
  • Mage_Log_Model_Resource_Visitor
  • Mage_Log_Model_Resource_Visitor_Collection
  • Mage_Log_Model_Resource_Visitor_Online
  • Mage_Log_Model_Resource_Visitor_Online_Collection
  • Mage_Log_Model_Visitor
  • Mage_Log_Model_Visitor_Online
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Magento
  4:  *
  5:  * NOTICE OF LICENSE
  6:  *
  7:  * This source file is subject to the Open Software License (OSL 3.0)
  8:  * that is bundled with this package in the file LICENSE.txt.
  9:  * It is also available through the world-wide-web at this URL:
 10:  * http://opensource.org/licenses/osl-3.0.php
 11:  * If you did not receive a copy of the license and are unable to
 12:  * obtain it through the world-wide-web, please send an email
 13:  * to license@magentocommerce.com so we can send you a copy immediately.
 14:  *
 15:  * DISCLAIMER
 16:  *
 17:  * Do not edit or add to this file if you wish to upgrade Magento to newer
 18:  * versions in the future. If you wish to customize Magento for your
 19:  * needs please refer to http://www.magentocommerce.com for more information.
 20:  *
 21:  * @category    Mage
 22:  * @package     Mage_Log
 23:  * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 24:  * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 25:  */
 26: 
 27: 
 28: /**
 29:  * Visitor log resource
 30:  *
 31:  * @category    Mage
 32:  * @package     Mage_Log
 33:  * @author      Magento Core Team <core@magentocommerce.com>
 34:  */
 35: class Mage_Log_Model_Resource_Visitor extends Mage_Core_Model_Resource_Db_Abstract
 36: {
 37:     /**
 38:      * Define main table
 39:      *
 40:      */
 41:     protected function _construct()
 42:     {
 43:         $this->_init('log/visitor', 'visitor_id');
 44:     }
 45: 
 46:     /**
 47:      * Prepare data for save
 48:      *
 49:      * @param Mage_Core_Model_Abstract $visitor
 50:      * @return array
 51:      */
 52:     protected function _prepareDataForSave(Mage_Core_Model_Abstract $visitor)
 53:     {
 54:         return array(
 55:             'session_id'        => $visitor->getSessionId(),
 56:             'first_visit_at'    => $visitor->getFirstVisitAt(),
 57:             'last_visit_at'     => $visitor->getLastVisitAt(),
 58:             'last_url_id'       => $visitor->getLastUrlId() ? $visitor->getLastUrlId() : 0,
 59:             'store_id'          => Mage::app()->getStore()->getId(),
 60:         );
 61:     }
 62: 
 63:     /**
 64:      * Saving information about url
 65:      *
 66:      * @param   Mage_Log_Model_Visitor $visitor
 67:      * @return  Mage_Log_Model_Resource_Visitor
 68:      */
 69:     protected function _saveUrlInfo($visitor)
 70:     {
 71:         $adapter    = $this->_getWriteAdapter();
 72:         $data       = new Varien_Object(array(
 73:             'url'    => Mage::helper('core/string')->substr($visitor->getUrl(), 0, 250),
 74:             'referer'=> Mage::helper('core/string')->substr($visitor->getHttpReferer(), 0, 250)
 75:         ));
 76:         $bind = $this->_prepareDataForTable($data, $this->getTable('log/url_info_table'));
 77: 
 78:         $adapter->insert($this->getTable('log/url_info_table'), $bind);
 79: 
 80:         $visitor->setLastUrlId($adapter->lastInsertId($this->getTable('log/url_info_table')));
 81: 
 82:         return $this;
 83:     }
 84: 
 85:     /**
 86:      * Save url info before save
 87:      *
 88:      * @param Mage_Core_Model_Abstract $visitor
 89:      * @return Mage_Log_Model_Resource_Visitor
 90:      */
 91:     protected function _beforeSave(Mage_Core_Model_Abstract $visitor)
 92:     {
 93:         if (!$visitor->getIsNewVisitor()) {
 94:             $this->_saveUrlInfo($visitor);
 95:         }
 96:         return $this;
 97:     }
 98: 
 99:     /**
100:      * Actions after save
101:      *
102:      * @param Mage_Core_Model_Abstract $visitor
103:      * @return Mage_Log_Model_Resource_Visitor
104:      */
105:     protected function _afterSave(Mage_Core_Model_Abstract $visitor)
106:     {
107:         if ($visitor->getIsNewVisitor()) {
108:             $this->_saveVisitorInfo($visitor);
109:             $visitor->setIsNewVisitor(false);
110:         } else {
111:             $this->_saveVisitorUrl($visitor);
112:             if ($visitor->getDoCustomerLogin() || $visitor->getDoCustomerLogout()) {
113:                 $this->_saveCustomerInfo($visitor);
114:             }
115:             if ($visitor->getDoQuoteCreate() || $visitor->getDoQuoteDestroy()) {
116:                 $this->_saveQuoteInfo($visitor);
117:             }
118:         }
119:         return $this;
120:     }
121: 
122:     /**
123:      * Perform actions after object load
124:      *
125:      * @param Varien_Object $object
126:      * @return Mage_Core_Model_Resource_Db_Abstract
127:      */
128:     protected function _afterLoad(Mage_Core_Model_Abstract $object)
129:     {
130:         parent::_afterLoad($object);
131:         // Add information about quote to visitor
132:         $adapter = $this->_getReadAdapter();
133:         $select = $adapter->select()->from($this->getTable('log/quote_table'), 'quote_id')
134:             ->where('visitor_id = ?', $object->getId())->limit(1);
135:         $result = $adapter->query($select)->fetch();
136:         if (isset($result['quote_id'])) {
137:             $object->setQuoteId((int) $result['quote_id']);
138:         }
139:         return $this;
140:     }
141: 
142:     /**
143:      * Saving visitor information
144:      *
145:      * @param   Mage_Log_Model_Visitor $visitor
146:      * @return  Mage_Log_Model_Resource_Visitor
147:      */
148:     protected function _saveVisitorInfo($visitor)
149:     {
150:         /* @var $stringHelper Mage_Core_Helper_String */
151:         $stringHelper = Mage::helper('core/string');
152: 
153:         $referer    = $stringHelper->cleanString($visitor->getHttpReferer());
154:         $referer    = $stringHelper->substr($referer, 0, 255);
155:         $userAgent  = $stringHelper->cleanString($visitor->getHttpUserAgent());
156:         $userAgent  = $stringHelper->substr($userAgent, 0, 255);
157:         $charset    = $stringHelper->cleanString($visitor->getHttpAcceptCharset());
158:         $charset    = $stringHelper->substr($charset, 0, 255);
159:         $language   = $stringHelper->cleanString($visitor->getHttpAcceptLanguage());
160:         $language   = $stringHelper->substr($language, 0, 255);
161: 
162:         $adapter = $this->_getWriteAdapter();
163:         $data = new Varien_Object(array(
164:             'visitor_id'            => $visitor->getId(),
165:             'http_referer'          => $referer,
166:             'http_user_agent'       => $userAgent,
167:             'http_accept_charset'   => $charset,
168:             'http_accept_language'  => $language,
169:             'server_addr'           => $visitor->getServerAddr(),
170:             'remote_addr'           => $visitor->getRemoteAddr(),
171:         ));
172:         $bind = $this->_prepareDataForTable($data, $this->getTable('log/visitor_info'));
173: 
174:         $adapter->insert($this->getTable('log/visitor_info'), $bind);
175:         return $this;
176:     }
177: 
178:     /**
179:      * Saving visitor and url relation
180:      *
181:      * @param   Mage_Log_Model_Visitor $visitor
182:      * @return  Mage_Log_Model_Resource_Visitor
183:      */
184:     protected function _saveVisitorUrl($visitor)
185:     {
186:         $data = new Varien_Object(array(
187:             'url_id'        => $visitor->getLastUrlId(),
188:             'visitor_id'    => $visitor->getId(),
189:             'visit_time'    => Mage::getSingleton('core/date')->gmtDate()
190:         ));
191:         $bind = $this->_prepareDataForTable($data, $this->getTable('log/url_table'));
192: 
193:         $this->_getWriteAdapter()->insert($this->getTable('log/url_table'), $bind);
194:         return $this;
195:     }
196: 
197:     /**
198:      * Saving information about customer
199:      *
200:      * @param   Mage_Log_Model_Visitor $visitor
201:      * @return  Mage_Log_Model_Resource_Visitor
202:      */
203:     protected function _saveCustomerInfo($visitor)
204:     {
205:         $adapter = $this->_getWriteAdapter();
206: 
207:         if ($visitor->getDoCustomerLogin()) {
208:             $data = new Varien_Object(array(
209:                 'visitor_id'    => $visitor->getVisitorId(),
210:                 'customer_id'   => $visitor->getCustomerId(),
211:                 'login_at'      => Mage::getSingleton('core/date')->gmtDate(),
212:                 'store_id'      => Mage::app()->getStore()->getId()
213:             ));
214:             $bind = $this->_prepareDataForTable($data, $this->getTable('log/customer'));
215: 
216:             $adapter->insert($this->getTable('log/customer'), $bind);
217:             $visitor->setCustomerLogId($adapter->lastInsertId($this->getTable('log/customer')));
218:             $visitor->setDoCustomerLogin(false);
219:         }
220: 
221:         if ($visitor->getDoCustomerLogout() && $logId = $visitor->getCustomerLogId()) {
222:             $data = new Varien_Object(array(
223:                 'logout_at' => Mage::getSingleton('core/date')->gmtDate(),
224:                 'store_id'  => (int)Mage::app()->getStore()->getId(),
225:             ));
226: 
227:             $bind = $this->_prepareDataForTable($data, $this->getTable('log/customer'));
228: 
229:             $condition = array(
230:                 'log_id = ?' => (int) $logId,
231:             );
232: 
233:             $adapter->update($this->getTable('log/customer'), $bind, $condition);
234: 
235:             $visitor->setDoCustomerLogout(false);
236:             $visitor->setCustomerId(null);
237:             $visitor->setCustomerLogId(null);
238:         }
239: 
240:         return $this;
241:     }
242: 
243:     /**
244:      * Saving information about quote
245:      *
246:      * @param   Mage_Log_Model_Visitor $visitor
247:      * @return  Mage_Log_Model_Resource_Visitor
248:      */
249:     protected function _saveQuoteInfo($visitor)
250:     {
251:         $adapter = $this->_getWriteAdapter();
252:         if ($visitor->getDoQuoteCreate()) {
253:             $data = new Varien_Object(array(
254:                 'quote_id'      => (int) $visitor->getQuoteId(),
255:                 'visitor_id'    => (int) $visitor->getId(),
256:                 'created_at'    => Mage::getSingleton('core/date')->gmtDate()
257:             ));
258: 
259:             $bind = $this->_prepareDataForTable($data, $this->getTable('log/quote_table'));
260: 
261:             $adapter->insert($this->getTable('log/quote_table'), $bind);
262: 
263:             $visitor->setDoQuoteCreate(false);
264:         }
265: 
266:         if ($visitor->getDoQuoteDestroy()) {
267:             /**
268:              * We have delete quote from log because if original quote was
269:              * deleted and Mysql restarted we will get key duplication error
270:              */
271:             $condition = array(
272:                 'quote_id = ?' => (int) $visitor->getQuoteId(),
273:             );
274: 
275:             $adapter->delete($this->getTable('log/quote_table'), $condition);
276: 
277:             $visitor->setDoQuoteDestroy(false);
278:             $visitor->setQuoteId(null);
279:         }
280:         return $this;
281:     }
282: }
283: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0