View source
<?php
namespace Drupal\rng\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\rng\Exception\MaxRegistrantsExceededException;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\user\UserInterface;
class Registration extends ContentEntityBase implements RegistrationInterface {
use EntityChangedTrait;
protected $identities_unsaved = [];
public function getEvent() {
return $this
->get('event')->entity;
}
public function getEventMeta() {
$event_manager = \Drupal::service('rng.event_manager');
return $event_manager
->getMeta($this
->getEvent());
}
public function setEvent(ContentEntityInterface $entity) {
$this
->set('event', [
'entity' => $entity,
]);
return $this;
}
public function label() {
$owner = $this
->getOwner();
$qty = $this
->getRegistrantQty();
$registrations = '';
if ($qty) {
$registrations = '[' . $qty . ']';
}
if ($owner) {
return t('@owner @regs', [
'@owner' => $owner
->label(),
'@regs' => $registrations,
]);
}
return t('New registration');
}
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function setCreatedTime($timestamp) {
$this
->set('created', $timestamp);
return $this;
}
public function getChangedTime() {
return $this
->get('changed')->value;
}
public function isConfirmed() {
return (bool) $this
->getEntityKey('published');
}
public function setConfirmed($confirmed) {
$this
->set('status', (bool) $confirmed);
return $this;
}
public function getOwner() {
return $this
->get('uid')->entity;
}
public function setOwner(UserInterface $account) {
$this
->set('uid', $account
->id());
return $this;
}
public function getOwnerId() {
return $this
->getEntityKey('owner');
}
public function setOwnerId($uid) {
$this
->set('uid', $uid);
return $this;
}
public function getRegistrantIds() {
return $this->registrant_ids = \Drupal::entityQuery('registrant')
->condition('registration', $this
->id(), '=')
->execute();
}
public function getRegistrants() {
if ($this
->getRegistrantQty()) {
$this
->createStubs();
}
$registrants = \Drupal::entityTypeManager()
->getStorage('registrant')
->loadMultiple($this
->getRegistrantIds());
if (!count($registrants)) {
$registrant_factory = \Drupal::service('rng.registrant.factory');
$registrant = $registrant_factory
->createRegistrant([
'event' => $this
->getEvent(),
]);
$registrant
->setRegistration($this);
$registrants[] = $registrant;
}
return $registrants;
}
public function hasIdentity(EntityInterface $identity) {
foreach ($this->identities_unsaved as $identity_unsaved) {
if ($identity == $identity_unsaved) {
return TRUE;
}
}
foreach ($this
->getRegistrants() as $registrant) {
if ($registrant
->hasIdentity($identity)) {
return TRUE;
}
}
return FALSE;
}
public function addIdentity(EntityInterface $identity) {
if ($this
->hasIdentity($identity)) {
throw new \Exception('Duplicate identity on registration');
}
if (!$this
->canAddRegistrants()) {
throw new MaxRegistrantsExceededException('Cannot add another registrant to this registration.');
}
$this->identities_unsaved[] = $identity;
return $this;
}
public function getGroups() {
return $this->groups
->referencedEntities();
}
public function addGroup(GroupInterface $group) {
if (!in_array($group, $this
->getGroups())) {
if ($group
->getEvent() != $this
->getEvent()) {
throw new \Exception('Group and registration events do not match.');
}
$this->groups
->appendItem($group);
}
return $this;
}
public function removeGroup($group_id) {
foreach ($this->groups
->getValue() as $key => $value) {
if ($value['target_id'] == $group_id) {
$this->groups
->removeItem($key);
}
}
return $this;
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('Registration ID'))
->setDescription(t('The registration ID.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The registration UUID.'))
->setReadOnly(TRUE);
$fields['vid'] = BaseFieldDefinition::create('integer')
->setLabel(t('Revision ID'))
->setDescription(t('The registration revision ID.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(new TranslatableMarkup('Confirmed'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDefaultValue(TRUE)
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'settings' => [
'display_label' => TRUE,
],
'weight' => 90,
])
->setDisplayConfigurable('form', TRUE);
$fields['type'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Type'))
->setDescription(t('The registration type.'))
->setSetting('target_type', 'registration_type')
->setReadOnly(TRUE);
$fields['langcode'] = BaseFieldDefinition::create('language')
->setLabel(t('Language code'))
->setDescription(t('The registration language code.'))
->setRevisionable(TRUE);
$fields['event'] = BaseFieldDefinition::create('dynamic_entity_reference')
->setLabel(t('Event'))
->setDescription(t('The event for the registration.'))
->setSetting('exclude_entity_types', 'true')
->setSetting('entity_type_ids', [
'registrant',
'registration',
])
->setDescription(t('The relationship between this registration and an event.'))
->setRevisionable(TRUE)
->setReadOnly(TRUE);
$fields['groups'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Groups'))
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setDescription(t('The groups the registration is assigned.'))
->setSetting('target_type', 'registration_group')
->addConstraint('RegistrationGroupSibling');
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Authored on'))
->setDescription(t('Time the Registration was created.'))
->setTranslatable(FALSE)
->setRevisionable(FALSE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Updated on'))
->setDescription(t('The time Registration was last updated.'))
->setTranslatable(TRUE)
->setRevisionable(TRUE);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Owner'))
->setDescription(t('The owner of the registration.'))
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setDefaultValueCallback('Drupal\\rng\\Entity\\Registration::getCurrentUserId')
->setTranslatable(TRUE)
->setDisplayConfigurable('view', TRUE)
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 5,
])
->setDisplayConfigurable('form', TRUE);
$fields['registrant_qty'] = BaseFieldDefinition::create('integer')
->setLabel(t('Registrant Qty'))
->setDescription(t('Number of registrants on this registration'))
->setDefaultValue(0)
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setTranslatable(TRUE)
->setRevisionable(TRUE);
return $fields;
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
if (!$this
->getEvent() instanceof ContentEntityBase) {
throw new EntityMalformedException('Invalid or missing event on registration.');
}
$registrants = $this
->getRegistrantIds();
$count = $this
->getRegistrantQty();
if (!empty($count) && $count < count($registrants)) {
throw new MaxRegistrantsExceededException('Too many registrants on this registration.');
}
$event_meta = $this
->getEventMeta();
if ($this
->isNew()) {
foreach ($event_meta
->getDefaultGroups() as $group) {
$this
->addGroup($group);
}
}
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
$registrant_factory = \Drupal::service('rng.registrant.factory');
foreach ($this->identities_unsaved as $k => $identity) {
$registrant = $registrant_factory
->createRegistrant([
'event' => $this
->getEvent(),
]);
$registrant
->setRegistration($this)
->setIdentity($identity)
->save();
unset($this->identities_unsaved[$k]);
}
$this
->createStubs();
}
protected function createStubs() {
$stub_count = $this
->getRegistrantQty();
if ($stub_count && $this
->canAddRegistrants()) {
$registrant_factory = \Drupal::service('rng.registrant.factory');
$registrant_count = count($this
->getRegistrantIds());
$stub_count -= $registrant_count;
while ($stub_count) {
$registrant = $registrant_factory
->createRegistrant([
'event' => $this
->getEvent(),
]);
$registrant
->setRegistration($this)
->save();
$stub_count--;
}
}
}
public static function preDelete(EntityStorageInterface $storage, array $entities) {
$registrant_storage = \Drupal::entityTypeManager()
->getStorage('registrant');
foreach ($entities as $registration) {
$ids = $registrant_storage
->getQuery()
->condition('registration', $registration
->id(), '=')
->execute();
$registrants = $registrant_storage
->loadMultiple($ids);
$registrant_storage
->delete($registrants);
}
parent::preDelete($storage, $entities);
}
public function getRegistrantQty() {
return $this
->get('registrant_qty')->value;
}
public function setRegistrantQty($qty) {
$registrants = $this
->getRegistrantIds();
if ($qty > 0) {
if (count($registrants) > $qty) {
throw new MaxRegistrantsExceededException('Cannot set registrant qty below number of current registrants.');
}
$event_meta = $this
->getEventMeta();
$max = $event_meta
->getRegistrantsMaximum();
if (!empty($max) && $max > -1 && $qty > $max) {
throw new MaxRegistrantsExceededException('Cannot set registrations above event maximum');
}
}
$this
->set('registrant_qty', $qty);
return $this;
}
public function canAddRegistrants() {
$registrants = $this
->getRegistrantIds();
$qty = $this
->getRegistrantQty();
if ($qty) {
return $qty > count($registrants);
}
return TRUE;
}
public static function getCurrentUserId() {
return [
\Drupal::currentUser()
->id(),
];
}
public function getDateString() {
$event_meta = $this
->getEventMeta();
return $event_meta
->getDateString();
}
}