View source
<?php
namespace Drupal\votingapi_widgets\Plugin;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\votingapi\Entity\Vote;
use Drupal\votingapi\VoteResultFunctionManager;
use Drupal\Core\Entity\EntityFormBuilderInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\Core\Config\ConfigFactoryInterface;
abstract class VotingApiWidgetBase extends PluginBase implements VotingApiWidgetInterface, ContainerFactoryPluginInterface {
protected $entityTypeManager;
protected $votingapiResult;
protected $entityFormBuilder;
protected $account;
protected $requestStack;
protected $configFactory;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, VoteResultFunctionManager $vote_result, EntityFormBuilderInterface $form_builder, AccountInterface $account, RequestStack $request_stack, ConfigFactoryInterface $config_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->votingapiResult = $vote_result;
$this->entityFormBuilder = $form_builder;
$this->account = $account;
$this->requestStack = $request_stack;
$this->configFactory = $config_factory;
}
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'));
}
public function getLabel() {
return $this->label;
}
public function getValues() {
return $this
->getPluginDefinition()['values'];
}
public function getForm($entity_type, $entity_bundle, $entity_id, $vote_type, $field_name, $settings) {
$vote = $this
->getEntityForVoting($entity_type, $entity_bundle, $entity_id, $vote_type, $field_name);
return $this->entityFormBuilder
->getForm($vote, 'votingapi_' . $this
->getPluginId(), [
'options' => $this
->getPluginDefinition()['values'],
'settings' => $settings,
'plugin' => $this,
'entity_type' => $entity_type,
'entity_bundle' => $entity_bundle,
'entity_id' => $entity_id,
'vote_type' => $vote_type,
'field_name' => $field_name,
]);
}
public abstract function getInitialVotingElement(array &$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;
}
$perm = 'vote on ' . $vote
->getVotedEntityType() . ':' . $entity
->bundle() . ':' . $vote->field_name->value;
if (!$vote
->isNew()) {
$perm = 'edit own vote on ' . $vote
->getVotedEntityType() . ':' . $entity
->bundle() . ':' . $vote->field_name->value;
}
return $account
->hasPermission($perm);
}
public function getEntityForVoting($entity_type, $entity_bundle, $entity_id, $vote_type, $field_name) {
$storage = $this->entityTypeManager
->getStorage('vote');
$voteData = [
'entity_type' => $entity_type,
'entity_id' => $entity_id,
'type' => $vote_type,
'field_name' => $field_name,
'user_id' => $this->account
->id(),
];
$vote = $storage
->create($voteData);
$timestamp_offset = $this
->getWindow('user_window', $entity_type, $entity_bundle, $field_name);
if ($this->account
->isAnonymous()) {
$voteData['vote_source'] = Vote::getCurrentIp();
$timestamp_offset = $this
->getWindow('anonymous_window', $entity_type, $entity_bundle, $field_name);
}
$query = $this->entityTypeManager
->getStorage('vote')
->getQuery();
foreach ($voteData as $key => $value) {
$query
->condition($key, $value);
}
if (!empty($timestamp_offset)) {
$query
->condition('timestamp', time() - $timestamp_offset, '>=');
}
$votes = $query
->execute();
if ($votes && count($votes) > 0) {
$vote = $storage
->load(array_shift($votes));
}
return $vote;
}
public function getResults($entity, $result_function = FALSE, $reset = FALSE) {
if ($reset) {
drupal_static_reset(__FUNCTION__);
}
$resultCache =& drupal_static(__FUNCTION__);
if (!$resultCache) {
$resultCache = $this->votingapiResult
->getResults($entity
->getVotedEntityType(), $entity
->getVotedEntityId());
}
if ($result_function) {
if (!$resultCache[$entity
->getEntityTypeId()][$entity
->getVotedEntityId()][$result_function]) {
return [];
}
return $resultCache[$entity
->getEntityTypeId()][$entity
->getVotedEntityId()][$result_function];
}
if (!$result_function) {
if (!isset($resultCache[$entity
->getEntityTypeId()][$entity
->getVotedEntityId()])) {
return [];
}
return $resultCache[$entity
->getEntityTypeId()][$entity
->getVotedEntityId()];
}
return [];
}
public function getWindow($window_type, $entity_type_id, $entity_bundle, $field_name) {
$config = FieldConfig::loadByName($entity_type_id, $entity_bundle, $field_name);
$window_field_setting = $config
->getSetting($window_type);
$use_site_default = FALSE;
if ($window_field_setting === NULL || $window_field_setting === "-1") {
$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);
$field_name = $vote->field_name->value;
$fieldResults = [];
foreach ($results as $key => $result) {
if (strrpos($key, $field_name) !== FALSE) {
$key = explode(':', $key);
$fieldResults[$key[0]] = $result != 0 ? ceil($result * 10) / 10 : 0;
}
}
return [
'#theme' => 'votingapi_widgets_summary',
'#vote' => $vote,
'#results' => $fieldResults,
'#field_name' => $vote->field_name->value,
];
}
}