View source
<?php
namespace Drupal\activity_creator;
use Drupal\activity_creator\Entity\Activity;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Controller\ControllerBase;
use Drupal\language\ConfigurableLanguageManagerInterface;
use Drupal\message\Entity\Message;
use Drupal\activity_creator\Plugin\ActivityDestinationManager;
class ActivityFactory extends ControllerBase {
protected $activityDestinationManager;
public function __construct(ActivityDestinationManager $activityDestinationManager) {
$this->activityDestinationManager = $activityDestinationManager;
}
public function createActivities(array $data) {
$activities = $this
->buildActivities($data);
return $activities;
}
protected function buildActivities(array $data) {
$activities = [];
$message = Message::load($data['mid']);
$activity_fields = [
'created' => $this
->getCreated($message),
'field_activity_destinations' => $this
->getFieldDestinations($data),
'field_activity_entity' => $this
->getFieldEntity($data),
'field_activity_message' => $this
->getFieldMessage($data),
'field_activity_output_text' => $this
->getFieldOutputText($message),
'field_activity_recipient_group' => $this
->getFieldRecipientGroup($data),
'field_activity_recipient_user' => $this
->getFieldRecipientUser($data),
'field_activity_status' => ACTIVITY_STATUS_RECEIVED,
'user_id' => $this
->getActor($data),
];
if ($this
->getAggregationSettings($message)) {
$activities = $this
->buildAggregatedActivites($data, $activity_fields);
}
else {
$activity = Activity::create($activity_fields);
$activity
->save();
$activities[] = $activity;
}
return $activities;
}
protected function getFieldDestinations(array $data, $allowed_destinations = []) {
$value = NULL;
if (isset($data['destination'])) {
$value = $data['destination'];
if (!empty($allowed_destinations)) {
foreach ($value as $key => $destination) {
if (!in_array($destination['value'], $allowed_destinations)) {
unset($value[$key]);
}
}
}
}
return $value;
}
protected function getFieldEntity($data) {
$value = NULL;
if (isset($data['related_object'])) {
$value = $data['related_object'];
}
return $value;
}
protected function getFieldMessage($data) {
$value = NULL;
if (isset($data['mid'])) {
$value = [];
$value[] = [
'target_id' => $data['mid'],
];
}
return $value;
}
protected function getFieldOutputText(Message $message, $arguments = []) {
$value = NULL;
if (isset($message)) {
$value = $this
->getMessageText($message);
if (!empty($value[1]) && !empty($arguments)) {
$text = str_replace('@count', $arguments['@count'], $value[1]);
}
else {
$text = $value[0];
}
$value = [
'0' => [
'value' => $text,
'format' => 'basic_html',
],
];
}
return $value;
}
protected function getCreated(Message $message) {
$value = NULL;
if (isset($message)) {
$value = $message
->getCreatedTime();
}
return $value;
}
protected function getAggregationSettings(Message $message) {
$message_template = $message
->getTemplate();
return $message_template
->getThirdPartySetting('activity_logger', 'activity_aggregate', NULL);
}
protected function buildAggregatedActivites($data, $activity_fields) {
$activities = [];
$common_destinations = $this->activityDestinationManager
->getListByProperties('isCommon', TRUE);
$personal_destinations = $this->activityDestinationManager
->getListByProperties('isCommon', FALSE);
$related_activities = $this
->getAggregationRelatedActivities($data);
if (!empty($related_activities)) {
foreach ($related_activities as $related_activity) {
$destination = $related_activity->field_activity_destinations->value;
if ($related_activity
->getOwnerId() == $this
->getActor($data) || in_array($destination, $common_destinations)) {
$related_activity
->delete();
}
else {
$related_activity
->set('field_activity_destinations', [
'stream_profile',
]);
$related_activity
->save();
}
}
$profile_activity_fields = $activity_fields;
$arguments = [];
$message = Message::load($data['mid']);
$count = $this
->getAggregationAuthorsCount($data);
if (is_numeric($count) && $count > 1) {
$arguments = [
'@count' => $count - 1,
];
}
$activity_fields['field_activity_output_text'] = $this
->getFieldOutputText($message, $arguments);
$activity_fields['field_activity_destinations'] = $this
->getFieldDestinations($data, $common_destinations);
$profile_activity_fields['field_activity_destinations'] = $this
->getFieldDestinations($data, $personal_destinations);
$activity = Activity::create($profile_activity_fields);
$activity
->save();
$activities[] = $activity;
}
$activity = Activity::create($activity_fields);
$activity
->save();
$activities[] = $activity;
return $activities;
}
protected function getAggregationRelatedActivities($data) {
$activities = [];
$related_object = $data['related_object'][0];
if (!empty($related_object['target_id']) && !empty($related_object['target_type'])) {
if ($related_object['target_type'] === 'comment') {
$comment_storage = \Drupal::entityTypeManager()
->getStorage('comment');
$comment = $comment_storage
->load($related_object['target_id']);
$commented_entity = $comment
->getCommentedEntity();
$comment_query = \Drupal::entityQuery('comment');
$comment_query
->condition('entity_id', $commented_entity
->id(), '=');
$comment_query
->condition('entity_type', $commented_entity
->getEntityTypeId(), '=');
$comment_ids = $comment_query
->execute();
if (!empty($comment_ids)) {
$activity_query = \Drupal::entityQuery('activity');
$activity_query
->condition('field_activity_entity.target_id', $comment_ids, 'IN');
$activity_query
->condition('field_activity_entity.target_type', $related_object['target_type'], '=');
$aggregatable_destinations = $this->activityDestinationManager
->getListByProperties('isAggregatable', TRUE);
$activity_query
->condition('field_activity_destinations.value', $aggregatable_destinations, 'IN');
$activity_ids = $activity_query
->execute();
if (!empty($activity_ids)) {
$activities = Activity::loadMultiple($activity_ids);
}
}
}
}
return $activities;
}
public static function getActivityRelatedEntity($data) {
$related_object = $data['related_object'][0];
if ($data['message_template'] === 'create_comment_reply') {
$comment_storage = \Drupal::entityTypeManager()
->getStorage('comment');
$comment = $comment_storage
->load($related_object['target_id']);
if ($comment) {
$parent_comment = $comment
->getParentComment();
if (!empty($parent_comment)) {
$related_object = [
'target_type' => $parent_comment
->getEntityTypeId(),
'target_id' => $parent_comment
->id(),
];
}
}
}
elseif (isset($related_object['target_type']) && $related_object['target_type'] === 'comment') {
$comment_storage = \Drupal::entityTypeManager()
->getStorage('comment');
$comment = $comment_storage
->load($related_object['target_id']);
if ($comment) {
$commented_entity = $comment
->getCommentedEntity();
if (!empty($commented_entity)) {
$related_object = [
'target_type' => $commented_entity
->getEntityTypeId(),
'target_id' => $commented_entity
->id(),
];
}
}
}
elseif (isset($related_object['target_type']) && $related_object['target_type'] === 'event_enrollment') {
$entity_storage = \Drupal::entityTypeManager()
->getStorage($related_object['target_type']);
$entity = $entity_storage
->load($related_object['target_id']);
if (!empty($entity)) {
$event_id = $entity
->getFieldValue('field_event', 'target_id');
if (!empty($event_id)) {
$related_object = [
'target_type' => 'node',
'target_id' => $event_id,
];
}
}
}
return $related_object;
}
protected function getAggregationAuthorsCount(array $data) {
$count = 0;
$related_object = $data['related_object'][0];
if (isset($related_object['target_type']) && $related_object['target_type'] === 'comment') {
$related_entity = $this
->getActivityRelatedEntity($data);
if (!empty($related_entity['target_id']) && !empty($related_entity['target_type'])) {
$query = \Drupal::database()
->select('comment_field_data', 'cfd');
$query
->addExpression('COUNT(DISTINCT cfd.uid)');
$query
->condition('cfd.status', 1);
$query
->condition('cfd.entity_type', $related_entity['target_type']);
$query
->condition('cfd.entity_id', $related_entity['target_id']);
$count = $query
->execute()
->fetchField();
}
}
return $count;
}
protected function getFieldRecipientGroup($data) {
$value = NULL;
if (isset($data['recipient'])) {
if ($data['recipient']['target_type'] === 'group') {
$value = [
$data['recipient'],
];
}
}
return $value;
}
protected function getFieldRecipientUser($data) {
$value = NULL;
if (isset($data['recipient']) && is_array($data['recipient'])) {
if ($data['recipient']['target_type'] === 'user') {
$value = [
$data['recipient'],
];
}
}
return $value;
}
protected function getActor(array $data) {
$value = 0;
if (isset($data['actor'])) {
$value = $data['actor'];
}
return $value;
}
public function getMessageText(Message $message, $langcode = '') {
$message_template = $message
->getTemplate();
$message_arguments = $message
->getArguments();
$message_template_text = $message_template
->get('text');
if (!empty($langcode)) {
$language_manager = \Drupal::languageManager();
if ($language_manager instanceof ConfigurableLanguageManagerInterface) {
$config_translation = $language_manager
->getLanguageConfigOverride($langcode, 'message.template.' . $message_template
->id());
$translated_text = $config_translation
->get('text');
if ($translated_text) {
$message_template_text = $translated_text;
}
}
}
$output = $this
->processArguments($message_arguments, $message_template_text, $message);
$token_options = $message_template
->getSetting('token options', []);
if (!empty($token_options['token replace'])) {
$output = $this
->processTokens($output, !empty($token_options['clear']), $message);
}
return $output;
}
protected function processArguments(array $arguments, array $output, Message $message) {
if (empty($arguments)) {
return $output;
}
foreach ($arguments as $key => $value) {
if (is_array($value) && !empty($value['callback']) && is_callable($value['callback'])) {
$value += [
'pass message' => FALSE,
];
if ($value['pass message']) {
$value['arguments']['message'] = $message;
}
$arguments[$key] = call_user_func_array($value['callback'], $value['arguments']);
}
}
foreach ($output as $key => $value) {
$output[$key] = new FormattableMarkup($value, $arguments);
}
return $output;
}
protected function processTokens(array $output, $clear, Message $message) {
$options = [
'clear' => $clear,
];
foreach ($output as $key => $value) {
if (is_string($value)) {
$output[$key] = \Drupal::token()
->replace($value, [
'message' => $message,
], $options);
}
else {
if (isset($value['value'])) {
$output[$key] = \Drupal::token()
->replace($value['value'], [
'message' => $message,
], $options);
}
}
}
return $output;
}
}