public function ActivitySendEmailJobType::process in Open Social 10.2.x
Same name and namespace in other branches
- 10.3.x modules/custom/activity_send/modules/activity_send_email/src/Plugin/AdvancedQueue/JobType/ActivitySendEmailJobType.php \Drupal\activity_send_email\Plugin\AdvancedQueue\JobType\ActivitySendEmailJobType::process()
- 10.1.x modules/custom/activity_send/modules/activity_send_email/src/Plugin/AdvancedQueue/JobType/ActivitySendEmailJobType.php \Drupal\activity_send_email\Plugin\AdvancedQueue\JobType\ActivitySendEmailJobType::process()
File
- modules/
custom/ activity_send/ modules/ activity_send_email/ src/ Plugin/ AdvancedQueue/ JobType/ ActivitySendEmailJobType.php, line 132
Class
- ActivitySendEmailJobType
- Advanced Queue Job to process email workers and send them to SendGrid.
Namespace
Drupal\activity_send_email\Plugin\AdvancedQueue\JobTypeCode
public function process(Job $job) {
try {
// Get the Job data.
$data = $job
->getPayload();
// First make sure it's an actual Activity entity.
$activity_storage = $this->entityTypeManager
->getStorage('activity');
if (!empty($data['entity_id']) && ($activity = $activity_storage
->load($data['entity_id']))) {
// Check if activity related entity exist.
/** @var \Drupal\activity_creator\Entity\Activity $activity */
if (!$activity
->getRelatedEntity() instanceof EntityInterface) {
$activity
->delete();
$this->activityNotifications
->deleteNotificationsbyIds([
$activity
->id(),
]);
$this
->getLogger('activity_send_email_worker')
->notice('The activity was already deleted. We marked it as successful.');
return JobResult::success('The activity was already deleted. We marked it as successful.');
}
// Is the website multilingual.
$is_multilingual = $this->languageManager
->isMultilingual();
if (empty($data['recipients'])) {
$recipients = array_column($activity->field_activity_recipient_user
->getValue(), 'target_id');
if (count($recipients) > 50) {
if ($is_multilingual) {
// We also want to send emails to users per language in a given
// frequency.
foreach ($languages = $this->languageManager
->getLanguages() as $language) {
$langcode = $language
->getId();
// Load all user by given language.
$user_ids_per_language = $this->database
->select('users_field_data', 'ufd')
->fields('ufd', [
'uid',
])
->condition('uid', $recipients, 'IN')
->condition('preferred_langcode', $langcode)
->execute()
->fetchAllKeyed(0, 0);
// Prepare the batch per language.
$this
->prepareBatch($data, $user_ids_per_language, $langcode);
}
}
else {
$this
->prepareBatch($data, $recipients);
}
// We split up in batches. We can stop processing this specific queue
// item.
$this
->getLogger('activity_send_email_worker')
->notice('The Job was not finished correctly, no error thrown.');
return JobResult::success('The Job was has been split up in batches.');
}
}
else {
$recipients = $data['recipients'];
}
if (!empty($recipients)) {
// Get Message Template id.
$message_storage = $this->entityTypeManager
->getStorage('message');
/** @var \Drupal\message\Entity\Message $message */
$message = $message_storage
->load($activity
->getFieldValue('field_activity_message', 'target_id'));
$message_template_id = $message
->getTemplate()
->id();
// Prepare an array of all details required to process the item.
$parameters = [
'activity' => $activity,
'message' => $message,
'message_template_id' => $message_template_id,
];
// We want to give preference to users who have set notification
// settings as 'immediately'.
$email_frequencies = [
'immediately',
'daily',
'weekly',
'none',
];
// Let's store the users IDs which will be processed by the loop.
$processed_users = [];
foreach ($email_frequencies as $email_frequency) {
// Get the 'target recipients' of who have their 'email notification
// preference' matching to current $email_frequency.
if ($target_recipients = EmailActivityDestination::getSendEmailUsersIdsByFrequency($recipients, $message_template_id, $email_frequency)) {
// Update process users.
$processed_users = array_merge($processed_users, $target_recipients);
// We load all the target accounts.
$parameters['target_recipients'] = $target_recipients;
// We set the frequency of email.
$parameters['frequency'] = $email_frequency;
// If the batch has langcode.
if (!empty($data['langcode'])) {
$parameters['langcode'] = $data['langcode'];
}
// Send for further processing.
$this
->sendToFrequencyManager($parameters);
}
}
// There is possibility where the users have not saved their
// 'email notification preferences'. So, we check the difference
// between the processed user IDs and original recipients users IDs
// and send emails according to default 'frequency' set by site
// manager. If SM has also not set, take 'immediately' as frequency.
if ($remaining_users = array_diff($recipients, $processed_users)) {
// Grab the platform default "Email notification frequencies".
$template_frequencies = $this->swiftmailSettings
->get('template_frequencies') ?: [];
// Determine email frequency to use, defaults to immediately.
$parameters['frequency'] = $template_frequencies[$message_template_id] ?? FREQUENCY_IMMEDIATELY;
$parameters['target_recipients'] = $remaining_users;
$this
->sendToFrequencyManager($parameters);
}
}
}
// Mark the Job as successful.
$this
->getLogger('activity_send_email_worker')
->notice('The job was finished correctly.');
return JobResult::success('The job was finished correctly.');
} catch (\Exception $e) {
$this
->getLogger('activity_send_email_worker')
->error($e
->getMessage());
return JobResult::failure($e
->getMessage());
}
}