HeartbeatStreamServices.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace Drupal\heartbeat;
  3. use Drupal\Core\Entity\EntityTypeManager;
  4. use Drupal\Core\Entity\EntityTypeRepository;
  5. use Drupal\Core\Entity\Query\QueryFactory;
  6. use Drupal\Core\Config\ConfigFactoryInterface;
  7. /**
  8. * Class HeartbeatStreamServices.
  9. *
  10. * @package Drupal\heartbeat
  11. */
  12. class HeartbeatStreamServices {
  13. protected $lastId;
  14. protected $latestTimestamp;
  15. /**
  16. * Drupal\Core\Entity\EntityTypeManager definition.
  17. *
  18. * @var EntityTypeManager
  19. */
  20. protected $entityTypeManager;
  21. /**
  22. * Drupal\Core\Entity\EntityTypeRepository definition.
  23. *
  24. * @var EntityTypeRepository
  25. */
  26. protected $entityTypeRepository;
  27. /**
  28. * Drupal\Core\Entity\Query\QueryFactory definition.
  29. *
  30. * @var \Drupal\Core\Entity\Query\QueryFactory
  31. */
  32. protected $entityQuery;
  33. /**
  34. * The configuration factory.
  35. *
  36. * @var \Drupal\Core\Config\ConfigFactoryInterface
  37. */
  38. protected $configFactory;
  39. /**
  40. * Constructor.
  41. * @param EntityTypeManager $entityTypeManager
  42. * @param EntityTypeRepository $entityTypeRepository
  43. * @param QueryFactory $entityQuery
  44. */
  45. public function __construct(EntityTypeManager $entityTypeManager, EntityTypeRepository $entityTypeRepository, QueryFactory $entityQuery, ConfigFactoryInterface $configFactory) {
  46. $this->entityTypeManager = $entityTypeManager;
  47. $this->entityTypeRepository = $entityTypeRepository;
  48. $this->entityQuery = $entityQuery;
  49. $this->configFactory = $configFactory;
  50. }
  51. /**
  52. * Returns a loaded HeartbeatStream entity
  53. * @param $id
  54. * @return \Drupal\Core\Entity\EntityInterface|null
  55. * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
  56. */
  57. public function getEntityById($id) {
  58. return $this->entityTypeManager->getStorage('heartbeat_stream')->load($id);
  59. }
  60. /**
  61. * Returns an array of HeartbeatType strings for a given
  62. * HeartbeatStream specified by ID
  63. * @param $id
  64. * @return mixed
  65. * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
  66. */
  67. public function getTypesById($id) {
  68. return $this->entityTypeManager->getStorage('heartbeat_stream')->load($id)->get('types');
  69. }
  70. /**
  71. * Returns an array of HeartbeatStream entities
  72. * HeartbeatStream specified by ID
  73. * @return mixed
  74. */
  75. public function loadAllEntities() {
  76. return $this->entityQuery->get('heartbeat_stream')->execute();
  77. }
  78. public function loadStream($type) {
  79. return $this->entityQuery->get('heartbeat_stream')->condition('name', $type)->execute();
  80. }
  81. public function loadAllStreams() {
  82. $types = null;
  83. foreach ($this->getAllStreams() as $stream) {
  84. foreach ($stream->getTypes() as $type) {
  85. $types[] = $type;
  86. }
  87. }
  88. return $this->entityTypeManager->getStorage('heartbeat')->loadMultiple($this->entityQuery->get('heartbeat')->condition('status', 1)->condition('type', array_column($types, 'target_id'), 'IN')->sort('created', 'DESC')->execute());
  89. }
  90. /*
  91. * Load all available HeartbeatStream entities
  92. */
  93. public function getAllStreams() {
  94. return $this->entityTypeManager->getStorage('heartbeat_stream')->loadMultiple($this->loadAllEntities());
  95. }
  96. public function createStreamForUids($uids) {
  97. return $this->entityTypeManager->getStorage('heartbeat')->loadMultiple($this->entityQuery->get('heartbeat')->condition('status', 1)->condition('uid', $uids, 'IN')->sort('created', 'DESC')->execute());
  98. }
  99. public function createStreamByType($type) {
  100. $stream = $this->entityTypeManager->getStorage('heartbeat_stream')->load(array_values($this->loadStream($type))[0]);
  101. if ($stream !== null) {
  102. $types = array();
  103. foreach ($stream->getTypes() as $heartbeatType) {
  104. if (!empty($heartbeatType['target_id']) && $heartbeatType['target_id'] !== "0") {
  105. $types[] = $heartbeatType['target_id'];
  106. }
  107. }
  108. $beats = $this->entityTypeManager->getStorage('heartbeat')->loadMultiple($this->entityQuery->get('heartbeat')->condition('status', 1)->condition('type', $types, 'IN')->sort('created', 'DESC')->execute());
  109. if (count($beats) > 0) {
  110. $this->lastId = call_user_func('end', array_keys($beats));
  111. $this->configFactory->getEditable('heartbeat_update_feed.settings')->set('lastId', $this->lastId)->set('update', false)->set('timestamp', array_values($beats)[0]->getRevisionCreationTime())->save();
  112. return $beats;
  113. }
  114. }
  115. return null;
  116. }
  117. public function createStreamForUidsByType($uids, $type) {
  118. $currentUid = \Drupal::currentUser()->id();
  119. $stream = $this->entityTypeManager->getStorage('heartbeat_stream')->load(array_values($this->loadStream($type))[0]);
  120. if ($stream !== null) {
  121. $types = array();
  122. foreach ($stream->getTypes() as $heartbeatType) {
  123. if (!empty($heartbeatType['target_id']) && $heartbeatType['target_id'] !== "0") {
  124. $types[] = $heartbeatType['target_id'];
  125. }
  126. }
  127. $uids[] = $currentUid;
  128. $beats = $this->entityTypeManager->getStorage('heartbeat')->loadMultiple($this->entityQuery->get('heartbeat')->condition('status', 1)->condition('type', $types, 'IN')->condition('uid', $uids, 'IN')->sort('created', 'DESC')->execute());
  129. if (count($beats) > 0) {
  130. $this->lastId = call_user_func('end', array_keys($beats));
  131. $this->configFactory->getEditable('heartbeat_update_feed.settings')->set('lastId', $this->lastId)->set('update', false)->set('timestamp', array_values($beats)[0]->getRevisionCreationTime())->save();
  132. return $beats;
  133. }
  134. }
  135. return null;
  136. }
  137. public function updateStreamForUidsByType($uids, $type) {
  138. $currentUid = \Drupal::currentUser()->id();
  139. $stream = $this->entityTypeManager->getStorage('heartbeat_stream')->load(array_values($this->loadStream($type))[0]);
  140. $uids[] = $currentUid;
  141. return $this->entityTypeManager->getStorage('heartbeat')->loadMultiple($this->entityQuery->get('heartbeat')->condition('status', 1)->condition('revision_created', $this->latestTimestamp, '>')->condition('type', array_column($stream->getTypes(), 'target_id'), 'IN')->condition('uid', $uids, 'IN')->sort('created', 'DESC')->execute());
  142. }
  143. }