MoxtraMembershipController.php in Opigno Moxtra 3.x
File
src/Controller/MoxtraMembershipController.php
View source
<?php
namespace Drupal\opigno_moxtra\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Connection;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\user\Entity\User;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
class MoxtraMembershipController extends ControllerBase {
protected $connection;
protected $formBuilder;
public function __construct(Connection $connection, FormBuilderInterface $formBuilder) {
$this->connection = $connection;
$this->formBuilder = $formBuilder;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('database'), $container
->get('form_builder'));
}
public function addToGroupWorkspaceUserAutocomplete() {
$matches = [];
$string = \Drupal::request()->query
->get('q');
if ($string) {
$like_string = '%' . $this->connection
->escapeLike($string) . '%';
$curr_group = \Drupal::routeMatch()
->getParameter('group');
$workspace = \Drupal::routeMatch()
->getParameter('workspace');
$query = \Drupal::entityQuery('user')
->condition('uid', 0, '<>');
$cond_group = $query
->orConditionGroup()
->condition('mail', $like_string, 'LIKE')
->condition('name', $like_string, 'LIKE');
$query = $query
->condition($cond_group)
->sort('name');
$uids = $query
->execute();
$users = User::loadMultiple($uids);
$current_members = $workspace
->getMembersIds();
foreach ($users as $user) {
$id = $user
->id();
$name = $user
->getDisplayName();
if ($curr_group
->getMember($user) === FALSE || in_array($id, $current_members) || !_opigno_moxtra_is_user_enabled($user)) {
continue;
}
$matches[] = [
'value' => "{$name} ({$id})",
'label' => $name,
'id' => $id,
];
}
}
return new JsonResponse($matches);
}
public function addToAllWorkspaceUserAutocomplete() {
$matches = [];
$string = \Drupal::request()->query
->get('q');
if ($string) {
$like_string = '%' . $this->connection
->escapeLike($string) . '%';
$workspace = \Drupal::routeMatch()
->getParameter('workspace');
$query = \Drupal::entityQuery('user')
->condition('uid', 0, '<>');
$cond_group = $query
->orConditionGroup()
->condition('mail', $like_string, 'LIKE')
->condition('name', $like_string, 'LIKE');
$query = $query
->condition($cond_group)
->sort('name');
$uids = $query
->execute();
$users = User::loadMultiple($uids);
$current_members = $workspace
->getMembersIds();
foreach ($users as $user) {
$id = $user
->id();
$name = $user
->getDisplayName();
if (in_array($id, $current_members) || $id == 0 || !_opigno_moxtra_is_user_enabled($user)) {
continue;
}
$matches[] = [
'value' => "{$name} ({$id})",
'label' => $name,
'id' => $id,
];
}
}
return new JsonResponse($matches);
}
}