View source
<?php
namespace Drupal\activity_viewer\Plugin\views\filter;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\social_group\SocialGroupHelperService;
use Drupal\views\Plugin\views\filter\FilterPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ActivityFilterPersonalisedHomepage extends FilterPluginBase {
protected $groupHelper;
protected $connection;
protected $moduleHandler;
public function __construct(array $configuration, $plugin_id, $plugin_definition, SocialGroupHelperService $group_helper, Connection $connection, ModuleHandlerInterface $module_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->groupHelper = $group_helper;
$this->connection = $connection;
$this->moduleHandler = $module_handler;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('social_group.helper_service'), $container
->get('database'), $container
->get('module_handler'));
}
public function canExpose() {
return FALSE;
}
public function query() {
$account = $this->view
->getUser();
$skip_roles = [
'administrator',
'contentmanager',
'sitemanager',
];
$hide_from_view = $nids = $pids = $cids = [];
if (!empty(array_intersect($skip_roles, $account
->getRoles()))) {
return;
}
$this
->ensureMyTable();
$group_memberships = $this->groupHelper
->getAllGroupsForUser($account
->id());
$filter_query = $this->query;
$filter_query
->addTable('activity__field_activity_entity');
$filter_query
->addTable('activity__field_activity_recipient_group');
$filter_query
->addTable('activity__field_activity_recipient_user');
$and_wrapper = new Condition('AND');
$or = new Condition('OR');
if ($account
->hasPermission('access content')) {
$nids = $this
->getAvailableNodeIds($account, $group_memberships);
if (!empty($nids)) {
$node_access = $or
->andConditionGroup()
->condition('activity__field_activity_entity.field_activity_entity_target_type', 'node')
->condition('activity__field_activity_entity.field_activity_entity_target_id', $nids, 'IN');
$or
->condition($node_access);
}
else {
$hide_from_view[] = 'node';
}
}
$pids = $this
->getAvailablePostIds($account, $group_memberships);
if (!empty($pids)) {
$post_access = $or
->andConditionGroup()
->condition('activity__field_activity_entity.field_activity_entity_target_type', 'post')
->condition('activity__field_activity_entity.field_activity_entity_target_id', $pids, 'IN');
$or
->condition($post_access);
}
else {
$hide_from_view[] = 'post';
}
if ($account
->hasPermission('access comments')) {
$cids = $this
->getAvailableCommentIds($nids, $pids);
if (!empty($cids)) {
$comments_access = $or
->andConditionGroup()
->condition('activity__field_activity_entity.field_activity_entity_target_type', 'comment')
->condition('activity__field_activity_entity.field_activity_entity_target_id', $cids, 'IN');
$or
->condition($comments_access);
}
else {
$hide_from_view[] = 'comment';
}
}
if (!empty($hide_from_view)) {
$and_wrapper
->condition('activity__field_activity_entity.field_activity_entity_target_type', $hide_from_view, 'NOT IN');
}
if (!empty($or
->conditions()[0])) {
$and_wrapper
->condition($or);
}
if ($account
->isAnonymous()) {
$anonymous_access = new Condition('OR');
$anonymous_access
->condition('activity__field_activity_recipient_user.field_activity_recipient_user_target_id', '0');
$anonymous_access
->isNull('activity__field_activity_recipient_user.field_activity_recipient_user_target_id');
$and_wrapper
->condition($anonymous_access);
}
if (!empty($and_wrapper
->conditions()[0])) {
$filter_query
->addWhere('visibility', $and_wrapper);
}
}
public function getCacheContexts() {
$contexts = parent::getCacheContexts();
$contexts[] = 'user';
return $contexts;
}
protected function getAvailableNodeIds(AccountInterface $user, array $memberships) {
$query = $this->connection
->select('node_field_data', 'nfd');
$query
->fields('nfd', [
'nid',
]);
$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();
if ($user
->isAuthenticated()) {
$community_access = $or
->andConditionGroup()
->condition('nfcv.field_content_visibility_value', [
'community',
'public',
], 'IN')
->isNull('gcfd.entity_id');
$or
->condition($community_access);
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);
}
}
else {
$anonymous_access = $or
->andConditionGroup()
->condition('nfcv.field_content_visibility_value', 'public')
->isNull('gcfd.entity_id');
$or
->condition($anonymous_access);
}
$or
->isNull('nfcv.entity_id');
$query
->condition($or);
$this->moduleHandler
->alter('activity_viewer_available_nodes_query', $query, $user);
$node_status = [
'1',
];
if ($user
->hasPermission('view any unpublished content')) {
$node_status[] = '0';
}
$query
->condition('nfd.status', $node_status, 'IN');
$nids = $query
->execute()
->fetchCol();
return array_unique($nids);
}
protected function getAvailablePostIds(AccountInterface $user, array $memberships) {
$query = $this->connection
->select('post_field_data', 'pfd');
$query
->fields('pfd', [
'id',
]);
$query
->leftJoin('post__field_visibility', 'pfv', 'pfv.entity_id = pfd.id');
$query
->leftJoin('post__field_recipient_group', 'pfrg', 'pfrg.entity_id = pfd.id');
$or = $query
->orConditionGroup();
if ($user
->isAuthenticated()) {
if ($user
->hasPermission('view community posts')) {
$community_access = $or
->andConditionGroup()
->condition('pfv.field_visibility_value', [
'0',
'1',
'2',
], 'IN')
->isNull('pfrg.entity_id');
$or
->condition($community_access);
}
if (count($memberships) > 0) {
$access_by_group = $or
->andConditionGroup();
$access_by_group
->condition('pfv.field_visibility_value', [
'0',
'1',
'2',
'3',
], 'IN');
$access_by_group
->condition('pfrg.field_recipient_group_target_id', $memberships, 'IN');
$or
->condition($access_by_group);
}
}
else {
if ($user
->hasPermission('view public posts')) {
$anonymous_access = $or
->andConditionGroup()
->condition('pfv.field_visibility_value', '1')
->isNull('pfrg.entity_id');
$or
->condition($anonymous_access);
}
}
$or
->isNull('pfv.entity_id');
$query
->condition($or);
$this->moduleHandler
->alter('activity_viewer_available_posts_query', $query, $user);
$post_status = [
'1',
];
if ($user
->hasPermission('view unpublished post entities')) {
$post_status[] = '0';
}
$query
->condition('pfd.status', $post_status, 'IN');
$pids = $query
->execute()
->fetchCol();
return array_unique($pids);
}
protected function getAvailableCommentIds(array $node_ids, array $post_ids) {
$query = $this->connection
->select('comment_field_data', 'cfd');
$query
->fields('cfd', [
'cid',
]);
$or = $query
->orConditionGroup();
if (empty($node_ids) && empty($post_ids)) {
return [];
}
if (!empty($node_ids)) {
$node_access = $or
->andConditionGroup();
$node_access
->condition('entity_type', 'node');
$node_access
->condition('entity_id', $node_ids, 'IN');
$or
->condition($node_access);
}
if (!empty($post_ids)) {
$post_access = $or
->andConditionGroup();
$post_access
->condition('entity_type', 'post');
$post_access
->condition('entity_id', $post_ids, 'IN');
$or
->condition($post_access);
}
if (!empty($or
->conditions()[0])) {
$query
->condition($or);
}
$query
->condition('cfd.status', '1');
$cids = $query
->execute()
->fetchCol();
return array_unique($cids);
}
}