/** * Retrieves recent activity for customers (sorted by last visit date) */ function getactivityAction() { if (!$this->_authenticate()) { return; } try { $request = $this->getRequest(); $helper = Mage::helper('eyehubspot'); $maxperpage = $request->getParam('maxperpage', self::MAX_CUSTOMER_PERPAGE); $maxAssociated = $request->getParam('maxassoc', self::MAX_ASSOC_PRODUCT_LIMIT); $start = date('Y-m-d H:i:s', $request->getParam('start', 0)); $end = date('Y-m-d H:i:s', time() - 300); $websiteId = Mage::app()->getWebsite()->getId(); $store = Mage::app()->getStore(); $storeId = Mage::app()->getStore()->getId(); $collection = Mage::getModel('customer/customer')->getCollection(); $resource = Mage::getSingleton('core/resource'); $read = $resource->getConnection('core_read'); $customerData = array(); try { // because of limitations in the log areas of magento, we cannot use the // standard collection to retreive the results $select = $read->select()->from(array('lc' => $resource->getTableName('log/customer')))->joinInner(array('lv' => $resource->getTableName('log/visitor')), 'lc.visitor_id = lv.visitor_id')->joinInner(array('vi' => $resource->getTableName('log/visitor_info')), 'lc.visitor_id = vi.visitor_id')->joinInner(array('c' => $resource->getTableName('customer/entity')), 'c.entity_id = lc.customer_id', array('email' => 'email', 'customer_since' => 'created_at'))->joinInner(array('p' => $resource->getTableName('log/url_info_table')), 'p.url_id = lv.last_url_id', array('last_url' => 'p.url', 'last_referer' => 'p.referer'))->where('lc.customer_id > 0')->where("lv.last_visit_at >= '{$start}'")->where("lv.last_visit_at < '{$end}'")->order('lv.last_visit_at')->limit($maxperpage); $collection = $read->fetchAll($select); } catch (Exception $e) { $this->_outputError(self::ERROR_CODE_UNSUPPORTED_SQL, 'DB Exception on query', $e); return; } foreach ($collection as $assoc) { $log = new Varien_Object($assoc); $customerId = $log->getCustomerId(); // merge and replace older data with newer if (isset($customerData[$customerId])) { $temp = $customerData[$customerId]; $log->addData($temp->getData()); $log->setFirstVisitAt($temp->getFirstVisitAt()); } else { $log->setViewed($helper->getProductViewedList($customerId, $maxAssociated)); $log->setCompare($helper->getProductCompareList($customerId, $maxAssociated)); $log->setWishlist($helper->getProductWishlist($customerId, $maxAssociated)); } $log->unsetData('session_id'); $customerData[$customerId] = $log; } } catch (Exception $e) { $this->_outputError(self::ERROR_CODE_UNKNOWN_EXCEPTION, 'Unknown exception on request', $e); return; } $this->_outputJson(array('visitors' => $helper->convertAttributeData($customerData), 'website' => $websiteId, 'store' => $storeId)); }