<?php

namespace Club\EventBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * EventRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class EventRepository extends EntityRepository
{
    public function getByDate(\DateTime $date)
    {
        $c = new \DateTime($date->format('Y-m-d'));;

        $start = $c->format('Y-m-d');
        $c->modify('+1 month');
        $c->modify('-1 day');
        $stop = $c->format('Y-m-d');

        return $this->_em->createQueryBuilder()
            ->select('e')
            ->from('ClubEventBundle:Event','e')
            ->where('e.start_date >= :start_date')
            ->andWhere('e.stop_date <= :stop_date')
            ->setParameter('start_date', $start)
            ->setParameter('stop_date', $stop)
            ->getQuery()
            ->getResult();
    }

    public function getComing($public=true)
    {
        $qb = $this->createQueryBuilder('e')
            ->where('e.start_date >= :start_date')
            ->orderBy('e.start_date', 'ASC')
            ->setParameter('start_date', new \DateTime());

        if (!$public) {
            $qb
                ->andWhere('e.public = :public')
                ->setParameter('public', true);
        }

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

    public function getAllBetween(\DateTime $start, \DateTime $end=null, \Club\UserBundle\Entity\User $user=null)
    {
        $qb = $this->_em->createQueryBuilder()
            ->select('e')
            ->from('ClubEventBundle:Event','e')
            ->where('e.start_date >= :start_date')
            ->setParameter('start_date', $start->format('Y-m-d H:i:s'));

        if ($end != null) {
            $qb
                ->andWhere('e.start_date <= :stop_date')
                ->setParameter('stop_date', $end->format('Y-m-d H:i:s'));
        }

        if ($user != null) {
            $qb
                ->leftJoin('e.attends', 'a')
                ->andWhere('a.user = :user')
                ->setParameter('user', $user->getId());
        }

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