View source
<?php
namespace Drupal\workbench_access\Plugin\views\filter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\filter\ManyToOne;
use Drupal\views\Views;
use Drupal\views\ManyToOneHelper;
use Drupal\workbench_access\Entity\AccessSchemeInterface;
use Drupal\workbench_access\UserSectionStorageInterface;
use Drupal\workbench_access\WorkbenchAccessManager;
use Drupal\workbench_access\WorkbenchAccessManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Section extends ManyToOne {
protected $scheme;
protected $manager;
protected $userSectionStorage;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
return $instance
->setScheme($container
->get('entity_type.manager')
->getStorage('access_scheme')
->load($configuration['scheme']))
->setManager($container
->get('plugin.manager.workbench_access.scheme'))
->setUserSectionStorage($container
->get('workbench_access.user_section_storage'));
}
public function setManager(WorkbenchAccessManagerInterface $manager) {
$this->manager = $manager;
return $this;
}
public function setUserSectionStorage(UserSectionStorageInterface $userSectionStorage) {
$this->userSectionStorage = $userSectionStorage;
return $this;
}
public function setScheme(AccessSchemeInterface $scheme) {
$this->scheme = $scheme;
return $this;
}
public function getValueOptions() {
if (isset($this->valueOptions)) {
return $this->valueOptions;
}
$this->valueOptions = [];
if (!empty($this->scheme)) {
$scheme = $this->scheme
->getAccessScheme();
if ($this->manager
->userInAll($this->scheme)) {
$list = WorkbenchAccessManager::getAllSections($this->scheme, FALSE);
}
else {
$list = $this->userSectionStorage
->getUserSections($this->scheme);
if (!empty($this->options['section_filter']['show_hierarchy'])) {
$list = $this
->getChildren($list);
}
}
foreach ($list as $id) {
if ($section = $scheme
->load($id)) {
$this->valueOptions[$id] = str_repeat('-', $section['depth']) . ' ' . $section['label'];
}
}
}
return $this->valueOptions;
}
protected function valueForm(&$form, FormStateInterface $form_state) {
parent::valueForm($form, $form_state);
if (!$form_state
->get('exposed')) {
$this->helper
->buildOptionsForm($form, $form_state);
}
else {
$options = $this->valueOptions;
$exposed_options = [];
$empty = [
0 => $this
->t('All'),
];
if ($this->options['value'] != $empty && !isset($this->options['value']['all'])) {
foreach ($options as $key => $value) {
if (!isset($this->options['value'][$key])) {
unset($options[$key]);
}
}
}
$form['value']['#options'] = $options;
}
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['operator']['default'] = 'in';
$options['value']['default'] = [
'All',
];
$options['expose']['contains']['reduce'] = [
'default' => TRUE,
];
$options['section_filter']['contains']['show_hierarchy'] = [
'default' => TRUE,
];
return $options;
}
public function defaultExposeOptions() {
parent::defaultExposeOptions();
$this->options['expose']['reduce'] = TRUE;
}
public function operators() {
$operators = [
'in' => [
'title' => $this
->t('Is one of'),
'short' => $this
->t('in'),
'short_single' => $this
->t('='),
'method' => 'opSimple',
'values' => 1,
],
'not in' => [
'title' => $this
->t('Is not one of'),
'short' => $this
->t('not in'),
'short_single' => $this
->t('<>'),
'method' => 'opSimple',
'values' => 1,
],
];
return $operators;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['section_filter']['show_hierarchy'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Show children'),
'#default_value' => !empty($this->options['section_filter']['show_hierarchy']),
'#description' => $this
->t('If checked, the filter will return the selected item and all its children.'),
];
}
public function acceptExposedInput($input) {
if (empty($this->options['exposed'])) {
return TRUE;
}
if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id']) && isset($input[$this->options['expose']['operator_id']])) {
$this->operator = $input[$this->options['expose']['operator_id']];
}
if (!empty($this->options['expose']['identifier'])) {
$value = $input[$this->options['expose']['identifier']];
if (empty($this->options['expose']['required'])) {
if (($this->operator == 'empty' || $this->operator == 'not empty') && $value === '') {
$value = ' ';
}
}
if (isset($value)) {
$this->value = $value;
if (empty($this->alwaysMultiple) && empty($this->options['expose']['multiple']) && !is_array($value)) {
$this->value = [
$value,
];
}
}
else {
return FALSE;
}
}
return TRUE;
}
public function query() {
$helper = new ManyToOneHelper($this);
if (empty($this->value) || strtolower(current($this->value)) == 'all') {
if ($this->manager
->userInAll($this->scheme)) {
return;
}
else {
$values = $this->userSectionStorage
->getUserSections($this->scheme);
}
}
if (!empty($this->table)) {
$alias = $this->query
->ensureTable($this->table);
foreach ($this->scheme
->getAccessScheme()
->getViewsJoin($this
->getEntityType(), $this->realField, $alias) as $configuration) {
$type = 'standard';
if (isset($configuration['left_query'])) {
$type = 'subquery';
}
$join = Views::pluginManager('join')
->createInstance($type, $configuration);
$this->tableAlias = $helper
->addTable($join, $configuration['table_alias']);
$this->realField = $configuration['real_field'];
}
if (!isset($values)) {
$values = $this->value;
}
if (!empty($this->options['section_filter']['show_hierarchy'])) {
$values = $this
->getChildren($values);
}
if ($this->table == 'users') {
$new_values = [];
$scheme = $this->scheme
->getAccessScheme();
foreach ($values as $id) {
$section_storage = \Drupal::service('entity_type.manager')
->getStorage('section_association');
if ($association = $section_storage
->loadSection($this->scheme
->id(), $id)) {
$new_values[] = $association
->id();
}
}
$values = $new_values;
}
if (!empty($values)) {
$this->scheme
->getAccessScheme()
->addWhere($this, $values);
}
else {
$this->query
->addWhereExpression($this->options['group'], '1 = 0');
}
}
}
protected function getChildren(array $values) {
$tree = $this->scheme
->getAccessScheme()
->getTree();
$children = [];
foreach ($values as $id) {
foreach ($tree as $key => $data) {
if ($id == $key) {
$children += array_keys($data);
}
else {
foreach ($data as $iid => $item) {
if ($iid == $id || in_array($id, $item['parents'])) {
$children[] = $iid;
}
}
}
}
}
return $children;
}
}