From 296398da36ae565e47a07e4d7553647a33137010 Mon Sep 17 00:00:00 2001 From: Lio Novelli Date: Wed, 6 Mar 2024 00:36:34 +0100 Subject: [PATCH] Posiljanje mailov na prave naslove ob pravih spremembah. --- web/modules/custom/yufu_admin/README.md | 12 ++ .../yufu_admin/translations/yufu_admin.sl.po | 4 + .../custom/yufu_admin/yufu_admin.module | 119 ++++++++++++++++-- 3 files changed, 126 insertions(+), 9 deletions(-) create mode 100644 web/modules/custom/yufu_admin/README.md create mode 100644 web/modules/custom/yufu_admin/translations/yufu_admin.sl.po diff --git a/web/modules/custom/yufu_admin/README.md b/web/modules/custom/yufu_admin/README.md new file mode 100644 index 0000000..acd912b --- /dev/null +++ b/web/modules/custom/yufu_admin/README.md @@ -0,0 +1,12 @@ +# Yufu Admin + +Administrativna in uredniška funkcionalnost. + +Obveščanje preko mailov, ko se spremeni revizija koncepta. + +## Testiranje + +1. Kot anonimni uporabnik predlagamo spremembe koncepta preko frontend apija. +2. Kot urednik objavimo predlagane spremembe. + - Fino bi bilo imet pregled (view) predlaganih in še ne objavljenih sprememb. +3. Pogledamo v mailhog, če so se maili poslali. (ddev status) diff --git a/web/modules/custom/yufu_admin/translations/yufu_admin.sl.po b/web/modules/custom/yufu_admin/translations/yufu_admin.sl.po new file mode 100644 index 0000000..b9ad7e0 --- /dev/null +++ b/web/modules/custom/yufu_admin/translations/yufu_admin.sl.po @@ -0,0 +1,4 @@ +# Slovenian translations for yufu_admin module +# +msgid "There was a problem sending your message and it was not sent. For transition @transition." +msgstr "Prišlo je do problema pri pošiljanju e-poštnega sporočila in le to ni bilo poslano. Za tranzicijo @transition" diff --git a/web/modules/custom/yufu_admin/yufu_admin.module b/web/modules/custom/yufu_admin/yufu_admin.module index 7d7b030..edbd3f3 100644 --- a/web/modules/custom/yufu_admin/yufu_admin.module +++ b/web/modules/custom/yufu_admin/yufu_admin.module @@ -15,7 +15,7 @@ use Drupal\node\NodeInterface; function yufu_admin_entity_operation(EntityInterface $entity) { $operations = []; $entityType = $entity->getEntityType(); - // @todo Only for entity node - should we add other entities as well? + // @TODO Only for entity node - should we add other entities as well? if ($entityType->id() === 'node' && \Drupal::currentUser()->hasPermission('use jsonapi operation link')) { // Build the url. $url = Url::fromRoute(sprintf('jsonapi.%s--%s.individual', $entityType->id(), $entity->bundle()), @@ -67,12 +67,15 @@ function yufu_admin_node_presave(EntityInterface $entity) { $transition = _yufu_admin_get_node_transition($entity); } switch ($transition) { + case 'stay_draft': case 'concept_drafted': // Send email to editors. _yufu_admin_concept_drafted_mail($entity); break; case 'concept_approved': // Send email to revision creator (user). + // @TODO Maybe notify other editors. + _yufu_admin_concept_approved_send_mail($entity); break; case 'concept_rejectd': // Send email to revision creator (user). @@ -93,10 +96,10 @@ function yufu_admin_node_presave(EntityInterface $entity) { * Name of the transition. */ function _yufu_admin_get_node_transition(NodeInterface $node) { - $original = $node->original; + $original = $node->original ?? NULL; $moderation_state = $node->moderation_state->value; - $previous_state = $original->moderation_state->value; - if ($moderation_state == 'draft' && $previous_state == 'published') { + $previous_state = $original?->moderation_state->value ?? NULL; + if ($moderation_state == 'draft' && in_array($previous_state, ['published', NULL])) { return 'concept_drafted'; } else if ($moderation_state == 'published' && $previous_state == 'draft') { @@ -111,34 +114,55 @@ function _yufu_admin_get_node_transition(NodeInterface $node) { return NULL; } +/** + * Notify user that their concept (revision) was published. + * + * @param \Drupal\node\NodeInterface $node + * Concept node that was transitioned into published state. + */ function _yufu_admin_concept_drafted_mail(NodeInterface $node) { $mailManager = \Drupal::service('plugin.manager.mail'); $module = 'yufu_admin'; + // @TODO - key doesn't need to be defined again. $key = 'concept_drafted'; // To all editors. $to = _yufu_admin_get_all_editors_emails(); $params['message'] = t('New concept created: @title - @url', [ - 'title' => $node->getTitle(), - 'url' => $node->toUrl()->toString(), + '@title' => $node->getTitle(), + '@url' => $node->toUrl()->toString(), ]); - $params['node_title'] = $entity->label(); + $params['node_title'] = $node->getTitle(); $langcode = \Drupal::currentUser()->getPreferredLangcode(); $send = true; $result = $mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send); + // @TODO: Remove code duplications (_yufu_admin_concept_approved_send_mail). if ($result['result'] !== true) { - drupal_set_message(t('There was a problem sending your message and it was not sent.'), 'error'); + Drupal::logger('yufu_admin')->error(t('There was a problem sending your message and it was not sent. For transition @transition.', [ + '@transition' => $key, + ]), 'error'); + Drupal::messenger()->addError(t('There was a problem sending your message and it was not sent. For transition @transition.', [ + '@transition' => $key, + ]), 'error'); } else { - drupal_set_message(t('Your message has been sent.')); + Drupal::logger('yufu_admin')->notice(t('Your message has been sent.')); + Drupal::messenger()->addStatus(t('Your message has been sent.')); } } +/** + * Get a list of emails of all editors. + * + * @return array + * List of strings. + */ function _yufu_admin_get_all_editors_emails() { $user_storage = \Drupal::entityTypeManager()->getStorage('user'); $ids = $user_storage->getQuery() ->condition('status', 1) ->condition('roles', 'administrator') + ->accessCheck(FALSE) ->execute(); $editors = $user_storage->loadMultiple($ids); $emails = []; @@ -148,3 +172,80 @@ function _yufu_admin_get_all_editors_emails() { } return $emails; } + +/** + * Notify user that their concept (revision) was published. + * + * @param \Drupal\node\NodeInterface $node + * Concept node that was transitioned into published state. + */ +function _yufu_admin_concept_approved_send_mail(NodeInterface $node) { + $mailManager = \Drupal::service('plugin.manager.mail'); + $module = 'yufu_admin'; + // @TODO - key doesn't need to be defined again. + $key = 'concept_approved'; + // To the revision's authors. + // @TODO - to make this work properly, we should get a list of all authors of + // unpublished revisions so far since the last published revision. + if ($to = _yufu_admin_get_pioneer_email($node)) { + $params['message'] = t('New concept created: @title - @url', [ + '@title' => $node->getTitle(), + '@url' => $node->toUrl()->toString(), + ]); + } + else { + $to = implode(',', _yufu_admin_get_all_editors_emails()); + $params['message'] = t('Published changes not attributed to any of knownn users: @title - @url', [ + 'title' => $node->getTitle(), + 'url' => $node->toUrl()->toString(), + ]); + } + $params['node_title'] = $node->getTitle(); + $langcode = \Drupal::currentUser()->getPreferredLangcode(); + $send = TRUE; + + $result = $mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send); + if ($result['result'] !== TRUE) { + Drupal::logger('yufu_admin')->error(t('There was a problem sending your message and it was not sent. For transition @transition.', [ + '@transition' => $key, + ]), 'error'); + Drupal::messenger()->addError(t('There was a problem sending your message and it was not sent. For transition @transition.', [ + '@transition' => $key, + ]), 'error'); + } + else { + Drupal::logger('yufu_admin')->notice(t('Your message has been sent.')); + Drupal::messenger()->addStatus(t('Your message has been sent.')); + } +} + +/** + * Get the email of person (pioneer) that worked on proposed changes. + * + * @return string|NULL + * List of strings. + */ +function _yufu_admin_get_pioneer_email(NodeInterface $node) { + // $email = $node->uid->entity; + if ($original = $node->original) { + $user = $original->getRevisionUser(); + $email = $user->getEmail(); + return $email ?? NULL; + } + // @TODO: Make this code nicer. + $user = $node->getRevisionUser(); + $email = $user->getEmail(); + return $email ?? NULL; +} + + +/** + * Get a list of emails of people that worked on proposed changes. + * + * @return array + * List of strings. + */ +function _yufu_admin_get_all_pioneer_emails() { + // @TODO: Think it through if it is really needed. + return []; +}