Event.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Reports\Model;
  7. /**
  8. * Events model
  9. *
  10. * @method string getLoggedAt()
  11. * @method \Magento\Reports\Model\Event setLoggedAt(string $value)
  12. * @method int getEventTypeId()
  13. * @method \Magento\Reports\Model\Event setEventTypeId(int $value)
  14. * @method int getObjectId()
  15. * @method \Magento\Reports\Model\Event setObjectId(int $value)
  16. * @method int getSubjectId()
  17. * @method \Magento\Reports\Model\Event setSubjectId(int $value)
  18. * @method int getSubtype()
  19. * @method \Magento\Reports\Model\Event setSubtype(int $value)
  20. * @method int getStoreId()
  21. * @method \Magento\Reports\Model\Event setStoreId(int $value)
  22. *
  23. * @author Magento Core Team <core@magentocommerce.com>
  24. * @api
  25. * @since 100.0.2
  26. */
  27. class Event extends \Magento\Framework\Model\AbstractModel
  28. {
  29. const EVENT_PRODUCT_VIEW = 1;
  30. const EVENT_PRODUCT_SEND = 2;
  31. const EVENT_PRODUCT_COMPARE = 3;
  32. const EVENT_PRODUCT_TO_CART = 4;
  33. const EVENT_PRODUCT_TO_WISHLIST = 5;
  34. const EVENT_WISHLIST_SHARE = 6;
  35. /**
  36. * @var \Magento\Framework\Stdlib\DateTime\DateTimeFactory
  37. */
  38. protected $_dateFactory;
  39. /**
  40. * @var \Magento\Reports\Model\Event\TypeFactory
  41. */
  42. protected $_eventTypeFactory;
  43. /**
  44. * @param \Magento\Framework\Model\Context $context
  45. * @param \Magento\Framework\Registry $registry
  46. * @param \Magento\Framework\Stdlib\DateTime\DateTimeFactory $dateFactory
  47. * @param \Magento\Reports\Model\Event\TypeFactory $eventTypeFactory
  48. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  49. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  50. * @param array $data
  51. */
  52. public function __construct(
  53. \Magento\Framework\Model\Context $context,
  54. \Magento\Framework\Registry $registry,
  55. \Magento\Framework\Stdlib\DateTime\DateTimeFactory $dateFactory,
  56. \Magento\Reports\Model\Event\TypeFactory $eventTypeFactory,
  57. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  58. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  59. array $data = []
  60. ) {
  61. parent::__construct($context, $registry, $resource, $resourceCollection, $data);
  62. $this->_dateFactory = $dateFactory;
  63. $this->_eventTypeFactory = $eventTypeFactory;
  64. }
  65. /**
  66. * Initialize resource
  67. *
  68. * @return void
  69. */
  70. protected function _construct()
  71. {
  72. $this->_init(\Magento\Reports\Model\ResourceModel\Event::class);
  73. }
  74. /**
  75. * Before Event save process
  76. *
  77. * @return $this
  78. */
  79. public function beforeSave()
  80. {
  81. $date = $this->_dateFactory->create();
  82. $this->setLoggedAt($date->gmtDate());
  83. return parent::beforeSave();
  84. }
  85. /**
  86. * Update customer type after customer login
  87. *
  88. * @param int $visitorId
  89. * @param int $customerId
  90. * @param array $types
  91. * @return $this
  92. */
  93. public function updateCustomerType($visitorId, $customerId, $types = null)
  94. {
  95. if ($types === null) {
  96. $types = [];
  97. $typesCollection = $this->_eventTypeFactory->create()->getCollection();
  98. foreach ($typesCollection as $eventType) {
  99. if ($eventType->getCustomerLogin()) {
  100. $types[$eventType->getId()] = $eventType->getId();
  101. }
  102. }
  103. }
  104. $this->getResource()->updateCustomerType($this, $visitorId, $customerId, $types);
  105. return $this;
  106. }
  107. /**
  108. * Clean events (visitors)
  109. *
  110. * @return $this
  111. */
  112. public function clean()
  113. {
  114. $this->getResource()->clean($this);
  115. return $this;
  116. }
  117. }