mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-09 02:54:26 +00:00
100 lines
2.4 KiB
PHP
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;
|
|
}
|
|
}
|
|
}
|