<?php

namespace VGM\WOBundle\Repository;

use Doctrine\ORM\EntityRepository;

/**
 * NoticeLoopRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class NoticeLoopRepository extends EntityRepository
{
    public function getNewLoopNoticesCount($sup_deps_ids = null)
    {
        $qb = $this->createQueryBuilder('nl')
            ->leftJoin('nl.loop', 'loop')
            ->where('nl.dismissed is NULL')
            ->andWhere('nl.archived is NULL');

        if($sup_deps_ids != null){
            $qb->andWhere('loop.department IN (:sup_deps_ids)');
            $qb->setParameter('sup_deps_ids', $sup_deps_ids);
        }

        $query = $qb->getQuery();

        $results = $query->getResult();

        return count($results);
    }
    public function getActiveLoopNotices($sup_deps_ids = null)
    {
        $qb = $this->createQueryBuilder('nl')
            ->leftJoin('nl.loop', 'loop')
            ->where('nl.dismissed is NULL')
            /*->addOrderBy('loop.date')
            ->addOrderBy('loop.started_at')*/;

        if($sup_deps_ids != null){
            $qb->andWhere('loop.department IN (:sup_deps_ids)');
            $qb->setParameter('sup_deps_ids', $sup_deps_ids);
        }

        $query = $qb->getQuery();

        return $query->getResult();
    }
    public function getDismissedLoopNotices($sup_deps_ids = null)
    {
        $qb = $this->createQueryBuilder('nl')
            ->leftJoin('nl.loop', 'loop')
            ->where('nl.dismissed is not NULL')
            ->addOrderBy('nl.generated')
            ->addOrderBy('nl.dismissed');

        if($sup_deps_ids != null){
            $qb->andWhere('loop.department IN (:sup_deps_ids)');
            $qb->setParameter('sup_deps_ids', $sup_deps_ids);
        }

        $query = $qb->getQuery();

        return $query->getResult();
    }
}
