View source
<?php
namespace Drupal\social_follow_taxonomy\Plugin\ActivityContext;
use Drupal\activity_creator\ActivityFactory;
use Drupal\activity_creator\Plugin\ActivityContextBase;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Query\Sql\QueryFactory;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\social_group\SocialGroupHelperService;
use Drupal\user\UserInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FollowTaxonomyActivityContext extends ActivityContextBase {
protected $moduleHandler;
protected $connection;
protected $groupHelperService;
public function __construct(array $configuration, $plugin_id, $plugin_definition, QueryFactory $entity_query, EntityTypeManagerInterface $entity_type_manager, ActivityFactory $activity_factory, ModuleHandlerInterface $module_handler, Connection $connection, SocialGroupHelperService $group_helper_service) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_query, $entity_type_manager, $activity_factory);
$this->moduleHandler = $module_handler;
$this->connection = $connection;
$this->groupHelperService = $group_helper_service;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity.query.sql'), $container
->get('entity_type.manager'), $container
->get('activity_creator.activity_factory'), $container
->get('module_handler'), $container
->get('database'), $container
->get('social_group.helper_service'));
}
public function getRecipients(array $data, $last_uid, $limit) {
if (!empty($data['actor']) && $data['actor'] === 0) {
return [];
}
$recipients = [];
if (isset($data['related_object']) && !empty($data['related_object'])) {
$related_entity = $this->activityFactory
->getActivityRelatedEntity($data);
if ($related_entity['target_type'] == 'node' || $related_entity['target_type'] == 'post') {
$recipients += $this
->getRecipientsWhoFollowTaxonomy($related_entity, $data);
}
}
return $recipients;
}
public function taxonomyTermsList($entity) {
$term_ids = social_follow_taxonomy_terms_list($entity);
return $term_ids;
}
public function getRecipientsWhoFollowTaxonomy(array $related_entity, array $data) {
$recipients = [];
$entity = $this->entityTypeManager
->getStorage($related_entity['target_type'])
->load($related_entity['target_id']);
if (!empty($entity)) {
$tids = $this
->taxonomyTermsList($entity);
}
if (empty($tids)) {
return [];
}
$uids = $this->connection
->select('flagging', 'f')
->fields('f', [
'uid',
])
->condition('flag_id', 'follow_term')
->condition('entity_type', 'taxonomy_term')
->condition('entity_id', $tids, 'IN')
->groupBy('uid')
->execute()
->fetchCol();
$users = $this->entityTypeManager
->getStorage('user')
->loadMultiple($uids);
foreach ($users as $recipient) {
if (!$recipient instanceof UserInterface) {
continue;
}
if ($recipient
->isBlocked() || !$recipient
->getLastLoginTime()) {
continue;
}
if ($recipient
->id() === $entity
->getOwnerId()) {
continue;
}
if (!$this
->haveAccessToNode($recipient, $entity
->id())) {
continue;
}
$recipients[] = [
'target_type' => 'user',
'target_id' => $recipient
->id(),
];
}
return $recipients;
}
public function isValidEntity(EntityInterface $entity) {
if (!$entity instanceof ContentEntityInterface) {
return FALSE;
}
switch ($entity
->getEntityTypeId()) {
case 'node':
case 'post':
foreach ($this
->getListOfTagsFields() as $field_name) {
if ($entity
->hasField($field_name) && !$entity
->get($field_name)
->isEmpty()) {
return TRUE;
}
}
return FALSE;
}
return FALSE;
}
public function getListOfTagsFields() {
$fields_to_check = [
'social_tagging',
];
$this->moduleHandler
->alter('social_follow_taxonomy_fields', $fields_to_check);
return $fields_to_check;
}
protected function haveAccessToNode(UserInterface $recipient, $nid) {
$query = $this->connection
->select('node_field_data', 'nfd');
$query
->leftJoin('node__field_content_visibility', 'nfcv', 'nfcv.entity_id = nfd.nid');
$query
->leftJoin('group_content_field_data', 'gcfd', 'gcfd.entity_id = nfd.nid');
$or = $query
->orConditionGroup();
$community_access = $or
->andConditionGroup()
->condition('nfcv.field_content_visibility_value', [
'community',
'public',
], 'IN')
->isNull('gcfd.entity_id');
$or
->condition($community_access);
$memberships = $this->groupHelperService
->getAllGroupsForUser($recipient
->id());
if (count($memberships) > 0) {
$access_by_group = $or
->andConditionGroup();
$access_by_group
->condition('nfcv.field_content_visibility_value', [
'group',
'community',
'public',
], 'IN');
$access_by_group
->condition('gcfd.type', '%-group_node-%', 'LIKE');
$access_by_group
->condition('gcfd.gid', $memberships, 'IN');
$or
->condition($access_by_group);
}
$or
->isNull('nfcv.entity_id');
$query
->condition($or);
$query
->condition('nfd.nid', $nid);
$query
->groupBy('nfd.nid');
$query
->addExpression('COUNT(*)');
$nids = $query
->execute()
->fetchField();
return !empty($nids);
}
}