Skip to content

Commit

Permalink
User filter list extendable.
Browse files Browse the repository at this point in the history
  • Loading branch information
ngodfraind committed Feb 17, 2016
1 parent e5afc52 commit 27fe302
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 79 deletions.
17 changes: 1 addition & 16 deletions Controller/API/User/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use Claroline\CoreBundle\Manager\GroupManager;
use Claroline\CoreBundle\Manager\RoleManager;
use Claroline\CoreBundle\Manager\MailManager;
use Claroline\CoreBundle\Manager\FacetManager;
use Claroline\CoreBundle\Manager\AuthenticationManager;
use Claroline\CoreBundle\Manager\ProfilePropertyManager;
use JMS\DiExtraBundle\Annotation as DI;
Expand Down Expand Up @@ -55,7 +54,6 @@ class UserController extends FOSRestController
* "groupManager" = @DI\Inject("claroline.manager.group_manager"),
* "om" = @DI\Inject("claroline.persistence.object_manager"),
* "profilePropertyManager" = @DI\Inject("claroline.manager.profile_property_manager"),
* "facetManager" = @DI\Inject("claroline.manager.facet_manager"),
* "mailManager" = @DI\Inject("claroline.manager.mail_manager"),
* "apiManager" = @DI\Inject("claroline.manager.api_manager")
* })
Expand All @@ -70,7 +68,6 @@ public function __construct(
GroupManager $groupManager,
RoleManager $roleManager,
ObjectManager $om,
FacetManager $facetManager,
ProfilePropertyManager $profilePropertyManager,
MailManager $mailManager,
ApiManager $apiManager
Expand All @@ -82,7 +79,6 @@ public function __construct(
$this->localeManager = $localeManager;
$this->request = $request;
$this->userManager = $userManager;
$this->facetManager = $facetManager;
$this->groupManager = $groupManager;
$this->roleManager = $roleManager;
$this->om = $om;
Expand Down Expand Up @@ -142,18 +138,7 @@ public function getSearchUsersAction($page, $limit)
*/
public function getUserSearchableFieldsAction()
{
$fields = $this->facetManager->getFieldFacets();

$baseFields = User::getSearchableFields();

foreach ($fields as $field) {
$baseFields[] = $field->getName();
}

$baseFields[] = 'group_name';
$baseFields[] = 'organization_name';

return $baseFields;
return $this->userManager->getUserSearchableFields();
}

/**
Expand Down
48 changes: 48 additions & 0 deletions Event/UserAddFilterEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of the Claroline Connect package.
*
* (c) Claroline Consortium <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Claroline\CoreBundle\Event;

use Claroline\CoreBundle\Entity\User;
use Symfony\Component\EventDispatcher\Event;

class UserAddFilterEvent extends Event
{
private $filters;

public function __construct($filters)
{
$this->filters = $filters;
}

public function addFilter($filter)
{
$this->filters[] = $filter;
}

public function removeFilter($filter)
{
if (($key = array_search($filter, $this->filters)) !== false) {
unset($filters[$key]);
}

}

public function getFilters()
{
return $this->filters;
}

public function setFilters($filters)
{
$this->filters = $filters;
}
}
30 changes: 30 additions & 0 deletions Event/UserEditSearchEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Claroline Connect package.
*
* (c) Claroline Consortium <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Claroline\CoreBundle\Event;

use Symfony\Component\EventDispatcher\Event;
use Doctrine\ORM\QueryBuilder;

class UserEditSearchEvent extends Event
{
private $qb;

public function __construct(QueryBuilder $qb)
{
$this->qb = $qb;
}

public function getQueryBuilder()
{
return $this->qb;
}
}
87 changes: 86 additions & 1 deletion Manager/UserManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Claroline\CoreBundle\Library\Workspace\Configuration;
use Claroline\CoreBundle\Manager\MailManager;
use Claroline\CoreBundle\Manager\TransfertManager;
use Claroline\CoreBundle\Manager\FacetManager;
use Claroline\CoreBundle\Pager\PagerFactory;
use Claroline\CoreBundle\Persistence\ObjectManager;
use Doctrine\Common\Collections\ArrayCollection;
Expand Down Expand Up @@ -1508,8 +1509,92 @@ public function hideEmailValidation(User $user)
$this->objectManager->flush();
}

/**
* Big user search method ! hell yeah !
*/
public function searchPartialList($searches, $page, $limit, $count = false)
{
return $this->userRepo->searchPartialList($searches, $page, $limit, $count);
$baseFieldsName = User::getUserSearchableFields();
$facetFields = $this->objectManager->getRepository('ClarolineCoreBundle:Facet\FieldFacet')->findAll();
$facetFieldsName = array();

foreach ($facetFields as $facetField) {
$facetFieldsName[] = $facetField->getName();
}

$qb = $this->objectManager->createQueryBuilder();
$count ? $qb->select('count(u)'): $qb->select('u');
$qb->from('Claroline\CoreBundle\Entity\User', 'u')
->where('u.isEnabled = true');

foreach ($searches as $key => $search) {
foreach ($search as $id => $el) {
if (in_array($key, $baseFieldsName)) {
$qb->andWhere("UPPER (u.{$key}) LIKE :{$key}{$id}");
$qb->setParameter($key . $id, '%' . strtoupper($el) . '%');
} elseif (in_array($key, $facetFieldsName)) {
$qb->join('u.fieldsFacetValue', "ffv{$id}");
$qb->join("ffv{$id}.fieldFacet", "f{$id}");
$qb->andWhere("UPPER (ffv{$id}.stringValue) LIKE :{$key}{$id}");
$qb->orWhere("ffv{$id}.floatValue = :{$key}{$id}");
$qb->andWhere("f{$id}.name LIKE :facet{$id}");
$qb->setParameter($key . $id, '%' . strtoupper($el) . '%');
$qb->setParameter("facet{$id}", $key);
} elseif ($key === 'group_name') {
$qb->join('u.groups', "g{$id}");
$qb->andWhere("UPPER (g{$id}.name) LIKE :{$key}{$id}");
$qb->setParameter($key . $id, '%' . strtoupper($el) . '%');
} if ($key === 'group_id') {
$qb->join('u.groups', "g{$id}");
$qb->andWhere("g{$id}.id = :{$key}{$id}");
$qb->setParameter($key . $id, $el);
} if ($key === 'organization_name') {
$qb->join('u.organizations', "o{$id}");
$qb->andWhere("UPPER (o{$id}.name) LIKE :{$key}{$id}");
$qb->setParameter($key . $id, '%' . strtoupper($el) . '%');
} if ($key === 'organization_id') {
$qb->join('u.organizations', "o{$id}");
$qb->andWhere('o{$id}.id = :id');
$qb->setParameter($key . $id, $el);
}
}
}

$event = $this->strictEventDispatcher->dispatch(
'user_edit_search_event',
'UserEditSearch',
array($qb)
);

$query = $qb->getQuery();

if ($page && $limit && !$count) {
$query->setMaxResults($limit);
$query->setFirstResult($page * $limit);
}

return $count ? $query->getSingleScalarResult(): $query->getResult();
}

public function getUserSearchableFields()
{
$fields = $this->container->get('claroline.manager.facet_manager')->getFieldFacets();

$baseFields = User::getSearchableFields();

foreach ($fields as $field) {
$baseFields[] = $field->getName();
}

$baseFields[] = 'group_name';
$baseFields[] = 'organization_name';

$event = $this->strictEventDispatcher->dispatch(
'user_add_filter_event',
'UserAddFilter',
array($baseFields)
);

return $event->getFilters();
}
}
5 changes: 5 additions & 0 deletions Persistence/ObjectManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ public function forceFlush()
}
}

public function createQueryBuilder()
{
return $this->wrapped->createQueryBuilder();
}

/**
* Starts a transaction.
*
Expand Down
61 changes: 0 additions & 61 deletions Repository/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -1585,65 +1585,4 @@ public function findForApi($data)

return $query->getOneOrNullResult();
}

/**
* Big method for searching users with filters. It can handle most use cases.
*/
public function searchPartialList(array $searches = array(), $page = null, $limit = null, $count = false)
{
$baseFieldsName = User::getUserSearchableFields();
$facetFields = $this->_em->getRepository('ClarolineCoreBundle:Facet\FieldFacet')->findAll();
$facetFieldsName = array();

foreach ($facetFields as $facetField) {
$facetFieldsName[] = $facetField->getName();
}

$qb = $this->_em->createQueryBuilder();
$count ? $qb->select('count(u)'): $qb->select('u');
$qb->from('Claroline\CoreBundle\Entity\User', 'u')
->where('u.isEnabled = true');

foreach ($searches as $key => $search) {
foreach ($search as $id => $el) {
if (in_array($key, $baseFieldsName)) {
$qb->andWhere("UPPER (u.{$key}) LIKE :{$key}{$id}");
$qb->setParameter($key . $id, '%' . strtoupper($el) . '%');
} elseif (in_array($key, $facetFieldsName)) {
$qb->join('u.fieldsFacetValue', "ffv{$id}");
$qb->join("ffv{$id}.fieldFacet", "f{$id}");
$qb->andWhere("UPPER (ffv{$id}.stringValue) LIKE :{$key}{$id}");
$qb->orWhere("ffv{$id}.floatValue = :{$key}{$id}");
$qb->andWhere("f{$id}.name LIKE :facet{$id}");
$qb->setParameter($key . $id, '%' . strtoupper($el) . '%');
$qb->setParameter("facet{$id}", $key);
} elseif ($key === 'group_name') {
$qb->join('u.groups', "g{$id}");
$qb->andWhere("UPPER (g{$id}.name) LIKE :{$key}{$id}");
$qb->setParameter($key . $id, '%' . strtoupper($el) . '%');
} if ($key === 'group_id') {
$qb->join('u.groups', "g{$id}");
$qb->andWhere("g{$id}.id = :{$key}{$id}");
$qb->setParameter($key . $id, $el);
} if ($key === 'organization_name') {
$qb->join('u.organizations', "o{$id}");
$qb->andWhere("UPPER (o{$id}.name) LIKE :{$key}{$id}");
$qb->setParameter($key . $id, '%' . strtoupper($el) . '%');
} if ($key === 'organization_id') {
$qb->join('u.organizations', "o{$id}");
$qb->andWhere('o{$id}.id = :id');
$qb->setParameter($key . $id, $el);
}
}
}

$query = $qb->getQuery();

if ($page && $limit && !$count) {
$query->setMaxResults($limit);
$query->setFirstResult($page * $limit);
}

return $count ? $query->getSingleScalarResult(): $query->getResult();
}
}
6 changes: 6 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ services:
arguments:
- Claroline\CoreBundle\Entity\Log\Log

claroline.repository.user:
class: Claroline\CoreBundle\Repository\UserRepository
factory: ["@doctrine.orm.entity_manager", getRepository]
arguments:
- Claroline\CoreBundle\Entity\User

# pseudo-services used for dynamic parameter injection
# (see Claroline\CoreBundle\DependencyInjection\Compiler\DynamicConfigPass
claroline.session.storage_options:
Expand Down
1 change: 0 additions & 1 deletion Resources/modules/users/Partial/user_content.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
</div>
</div>
<div>
{{ uc.connectedUser }}
<dtable options="uc.dataTableOptions" rows="uc.users" class="material" on-page="uc.paging(offset, size)" selected="uc.selected"></dtable>
</div>
</div>

0 comments on commit 27fe302

Please sign in to comment.