PageRenderTime 42ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Reports/Model/Resource/Report/Product/Viewed/Collection.php

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 367 lines | 233 code | 40 blank | 94 comment | 66 complexity | 7f7ba5b8925ab4f3d54af661861e8819 MD5 | raw file
  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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Reports
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Report most viewed collection
  28. */
  29. class Mage_Reports_Model_Resource_Report_Product_Viewed_Collection
  30. extends Mage_Reports_Model_Resource_Report_Collection_Abstract
  31. {
  32. /**
  33. * Rating limit
  34. *
  35. * @var int
  36. */
  37. protected $_ratingLimit = 5;
  38. /**
  39. * Selected columns
  40. *
  41. * @var array
  42. */
  43. protected $_selectedColumns = array();
  44. /**
  45. * Initialize custom resource model
  46. *
  47. */
  48. public function __construct()
  49. {
  50. parent::_construct();
  51. $this->setModel('adminhtml/report_item');
  52. $this->_resource = Mage::getResourceModel('sales/report')
  53. ->init(Mage_Reports_Model_Resource_Report_Product_Viewed::AGGREGATION_DAILY);
  54. $this->setConnection($this->getResource()->getReadConnection());
  55. // overwrite default behaviour
  56. $this->_applyFilters = false;
  57. }
  58. /**
  59. * Retrieve selected columns
  60. *
  61. * @return array
  62. */
  63. protected function _getSelectedColumns()
  64. {
  65. $adapter = $this->getConnection();
  66. if (!$this->_selectedColumns) {
  67. if ($this->isTotals()) {
  68. $this->_selectedColumns = $this->getAggregatedColumns();
  69. } else {
  70. $this->_selectedColumns = array(
  71. 'period' => sprintf('MAX(%s)', $adapter->getDateFormatSql('period', '%Y-%m-%d')),
  72. 'views_num' => 'SUM(views_num)',
  73. 'product_id' => 'product_id',
  74. 'product_name' => 'MAX(product_name)',
  75. 'product_price' => 'MAX(product_price)',
  76. );
  77. if ('year' == $this->_period) {
  78. $this->_selectedColumns['period'] = $adapter->getDateFormatSql('period', '%Y');
  79. } elseif ('month' == $this->_period) {
  80. $this->_selectedColumns['period'] = $adapter->getDateFormatSql('period', '%Y-%m');
  81. }
  82. }
  83. }
  84. return $this->_selectedColumns;
  85. }
  86. /**
  87. * Make select object for date boundary
  88. *
  89. * @param mixed $from
  90. * @param mixed $to
  91. * @return Zend_Db_Select
  92. */
  93. protected function _makeBoundarySelect($from, $to)
  94. {
  95. $adapter = $this->getConnection();
  96. $cols = $this->_getSelectedColumns();
  97. $cols['views_num'] = 'SUM(views_num)';
  98. $select = $adapter->select()
  99. ->from($this->getResource()->getMainTable(), $cols)
  100. ->where('period >= ?', $from)
  101. ->where('period <= ?', $to)
  102. ->group('product_id')
  103. ->order('views_num DESC')
  104. ->limit($this->_ratingLimit);
  105. $this->_applyStoresFilterToSelect($select);
  106. return $select;
  107. }
  108. /**
  109. * Init collection select
  110. *
  111. * @return Mage_Reports_Model_Resource_Report_Product_Viewed_Collection
  112. */
  113. protected function _initSelect()
  114. {
  115. $select = $this->getSelect();
  116. // if grouping by product, not by period
  117. if (!$this->_period) {
  118. $cols = $this->_getSelectedColumns();
  119. $cols['views_num'] = 'SUM(views_num)';
  120. if ($this->_from || $this->_to) {
  121. $mainTable = $this->getTable(Mage_Reports_Model_Resource_Report_Product_Viewed::AGGREGATION_DAILY);
  122. $select->from($mainTable, $cols);
  123. } else {
  124. $mainTable = $this->getTable(Mage_Reports_Model_Resource_Report_Product_Viewed::AGGREGATION_YEARLY);
  125. $select->from($mainTable, $cols);
  126. }
  127. //exclude removed products
  128. $subSelect = $this->getConnection()->select();
  129. $subSelect->from(array('existed_products' => $this->getTable('catalog/product')), new Zend_Db_Expr('1)'));
  130. $select->exists($subSelect, $mainTable . '.product_id = existed_products.entity_id')
  131. ->group('product_id')
  132. ->order('views_num ' . Varien_Db_Select::SQL_DESC)
  133. ->limit($this->_ratingLimit);
  134. return $this;
  135. }
  136. if ('year' == $this->_period) {
  137. $mainTable = $this->getTable(Mage_Reports_Model_Resource_Report_Product_Viewed::AGGREGATION_YEARLY);
  138. $select->from($mainTable, $this->_getSelectedColumns());
  139. } elseif ('month' == $this->_period) {
  140. $mainTable = $this->getTable(Mage_Reports_Model_Resource_Report_Product_Viewed::AGGREGATION_MONTHLY);
  141. $select->from($mainTable, $this->_getSelectedColumns());
  142. } else {
  143. $mainTable = $this->getTable(Mage_Reports_Model_Resource_Report_Product_Viewed::AGGREGATION_DAILY);
  144. $select->from($mainTable, $this->_getSelectedColumns());
  145. }
  146. if (!$this->isTotals()) {
  147. $select->group(array('period', 'product_id'));
  148. }
  149. $select->where('rating_pos <= ?', $this->_ratingLimit);
  150. return $this;
  151. }
  152. /**
  153. * Get SQL for get record count
  154. *
  155. * @return Varien_Db_Select
  156. */
  157. public function getSelectCountSql()
  158. {
  159. $this->_renderFilters();
  160. $select = clone $this->getSelect();
  161. $select->reset(Zend_Db_Select::ORDER);
  162. return $this->getConnection()->select()->from($select, 'COUNT(*)');
  163. }
  164. /**
  165. * Set ids for store restrictions
  166. *
  167. * @param array $storeIds
  168. * @return Mage_Reports_Model_Resource_Report_Product_Viewed_Collection
  169. */
  170. public function addStoreRestrictions($storeIds)
  171. {
  172. if (!is_array($storeIds)) {
  173. $storeIds = array($storeIds);
  174. }
  175. $currentStoreIds = $this->_storesIds;
  176. if (isset($currentStoreIds) && $currentStoreIds != Mage_Core_Model_App::ADMIN_STORE_ID
  177. && $currentStoreIds != array(Mage_Core_Model_App::ADMIN_STORE_ID)) {
  178. if (!is_array($currentStoreIds)) {
  179. $currentStoreIds = array($currentStoreIds);
  180. }
  181. $this->_storesIds = array_intersect($currentStoreIds, $storeIds);
  182. } else {
  183. $this->_storesIds = $storeIds;
  184. }
  185. return $this;
  186. }
  187. /**
  188. * Redeclare parent method for applying filters after parent method
  189. * but before adding unions and calculating totals
  190. *
  191. * @return Mage_Reports_Model_Resource_Report_Product_Viewed_Collection
  192. */
  193. protected function _beforeLoad()
  194. {
  195. parent::_beforeLoad();
  196. $this->_applyStoresFilter();
  197. if ($this->_period) {
  198. $selectUnions = array();
  199. // apply date boundaries (before calling $this->_applyDateRangeFilter())
  200. $dtFormat = Varien_Date::DATE_INTERNAL_FORMAT;
  201. $periodFrom = (!is_null($this->_from) ? new Zend_Date($this->_from, $dtFormat) : null);
  202. $periodTo = (!is_null($this->_to) ? new Zend_Date($this->_to, $dtFormat) : null);
  203. if ('year' == $this->_period) {
  204. if ($periodFrom) {
  205. // not the first day of the year
  206. if ($periodFrom->toValue(Zend_Date::MONTH) != 1 || $periodFrom->toValue(Zend_Date::DAY) != 1) {
  207. $dtFrom = $periodFrom->getDate();
  208. // last day of the year
  209. $dtTo = $periodFrom->getDate()->setMonth(12)->setDay(31);
  210. if (!$periodTo || $dtTo->isEarlier($periodTo)) {
  211. $selectUnions[] = $this->_makeBoundarySelect(
  212. $dtFrom->toString($dtFormat),
  213. $dtTo->toString($dtFormat)
  214. );
  215. // first day of the next year
  216. $this->_from = $periodFrom->getDate()
  217. ->addYear(1)
  218. ->setMonth(1)
  219. ->setDay(1)
  220. ->toString($dtFormat);
  221. }
  222. }
  223. }
  224. if ($periodTo) {
  225. // not the last day of the year
  226. if ($periodTo->toValue(Zend_Date::MONTH) != 12 || $periodTo->toValue(Zend_Date::DAY) != 31) {
  227. $dtFrom = $periodTo->getDate()->setMonth(1)->setDay(1); // first day of the year
  228. $dtTo = $periodTo->getDate();
  229. if (!$periodFrom || $dtFrom->isLater($periodFrom)) {
  230. $selectUnions[] = $this->_makeBoundarySelect(
  231. $dtFrom->toString($dtFormat),
  232. $dtTo->toString($dtFormat)
  233. );
  234. // last day of the previous year
  235. $this->_to = $periodTo->getDate()
  236. ->subYear(1)
  237. ->setMonth(12)
  238. ->setDay(31)
  239. ->toString($dtFormat);
  240. }
  241. }
  242. }
  243. if ($periodFrom && $periodTo) {
  244. // the same year
  245. if ($periodFrom->toValue(Zend_Date::YEAR) == $periodTo->toValue(Zend_Date::YEAR)) {
  246. $dtFrom = $periodFrom->getDate();
  247. $dtTo = $periodTo->getDate();
  248. $selectUnions[] = $this->_makeBoundarySelect(
  249. $dtFrom->toString($dtFormat),
  250. $dtTo->toString($dtFormat)
  251. );
  252. $this->getSelect()->where('1<>1');
  253. }
  254. }
  255. }
  256. else if ('month' == $this->_period) {
  257. if ($periodFrom) {
  258. // not the first day of the month
  259. if ($periodFrom->toValue(Zend_Date::DAY) != 1) {
  260. $dtFrom = $periodFrom->getDate();
  261. // last day of the month
  262. $dtTo = $periodFrom->getDate()->addMonth(1)->setDay(1)->subDay(1);
  263. if (!$periodTo || $dtTo->isEarlier($periodTo)) {
  264. $selectUnions[] = $this->_makeBoundarySelect(
  265. $dtFrom->toString($dtFormat),
  266. $dtTo->toString($dtFormat)
  267. );
  268. // first day of the next month
  269. $this->_from = $periodFrom->getDate()->addMonth(1)->setDay(1)->toString($dtFormat);
  270. }
  271. }
  272. }
  273. if ($periodTo) {
  274. // not the last day of the month
  275. if ($periodTo->toValue(Zend_Date::DAY) != $periodTo->toValue(Zend_Date::MONTH_DAYS)) {
  276. $dtFrom = $periodTo->getDate()->setDay(1); // first day of the month
  277. $dtTo = $periodTo->getDate();
  278. if (!$periodFrom || $dtFrom->isLater($periodFrom)) {
  279. $selectUnions[] = $this->_makeBoundarySelect(
  280. $dtFrom->toString($dtFormat),
  281. $dtTo->toString($dtFormat)
  282. );
  283. // last day of the previous month
  284. $this->_to = $periodTo->getDate()->setDay(1)->subDay(1)->toString($dtFormat);
  285. }
  286. }
  287. }
  288. if ($periodFrom && $periodTo) {
  289. // the same month
  290. if ($periodFrom->toValue(Zend_Date::YEAR) == $periodTo->toValue(Zend_Date::YEAR)
  291. && $periodFrom->toValue(Zend_Date::MONTH) == $periodTo->toValue(Zend_Date::MONTH)
  292. ) {
  293. $dtFrom = $periodFrom->getDate();
  294. $dtTo = $periodTo->getDate();
  295. $selectUnions[] = $this->_makeBoundarySelect(
  296. $dtFrom->toString($dtFormat),
  297. $dtTo->toString($dtFormat)
  298. );
  299. $this->getSelect()->where('1<>1');
  300. }
  301. }
  302. }
  303. $this->_applyDateRangeFilter();
  304. // add unions to select
  305. if ($selectUnions) {
  306. $unionParts = array();
  307. $cloneSelect = clone $this->getSelect();
  308. $helper = Mage::getResourceHelper('core');
  309. $unionParts[] = '(' . $cloneSelect . ')';
  310. foreach ($selectUnions as $union) {
  311. $query = $helper->getQueryUsingAnalyticFunction($union);
  312. $unionParts[] = '(' . $query . ')';
  313. }
  314. $this->getSelect()->reset()->union($unionParts, Zend_Db_Select::SQL_UNION_ALL);
  315. }
  316. if ($this->isTotals()) {
  317. // calculate total
  318. $cloneSelect = clone $this->getSelect();
  319. $this->getSelect()->reset()->from($cloneSelect, $this->getAggregatedColumns());
  320. } else {
  321. // add sorting
  322. $this->getSelect()->order(array('period ASC', 'views_num DESC'));
  323. }
  324. }
  325. return $this;
  326. }
  327. }