Collection.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model\ResourceModel\Online\Grid;
  7. use Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult;
  8. use Magento\Customer\Model\Visitor;
  9. use Magento\Framework\Api;
  10. use Magento\Framework\Event\ManagerInterface as EventManager;
  11. use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy;
  12. use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory;
  13. use Psr\Log\LoggerInterface as Logger;
  14. /**
  15. * Flat customer online grid collection
  16. *
  17. * @author Magento Core Team <core@magentocommerce.com>
  18. */
  19. class Collection extends SearchResult
  20. {
  21. /**
  22. * Value of seconds in one minute
  23. */
  24. const SECONDS_IN_MINUTE = 60;
  25. /**
  26. * @var \Magento\Framework\Stdlib\DateTime\DateTime
  27. */
  28. protected $date;
  29. /**
  30. * @var Visitor
  31. */
  32. protected $visitorModel;
  33. /**
  34. * @param EntityFactory $entityFactory
  35. * @param Logger $logger
  36. * @param FetchStrategy $fetchStrategy
  37. * @param EventManager $eventManager
  38. * @param string $mainTable
  39. * @param string $resourceModel
  40. * @param Visitor $visitorModel
  41. * @param \Magento\Framework\Stdlib\DateTime\DateTime $date
  42. */
  43. public function __construct(
  44. EntityFactory $entityFactory,
  45. Logger $logger,
  46. FetchStrategy $fetchStrategy,
  47. EventManager $eventManager,
  48. $mainTable,
  49. $resourceModel,
  50. Visitor $visitorModel,
  51. \Magento\Framework\Stdlib\DateTime\DateTime $date
  52. ) {
  53. $this->date = $date;
  54. $this->visitorModel = $visitorModel;
  55. parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel);
  56. }
  57. /**
  58. * Init collection select
  59. *
  60. * @return $this
  61. */
  62. protected function _initSelect()
  63. {
  64. parent::_initSelect();
  65. $connection = $this->getConnection();
  66. $lastDate = $this->date->gmtTimestamp() - $this->visitorModel->getOnlineInterval() * self::SECONDS_IN_MINUTE;
  67. $this->getSelect()->joinLeft(
  68. ['customer' => $this->getTable('customer_entity')],
  69. 'customer.entity_id = main_table.customer_id',
  70. ['email', 'firstname', 'lastname']
  71. )->where(
  72. 'main_table.last_visit_at >= ?',
  73. $connection->formatDate($lastDate)
  74. );
  75. $expression = $connection->getCheckSql(
  76. 'main_table.customer_id IS NOT NULL AND main_table.customer_id != 0',
  77. $connection->quote(Visitor::VISITOR_TYPE_CUSTOMER),
  78. $connection->quote(Visitor::VISITOR_TYPE_VISITOR)
  79. );
  80. $this->getSelect()->columns(['visitor_type' => $expression]);
  81. return $this;
  82. }
  83. /**
  84. * Add field filter to collection
  85. *
  86. * @param string|array $field
  87. * @param string|int|array|null $condition
  88. * @return \Magento\Cms\Model\ResourceModel\Block\Collection
  89. */
  90. public function addFieldToFilter($field, $condition = null)
  91. {
  92. if ($field == 'visitor_type') {
  93. $field = 'customer_id';
  94. if (is_array($condition) && isset($condition['eq'])) {
  95. $condition = $condition['eq'] == Visitor::VISITOR_TYPE_CUSTOMER ? ['gt' => 0] : ['null' => true];
  96. }
  97. }
  98. return parent::addFieldToFilter($field, $condition);
  99. }
  100. }