View source
<?php
namespace Drupal\flag;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\SessionManagerInterface;
use Drupal\user\Entity\User;
use Drupal\user\UserInterface;
class FlagService implements FlagServiceInterface {
protected $currentUser;
protected $entityTypeManager;
protected $sessionManager;
public function __construct(AccountInterface $current_user, EntityTypeManagerInterface $entity_type_manager, SessionManagerInterface $session_manager) {
$this->currentUser = $current_user;
$this->entityTypeManager = $entity_type_manager;
$this->sessionManager = $session_manager;
}
public function getAllFlags($entity_type = NULL, $bundle = NULL) {
$query = $this->entityTypeManager
->getStorage('flag')
->getQuery();
if ($entity_type != NULL) {
$query
->condition('entity_type', $entity_type);
}
$ids = $query
->execute();
$flags = $this
->getFlagsByIds($ids);
if (isset($bundle)) {
$flags = array_filter($flags, function (FlagInterface $flag) use ($bundle) {
$bundles = $flag
->getApplicableBundles();
return in_array($bundle, $bundles);
});
}
return $flags;
}
public function getFlagging(FlagInterface $flag, EntityInterface $entity, AccountInterface $account = NULL, $session_id = NULL) {
$this
->populateFlaggerDefaults($account, $session_id);
$flaggings = $this
->getEntityFlaggings($flag, $entity, $account, $session_id);
return !empty($flaggings) ? reset($flaggings) : NULL;
}
protected function ensureSession() {
if ($this->currentUser
->isAnonymous() && !$this->sessionManager
->isStarted()) {
$_SESSION['flag'] = TRUE;
$this->sessionManager
->start();
}
}
public function populateFlaggerDefaults(AccountInterface &$account = NULL, &$session_id = NULL) {
if (!isset($account)) {
$account = $this->currentUser;
if (!isset($session_id) && $account
->isAnonymous()) {
$session_id = $this->sessionManager
->getId();
}
}
elseif ($account
->isAnonymous() && $session_id === NULL) {
throw new \LogicException('Anonymous users must be identified by session_id');
}
}
public function getEntityFlaggings(FlagInterface $flag, EntityInterface $entity, AccountInterface $account = NULL, $session_id = NULL) {
$query = $this->entityTypeManager
->getStorage('flagging')
->getQuery();
$query
->condition('flag_id', $flag
->id());
if (!is_null($account)) {
if (!$flag
->isGlobal()) {
$query
->condition('uid', $account
->id());
if ($account
->isAnonymous()) {
if (empty($session_id)) {
throw new \LogicException('An anonymous user must be identified by session ID.');
}
$query
->condition('session_id', $session_id);
}
}
}
$query
->condition('entity_type', $entity
->getEntityTypeId())
->condition('entity_id', $entity
->id());
$ids = $query
->execute();
return $this
->getFlaggingsByIds($ids);
}
public function getAllEntityFlaggings(EntityInterface $entity, AccountInterface $account = NULL, $session_id = NULL) {
$query = $this->entityTypeManager
->getStorage('flagging')
->getQuery();
if (!empty($account)) {
$global_or_user = $query
->orConditionGroup()
->condition('global', 1)
->condition('uid', $account
->id());
$query
->condition($global_or_user);
if ($account
->isAnonymous()) {
if (empty($session_id)) {
throw new \LogicException('An anonymous user must be identified by session ID.');
}
$query
->condition('session_id', $session_id);
}
}
$query
->condition('entity_type', $entity
->getEntityTypeId())
->condition('entity_id', $entity
->id());
$ids = $query
->execute();
return $this
->getFlaggingsByIds($ids);
}
public function getFlagById($flag_id) {
return $this->entityTypeManager
->getStorage('flag')
->load($flag_id);
}
public function getFlaggableById(FlagInterface $flag, $entity_id) {
return $this->entityTypeManager
->getStorage($flag
->getFlaggableEntityTypeId())
->load($entity_id);
}
public function getFlaggingUsers(EntityInterface $entity, FlagInterface $flag = NULL) {
$query = $this->entityTypeManager
->getStorage('flagging')
->getQuery();
$query
->condition('entity_type', $entity
->getEntityTypeId())
->condition('entity_id', $entity
->id());
if (!empty($flag)) {
$query
->condition('flag_id', $flag
->id());
}
$ids = $query
->execute();
$flaggings = $this
->getFlaggingsByIds($ids);
$user_ids = [];
foreach ($flaggings as $flagging) {
$user_ids[] = $flagging
->get('uid')
->first()
->getValue()['target_id'];
}
return $this->entityTypeManager
->getStorage('user')
->loadMultiple($user_ids);
}
public function flag(FlagInterface $flag, EntityInterface $entity, AccountInterface $account = NULL, $session_id = NULL) {
$bundles = $flag
->getBundles();
$this
->ensureSession();
$this
->populateFlaggerDefaults($account, $session_id);
if ($flag
->getFlaggableEntityTypeId() != $entity
->getEntityTypeId()) {
throw new \LogicException('The flag does not apply to entities of this type.');
}
if (!empty($bundles) && !in_array($entity
->bundle(), $bundles)) {
throw new \LogicException('The flag does not apply to the bundle of the entity.');
}
if ($flag
->isFlagged($entity, $account, $session_id)) {
throw new \LogicException('The user has already flagged the entity with the flag.');
}
$flagging = $this->entityTypeManager
->getStorage('flagging')
->create([
'uid' => $account
->id(),
'session_id' => $session_id,
'flag_id' => $flag
->id(),
'entity_id' => $entity
->id(),
'entity_type' => $entity
->getEntityTypeId(),
'global' => $flag
->isGlobal(),
]);
$flagging
->save();
return $flagging;
}
public function unflag(FlagInterface $flag, EntityInterface $entity, AccountInterface $account = NULL, $session_id = NULL) {
$bundles = $flag
->getBundles();
$this
->populateFlaggerDefaults($account, $session_id);
if ($flag
->getFlaggableEntityTypeId() != $entity
->getEntityTypeId()) {
throw new \LogicException('The flag does not apply to entities of this type.');
}
if (!empty($bundles) && !in_array($entity
->bundle(), $bundles)) {
throw new \LogicException('The flag does not apply to the bundle of the entity.');
}
$flagging = $this
->getFlagging($flag, $entity, $account, $session_id);
if (!$flagging) {
throw new \LogicException('The entity is not flagged by the user.');
}
$flagging
->delete();
}
public function unflagAllByFlag(FlagInterface $flag) {
$query = $this->entityTypeManager
->getStorage('flagging')
->getQuery();
$query
->condition('flag_id', $flag
->id());
$ids = $query
->execute();
$flaggings = $this
->getFlaggingsByIds($ids);
$this->entityTypeManager
->getStorage('flagging')
->delete($flaggings);
}
public function unflagAllByEntity(EntityInterface $entity) {
$query = $this->entityTypeManager
->getStorage('flagging')
->getQuery();
$query
->condition('entity_type', $entity
->getEntityTypeId())
->condition('entity_id', $entity
->id());
$ids = $query
->execute();
$flaggings = $this
->getFlaggingsByIds($ids);
$this->entityTypeManager
->getStorage('flagging')
->delete($flaggings);
}
public function unflagAllByUser(AccountInterface $account, $session_id = NULL) {
$query = $this->entityTypeManager
->getStorage('flagging')
->getQuery();
$query
->condition('uid', $account
->id());
if ($account
->isAnonymous()) {
if (empty($session_id)) {
throw new \LogicException('An anonymous user must be identified by session ID.');
}
$query
->condition('session_id', $session_id);
}
$ids = $query
->execute();
$flaggings = $this
->getFlaggingsByIds($ids);
$this->entityTypeManager
->getStorage('flagging')
->delete($flaggings);
}
public function userFlagRemoval(UserInterface $account) {
$this
->unflagAllByUser($account);
$this
->unflagAllByEntity($account);
}
protected function getFlagsByIds(array $ids) {
return $this->entityTypeManager
->getStorage('flag')
->loadMultiple($ids);
}
protected function getFlaggingsByIds(array $ids) {
return $this->entityTypeManager
->getStorage('flagging')
->loadMultiple($ids);
}
}