252 lines
8.1 KiB
PHP
252 lines
8.1 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: Your concept (changes) were 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: Your concept (changes) were 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.
|
|
_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).
|
|
// This transition happens on revision delete.
|
|
// Curently it is dead.
|
|
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) {
|
|
$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(),
|
|
]);
|
|
$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::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 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 = [];
|
|
foreach ($editors as $editor) {
|
|
/* @var \Drupal\user\UserInterface $user */
|
|
$emails[] = $editor->getEmail();
|
|
}
|
|
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 [];
|
|
}
|