Collection.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Report Reviews collection
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Reports\Model\ResourceModel\Report;
  12. /**
  13. * @api
  14. * @since 100.0.2
  15. */
  16. class Collection extends \Magento\Framework\Data\Collection
  17. {
  18. /**
  19. * From value
  20. *
  21. * @var \DateTime
  22. */
  23. protected $_from;
  24. /**
  25. * To value
  26. *
  27. * @var \DateTime
  28. */
  29. protected $_to;
  30. /**
  31. * Report period
  32. *
  33. * @var int
  34. */
  35. protected $_period;
  36. /**
  37. * Intervals
  38. *
  39. * @var int
  40. */
  41. protected $_intervals;
  42. /**
  43. * Intervals
  44. *
  45. * @var int
  46. */
  47. protected $_reports;
  48. /**
  49. * Page size
  50. *
  51. * @var int
  52. */
  53. protected $_pageSize;
  54. /**
  55. * Array of store ids
  56. *
  57. * @var array
  58. */
  59. protected $_storeIds;
  60. /**
  61. * Set the resource report collection class
  62. *
  63. * @var string
  64. */
  65. protected $_reportCollection = null;
  66. /**
  67. * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
  68. */
  69. protected $_localeDate;
  70. /**
  71. * @var \Magento\Reports\Model\ResourceModel\Report\Collection\Factory
  72. */
  73. protected $_collectionFactory;
  74. /**
  75. * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
  76. * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
  77. * @param \Magento\Reports\Model\ResourceModel\Report\Collection\Factory $collectionFactory
  78. */
  79. public function __construct(
  80. \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
  81. \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
  82. \Magento\Reports\Model\ResourceModel\Report\Collection\Factory $collectionFactory
  83. ) {
  84. $this->_localeDate = $localeDate;
  85. $this->_collectionFactory = $collectionFactory;
  86. parent::__construct($entityFactory);
  87. }
  88. /**
  89. * Set period
  90. * @codeCoverageIgnore
  91. *
  92. * @param int $period
  93. * @return $this
  94. */
  95. public function setPeriod($period)
  96. {
  97. $this->_period = $period;
  98. return $this;
  99. }
  100. /**
  101. * Set interval
  102. * @codeCoverageIgnore
  103. *
  104. * @param \DateTimeInterface $fromDate
  105. * @param \DateTimeInterface $toDate
  106. * @return $this
  107. */
  108. public function setInterval(\DateTimeInterface $fromDate, \DateTimeInterface $toDate)
  109. {
  110. $this->_from = new \DateTime($fromDate->format('Y-m-d'), $fromDate->getTimezone());
  111. $this->_to = new \DateTime($toDate->format('Y-m-d'), $toDate->getTimezone());
  112. return $this;
  113. }
  114. /**
  115. * Get intervals
  116. *
  117. * @return array
  118. */
  119. protected function _getIntervals()
  120. {
  121. if (!$this->_intervals) {
  122. $this->_intervals = [];
  123. if (!$this->_from && !$this->_to) {
  124. return $this->_intervals;
  125. }
  126. $dateStart = $this->_from;
  127. $dateEnd = $this->_to;
  128. $firstInterval = true;
  129. while ($dateStart <= $dateEnd) {
  130. switch ($this->_period) {
  131. case 'day':
  132. $interval = $this->_getDayInterval($dateStart);
  133. $dateStart->modify('+1 day');
  134. break;
  135. case 'month':
  136. $interval = $this->_getMonthInterval($dateStart, $dateEnd, $firstInterval);
  137. $firstInterval = false;
  138. break;
  139. case 'year':
  140. $interval = $this->_getYearInterval($dateStart, $dateEnd, $firstInterval);
  141. $firstInterval = false;
  142. break;
  143. default:
  144. break 2;
  145. }
  146. $this->_intervals[$interval['period']] = new \Magento\Framework\DataObject($interval);
  147. }
  148. }
  149. return $this->_intervals;
  150. }
  151. /**
  152. * Get interval for a day
  153. *
  154. * @param \DateTime $dateStart
  155. * @return array
  156. */
  157. protected function _getDayInterval(\DateTime $dateStart)
  158. {
  159. $interval = [
  160. 'period' => $this->_localeDate->formatDateTime(
  161. $dateStart,
  162. \IntlDateFormatter::SHORT,
  163. \IntlDateFormatter::NONE
  164. ),
  165. 'start' => $this->_localeDate->convertConfigTimeToUtc($dateStart->format('Y-m-d 00:00:00')),
  166. 'end' => $this->_localeDate->convertConfigTimeToUtc($dateStart->format('Y-m-d 23:59:59')),
  167. ];
  168. return $interval;
  169. }
  170. /**
  171. * Get interval for a month
  172. *
  173. * @param \DateTime $dateStart
  174. * @param \DateTime $dateEnd
  175. * @param bool $firstInterval
  176. * @return array
  177. */
  178. protected function _getMonthInterval(\DateTime $dateStart, \DateTime $dateEnd, $firstInterval)
  179. {
  180. $interval = [];
  181. $interval['period'] = $dateStart->format('m/Y');
  182. if ($firstInterval) {
  183. $interval['start'] = $this->_localeDate->convertConfigTimeToUtc($dateStart->format('Y-m-d 00:00:00'));
  184. } else {
  185. $interval['start'] = $this->_localeDate->convertConfigTimeToUtc($dateStart->format('Y-m-01 00:00:00'));
  186. }
  187. if ($dateStart->diff($dateEnd)->m == 0) {
  188. $interval['end'] = $this->_localeDate->convertConfigTimeToUtc(
  189. $dateStart->setDate(
  190. $dateStart->format('Y'),
  191. $dateStart->format('m'),
  192. $dateEnd->format('d')
  193. )->format(
  194. 'Y-m-d 23:59:59'
  195. )
  196. );
  197. } else {
  198. // Transform the start date to UTC whilst preserving the date. This is required as getTimestamp()
  199. // is in UTC which may result in a different month from the original start date due to time zones.
  200. $dateStartUtc = (new \DateTime())->createFromFormat('d-m-Y g:i:s', $dateStart->format('d-m-Y 00:00:00'));
  201. $interval['end'] = $this->_localeDate->convertConfigTimeToUtc(
  202. $dateStart->format('Y-m-' . date('t', $dateStartUtc->getTimestamp()) . ' 23:59:59')
  203. );
  204. }
  205. $dateStart->modify('+1 month');
  206. if ($dateStart->diff($dateEnd)->m == 0) {
  207. $dateStart->setDate($dateStart->format('Y'), $dateStart->format('m'), 1);
  208. }
  209. return $interval;
  210. }
  211. /**
  212. * Get Interval for a year
  213. *
  214. * @param \DateTime $dateStart
  215. * @param \DateTime $dateEnd
  216. * @param bool $firstInterval
  217. * @return array
  218. */
  219. protected function _getYearInterval(\DateTime $dateStart, \DateTime $dateEnd, $firstInterval)
  220. {
  221. $interval = [];
  222. $interval['period'] = $dateStart->format('Y');
  223. $interval['start'] = $firstInterval
  224. ? $this->_localeDate->convertConfigTimeToUtc($dateStart->format('Y-m-d 00:00:00'))
  225. : $this->_localeDate->convertConfigTimeToUtc($dateStart->format('Y-01-01 00:00:00'));
  226. $interval['end'] = $dateStart->diff($dateEnd)->y == 0
  227. ? $this->_localeDate->convertConfigTimeToUtc(
  228. $dateStart->setDate($dateStart->format('Y'), $dateEnd->format('m'), $dateEnd->format('d'))
  229. ->format('Y-m-d 23:59:59')
  230. )
  231. : $this->_localeDate->convertConfigTimeToUtc($dateStart->format('Y-12-31 23:59:59'));
  232. $dateStart->modify('+1 year');
  233. if ($dateStart->diff($dateEnd)->y == 0) {
  234. $dateStart->setDate($dateStart->format('Y'), 1, 1);
  235. }
  236. return $interval;
  237. }
  238. /**
  239. * Return date periods
  240. *
  241. * @return array
  242. */
  243. public function getPeriods()
  244. {
  245. return ['day' => __('Day'), 'month' => __('Month'), 'year' => __('Year')];
  246. }
  247. /**
  248. * Set store ids
  249. * @codeCoverageIgnore
  250. *
  251. * @param array $storeIds
  252. * @return $this
  253. */
  254. public function setStoreIds($storeIds)
  255. {
  256. $this->_storeIds = $storeIds;
  257. return $this;
  258. }
  259. /**
  260. * Get store ids
  261. * @codeCoverageIgnore
  262. *
  263. * @return array
  264. */
  265. public function getStoreIds()
  266. {
  267. return $this->_storeIds;
  268. }
  269. /**
  270. * Get size
  271. *
  272. * @return int
  273. */
  274. public function getSize()
  275. {
  276. return count($this->_getIntervals());
  277. }
  278. /**
  279. * Set page size
  280. * @codeCoverageIgnore
  281. *
  282. * @param int $size
  283. * @return $this
  284. */
  285. public function setPageSize($size)
  286. {
  287. $this->_pageSize = $size;
  288. return $this;
  289. }
  290. /**
  291. * Get page size
  292. * @codeCoverageIgnore
  293. *
  294. * @return int
  295. */
  296. public function getPageSize()
  297. {
  298. return $this->_pageSize;
  299. }
  300. /**
  301. * Get report for some interval
  302. *
  303. * @param int $fromDate
  304. * @param int $toDate
  305. * @return \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  306. */
  307. protected function _getReport($fromDate, $toDate)
  308. {
  309. if ($this->_reportCollection === null) {
  310. return [];
  311. }
  312. $reportResource = $this->_collectionFactory->create($this->_reportCollection);
  313. $reportResource->setDateRange($fromDate, $toDate)->setStoreIds($this->getStoreIds());
  314. return $reportResource;
  315. }
  316. /**
  317. * Get Reports based on intervals
  318. *
  319. * @return array
  320. */
  321. public function getReports()
  322. {
  323. if (!$this->_reports) {
  324. $reports = [];
  325. foreach ($this->_getIntervals() as $interval) {
  326. $interval->setChildren($this->_getReport($interval->getStart(), $interval->getEnd()));
  327. if (count($interval->getChildren()) == 0) {
  328. $interval->setIsEmpty(true);
  329. }
  330. $reports[] = $interval;
  331. }
  332. $this->_reports = $reports;
  333. }
  334. return $this->_reports;
  335. }
  336. /**
  337. * Load data
  338. *
  339. * @param bool $printQuery
  340. * @param bool $logQuery
  341. * @return $this
  342. *
  343. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  344. */
  345. public function loadData($printQuery = false, $logQuery = false)
  346. {
  347. $this->_items = $this->getReports();
  348. return $this;
  349. }
  350. }