View source
<?php
namespace Drupal\poll\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\poll\PollInterface;
use Drupal\user\UserInterface;
class Poll extends ContentEntityBase implements PollInterface {
public function setQuestion($question) {
$this
->set('question', $question);
return $this;
}
public function setCreated($created) {
$this
->set('created', $created);
return $this;
}
public function getCreated() {
return $this
->get('created')->value;
}
public function getRuntime() {
return $this
->get('runtime')->value;
}
public function setRuntime($runtime) {
$this
->set('runtime', $runtime);
return $this;
}
public function getAnonymousVoteAllow() {
return $this
->get('anonymous_vote_allow')->value;
}
public function setAnonymousVoteAllow($anonymous_vote_allow) {
$this
->set('anonymous_vote_allow', $anonymous_vote_allow);
return $this;
}
public function getCancelVoteAllow() {
return $this
->get('cancel_vote_allow')->value;
}
public function setCancelVoteAllow($cancel_vote_allow) {
$this
->set('cancel_vote_allow', $cancel_vote_allow);
return $this;
}
public function getResultVoteAllow() {
return $this
->get('result_vote_allow')->value;
}
public function setResultVoteAllow($result_vote_allow) {
$this
->set('result_vote_allow', $result_vote_allow);
return $this;
}
public function isOpen() {
return (bool) $this
->get('status')->value;
}
public function getOwner() {
return $this
->get('uid')->entity;
}
public function getOwnerId() {
return $this
->get('uid')->target_id;
}
public function setOwnerId($uid) {
$this
->set('uid', $uid);
return $this;
}
public function setOwner(UserInterface $account) {
$this
->set('uid', $account
->id());
return $this;
}
public function isClosed() {
return (bool) $this
->get('status')->value == 0;
}
public function close() {
return $this
->set('status', 0);
}
public function open() {
return $this
->set('status', 1);
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('Poll ID'))
->setDescription(t('The ID of the poll.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Author'))
->setDescription(t('The poll author.'))
->setSetting('target_type', 'user')
->setTranslatable(TRUE)
->setDefaultValueCallback('Drupal\\poll\\Entity\\Poll::getCurrentUserId')
->setDisplayOptions('form', array(
'type' => 'entity_reference_autocomplete',
'weight' => -10,
'settings' => array(
'match_operator' => 'CONTAINS',
'size' => '60',
'autocomplete_type' => 'tags',
'placeholder' => '',
),
))
->setDisplayConfigurable('form', TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The poll UUID.'))
->setReadOnly(TRUE);
$fields['question'] = BaseFieldDefinition::create('string')
->setLabel(t('Question'))
->setDescription(t('The poll question.'))
->setRequired(TRUE)
->setTranslatable(TRUE)
->setSetting('max_length', 255)
->setDisplayOptions('form', array(
'type' => 'string_textfield',
'weight' => -100,
))
->setDisplayConfigurable('form', TRUE);
$fields['langcode'] = BaseFieldDefinition::create('language')
->setLabel(t('Language code'))
->setDescription(t('The poll language code.'));
$fields['choice'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Choice'))
->setSetting('target_type', 'poll_choice')
->setDescription(t('Enter the poll choices.'))
->setRequired(TRUE)
->setTranslatable(FALSE)
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setSetting('max_length', 255)
->setDisplayOptions('form', [
'type' => 'poll_choice_default',
'settings' => [],
'weight' => -20,
])
->setDisplayConfigurable('form', TRUE);
$duration = array(
86400,
2 * 86400,
3 * 86400,
4 * 86400,
5 * 86400,
6 * 86400,
604800,
2 * 604800,
3 * 604800,
2592000,
2 * 2592000,
3 * 2592000,
6 * 2592000,
9 * 2592000,
31536000,
);
$period = array(
0 => t('Unlimited'),
) + array_map(array(
\Drupal::service('date.formatter'),
'formatInterval',
), array_combine($duration, $duration));
$fields['runtime'] = BaseFieldDefinition::create('list_integer')
->setLabel(t('Poll Duration'))
->setDescription(t('After this period, the poll will be closed automatically.'))
->setSetting('unsigned', TRUE)
->setRequired(TRUE)
->setSetting('allowed_values', $period)
->setDefaultValue(0)
->setDisplayOptions('form', array(
'type' => 'options_select',
'weight' => 0,
))
->setDisplayConfigurable('form', TRUE);
$fields['anonymous_vote_allow'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Allow anonymous votes'))
->setDescription(t('A flag indicating whether anonymous users are allowed to vote.'))
->setDefaultValue(0)
->setDisplayOptions('form', array(
'type' => 'boolean_checkbox',
'settings' => array(
'display_label' => TRUE,
),
'weight' => 1,
))
->setDisplayConfigurable('form', TRUE);
$fields['cancel_vote_allow'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Allow cancel votes'))
->setDescription(t('A flag indicating whether users may cancel their vote.'))
->setDefaultValue(1)
->setDisplayOptions('form', array(
'type' => 'boolean_checkbox',
'settings' => array(
'display_label' => TRUE,
),
'weight' => 2,
))
->setDisplayConfigurable('form', TRUE);
$fields['result_vote_allow'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Allow view results'))
->setDescription(t('A flag indicating whether users may see the results before voting.'))
->setDefaultValue(0)
->setDisplayOptions('form', array(
'type' => 'boolean_checkbox',
'settings' => array(
'display_label' => TRUE,
),
'weight' => 3,
))
->setDisplayConfigurable('form', TRUE);
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Active'))
->setDescription(t('A flag indicating whether the poll is active.'))
->setDefaultValue(1)
->setDisplayOptions('form', array(
'type' => 'boolean_checkbox',
'settings' => array(
'display_label' => TRUE,
),
'weight' => -5,
))
->setDisplayConfigurable('form', TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('When the poll was created, as a Unix timestamp.'));
return $fields;
}
public static function getCurrentUserId() {
return array(
\Drupal::currentUser()
->id(),
);
}
public static function sort($a, $b) {
return strcmp($a
->label(), $b
->label());
}
public function hasUserVoted() {
$vote_storage = \Drupal::service('poll_vote.storage');
return $vote_storage
->getUserVote($this);
}
public function getOptions() {
$options = array();
if (count($this->choice)) {
foreach ($this->choice as $choice_item) {
$options[$choice_item->target_id] = \Drupal::service('entity.repository')
->getTranslationFromContext($choice_item->entity, $this
->language()
->getId())
->label();
}
}
return $options;
}
public function getOptionValues() {
$options = array();
if (count($this->choice)) {
foreach ($this->choice as $choice_item) {
$options[$choice_item->target_id] = 1;
}
}
return $options;
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
foreach ($this->choice as $choice_item) {
if ($choice_item->entity && $choice_item->entity
->needsSaving()) {
$choice_item->entity
->save();
$choice_item->target_id = $choice_item->entity
->id();
}
}
if (!$this
->isNew()) {
$original_choices = [];
foreach ($this->original->choice as $choice_item) {
$original_choices[] = $choice_item->target_id;
}
$current_choices = [];
foreach ($this->choice as $choice_item) {
$current_choices[] = $choice_item->target_id;
}
$removed_choices = array_diff($original_choices, $current_choices);
if ($removed_choices) {
\Drupal::service('poll_vote.storage')
->deleteChoicesVotes($removed_choices);
$storage = \Drupal::entityTypeManager()
->getStorage('poll_choice');
$storage
->delete($storage
->loadMultiple($removed_choices));
}
}
}
public static function postDelete(EntityStorageInterface $storage, array $entities) {
parent::postDelete($storage, $entities);
foreach ($entities as $entity) {
$storage
->deleteVotes($entity);
}
$choices = [];
foreach ($entities as $entity) {
$choices = array_merge($choices, $entity->choice
->referencedEntities());
}
if ($choices) {
\Drupal::entityTypeManager()
->getStorage('poll_choice')
->delete($choices);
}
}
public function getVotes() {
$vote_storage = \Drupal::service('poll_vote.storage');
return $vote_storage
->getVotes($this);
}
}