diff --git a/application/Espo/Core/Utils/Database/Orm/Converter.php b/application/Espo/Core/Utils/Database/Orm/Converter.php index 7e6cbced47..cd02a2aa0a 100644 --- a/application/Espo/Core/Utils/Database/Orm/Converter.php +++ b/application/Espo/Core/Utils/Database/Orm/Converter.php @@ -308,6 +308,15 @@ class Converter 'type' => 'varchar', 'notStorable' => true, ); + + $ormMeta[$entityName]['fields']['followersIds'] = array( + 'type' => 'jsonArray', + 'notStorable' => true, + ); + $ormMeta[$entityName]['fields']['followersNames'] = array( + 'type' => 'jsonObject', + 'notStorable' => true, + ); } } //END: add a field 'isFollowed' for stream => true diff --git a/application/Espo/Services/Record.php b/application/Espo/Services/Record.php index 9a841cf301..c281a7bac7 100644 --- a/application/Espo/Services/Record.php +++ b/application/Espo/Services/Record.php @@ -63,6 +63,8 @@ class Record extends \Espo\Core\Services\Base protected $mergeLinkList = array(); + const FOLLOWERS_LIMIT = 10; + public function __construct() { parent::__construct(); @@ -169,6 +171,17 @@ class Record extends \Espo\Core\Services\Base } } + protected function loadFollowers(Entity $entity) + { + if ($this->getMetadata()->get("scopes.".$entity->getEntityType().".stream")) { + $data = $this->getStreamService()->getEntityFollowers($entity, self::FOLLOWERS_LIMIT); + if ($data) { + $entity->set('followersIds', $data['idList']); + $entity->set('followersNames', $data['nameMap']); + } + } + } + protected function loadIsEditable(Entity $entity) { $entity->set('isEditable', $this->getAcl()->check($entity, 'edit')); @@ -231,6 +244,7 @@ class Record extends \Espo\Core\Services\Base $this->loadLinkMultipleFields($entity); $this->loadParentNameFields($entity); $this->loadIsFollowed($entity); + $this->loadFollowers($entity); $this->loadEmailAddressField($entity); $this->loadPhoneNumberField($entity); $this->loadNotJoinedLinkFields($entity); diff --git a/application/Espo/Services/Stream.php b/application/Espo/Services/Stream.php index 07e4d5d962..933daf71f4 100644 --- a/application/Espo/Services/Stream.php +++ b/application/Espo/Services/Stream.php @@ -771,5 +771,43 @@ class Stream extends \Espo\Core\Services\Base $this->getEntityManager()->saveEntity($note); } } + + public function getEntityFollowers(Entity $entity, $limit = false) + { + $query = $this->getEntityManager()->getQuery(); + $pdo = $this->getEntityManager()->getPDO(); + + if (!$limit) { + $limit = 500; + } + + $sql = $query->createSelectQuery('User', array( + 'select' => ['id', 'name'], + 'join' => " + subscription AS `subscription` ON + subscription.user_id = user.id AND + subscription.entity_id = ".$query->quote($entity->id)." AND + subscription.entity_type = ".$query->quote($entity->getEntityType())." + ", + 'offset' => 0, + 'limit' => $limit + )); + + $sth = $pdo->prepare($sql); + $sth->execute(); + + $data = array( + 'idList' => [], + 'nameMap' => array() + ); + + while ($row = $sth->fetch(\PDO::FETCH_ASSOC)) { + $data['idList'][] = $row['id']; + $data['nameMap'][$row['id']] = $row['name']; + } + + return $data; + + } }