diff --git a/src/NotificationFactory.php b/src/NotificationFactory.php index 02005d860fa530fe2cf828168a4eb88bfe705eb8..6cfe4c22c9eb9344995c3b19bb81884327ef8e7a 100644 --- a/src/NotificationFactory.php +++ b/src/NotificationFactory.php @@ -58,21 +58,26 @@ final class NotificationFactory { if (!empty($conditional_notifications)) { foreach ($conditional_notifications as $notification) { if ($notification->getStatus()) { - // Get the selected condition now from ECA - $condition_eca_model_machine_name = $notification->getNotificationConditionEca(); - $event = new ConditionInsertTriggerEvent($entity, $entity->getEntityTypeId(), $entity->bundle(), 'default', $condition_eca_model_machine_name); - $this->eventDispatcher->dispatch($event, ConditionInsertTriggerEvent::EVENT_NAME); - $is_condition = $event->result; - \Drupal::logger('debug')->info('ECA Conditional Notification on Insert: @dat a : @template', ['@data' => $is_condition, '@template' => $notification->label()]); - if ($is_condition) { - $data['notification'] = $notification->id(); - $data['entity_type'] = $entity->getEntityTypeId(); - $data['entity_id'] = $entity->id(); - // Only send out if there is no override inactive - if ($this->isActiveOverride($notification, $entity)) { - $this->addToQueue($data); - } - } + if ( + $notification->getNotificationEntityType() === $entity->getEntityTypeId() && + $notification->getNotificationEntityBundle() === $entity->bundle() + ) { + // Get the selected condition now from ECA + $condition_eca_model_machine_name = $notification->getNotificationConditionEca(); + $event = new ConditionInsertTriggerEvent($entity, $entity->getEntityTypeId(), $entity->bundle(), 'default', $condition_eca_model_machine_name); + $this->eventDispatcher->dispatch($event, ConditionInsertTriggerEvent::EVENT_NAME); + $is_condition = $event->result; + \Drupal::logger('debug')->info('ECA Conditional Notification on Insert: @dat a : @template', ['@data' => $is_condition, '@template' => $notification->label()]); + if ($is_condition) { + $data['notification'] = $notification->id(); + $data['entity_type'] = $entity->getEntityTypeId(); + $data['entity_id'] = $entity->id(); + // Only send out if there is no override inactive + if ($this->isActiveOverride($notification, $entity)) { + $this->addToQueue($data); + } + } + } } } } @@ -123,37 +128,42 @@ final class NotificationFactory { if (!empty($conditional_notifications)) { foreach ($conditional_notifications as $notification) { if ($notification->getStatus()) { - // Get the selected condition now from ECA - $condition_eca_model_machine_name = $notification->getNotificationConditionEca(); - // ECA Condition - $event = new ConditionUpdateTriggerEvent($entity, $entity->getEntityTypeId(), $entity->bundle(), 'default', $condition_eca_model_machine_name); - $this->eventDispatcher->dispatch($event, ConditionUpdateTriggerEvent::EVENT_NAME); - $is_condition = $event->result; - \Drupal::logger('debug')->info('ECA Recipients Event on UPDATE: @data : @template', ['@data' => $is_condition, '@template' => $notification->label()]); - if ($is_condition) { - if ($override_notification = $this->checkForNotificationOverride($notification, $entity)) { - $data['notification_id'] = $override_notification->id(); - $data['entity_type'] = $entity->getEntityTypeId(); - $data['entity_id'] = $entity->id(); + if ( + $notification->getNotificationEntityType() === $entity->getEntityTypeId() && + $notification->getNotificationEntityBundle() === $entity->bundle() + ) { + // Get the selected condition now from ECA + $condition_eca_model_machine_name = $notification->getNotificationConditionEca(); + // ECA Condition + $event = new ConditionUpdateTriggerEvent($entity, $entity->getEntityTypeId(), $entity->bundle(), 'default', $condition_eca_model_machine_name); + $this->eventDispatcher->dispatch($event, ConditionUpdateTriggerEvent::EVENT_NAME); + $is_condition = $event->result; + \Drupal::logger('debug')->info('ECA Recipients Event on UPDATE: @data : @template', ['@data' => $is_condition, '@template' => $notification->label()]); + if ($is_condition) { + if ($override_notification = $this->checkForNotificationOverride($notification, $entity)) { + $data['notification_id'] = $override_notification->id(); + $data['entity_type'] = $entity->getEntityTypeId(); + $data['entity_id'] = $entity->id(); + } + else { + $data['notification_id'] = $notification->id(); + $data['entity_type'] = $entity->getEntityTypeId(); + $data['entity_id'] = $entity->id(); + } + $this->addToQueue($data); } else { - $data['notification_id'] = $notification->id(); - $data['entity_type'] = $entity->getEntityTypeId(); - $data['entity_id'] = $entity->id(); + $debug_data = [ + 'notification_id' => $notification->id(), + 'entity_type' => $entity->getEntityTypeId(), + 'entity_id' => $entity->id(), + ]; + $debug = json_encode($debug_data); + $this->getLogger('conditional_notification') + ->warning('A condition was unsuccessful: Debug Info: @debug.', [ + '@debug' => $debug + ]); } - $this->addToQueue($data); - } - else { - $debug_data = [ - 'notification_id' => $notification->id(), - 'entity_type' => $entity->getEntityTypeId(), - 'entity_id' => $entity->id(), - ]; - $debug = json_encode($debug_data); - $this->getLogger('conditional_notification') - ->warning('A condition was unsuccessful: Debug Info: @debug.', [ - '@debug' => $debug - ]); } } } @@ -294,78 +304,83 @@ final class NotificationFactory { foreach ($conditional_notifications as $notification) { // Only process enabled notifications. if ($notification->getStatus()) { - // Get the selected condition now from ECA - $condition_eca_model_machine_name = $notification->getNotificationConditionEca(); - // Conditons now ECA Models - $event = new ConditionCronTriggerEvent($entity, $entity->getEntityTypeId(), $entity->bundle(), 'default', $condition_eca_model_machine_name); - $this->eventDispatcher->dispatch($event, ConditionCronTriggerEvent::EVENT_NAME); - $is_condition = $event->result; - if ($is_condition) { - \Drupal::logger('debug')->info('ECA Conditional Notification on CRON: @data : @template', ['@data' => $is_condition, '@tempalte' => $notification->id()]); - if ($override_notification = $this->checkForNotificationOverride($notification, $entity)) { - $data['notification_id'] = $override_notification->id(); - $data['entity_type'] = $entity->getEntityTypeId(); - $data['entity_id'] = $entity->id(); - $data['time_offset_enabled'] = $override_notification->getTimeOffsetEnabled(); - // Check field - if ($date_field = $override_notification->getTimeOffsetDateFieldOnly()) { - $data['entity_date_field'] = $date_field; - if ($date_field_part = $override_notification->getTimeOffsetDateFieldPart()) { - $data['entity_date_field_part'] = $date_field_part; - $data['entity_date_field_timestamp'] = $entity->{$date_field}->{$date_field_part}; + if ( + $notification->getNotificationEntityType() === $entity->getEntityTypeId() && + $notification->getNotificationEntityBundle() === $entity->bundle() + ) { + // Get the selected condition now from ECA + $condition_eca_model_machine_name = $notification->getNotificationConditionEca(); + // Conditons now ECA Models + $event = new ConditionCronTriggerEvent($entity, $entity->getEntityTypeId(), $entity->bundle(), 'default', $condition_eca_model_machine_name); + $this->eventDispatcher->dispatch($event, ConditionCronTriggerEvent::EVENT_NAME); + $is_condition = $event->result; + if ($is_condition && $notification->getEntityType() === $entity->getEntityTypeId()) { + \Drupal::logger('debug')->info('ECA Conditional Notification on CRON: @data : @template', ['@data' => $is_condition, '@tempalte' => $notification->id()]); + if ($override_notification = $this->checkForNotificationOverride($notification, $entity)) { + $data['notification_id'] = $override_notification->id(); + $data['entity_type'] = $entity->getEntityTypeId(); + $data['entity_id'] = $entity->id(); + $data['time_offset_enabled'] = $override_notification->getTimeOffsetEnabled(); + // Check field + if ($date_field = $override_notification->getTimeOffsetDateFieldOnly()) { + $data['entity_date_field'] = $date_field; + if ($date_field_part = $override_notification->getTimeOffsetDateFieldPart()) { + $data['entity_date_field_part'] = $date_field_part; + $data['entity_date_field_timestamp'] = $entity->{$date_field}->{$date_field_part}; + } + else { + $data['entity_date_field_part'] = NULL; + $data['entity_date_field_timestamp'] = $entity->get($date_field)->value; + } + // Calculate offset + $time_offset = $override_notification->getTimeOffset(); + $time_offset_type = $override_notification->getTimeOffsetType(); + $data['time_offset_type'] = $time_offset_type; + $data['time_offset'] = $time_offset; + $data['entity_date_offset'] = $data['entity_date_field_timestamp'] + ($this->getCalculatedOffset($time_offset, $time_offset_type)); + $data['entity_date_string_day'] = $this->getCalculatedOffsetString($data['entity_date_offset']); + $data['entity_date_string_hour'] = $this->getCalculatedOffsetString($data['entity_date_offset'], 'hours'); } - else { - $data['entity_date_field_part'] = NULL; - $data['entity_date_field_timestamp'] = $entity->get($date_field)->value; - } - // Calculate offset - $time_offset = $override_notification->getTimeOffset(); - $time_offset_type = $override_notification->getTimeOffsetType(); - $data['time_offset_type'] = $time_offset_type; - $data['time_offset'] = $time_offset; - $data['entity_date_offset'] = $data['entity_date_field_timestamp'] + ($this->getCalculatedOffset($time_offset, $time_offset_type)); - $data['entity_date_string_day'] = $this->getCalculatedOffsetString($data['entity_date_offset']); - $data['entity_date_string_hour'] = $this->getCalculatedOffsetString($data['entity_date_offset'], 'hours'); } - } - else { - $data['notification_id'] = $notification->id(); - $data['entity_type'] = $entity->getEntityTypeId(); - $data['entity_id'] = $entity->id(); - $data['time_offset_enabled'] = $notification->getTimeOffsetEnabled(); - // Check field - if ($date_field = $notification->getTimeOffsetDateFieldOnly()) { - $data['entity_date_field'] = $date_field; - if ($date_field_part = $notification->getTimeOffsetDateFieldPart()) { - $data['entity_date_field_part'] = $date_field_part; - $data['entity_date_field_timestamp'] = $entity->{$date_field}->{$date_field_part}; + else { + $data['notification_id'] = $notification->id(); + $data['entity_type'] = $entity->getEntityTypeId(); + $data['entity_id'] = $entity->id(); + $data['time_offset_enabled'] = $notification->getTimeOffsetEnabled(); + // Check field + if ($date_field = $notification->getTimeOffsetDateFieldOnly()) { + $data['entity_date_field'] = $date_field; + if ($date_field_part = $notification->getTimeOffsetDateFieldPart()) { + $data['entity_date_field_part'] = $date_field_part; + $data['entity_date_field_timestamp'] = $entity->{$date_field}->{$date_field_part}; + } + else { + $data['entity_date_field_part'] = NULL; + $data['entity_date_field_timestamp'] = $entity->get($date_field)->date->getTimestamp(); + } + // Calculate offset + $time_offset = $notification->getTimeOffset(); + $time_offset_type = $notification->getTimeOffsetType(); + $data['time_offset_type'] = $time_offset_type; + $data['time_offset'] = $time_offset; + $calculdated_offset = $this->getCalculatedOffset($time_offset, $time_offset_type); + $data['entity_date_offset'] = $data['entity_date_field_timestamp'] + ($calculdated_offset); + $data['entity_date_string_day'] = $this->getCalculatedOffsetString($data['entity_date_offset']); + $data['entity_date_string_hour'] = $this->getCalculatedOffsetString($data['entity_date_offset'], 'hours'); } - else { - $data['entity_date_field_part'] = NULL; - $data['entity_date_field_timestamp'] = $entity->get($date_field)->date->getTimestamp(); - } - // Calculate offset - $time_offset = $notification->getTimeOffset(); - $time_offset_type = $notification->getTimeOffsetType(); - $data['time_offset_type'] = $time_offset_type; - $data['time_offset'] = $time_offset; - $calculdated_offset = $this->getCalculatedOffset($time_offset, $time_offset_type); - $data['entity_date_offset'] = $data['entity_date_field_timestamp'] + ($calculdated_offset); - $data['entity_date_string_day'] = $this->getCalculatedOffsetString($data['entity_date_offset']); - $data['entity_date_string_hour'] = $this->getCalculatedOffsetString($data['entity_date_offset'], 'hours'); } - } - // Only Create log if all conditions met - if ($data['time_offset_enabled'] && $data['entity_date_offset']) { - $this->addToLogQueue($data); - } - else { - $debug = json_encode($data); - $this->getLogger('conditional_notification') - ->warning('Problem creating log. Debug Info: @debug.', [ - '@debug' => $debug - ]); + // Only Create log if all conditions met + if ($data['time_offset_enabled'] && $data['entity_date_offset']) { + $this->addToLogQueue($data); + } + else { + $debug = json_encode($data); + $this->getLogger('conditional_notification') + ->warning('Problem creating log. Debug Info: @debug.', [ + '@debug' => $debug + ]); + } } } } diff --git a/src/Plugin/QueueWorker/NotificationQueue.php b/src/Plugin/QueueWorker/NotificationQueue.php index 47e7f6d29c6e3196ce162af3bf4391829ec09327..863cee2d6d337e0ec9878767971fa0dfd365bc55 100644 --- a/src/Plugin/QueueWorker/NotificationQueue.php +++ b/src/Plugin/QueueWorker/NotificationQueue.php @@ -111,38 +111,53 @@ final class NotificationQueue extends QueueWorkerBase implements ContainerFactor $notification = $this->entityTypeManager->getStorage('cn_template')->load($notification_id); if ($notification instanceof CnTemplateInterface) { - // Get the context and get the recipients - // Check if new eca conditions work - $recipients_eca_model_machine_name = $notification->getNotificationContextEca(); - $event = new RecipientsEvent($entity, $data['entity_type'], $entity->bundle(), 'default', $recipients_eca_model_machine_name); - $this->eventDispatcher->dispatch($event, RecipientsEvent::EVENT_NAME); - $recipients = $event->result; - if (!empty($recipients) && is_array($recipients)) { - $users = $this->entityTypeManager->getStorage('user')->loadMultiple($recipients); - if (!empty($users)) { - foreach ($users as $user) { - $preferred_language = $user->getPreferredLangcode(); - $user_language = $this->languageManager->getLanguage($preferred_language); - $notification_content = $this->getNotificationContent($notification_id, $user_language); - $this->sendMail($user->getEmail(), $preferred_language, $notification_content, $user, $entity_data); + + if ( + $notification->getNotificationEntityType() === $entity->getEntityTypeId() && + $notification->getNotificationEntityBundle() === $entity->bundle() + ) { + + // Get the selected condition now from ECA + $condition_eca_model_machine_name = $notification->getNotificationConditionEca(); + // ECA Condition + $event = new ConditionUpdateTriggerEvent($entity, $entity->getEntityTypeId(), $entity->bundle(), 'default', $condition_eca_model_machine_name); + $this->eventDispatcher->dispatch($event, ConditionUpdateTriggerEvent::EVENT_NAME); + $is_condition = $event->result; + if ($is_condition) { + // Get the context and get the recipients + // Check if new eca conditions work + $recipients_eca_model_machine_name = $notification->getNotificationContextEca(); + $event = new RecipientsEvent($entity, $data['entity_type'], $entity->bundle(), 'default', $recipients_eca_model_machine_name); + $this->eventDispatcher->dispatch($event, RecipientsEvent::EVENT_NAME); + $recipients = $event->result; + if (!empty($recipients) && is_array($recipients)) { + $users = $this->entityTypeManager->getStorage('user')->loadMultiple($recipients); + if (!empty($users)) { + foreach ($users as $user) { + $preferred_language = $user->getPreferredLangcode(); + $user_language = $this->languageManager->getLanguage($preferred_language); + $notification_content = $this->getNotificationContent($notification_id, $user_language); + $this->sendMail($user->getEmail(), $preferred_language, $notification_content, $user, $entity_data); + } + } + else { + \Drupal::service('conditional_notification.issue_informer')->sendIssueInformerMail($data); + $this->getLogger('conditional_notification')->warning('No users found to send notifications to!'); + } + } + else { + \Drupal::service('conditional_notification.issue_informer')->sendIssueInformerMail($data); + $debug = json_encode($data); + $this->getLogger('conditional_notification') + ->warning('No recipients found. Check the following plugin: @context. Debug info: @debug', [ + '@context' => $recipients_eca_model_machine_name, '@debug' => $debug + ]); } } else { - \Drupal::service('conditional_notification.issue_informer')->sendIssueInformerMail($data); - $this->getLogger('conditional_notification')->warning('No users found to send notifications to!'); + $this->getLogger('conditional_notification')->warning('No notification configured!'); } } - else { - \Drupal::service('conditional_notification.issue_informer')->sendIssueInformerMail($data); - $debug = json_encode($data); - $this->getLogger('conditional_notification') - ->warning('No recipients found. Check the following plugin: @context. Debug info: @debug', [ - '@context' => $recipients_eca_model_machine_name, '@debug' => $debug - ]); - } - } - else { - $this->getLogger('conditional_notification')->warning('No notification configured!'); } }