manifest/web/modules/custom/yufu_admin/yufu_admin.module

279 lines
8.7 KiB
PHP

<?php
/**
* @file
* Primary module hooks for Yufu Admin module.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
use Drupal\node\NodeInterface;
/**
* Implements hook_entity_operation().
*/
function yufu_admin_entity_operation(EntityInterface $entity) {
$operations = [];
$entityType = $entity->getEntityType();
// @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()),
['entity' => $entity->uuid()]
);
$operations['view-jsonapi-output'] = [
'title' => t('jsonapi Output'),
'weight' => 50,
'url' => $url,
];
}
return $operations;
}
/**
* Implements hook_mail().
*/
function yufu_admin_mail($key, &$message, $params) {
$options = ['langcode' => $message['langcode']];
switch ($key) {
case 'concept_drafted':
$message['from'] = \Drupal::config('system.site')->get('mail');
$message['subject'] = t('[YUFU] New concept draft: @title', ['@title' => $params['node_title']], $options);
$message['body'][] = $params['message'];
break;
case 'concept_approved':
$message['from'] = \Drupal::config('system.site')->get('mail');
$message['subject'] = t('[YUFU] Concept changes approved: @title', ['@title' => $params['node_title']], $options);
$message['body'][] = $params['message'];
break;
case 'concept_rejected':
$message['from'] = \Drupal::config('system.site')->get('mail');
$message['subject'] = t('[YUFU] Concept changes rejected: @title', ['@title' => $params['node_title']], $options);
$message['body'][] = $params['message'];
}
}
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function yufu_admin_node_presave(EntityInterface $entity) {
if ($entity instanceOf NodeInterface && $entity->bundle() == 'concept') {
// Get transition: concept_drafted, concept_approved, concept_rejected.
if ($entity->isNew()) {
$transition = 'concept_drafted';
}
else {
$transition = _yufu_admin_get_node_transition($entity);
}
switch ($transition) {
case 'stay_draft':
case 'concept_drafted':
// Send email to editors.
$params = _yufu_admin_concept_drafted_mail($entity);
$result = _yufu_admin_send_email($transition, $params['to'], $params);
_yufu_admin_status_message($result['result'], $transition, $entity, $params['to']);
break;
case 'concept_approved':
// Send email to revision creator (user).
// @TODO Maybe notify other editors.
$params = _yufu_admin_concept_approved_get_message($entity);
$result = _yufu_admin_send_email($transition, $params['to'], $params);
_yufu_admin_status_message($result['result'], $transition, $entity, $params['to']);
break;
case 'concept_rejectd':
// Send email to revision creator (user).
// This transition happens on revision delete.
// Curently it is dead.
// @TODO https://git.kompot.si/yufu/manifest/issues/45
break;
}
}
}
/**
* Compare original node moderation state with current state.
*
* @param \Drupal\node\NodeInterface $node
* Node to check transition on.
*
* @return string|null
* Name of the transition.
*/
function _yufu_admin_get_node_transition(NodeInterface $node) {
$original = $node->original ?? NULL;
$moderation_state = $node->moderation_state->value;
$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') {
return 'concept_approved';
}
else if ($moderation_state == 'published' && $previous_state == 'published') {
return 'stay_published';
}
else if ($moderation_state == 'draft' && $previous_state == 'draft') {
return 'stay_draft';
}
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) {
$to = _yufu_admin_get_all_editors_emails();
if ($node->isNew()) {
$params['message'] = t('New concept created: @title - @url', [
'@title' => $node->getTitle(),
'@url' => !$node->isNew() ? $node?->toUrl()?->toString() : '',
]);
}
else {
$params['message'] = t('New changes to concept proposed: @title - @url', [
'@title' => $node->getTitle(),
'@url' => !$node->isNew() ? $node?->toUrl()?->toString() : '',
]);
}
$params['node_title'] = $node->getTitle();
$params['to'] = implode(',', $to);
return $params;
}
/**
* Function for sending emails.
*
* @param string $key
* Transition of a concept.
* @param string $to
* List of recipients.
* @param array $params
* Message parameters.
*
* @return array
* Mail manager response.
*/
function _yufu_admin_send_email($key = '', $to = '', $params = []) {
$mailManager = \Drupal::service('plugin.manager.mail');
$module = 'yufu_admin';
$langcode = \Drupal::currentUser()->getPreferredLangcode();
$send = TRUE;
$result = $mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);
return $result;
}
/**
* Add notifications on sending of emails.
*/
function _yufu_admin_status_message($result, $transition = '', $node = NULL, $recipients = '') {
$log = t('For transition @transition of concept @title (@nid) to recipients @recipients.', [
'@transition' => $transition,
'@title' => $node?->getTitle() ?? t('Unknown'),
'@nid' => $node?->id() ?? '',
'@recipients' => $recipients,
]);
if ($result !== TRUE) {
$message = t('There was a problem sending your message and it was not sent. ');
Drupal::logger('yufu_admin')->error($message . $log);
Drupal::messenger()->addError($message . $log);
}
else {
$message = t('Your message has been sent. ');
Drupal::logger('yufu_admin')->notice($message . $log);
Drupal::messenger()->addStatus($message . $log);
}
}
/**
* 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', 'editor')
->accessCheck(FALSE)
->execute();
$editors = $user_storage->loadMultiple($ids);
$emails = [];
foreach ($editors as $editor) {
/* @var \Drupal\user\UserInterface $user */
if ($email = $editor?->getEmail()) {
$emails[] = $email;
}
else {
\Drupal::logger('yufu_admin')->warning('@editor (@uid) is missing email.',[
'@editor' => $editor?->getDisplayName() ?? '',
'@uid' => $editor->id(),
]);
}
}
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_get_message(NodeInterface $node) {
// 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('Your proposal of changes to concept were approved: @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->isNew() ? $node?->toUrl()?->toString() : '',
]);
}
$params['node_title'] = $node->getTitle();
$params['to'] = $to;
return $params;
}
/**
* 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 [];
}