public function getListByCriteria(UserCriteria $criteria, $results = true)
{
$qb = $this->createQueryBuilder('u');
$qb->andWhere('u.status = :status')->setParameter('status', $criteria->status);
if (!is_null($criteria->is_public)) {
$qb->andWhere('u.is_public = :is_public')->setParameter('is_public', $criteria->is_public);
}
foreach ($criteria->perametersOperators as $key => $operator) {
$qb->andWhere('u.' . $key . ' = :' . $key)->setParameter($key, $criteria->{$key});
}
if ($criteria->is_author) {
$qb->andWhere($qb->expr()->isNotNull("u.author"));
}
if (!empty($criteria->groups)) {
$em = $this->getEntityManager();
$groupRepo = $em->getRepository('Newscoop\\Entity\\User\\Group');
$users = array();
foreach ($criteria->groups as $groupId) {
$group = $groupRepo->findOneById($groupId);
if ($group instanceof \Newscoop\Entity\User\Group) {
$users = array_unique(array_merge($users, array_keys($group->getUsers()->toArray())), SORT_REGULAR);
}
}
$op = $criteria->excludeGroups ? 'notIn' : 'in';
$qb->andWhere($qb->expr()->{$op}('u.id', ':userIds'));
$qb->setParameter('userIds', $users);
}
if (!empty($criteria->query)) {
$qb->andWhere($qb->expr()->orX("(u.username LIKE :query)", "(u.email LIKE :query)"));
$qb->setParameter('query', '%' . trim($criteria->query, '%') . '%');
}
if (!empty($criteria->query_name)) {
$qb->andWhere($qb->expr()->orX("(u.last_name LIKE :query)", "(u.first_name LIKE :query)"));
$qb->setParameter('query', trim($criteria->query_name, '%') . '%');
$qb->groupBy('u.last_name', 'u.first_name');
}
if (!empty($criteria->nameRange)) {
$this->addNameRangeWhere($qb, $criteria->nameRange);
}
if (!empty($criteria->lastLoginDays)) {
$qb->andWhere('u.lastLogin > :lastLogin');
$date = new \DateTime();
$qb->setParameter('lastLogin', $date->modify('- ' . $criteria->lastLoginDays . ' days'));
}
if (count($criteria->attributes) > 0) {
$qb->leftJoin('u.attributes', 'ua');
$qb->andWhere('ua.attribute = ?1')->andWhere('ua.value = ?2')->setParameter(1, $criteria->attributes[0])->setParameter(2, $criteria->attributes[1]);
}
$list = new ListResult();
$countQb = clone $qb;
$countQb->select('COUNT(u)')->resetDQLPart('groupBy');
$list->count = (int) $countQb->getQuery()->getSingleScalarResult();
if ($criteria->firstResult != 0) {
$qb->setFirstResult($criteria->firstResult);
}
if ($criteria->maxResults != 0) {
$qb->setMaxResults($criteria->maxResults);
}
$metadata = $this->getClassMetadata();
foreach ($criteria->orderBy as $key => $order) {
if (array_key_exists($key, $metadata->columnNames)) {
$key = 'u.' . $key;
}
$qb->orderBy($key, $order);
}
if (!$results) {
return array($qb, $list->count);
}
$list->items = $qb->getQuery()->getResult();
return $list;
}