Your IP : 216.73.217.79


Current Path : /var/www/v3.cesa.co.za/src/SessionsBundle/Repository/
Upload File :
Current File : /var/www/v3.cesa.co.za/src/SessionsBundle/Repository/SessionsRepository.php

<?php

namespace App\SessionsBundle\Repository;

use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use App\SessionsBundle\Entity\Sessions;
use App\SessionsBundle\Entity\Sites;
use App\UserBundle\Entity\UserList;

/**
 * SessionsRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class SessionsRepository extends ServiceEntityRepository {

    public function __construct(ManagerRegistry $registry) {
        parent::__construct($registry, Sessions::class);
    }

    /**
     * Count the sessions for a given site
     *
     * @param Sites $site
     * @return integer
     */
    public function countBySite(Sites $site) {
        return $this->createQueryBuilder('se')
                        ->select('COUNT(se.id)')
                        ->where('se.sites = :site')->setParameter('site', $site)
                        ->setMaxResults(1)
                        ->getQuery()
                        ->getSingleScalarResult()
        ;
    }

    /**
     * Get the sessions for a given site
     *
     * @param Sites $site
     * @param integer $limit
     * @return array
     */
    public function getArray(Sites $site, $limit = 0) {
        $qb = $this->createQueryBuilder('se')
                        ->select('se.id')
                        ->where('se.sites = :site')->setParameter('site', $site);

        if ($limit > 0) {
            $qb->setMaxResults($limit);
        }

        return $qb->getQuery()
                        ->getArrayResult()
        ;
    }

    /**
     * Get the oldest session object
     *
     * @return array
     */
    public function getOldestSession() {
        $qb = $this->createQueryBuilder('se')
                ->orderBy('se.created_at', 'Asc')
                ->setMaxResults(1);

        return $qb->getQuery()->getOneOrNullResult();
    }

    /**
     * Get the sessions by month of a given date
     *
     * @param Sites $site
     * @param integer $limit
     * @return array
     */
    public function getSessionsInMonth(\DateTime $date) {

        $startDate = new \DateTimeImmutable($date->format("Y-m") . "-01");
        $startDate->setTime(0, 0, 0);
        $endDate = clone $startDate;
        $endDate = $startDate->modify('+1 month')->setTime(0, 0, 0);
        $startDate = $startDate->modify('-1 second');

        $qb = $this->createQueryBuilder('se')
                ->where('se.created_at < :end')
                ->setParameter('end', $endDate)
                ->setMaxResults(200000)
                ->orderBy('se.created_at', 'ASC')
        ;

//        echo $qb->getQuery()->getSQL() . "\r\n";
//        echo $startDate->format('‌​Y-m-d H:i:s') . "\r\n";
//        echo $endDate->format('‌​Y-m-d H:i:s') . "\r\n";

        return $qb->getQuery()->getArrayResult();
    }

    /**
     * Get the sessions by month of a given date
     *
     * @param Sites $site
     * @param integer $limit
     * @return array
     */
    public function getSessionsInWeek(\DateTime $date) {

        $startDate = new \DateTimeImmutable($date->format("Y-m-d"));
        $startDate->setTime(0, 0, 0);
        $endDate = clone $startDate;
        $endDate = $startDate->modify('+1 weeks')->setTime(0, 0, 0);
        $startDate = $startDate->modify('-1 second');

        $qb = $this->createQueryBuilder('se')
                ->where('se.created_at < :end')
                ->setParameter('end', $endDate)
                ->setMaxResults(200000)
                ->orderBy('se.created_at', 'ASC')
        ;

//        echo $qb->getQuery()->getSQL() . "\r\n";
//        echo $startDate->format('‌​Y-m-d H:i:s') . "\r\n";
//        echo $endDate->format('‌​Y-m-d H:i:s') . "\r\n";

        return $qb->getQuery()->getArrayResult();
    }

    /**
     * Get the sessions by month of a given date
     *
     * @param Sites $site
     * @param integer $limit
     * @return array
     */
    public function getSessionsIn3Days(\DateTime $date) {

        $startDate = new \DateTimeImmutable($date->format("Y-m-d"));
        $startDate->setTime(0, 0, 0);
        $endDate = clone $startDate;
        $endDate = $startDate->modify('+3 days')->setTime(0, 0, 0);
        $startDate = $startDate->modify('-1 second');

        $qb = $this->createQueryBuilder('se')
                ->where('se.created_at < :end')
                ->setParameter('end', $endDate)
                ->setMaxResults(200000)
                ->orderBy('se.created_at', 'ASC')
        ;

//        echo $qb->getQuery()->getSQL() . "\r\n";
//        echo $startDate->format('‌​Y-m-d H:i:s') . "\r\n";
//        echo $endDate->format('‌​Y-m-d H:i:s') . "\r\n";

        return $qb->getQuery()->getArrayResult();
    }

    /**
     * Get the sessions by month of a given date
     *
     * @param array $params
     * @return Sessions|null
     */
    public function findSession(array $params) {

        $startDate = new \DateTimeImmutable();
        $startDate = $startDate->modify('-32 days');

        $qb = $this->createQueryBuilder('se')
                ->where('se.updated_at >= :start')
                ->setParameter('start', $startDate)
        ;

        foreach ($params as $column => $val) {
            $qb->andWhere('se.' . $column . ' = :' . $column)
                    ->setParameter($column, $val);
        }

        $qb->setMaxResults(1);

//        echo $qb->getQuery()->getSQL() . "\n <br />";
//        echo $startDate->format('‌​Y-m-d H:i:s') . "\n <br />";
//        var_dump($params);
//        echo '<br />';
//        exit;

        return $qb->getQuery()->getOneOrNullResult();
    }

    /**
     * Get the oldest session object
     *
     * @return array
     */
    public function getOtherSessions(UserList $userList, Sessions $session, \DateTime $sessionsAfterDate) {

        $qb = $this->createQueryBuilder('se')
                        ->where('se.created_by = :userListId')->setParameter('userListId', $userList->getId())
                        ->andWhere('se.id != :sessionId')->setParameter('sessionId', $session->getId())
                        ->andWhere('se.updated_at > :afterDate')->setParameter('afterDate', $sessionsAfterDate);

        return $qb->getQuery()->execute();
    }
}