Entity/DocumentTagRepository.php
author ymh <ymh.work@gmail.com>
Tue, 21 Feb 2012 15:15:22 +0100
changeset 74 901463f9b11c
parent 67 989d9e117586
child 133 550862f07e6a
permissions -rwxr-xr-x
add headers for public repository release

<?php
/*
 * This file is part of the WikiTagBundle package.
 *
 * (c) IRI <http://www.iri.centrepompidou.fr/>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace IRI\Bundle\WikiTagBundle\Entity;

use Doctrine\ORM\Query;

use Doctrine\ORM\QueryBuilder;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NoResultException;

/**
 * DocumentTagRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class DocumentTagRepository extends EntityRepository
{
   /**
    *  Find ordered tags by document id
    */
    public function findOrderedTagsForDoc($doc_id)
    {
        return $this->getEntityManager()
        ->createQuery("SELECT doctag FROM WikiTagBundle:DocumentTag doctag JOIN doctag.document doc WHERE doc.externalId=:doc_id ORDER BY doctag.tagOrder ASC")
        ->setParameter("doc_id", strval($doc_id))
        ->getResult();
    }
    
    private function findMaxOrderInUow($entityList, $doc_id, $max_order) {
        foreach($entityList as $entity) {
            if(is_a($entity, "\IRI\Bundle\WikiTagBundle\Model\DocumentTagInterface") &&
            !is_null($entity->getDocument()) &&
            ( $entity->getDocument() === $doc_id || $entity->getDocument()->getId() === $doc_id ) )
            {
                $max_order = max(array($max_order, $entity->getTagOrder()));
            }
        }
        return $max_order;
    }
    
        
    /**
     *  Gets the max order of all tags for one document
     */
    public function getMaxOrder($doc_id)
    {
        $max_order_res = $this->getEntityManager()
            ->createQuery("SELECT MAX(doctag.tagOrder) FROM WikiTagBundle:DocumentTag doctag JOIN doctag.document doc WHERE doc.externalId= :doc_id")
            ->setParameter("doc_id", strval($doc_id))
            ->getSingleScalarResult();
        
        $max_order = 0;
        if(!is_null($max_order_res)) {
            $max_order = intval($max_order_res);
        }
        
        $uow = $this->getEntityManager()->getUnitOfWork();
        
        $max_order = $this->findMaxOrderInUow($uow->getScheduledEntityInsertions(), $doc_id, $max_order);
        $max_order = $this->findMaxOrderInUow($uow->getScheduledEntityUpdates(), $doc_id, $max_order);

        return $max_order;
    }
    
    /**
     *
     * Enter description here ...
     * @param unknown_type $external_id
     * @param array $filter_array
     * @return QueryBuilder
     */
    private function createQueryBuilderByDocumentExternalId($external_id, array $filter_array=null) {
        $qb = $this->createQueryBuilder("dt")
        ->join('dt.document', 'd')
        ->where('d.externalId = :external_id');

        $params = array("external_id"=>strval($external_id));
        
        if(!is_null($filter_array)) {
            foreach ($filter_array as $key => $value) {
                $qb = $qb->andWhere("dt.$key = :$key");
                //$params[$key] = $value;
            }
            $params = array_merge($params, $filter_array);
        }
        
        $qb = $qb->setParameters($params);
        
        return $qb;
    }
    
    /**
     *
     * Enter description here ...
     * @param unknown_type $external_id
     * @param array $filter_array
     */
    public function findByDocumentExternalId($external_id, array $filter_array=null)
    {
        $qb = $this->createQueryBuilderByDocumentExternalId($external_id, $filter_array);
        return $qb->getQuery()->getResult();
    }
    
   	/**
     *
     * Enter description here ...
     * @param unknown_type $external_id
     * @param array $filter_array
     */
    public function findOneByDocumentExternalId($external_id, array $filter_array=null) {
        
        $qb = $this->createQueryBuilderByDocumentExternalId($external_id, $filter_array)->setMaxResults(1);
        
        try {
            return $qb->getQuery()->getSingleResult();
        } catch (NoResultException $e) {
            return null;
        }
    }

}