View source
<?php
namespace Drupal\rate\Plugin;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\votingapi\VoteResultFunctionManager;
use Drupal\Core\Entity\EntityFormBuilderInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
class RateWidgetBase extends PluginBase {
protected $entityTypeManager;
protected $votingapiResult;
protected $entityFormBuilder;
protected $account;
protected $requestStack;
protected $configFactory;
protected $moduleHandler;
protected $botDetector;
public function __construct(EntityTypeManagerInterface $entity_type_manager, VoteResultFunctionManager $vote_result, EntityFormBuilderInterface $form_builder, AccountInterface $account, RequestStack $request_stack, ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, RateBotDetector $bot_detector) {
$this->entityTypeManager = $entity_type_manager;
$this->votingapiResult = $vote_result;
$this->entityFormBuilder = $form_builder;
$this->account = $account;
$this->requestStack = $request_stack;
$this->configFactory = $config_factory;
$this->moduleHandler = $module_handler;
$this->botDetector = $bot_detector;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('plugin.manager.votingapi.resultfunction'), $container
->get('entity.form_builder'), $container
->get('current_user'), $container
->get('request_stack'), $container
->get('config.factory'), $container
->get('module_handler'), $container
->get('bot_detector'));
}
public function getLabel() {
return $this->label;
}
public function getValues() {
return $this
->getPluginDefinition()['values'];
}
public function getForm($entity_type, $entity_bundle, $entity_id, $vote_type, $value_type, $rate_widget, $settings, $user_id = NULL) {
$options = $settings
->get('options');
$vote = $this
->getEntityForVoting($entity_type, $entity_bundle, $entity_id, $vote_type, $value_type, $rate_widget, $settings, $user_id);
$this->moduleHandler
->invokeAll('rate_widget_options_alter', [
&$options,
$entity_type,
$entity_bundle,
$entity_id,
$rate_widget,
$settings,
$user_id,
]);
$new_options = [];
$option_classes = [];
foreach ($options as $option) {
$new_options[$option['value']] = isset($option['label']) ? $option['label'] : '';
$option_classes[$option['value']] = isset($option['class']) ? $option['class'] : '';
}
$form = $this->entityFormBuilder
->getForm($vote, 'rate_vote', [
'settings' => $settings,
'plugin' => $this,
'options' => $new_options,
'classes' => $option_classes,
'show_own_vote' => '1',
'readonly' => FALSE,
'entity_type' => $entity_type,
'entity_bundle' => $entity_bundle,
'entity_id' => $entity_id,
'vote_type' => $vote_type,
'rate_widget' => $rate_widget,
]);
return $form;
}
public function canVote($vote, $account = FALSE) {
if (!$account) {
$account = $this->account;
}
$entity = $this->entityTypeManager
->getStorage($vote
->getVotedEntityType())
->load($vote
->getVotedEntityId());
if (!$entity) {
return FALSE;
}
if ($vote
->getVotedEntityType() == 'comment') {
$perm = 'cast rate vote on ' . $entity
->getFieldName() . ' on ' . $entity
->getCommentedEntityTypeId() . ' of ' . $entity
->getCommentedEntity()
->bundle();
}
else {
$perm = 'cast rate vote on ' . $vote
->getVotedEntityType() . ' of ' . $entity
->bundle();
}
$can_vote = $account
->hasPermission($perm);
$this->moduleHandler
->invokeAll('rate_can_vote', [
&$can_vote,
$vote,
$entity,
$account,
]);
return $can_vote;
}
public function isBotVote() {
$is_bot_vote = $this->botDetector
->checkIsBot();
return $is_bot_vote;
}
public function getEntityForVoting($entity_type, $entity_bundle, $entity_id, $vote_type, $value_type, $rate_widget, $settings, $user_id) {
$storage = $this->entityTypeManager
->getStorage('vote');
$vote_data = [
'entity_type' => $entity_type,
'entity_id' => $entity_id,
'type' => $vote_type,
'value_type' => $value_type,
'rate_widget' => $rate_widget,
];
$vote_data['user_id'] = !is_null($user_id) ? $user_id : $this->account
->id();
$this->moduleHandler
->invokeAll('rate_vote_data_alter', [
&$vote_data,
$entity_type,
$entity_bundle,
$entity_id,
$rate_widget,
$settings,
$user_id,
]);
$vote = $storage
->create($vote_data);
$voting_settings = $settings
->get('voting');
$timestamp_offset = $this
->getWindow('user_window', $entity_type, $entity_bundle, $rate_widget, $voting_settings);
if ($this->account
->isAnonymous()) {
$vote_data['vote_source'] = hash('sha256', serialize($this->requestStack
->getCurrentRequest()
->getClientIp()));
$timestamp_offset = $this
->getWindow('anonymous_window', $entity_type, $entity_bundle, $rate_widget, $voting_settings);
}
$query = $this->entityTypeManager
->getStorage('vote')
->getQuery();
foreach ($vote_data as $key => $value) {
$query
->condition($key, $value);
}
if ($timestamp_offset >= 0) {
$query
->condition('timestamp', time() - $timestamp_offset, '>');
}
$votes = $query
->execute();
if ($votes && count($votes) > 0) {
$vote = $storage
->load(array_shift($votes));
}
else {
if ($vote
->isNew()) {
$vote
->setValue(NULL);
}
}
return $vote;
}
public function getResults($entity, $result_function = FALSE, $reset = FALSE) {
if ($reset) {
drupal_static_reset(__FUNCTION__);
}
$resultCache =& drupal_static(__FUNCTION__);
if (!$resultCache || !isset($resultCache[$entity
->getVotedEntityType()][$entity
->getVotedEntityId()]) || !$entity
->id()) {
$resultCache[$entity
->getVotedEntityType()][$entity
->getVotedEntityId()] = $this->votingapiResult
->getResults($entity
->getVotedEntityType(), $entity
->getVotedEntityId());
}
$result = isset($resultCache[$entity
->getVotedEntityType()][$entity
->getVotedEntityId()]) ? $resultCache[$entity
->getVotedEntityType()][$entity
->getVotedEntityId()] : [];
$result = !empty($result) && array_key_exists($entity
->bundle(), $result) ? $result[$entity
->bundle()] : [];
if ($result_function && array_key_exists($result_function, $result) && $result[$result_function]) {
$result = $result[$result_function];
}
return $result;
}
public function getWindow($window_type, $entity_type_id, $entity_bundle, $rate_widget, $voting_settings) {
$window_field_setting = isset($voting_settings[$window_type]) ? $voting_settings[$window_type] : -2;
$use_site_default = FALSE;
if ($window_field_setting === NULL || $window_field_setting === -2) {
$use_site_default = TRUE;
}
$window = $window_field_setting;
if ($use_site_default) {
$voting_configuration = $this->configFactory
->get('votingapi.settings');
$window = $voting_configuration
->get($window_type);
}
return $window;
}
public function getVoteSummary(ContentEntityInterface $vote) {
$results = $this
->getResults($vote);
$widget_name = $vote->rate_widget->value;
$widget = $this->entityTypeManager
->getStorage('rate_widget')
->load($widget_name);
$field_results = [];
foreach ($results as $key => $result) {
if (strpos($key, '.') && strpos($key, ':')) {
if (substr($key, strrpos($key, '.') + 1) === $widget_name) {
$key = explode(':', $key);
$field_results[$key[0]] = $result != 0 ? ceil($result * 10) / 10 : 0;
}
}
}
return [
'#theme' => 'rate_widgets_summary',
'#vote' => $vote,
'#results' => $field_results,
'#rate_widget' => $widget_name,
'#widget_template' => $widget
->get('template'),
];
}
public function getVotes($entity_type, $entity_bundle, $entity_id, $vote_type, $value_type, $rate_widget, $user_id = FALSE) {
$storage = $this->entityTypeManager
->getStorage('vote');
$vote_data = [
'entity_type' => $entity_type,
'entity_id' => $entity_id,
'type' => $vote_type,
'value_type' => $value_type,
'rate_widget' => $rate_widget,
];
if (!empty($user_id)) {
$vote_data['user_id'] = $user_id;
}
$query = $this->entityTypeManager
->getStorage('vote')
->getQuery();
foreach ($vote_data as $key => $value) {
$query
->condition($key, $value);
}
$votes = $query
->execute();
$vote_values = [];
if ($votes && count($votes) > 0) {
foreach ($votes as $id) {
$vote = $storage
->load($id);
$vote_values[$vote
->getValue()][] = $vote
->getValue();
}
}
return $vote_values;
}
}