View source
<?php
namespace Drupal\rng;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface;
use Drupal\courier\Service\IdentityChannelManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\rng\Entity\Rule;
use Drupal\rng\Entity\RuleComponent;
class EventMeta implements EventMetaInterface {
protected $entity;
protected $entityManager;
protected $configFactory;
protected $selectionPluginManager;
protected $identityChannelManager;
protected $rngConfiguration;
protected $eventManager;
public function __construct(EntityManagerInterface $entity_manager, ConfigFactoryInterface $config_factory, SelectionPluginManagerInterface $selection_plugin_manager, IdentityChannelManagerInterface $identity_channel_manager, RngConfigurationInterface $rng_configuration, EventManagerInterface $event_manager, EntityInterface $entity) {
$this->entityManager = $entity_manager;
$this->configFactory = $config_factory;
$this->selectionPluginManager = $selection_plugin_manager;
$this->identityChannelManager = $identity_channel_manager;
$this->rngConfiguration = $rng_configuration;
$this->eventManager = $event_manager;
$this->entity = $entity;
}
public static function createInstance(ContainerInterface $container, EntityInterface $entity) {
return new static($container
->get('entity.manager'), $container
->get('config.factory'), $container
->get('plugin.manager.entity_reference_selection'), $container
->get('plugin.manager.identity_channel'), $container
->get('rng.configuration'), $container
->get('rng.event_manager'), $entity);
}
public function getEvent() {
return $this->entity;
}
public function getEventType() {
return $this->eventManager
->eventType($this->entity
->getEntityTypeId(), $this->entity
->bundle());
}
public function isAcceptingRegistrations() {
return !empty($this
->getEvent()->{EventManagerInterface::FIELD_STATUS}->value);
}
public function getReplyTo() {
return $this
->getEvent()->{EventManagerInterface::FIELD_EMAIL_REPLY_TO}->value;
}
public function duplicateRegistrantsAllowed() {
return !empty($this
->getEvent()->{EventManagerInterface::FIELD_ALLOW_DUPLICATE_REGISTRANTS}->value);
}
public function getRegistrationTypeIds() {
return array_map(function ($element) {
return $element['target_id'];
}, $this
->getEvent()->{EventManagerInterface::FIELD_REGISTRATION_TYPE}
->getValue());
}
public function getRegistrationTypes() {
return $this->entityManager
->getStorage('registration_type')
->loadMultiple($this
->getRegistrationTypeIds());
}
public function registrationTypeIsValid(RegistrationTypeInterface $registration_type) {
return in_array($registration_type
->id(), $this
->getRegistrationTypeIds());
}
public function removeRegistrationType($registration_type_id) {
$registration_types =& $this->entity->{EventManagerInterface::FIELD_REGISTRATION_TYPE};
foreach ($registration_types
->getValue() as $key => $value) {
if ($value['target_id'] == $registration_type_id) {
$registration_types
->removeItem($key);
}
}
return $this->entity;
}
public function removeGroup($group_id) {
$groups =& $this->entity->{EventManagerInterface::FIELD_REGISTRATION_GROUPS};
foreach ($groups
->getValue() as $key => $value) {
if ($value['target_id'] == $group_id) {
$groups
->removeItem($key);
}
}
return $this->entity;
}
public function getCapacity() {
$capacity = (int) $this
->getEvent()->{EventManagerInterface::FIELD_CAPACITY}->value;
if ($capacity != '' && is_numeric($capacity) && $capacity >= 0) {
return $capacity;
}
return EventMetaInterface::CAPACITY_UNLIMITED;
}
public function remainingCapacity() {
$capacity = $this
->getCapacity();
if ($capacity == EventMetaInterface::CAPACITY_UNLIMITED) {
return $capacity;
}
$remaining = $capacity - $this
->countRegistrations();
return $remaining > 0 ? $remaining : 0;
}
public function getRegistrantsMinimum() {
if (isset($this
->getEvent()->{EventManagerInterface::FIELD_REGISTRATION_REGISTRANTS_MINIMUM})) {
$field = $this
->getEvent()->{EventManagerInterface::FIELD_REGISTRATION_REGISTRANTS_MINIMUM};
$minimum = $field->value;
if ($minimum !== '' && is_numeric($minimum) && $minimum >= 0) {
return $minimum;
}
}
return 1;
}
public function getRegistrantsMaximum() {
if (isset($this
->getEvent()->{EventManagerInterface::FIELD_REGISTRATION_REGISTRANTS_MAXIMUM})) {
$field = $this
->getEvent()->{EventManagerInterface::FIELD_REGISTRATION_REGISTRANTS_MAXIMUM};
$maximum = $field->value;
if ($maximum !== '' && is_numeric($maximum) && $maximum >= 0) {
return $maximum;
}
}
return EventMetaInterface::CAPACITY_UNLIMITED;
}
function getDefaultGroups() {
$groups = [];
foreach ($this
->getEvent()->{EventManagerInterface::FIELD_REGISTRATION_GROUPS} as $group) {
$groups[] = $group->entity;
}
return $groups;
}
function buildQuery($entity_type) {
return $this->entityManager
->getStorage($entity_type)
->getQuery('AND')
->condition('event__target_type', $this
->getEvent()
->getEntityTypeId(), '=')
->condition('event__target_id', $this
->getEvent()
->id(), '=');
}
function buildRegistrationQuery() {
return $this
->buildQuery('registration');
}
function getRegistrations() {
$query = $this
->buildRegistrationQuery();
return $this->entityManager
->getStorage('registration')
->loadMultiple($query
->execute());
}
function countRegistrations() {
return $this
->buildRegistrationQuery()
->count()
->execute();
}
function buildRuleQuery() {
return $this
->buildQuery('rng_rule');
}
function getRules($trigger = NULL, $defaults = FALSE, $is_active = TRUE) {
$query = $this
->buildRuleQuery();
if ($trigger) {
$query
->condition('trigger_id', $trigger, '=');
}
if (isset($is_active)) {
$query
->condition('status', $is_active, '=');
}
$rules = $this->entityManager
->getStorage('rng_rule')
->loadMultiple($query
->execute());
if ($defaults && !$rules) {
return $this
->getDefaultRules($trigger);
}
return $rules;
}
public function getDefaultRules($trigger = NULL) {
$rules = [];
if ($trigger != 'rng_event.register') {
return $rules;
}
$default_rules = $this->entityManager
->getStorage('rng_event_type_rule')
->loadByProperties([
'entity_type' => $this
->getEvent()
->getEntityTypeId(),
'bundle' => $this
->getEvent()
->bundle(),
'trigger' => $trigger,
]);
foreach ($default_rules as $default_rule) {
$rule = Rule::create([
'event' => array(
'entity' => $this
->getEvent(),
),
'trigger_id' => $trigger,
'status' => TRUE,
]);
foreach ($default_rule
->getConditions() as $condition) {
$plugin_id = $condition['id'];
unset($condition['id']);
$component = RuleComponent::create()
->setType('condition')
->setPluginId($plugin_id)
->setConfiguration($condition);
$rule
->addComponent($component);
}
foreach ($default_rule
->getActions() as $action) {
$component = RuleComponent::create()
->setType('action')
->setPluginId($action['id'])
->setConfiguration($action['configuration']);
$rule
->addComponent($component);
}
$rules[] = $rule;
}
return $rules;
}
function isDefaultRules($trigger) {
return (bool) (!$this
->getRules($trigger));
}
public function trigger($trigger, $context = array()) {
$context['event'] = $this
->getEvent();
foreach ($this
->getRules($trigger) as $rule) {
if ($rule
->evaluateConditions()) {
foreach ($rule
->getActions() as $action) {
$action
->execute($context);
}
}
}
}
function buildGroupQuery() {
return $this
->buildQuery('registration_group');
}
function getGroups() {
$query = $this
->buildGroupQuery();
return $this->entityManager
->getStorage('registration_group')
->loadMultiple($query
->execute());
}
public function buildRegistrantQuery($entity_type_id = NULL) {
$query = $this->entityManager
->getStorage('registrant')
->getQuery('AND')
->condition('registration.entity.event__target_type', $this
->getEvent()
->getEntityTypeId(), '=')
->condition('registration.entity.event__target_id', $this
->getEvent()
->id(), '=');
if ($entity_type_id) {
$query
->condition('identity__target_type', $entity_type_id, '=');
}
return $query;
}
public function getRegistrants($entity_type_id = NULL) {
$query = $this
->buildRegistrantQuery($entity_type_id);
return $this->entityManager
->getStorage('registrant')
->loadMultiple($query
->execute());
}
public function canRegisterProxyIdentities() {
$identity_types = $this
->getCreatableIdentityTypes();
foreach ($identity_types as $entity_type_id => $bundles) {
$accessControl = $this->entityManager
->getAccessControlHandler($entity_type_id);
if ($this
->entityTypeHasBundles($entity_type_id)) {
foreach ($bundles as $bundle) {
if ($accessControl
->createAccess($bundle)) {
return TRUE;
}
}
}
elseif (!empty($bundles)) {
if ($accessControl
->createAccess()) {
return TRUE;
}
}
}
$identity_types = $this
->getIdentityTypes();
foreach ($identity_types as $entity_type_id => $bundles) {
$referencable_bundles = $this
->entityTypeHasBundles($entity_type_id) ? $bundles : [];
$count = $this
->countRngReferenceableEntities($entity_type_id, $referencable_bundles);
if ($count > 0) {
return TRUE;
}
}
return FALSE;
}
public function countProxyIdentities() {
$total = 0;
foreach ($this
->getIdentityTypes() as $entity_type_id => $bundles) {
if ($this
->entityTypeHasBundles($entity_type_id)) {
$total += $this
->countRngReferenceableEntities($entity_type_id, $bundles);
}
elseif (!empty($bundles)) {
$total += $this
->countRngReferenceableEntities($entity_type_id);
}
}
return $total;
}
protected function countRngReferenceableEntities($entity_type_id, $bundles = []) {
$selection_groups = $this->selectionPluginManager
->getSelectionGroups($entity_type_id);
if (isset($selection_groups['rng_register'])) {
$options = [
'target_type' => $entity_type_id,
'handler' => 'rng_register',
'handler_settings' => [
'event_entity_type' => $this
->getEvent()
->getEntityTypeId(),
'event_entity_id' => $this
->getEvent()
->id(),
],
];
if (!empty($bundles)) {
$options['handler_settings']['target_bundles'] = $bundles;
}
return $this->selectionPluginManager
->getInstance($options)
->countReferenceableEntities();
}
return 0;
}
public function getIdentityTypes() {
$event_type = $this
->getEventType();
$result = [];
$identity_types_available = $this->rngConfiguration
->getIdentityTypes();
foreach ($identity_types_available as $entity_type_id) {
$bundles = $this->entityManager
->getBundleInfo($entity_type_id);
foreach ($bundles as $bundle => $info) {
if ($event_type
->canIdentityTypeReference($entity_type_id, $bundle)) {
$result[$entity_type_id][] = $bundle;
}
}
}
return $result;
}
public function getCreatableIdentityTypes() {
$event_type = $this
->getEventType();
$result = [];
$identity_types_available = $this->rngConfiguration
->getIdentityTypes();
foreach ($identity_types_available as $entity_type_id) {
$bundles = $this->entityManager
->getBundleInfo($entity_type_id);
foreach ($bundles as $bundle => $info) {
if ($event_type
->canIdentityTypeCreate($entity_type_id, $bundle)) {
$result[$entity_type_id][] = $bundle;
}
}
}
return $result;
}
public function identitiesCanRegister($entity_type, array $entity_ids) {
$identity_types = $this
->getIdentityTypes();
if (isset($identity_types[$entity_type])) {
$options = [
'target_type' => $entity_type,
'handler' => 'rng_register',
'handler_settings' => [
'event_entity_type' => $this
->getEvent()
->getEntityTypeId(),
'event_entity_id' => $this
->getEvent()
->id(),
],
];
if ($this
->entityTypeHasBundles($entity_type)) {
$options['handler_settings']['target_bundles'] = $identity_types[$entity_type];
}
$selection = $this->selectionPluginManager
->getInstance($options);
return $selection
->validateReferenceableEntities($entity_ids);
}
return [];
}
function addDefaultAccess() {
$rules = $this
->getDefaultRules('rng_event.register');
foreach ($rules as $rule) {
$rule
->save();
}
}
protected function entityTypeHasBundles($entity_type_id) {
$entity_type = $this->entityManager
->getDefinition($entity_type_id);
return $entity_type
->getBundleEntityType() !== NULL;
}
}