FlaggingStorage.php in Flag 8.4
File
src/Entity/Storage/FlaggingStorage.php
View source
<?php
namespace Drupal\flag\Entity\Storage;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\Core\Session\AccountInterface;
class FlaggingStorage extends SqlContentEntityStorage implements FlaggingStorageInterface {
protected $flagIdsByEntity = [];
protected $globalFlagIdsByEntity = [];
public function resetCache(array $ids = NULL) {
parent::resetCache($ids);
$this->flagIdsByEntity = [];
$this->globalFlagIdsByEntity = [];
}
public function loadIsFlagged(EntityInterface $entity, AccountInterface $account, $session_id = NULL) {
if ($account
->isAnonymous() && is_null($session_id)) {
throw new \LogicException('Anonymous users must be identified by session_id');
}
$flag_ids = $this
->loadIsFlaggedMultiple([
$entity,
], $account, $session_id);
return $flag_ids[$entity
->id()];
}
public function loadIsFlaggedMultiple(array $entities, AccountInterface $account, $session_id = NULL) {
if ($account
->isAnonymous() && is_null($session_id)) {
throw new \LogicException('Anonymous users must be identified by session_id');
}
if (!$account
->isAnonymous()) {
$session_id = 0;
}
$flag_ids_by_entity = [];
if (!$entities) {
return $flag_ids_by_entity;
}
$entity_type_id = reset($entities)
->getEntityTypeId();
$ids_to_load = [];
foreach ($entities as $entity) {
if (isset($this->flagIdsByEntity[$account
->id()][$session_id][$entity_type_id][$entity
->id()])) {
$flag_ids_by_entity[$entity
->id()] = array_merge($this->flagIdsByEntity[$account
->id()][$session_id][$entity_type_id][$entity
->id()], $this->globalFlagIdsByEntity[$entity_type_id][$entity
->id()]);
}
else {
$ids_to_load[$entity
->id()] = [];
}
}
if (!$ids_to_load) {
return $flag_ids_by_entity;
}
if (!isset($this->flagIdsByEntity[$account
->id()][$session_id][$entity_type_id])) {
$this->flagIdsByEntity[$account
->id()][$session_id][$entity_type_id] = [];
}
if (!isset($this->globalFlagIdsByEntity[$entity_type_id])) {
$this->globalFlagIdsByEntity[$entity_type_id] = [];
}
$this->flagIdsByEntity[$account
->id()][$session_id][$entity_type_id] += $ids_to_load;
$this->globalFlagIdsByEntity[$entity_type_id] += $ids_to_load;
$flag_ids_by_entity += $ids_to_load;
$query = $this->database
->select('flagging', 'f')
->fields('f', [
'entity_id',
'flag_id',
'global',
])
->condition('entity_type', $entity_type_id)
->condition('entity_id', array_keys($ids_to_load), 'IN');
$user_or_global_condition = $query
->orConditionGroup()
->condition('global', 1);
if ($account
->isAnonymous()) {
$uid_and_session_condition = $query
->andConditionGroup()
->condition('uid', $account
->id())
->condition('session_id', $session_id);
$user_or_global_condition
->condition($uid_and_session_condition);
}
else {
$user_or_global_condition
->condition('uid', $account
->id());
}
$result = $query
->condition($user_or_global_condition)
->execute();
foreach ($result as $row) {
if ($row->global) {
$this->globalFlagIdsByEntity[$entity_type_id][$row->entity_id][$row->flag_id] = $row->flag_id;
}
else {
$this->flagIdsByEntity[$account
->id()][$session_id][$entity_type_id][$row->entity_id][$row->flag_id] = $row->flag_id;
}
$flag_ids_by_entity[$row->entity_id][$row->flag_id] = $row->flag_id;
}
return $flag_ids_by_entity;
}
protected function doPostSave(EntityInterface $entity, $update) {
parent::doPostSave($entity, $update);
if ($entity
->get('global')->value) {
if (isset($this->globalFlagIdsByEntity[$entity
->get('entity_type')->value][$entity
->get('entity_id')->value])) {
$this->globalFlagIdsByEntity[$entity
->get('entity_type')->value][$entity
->get('entity_id')->value][$entity
->get('flag_id')->value] = $entity
->get('flag_id')->value;
}
}
else {
if (isset($this->flagIdsByEntity[$entity
->get('uid')->target_id][$entity
->get('entity_type')->value][$entity
->get('entity_id')->value])) {
$this->flagIdsByEntity[$entity
->get('uid')->target_id][$entity
->get('entity_type')->value][$entity
->get('entity_id')->value][$entity
->get('flag_id')->value] = $entity
->get('flag_id')->value;
}
}
}
protected function doDelete($entities) {
parent::doDelete($entities);
foreach ($entities as $entity) {
if ($entity
->get('global')->value) {
if (isset($this->globalFlagIdsByEntity[$entity
->get('entity_type')->value][$entity
->get('entity_id')->value][$entity
->get('flag_id')->value])) {
unset($this->globalFlagIdsByEntity[$entity
->get('entity_type')->value][$entity
->get('entity_id')->value][$entity
->get('flag_id')->value]);
}
}
else {
if (isset($this->flagIdsByEntity[$entity
->get('uid')->target_id][$entity
->get('entity_type')->value][$entity
->get('entity_id')->value][$entity
->get('flag_id')->value])) {
unset($this->flagIdsByEntity[$entity
->get('uid')->target_id][$entity
->get('entity_type')->value][$entity
->get('entity_id')->value][$entity
->get('flag_id')->value]);
}
}
}
}
}