<?php
/**
 * MetaTag Repository
 * 
 * @author Elze Kool <info@visualmedia.nl>
 */
namespace VisualM\TagManagerBundle\Entity;

use VisualM\TagManagerBundle\Model\MetaTagBaseManager;
use Doctrine\ORM\QueryBuilder;
use VisualM\ModelManagementBundle\Definition\ModelManagerInterface;

/**
 * MetaTag Repository
 * 
 * @author Elze Kool <info@visualmedia.nl>
 */
class MetaTagRepository extends MetaTagBaseManager implements ModelManagerInterface
{
    /**
     * {@inheritdoc}
     */
    protected function processConditions(array $options, QueryBuilder &$qb)
    {
        foreach($options as $option => $value) {
            if ($option == 'id') {
                $qb->andWhere($qb->expr()->eq($this->getFullFieldName('id'), $qb->expr()->literal($value)));
            } else if ($option == 'content_id') {
                $qb->andWhere($qb->expr()->eq($this->getFullFieldName('contentId'), $qb->expr()->literal($value)));
            } else if (($option == 'meta_tag_preset_id') AND is_array($value)) {
                $value[] = 0; // saveguard against empty array
                $qb->andWhere($qb->expr()->in($this->getFullFieldName('metaTagPreset'), $value));
            } else if ($option == 'meta_tag_preset_id') {
                $qb->andWhere($qb->expr()->eq($this->getFullFieldName('metaTagPreset'), $qb->expr()->literal($value)));
            }
        }
    }

    /**
     * Get Distinct list of ContentIds for the MetaTags that match options
     *
     * @param array $options Options
     *
     * @return array ContentIds
     */
    public function getDistinctContentIds(array $options)
    {
        $qb = $this->createQueryBuilder($this->getEntityAlias())
            ->select($this->getFullFieldName('contentId'))
            ->distinct();

        $this->processConditions($options, $qb);

        $content_ids = [];
        foreach($qb->getQuery()->getResult() as $row) {
            $content_ids[] = $row['contentId'];
        }

        return $content_ids;
    }

    
}
