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: * Log Online visitors collection
30: *
31: * @category Mage
32: * @package Mage_Log
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Log_Model_Resource_Visitor_Online_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
36: {
37: /**
38: * joined fields array
39: *
40: * @var array
41: */
42: protected $_fields = array();
43:
44: /**
45: * Initialize collection model
46: *
47: */
48: protected function _construct()
49: {
50: $this->_init('log/visitor_online');
51: }
52:
53: /**
54: * Add Customer data to collection
55: *
56: * @return Mage_Log_Model_Resource_Visitor_Online_Collection
57: */
58: public function addCustomerData()
59: {
60: $customer = Mage::getModel('customer/customer');
61: // alias => attribute_code
62: $attributes = array(
63: 'customer_lastname' => 'lastname',
64: 'customer_firstname' => 'firstname',
65: 'customer_email' => 'email'
66: );
67:
68: foreach ($attributes as $alias => $attributeCode) {
69: $attribute = $customer->getAttribute($attributeCode);
70: /* @var $attribute Mage_Eav_Model_Entity_Attribute_Abstract */
71:
72: if ($attribute->getBackendType() == 'static') {
73: $tableAlias = 'customer_' . $attribute->getAttributeCode();
74:
75: $this->getSelect()->joinLeft(
76: array($tableAlias => $attribute->getBackend()->getTable()),
77: sprintf('%s.entity_id=main_table.customer_id', $tableAlias),
78: array($alias => $attribute->getAttributeCode())
79: );
80:
81: $this->_fields[$alias] = sprintf('%s.%s', $tableAlias, $attribute->getAttributeCode());
82: }
83: else {
84: $tableAlias = 'customer_' . $attribute->getAttributeCode();
85:
86: $joinConds = array(
87: sprintf('%s.entity_id=main_table.customer_id', $tableAlias),
88: $this->getConnection()->quoteInto($tableAlias . '.attribute_id=?', $attribute->getAttributeId())
89: );
90:
91: $this->getSelect()->joinLeft(
92: array($tableAlias => $attribute->getBackend()->getTable()),
93: join(' AND ', $joinConds),
94: array($alias => 'value')
95: );
96:
97: $this->_fields[$alias] = sprintf('%s.value', $tableAlias);
98: }
99: }
100:
101: $this->setFlag('has_customer_data', true);
102: return $this;
103: }
104:
105: /**
106: * Filter collection by specified website(s)
107: *
108: * @param int|array $websiteIds
109: * @return Mage_Log_Model_Resource_Visitor_Online_Collection
110: */
111: public function addWebsiteFilter($websiteIds)
112: {
113: if ($this->getFlag('has_customer_data')) {
114: $this->getSelect()
115: ->where('customer_email.website_id IN (?)', $websiteIds);
116: }
117: return $this;
118: }
119:
120: /**
121: * Add field filter to collection
122: * If $attribute is an array will add OR condition with following format:
123: * array(
124: * array('attribute'=>'firstname', 'like'=>'test%'),
125: * array('attribute'=>'lastname', 'like'=>'test%'),
126: * )
127: *
128: * @see self::_getConditionSql for $condition
129: *
130: * @param string $field
131: * @param null|string|array $condition
132: * @return Mage_Eav_Model_Entity_Collection_Abstract
133: */
134: public function addFieldToFilter($field, $condition = null)
135: {
136: if (isset($this->_fields[$field])) {
137: $field = $this->_fields[$field];
138: }
139:
140: return parent::addFieldToFilter($field, $condition);
141: }
142: }
143: