View source
<?php
namespace Drupal\rate;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\votingapi\VoteResultFunctionManager;
class RateVote {
use StringTranslationTrait;
protected $entityTypeManager;
protected $resultManager;
protected $database;
protected $botDetector;
protected $accountProxy;
protected $messenger;
public function __construct(EntityTypeManagerInterface $entity_type_manager, VoteResultFunctionManager $result_manager, Connection $database, RateBotDetector $bot_detector, AccountProxyInterface $account_proxy, MessengerInterface $messenger) {
$this->entityTypeManager = $entity_type_manager;
$this->resultManager = $result_manager;
$this->database = $database;
$this->botDetector = $bot_detector;
$this->accountProxy = $account_proxy;
$this->messenger = $messenger;
}
public function vote($entity_type_id, $entity_id, $vote_type_id, $value, $show_messages = TRUE) {
if (!$this
->validateVoteValue($vote_type_id, $value)) {
return;
}
$entity = $this->entityTypeManager
->getStorage($entity_type_id)
->load($entity_id);
$is_bot_vote = $this->botDetector
->checkIsBot();
if (!$is_bot_vote && $this->accountProxy
->hasPermission('cast rate vote on ' . $entity_type_id . ' of ' . $entity
->bundle())) {
$vote_storage = $this->entityTypeManager
->getStorage('vote');
$vote_ids = $vote_storage
->getUserVotes($this->accountProxy
->id(), $vote_type_id, $entity_type_id, $entity_id);
if (empty($vote_ids)) {
$vote_type = $this->entityTypeManager
->getStorage('vote_type')
->load($vote_type_id);
$vote = $vote_storage
->create([
'type' => $vote_type_id,
]);
$vote
->setVotedEntityId($entity_id);
$vote
->setVotedEntityType($entity_type_id);
$vote
->setValueType($vote_type
->getValueType());
$vote
->setValue($value);
$vote
->save();
$this->resultManager
->recalculateResults($entity_type_id, $entity_id, $vote_type_id);
if ($show_messages) {
$this->messenger
->addStatus($this
->t('Your :type vote was added.', [
':type' => $vote_type_id,
]));
}
}
elseif ($show_messages) {
$this->messenger
->addWarning($this
->t('You are not allowed to vote the same way multiple times.'));
}
}
}
public function undoVote($entity_type_id, $entity_id, $show_messages = TRUE) {
$entity = $this->entityTypeManager
->getStorage($entity_type_id)
->load($entity_id);
$is_bot_vote = $this->botDetector
->checkIsBot();
if (!$is_bot_vote && $this->accountProxy
->hasPermission('cast rate vote on ' . $entity_type_id . ' of ' . $entity
->bundle())) {
$vote_storage = $this->entityTypeManager
->getStorage('vote');
$vote_result = $vote_storage
->getUserVotes($this->accountProxy
->id(), NULL, $entity_type_id, $entity_id);
if (!empty($vote_result)) {
$vote_ids = array_keys($vote_result);
$vote_id = array_pop($vote_ids);
$vote = $vote_storage
->load($vote_id);
if ($vote) {
$vote
->delete();
}
if ($show_messages) {
$this->messenger
->addStatus($this
->t('Your vote was canceled.'));
}
}
elseif ($show_messages) {
$this->messenger
->addWarning($this
->t('A previous vote was not found.'));
}
}
}
public function validateVoteValue($vote_type_id, $value) {
$allowed_values = [
-1,
1,
];
if ($vote_type_id == 'fivestar') {
$allowed_values = [
1,
2,
3,
4,
5,
];
}
return in_array($value, $allowed_values);
}
}