Files
Phraseanet/lib/Alchemy/Phrasea/Model/Repositories/OrderRepository.php
Nicolas Le Goff b341495c88 Fix tests
2014-02-19 17:29:26 +01:00

100 lines
2.4 KiB
PHP

<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2014 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Model\Repositories;
use Alchemy\Phrasea\Model\Entities\User;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NoResultException;
/**
* OrderRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class OrderRepository extends EntityRepository
{
/**
* Returns the orders initiated by a given user.
*
* @param User $user
*
* @return array
*/
public function findByUser(User $user)
{
return $this->findBy(['user' => $user->getId()]);
}
/**
* Returns an array of all the orders, starting at $offsetStart, limited to $perPage
*
* @param array $baseIds
* @param integer $offsetStart
* @param integer $perPage
* @param string $sort
*
* @return array
*/
public function listOrders($baseIds, $offsetStart = 0, $perPage = 20, $sort = "created_on")
{
$qb = $this
->createQueryBuilder('o')
->innerJoin('o.elements', 'e');
if (!empty($baseIds)) {
$qb->where($qb->expr()->in('e.baseId', $baseIds));
}
if ($sort === 'user') {
$qb->orderBy('o.user', 'ASC');
} elseif ($sort === 'usage') {
$qb->orderBy('o.orderUsage', 'ASC');
} else {
$qb->orderBy('o.createdOn', 'ASC');
}
$qb
->setFirstResult((int) $offsetStart)
->setMaxResults(max(10, (int) $perPage));
return $qb->getQuery()->getResult();
}
/**
* Returns the total number of orders from an array of base_id
*
* @param array $baseIds
*
* @return integer
*/
public function countTotalOrders(array $baseIds = [])
{
$qb = $this
->createQueryBuilder('o');
$qb->select($qb->expr()->countDistinct('o.id'))
->innerJoin('o.elements', 'e');
if (!empty($baseIds)) {
$qb->where($qb->expr()->in('e.baseId', $baseIds));
}
$qb->groupBy('o.id');
try {
return $qb->getQuery()->getSingleScalarResult();
} catch (NoResultException $e) {
return 0;
}
}
}