<?php

namespace AppBundle\Entity\Core\Repository;

use AppBundle\Services\Exporter\Document;
use Doctrine\ORM\EntityRepository;

/**
 * Description of NotificationRepository
 *
 * @author sebastienhupin
 */
class FileRepository extends EntityRepository {
    
    /**
     * Find notification are not viewed by the user
     * 
     * @param array $ids
     * 
     * @return Array
     */
    public function findByDocument(Document $document) {
        $queryBuilder = $this->createQueryBuilder('o');
        if($document->getAccess()){
            $queryBuilder
                ->innerJoin('o.accessPersons', 'p')
                ->andWhere('p.id = :person')
                ->setParameter('person', $document->getAccess()->getPerson());
        }else{
            $queryBuilder
                ->innerJoin('o.organization', 'or');
        }

        $queryBuilder
            ->andWhere('o.name = :name')
            ->andWhere('o.type = :type')
            ->setParameter('name', $document->getName())
            ->setParameter('type', $document->getType());

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