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
00035 class Mage_Reports_Model_Mysql4_Product_Downloads_Collection extends Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
00036 {
00037 protected $_idFieldName = 'link_id';
00038
00039
00040
00041
00042
00043
00044 public function addSummary()
00045 {
00046 $this->getSelect()
00047 ->joinInner(
00048 array('d' => $this->getTable('downloadable/link_purchased_item')),
00049 'e.entity_id=d.product_id',
00050 array(
00051 'purchases' => 'd.number_of_downloads_bought',
00052 'downloads' => 'd.number_of_downloads_used'
00053 )
00054 )
00055 ->joinInner(
00056 array('l' => $this->getTable('downloadable/link_title')),
00057 'd.link_id=l.link_id',
00058 array('l.link_id')
00059 )
00060 ->joinLeft(
00061 array('l_store' => $this->getTable('downloadable/link_title')),
00062 $this->getConnection()->quoteInto('l.link_id=l_store.link_id AND l_store.store_id=?',$this->getStoreId()),
00063 array('link_title' => 'IFNULL(l_store.title, l.title)')
00064 )
00065 ->where('d.number_of_downloads_bought>0 OR d.number_of_downloads_used>0')
00066 ->group('d.link_id');
00067
00068 return $this;
00069 }
00070
00071
00072
00073
00074
00075
00076 public function setOrder($attribute, $dir='desc')
00077 {
00078 if ($attribute == 'purchases' || $attribute == 'downloads' || $attribute == 'link_title') {
00079 $this->getSelect()->order($attribute . ' ' . $dir);
00080 } else {
00081 parent::setOrder($attribute, $dir);
00082 }
00083 return $this;
00084 }
00085
00086
00087
00088
00089
00090
00091 public function addFieldToFilter($field, $condition=null)
00092 {
00093 if ($field == 'link_title') {
00094 $conditionSql = $this->_getConditionSql('l.title', $condition);
00095 $this->getSelect()->where($conditionSql);
00096 } else {
00097 parent::addFieldToFilter($field, $condition);
00098 }
00099 return $this;
00100 }
00101 }