View source
<?php
namespace Drupal\mass_contact;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\mass_contact\Entity\MassContactCategoryInterface;
class OptOut implements OptOutInterface {
protected $entityManager;
protected $configFactory;
public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) {
$this->configFactory = $config_factory;
$this->entityManager = $entity_type_manager;
}
public function getOptOutAccounts(array $categories = []) {
$config = $this->configFactory
->get('mass_contact.settings');
if ($config
->get('optout_enabled') === MassContactInterface::OPT_OUT_DISABLED) {
return [];
}
$query = $this->entityManager
->getStorage('user')
->getQuery();
$query
->condition('status', 1);
if ($config
->get('optout_enabled') === MassContactInterface::OPT_OUT_GLOBAL) {
$query
->condition(MassContactInterface::OPT_OUT_FIELD_ID, 0, '<>');
}
else {
$category_ids = array_map(function (MassContactCategoryInterface $category) {
return $category
->id();
}, $categories);
$group = $query
->orConditionGroup()
->condition(MassContactInterface::OPT_OUT_FIELD_ID, $category_ids, 'IN')
->condition(MassContactInterface::OPT_OUT_FIELD_ID, '1');
$query
->condition($group);
}
return $query
->execute();
}
}