View source
<?php
namespace Drupal\heartbeat;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Entity\EntityTypeRepository;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Database\Connection;
use Drupal\Core\Config\ConfigFactoryInterface;
class HeartbeatStreamServices {
protected $lastId;
protected $latestTimestamp;
protected $entityTypeManager;
protected $entityTypeRepository;
protected $entityQuery;
protected $configFactory;
protected $database;
public function __construct(EntityTypeManager $entityTypeManager, EntityTypeRepository $entityTypeRepository, QueryFactory $entityQuery, ConfigFactoryInterface $configFactory, Connection $database) {
$this->entityTypeManager = $entityTypeManager;
$this->entityTypeRepository = $entityTypeRepository;
$this->entityQuery = $entityQuery;
$this->configFactory = $configFactory;
$this->database = $database;
}
public function getEntityById($id) {
return $this->entityTypeManager
->getStorage('heartbeat_stream')
->load($id);
}
public function getTypesById($id) {
return $this->entityTypeManager
->getStorage('heartbeat_stream')
->load($id)
->get('types');
}
public function loadAllEntities() {
return $this->entityQuery
->get('heartbeat_stream')
->execute();
}
public function loadStream($type) {
return $this->entityQuery
->get('heartbeat_stream')
->condition('name', $type)
->execute();
}
public function loadAllStreams() {
$types = null;
foreach ($this
->getAllStreams() as $stream) {
foreach ($stream
->getTypes() as $type) {
$type = $type
->getValue();
$type = key($type) === 'target_id' ? $type : $type
->getValue()[0];
if (strlen($type['target_id']) > 1) {
$types[] = $type;
}
}
}
$cleanTypes = array_column($types, 'target_id');
return $this->entityTypeManager
->getStorage('heartbeat')
->loadMultiple($this->entityQuery
->get('heartbeat')
->condition('status', 1)
->condition('type', array_column($types, 'target_id'), 'IN')
->sort('created', 'DESC')
->range(0, 25)
->execute());
}
public function getAllStreams() {
return $this->entityTypeManager
->getStorage('heartbeat_stream')
->loadMultiple($this
->loadAllEntities());
}
public function createStreamForUids($uids) {
return $this->entityTypeManager
->getStorage('heartbeat')
->loadMultiple($this->entityQuery
->get('heartbeat')
->condition('status', 1)
->condition('uid', $uids, 'IN')
->sort('created', 'DESC')
->range(0, 25)
->execute());
}
public function createStreamByType($type) {
$stream = $this->entityTypeManager
->getStorage('heartbeat_stream')
->load(array_values($this
->loadStream($type))[0]);
if ($stream !== null) {
$types = array();
foreach ($stream
->getTypes() as $heartbeatType) {
$value = $heartbeatType
->getValue()['target_id'];
if ($value !== "0") {
$types[] = $value;
}
}
$beats = $this->entityTypeManager
->getStorage('heartbeat')
->loadMultiple($this->entityQuery
->get('heartbeat')
->condition('status', 1)
->condition('type', $types, 'IN')
->sort('created', 'DESC')
->range(0, 25)
->execute());
if (count($beats) > 0) {
$this->lastId = call_user_func('end', array_keys($beats));
$this->configFactory
->getEditable('heartbeat_update_feed.settings')
->set('lastId', $this->lastId)
->set('update', false)
->set('timestamp', array_values($beats)[0]
->getRevisionCreationTime())
->save();
return $beats;
}
}
return null;
}
public function createStreamForUidsByType($uids, $type) {
$stream = $this->entityTypeManager
->getStorage('heartbeat_stream')
->load(array_values($this
->loadStream($type))[0]);
if ($stream !== null) {
$types = array();
foreach ($stream
->getTypes() as $heartbeatType) {
$value = $heartbeatType
->getValue()['target_id'];
if ($value !== "0") {
$types[] = $value;
}
}
$beats = $this->entityTypeManager
->getStorage('heartbeat')
->loadMultiple($this->entityQuery
->get('heartbeat')
->condition('status', 1)
->condition('type', $types, 'IN')
->condition('uid', $uids, 'IN')
->sort('created', 'DESC')
->range(0, 25)
->execute());
if (count($beats) > 0) {
$this->lastId = call_user_func('end', array_keys($beats));
$this->configFactory
->getEditable('heartbeat_update_feed.settings')
->set('lastId', $this->lastId)
->set('update', false)
->set('timestamp', array_values($beats)[0]
->getRevisionCreationTime())
->save();
return $beats;
}
}
return null;
}
public function getOlderStreamForUidsByType($uids, $type, $hid) {
$stream = $this->entityTypeManager
->getStorage('heartbeat_stream')
->load(array_values($this
->loadStream($type))[0]);
if ($stream !== null) {
$types = array();
foreach ($stream
->getTypes() as $heartbeatType) {
$value = $heartbeatType
->getValue()['target_id'];
if ($value !== "0") {
$types[] = $value;
}
}
$beats = $this->entityTypeManager
->getStorage('heartbeat')
->loadMultiple($this->entityQuery
->get('heartbeat')
->condition('status', 1)
->condition('id', $hid, '<')
->condition('type', $types, 'IN')
->condition('uid', $uids, 'IN')
->sort('created', 'DESC')
->range(0, 25)
->execute());
if (count($beats) > 0) {
$this->lastId = call_user_func('end', array_keys($beats));
$this->configFactory
->getEditable('heartbeat_update_feed.settings')
->set('lastId', $this->lastId)
->set('update', false)
->set('timestamp', array_values($beats)[0]
->getRevisionCreationTime())
->save();
return $beats;
}
}
return null;
}
public function createHashStreamForUidsByType($uids, $type, $tid) {
$query = $this->database
->query('
SELECT id
FROM heartbeat_field_revision hr
INNER JOIN node n ON n.nid = hr.nid
INNER JOIN node__field_tags fu ON fu.entity_id = n.nid
WHERE fu.field_tags_target_id = :tid', array(
':tid' => $tid,
));
$hids = array();
foreach ($query
->fetchAll() as $id) {
$hids[] = $id->id;
}
if (!empty($hids)) {
$beats = $this->entityTypeManager
->getStorage('heartbeat')
->loadMultiple($this->entityQuery
->get('heartbeat')
->condition('status', 1)
->condition('uid', $uids, 'IN')
->condition('id', $hids, 'IN')
->sort('created', 'DESC')
->execute());
if (count($beats) > 0) {
$this->lastId = call_user_func('end', array_keys($beats));
$this->configFactory
->getEditable('heartbeat_update_feed.settings')
->set('lastId', $this->lastId)
->set('update', FALSE)
->set('timestamp', array_values($beats)[0]
->getRevisionCreationTime())
->save();
return $beats;
}
}
return null;
}
public function updateStreamForUidsByType($uids, $type) {
$currentUid = \Drupal::currentUser()
->id();
$stream = $this->entityTypeManager
->getStorage('heartbeat_stream')
->load(array_values($this
->loadStream($type))[0]);
$uids[] = $currentUid;
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());
}
public function createUsernameStreamForUidsByType($uids, $feed, $tid) {
$query = $this->database
->query('
SELECT coalesce(hr.id)
FROM heartbeat_field_revision hr
INNER JOIN node__field_username un
ON un.entity_id = hr.nid
LEFT JOIN node__field_users u
ON u.entity_id = hr.nid
WHERE u.field_users_target_id = :tid
OR un.field_username_target_id = :tid', array(
':tid' => $tid,
));
$hids = array();
foreach ($query
->fetchAllKeyed() as $id => $row) {
$hids[] = $id;
}
if (!empty($hids)) {
$beats = $this->entityTypeManager
->getStorage('heartbeat')
->loadMultiple($this->entityQuery
->get('heartbeat')
->condition('status', 1)
->condition('uid', $uids, 'IN')
->condition('id', $hids, 'IN')
->sort('created', 'DESC')
->execute());
if (count($beats) > 0) {
$this->lastId = call_user_func('end', array_keys($beats));
$this->configFactory
->getEditable('heartbeat_update_feed.settings')
->set('lastId', $this->lastId)
->set('update', FALSE)
->set('timestamp', array_values($beats)[0]
->getRevisionCreationTime())
->save();
return $beats;
}
}
return null;
}
}