<?php

/**
 * For the full copyright and license information, please view the
 * docs/licenses/LICENSE.txt file that was distributed with this source code.
 */

namespace PrestaShopBundle\Entity\Repository;

/**
 * AttributeGroupRepository.
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class AttributeGroupRepository extends \Doctrine\ORM\EntityRepository
{
    /**
     * Finds attribute groups by language and shop.
     *
     * @param int $idLang Language ID
     * @param int $idShop Shop ID
     *
     * @return \PrestaShopBundle\Entity\AttributeGroup[]
     */
    public function findByLangAndShop(int $idLang, int $idShop): array
    {
        return $this->createQueryBuilder('ag')
            ->addSelect('agl')
            ->join('ag.shops', 'ags')
            ->join('ag.attributeGroupLangs', 'agl')
            ->andWhere('agl.lang = :idLang')
            ->andWhere('ags.id = :idShop')
            ->orderBy('ag.position', 'ASC')
            ->setParameters([
                'idShop' => $idShop,
                'idLang' => $idLang,
            ])->getQuery()->getResult();
    }

    /**
     * Finds attribute groups by language and shops.
     *
     * @param int $idLang Language ID
     * @param int[] $idShops Shop IDs
     *
     * @return \PrestaShopBundle\Entity\AttributeGroup[]
     */
    public function findByLangForShops(int $idLang, array $idShops): array
    {
        return $this->createQueryBuilder('ag')
            ->addSelect('agl')
            ->join('ag.shops', 'ags')
            ->join('ag.attributeGroupLangs', 'agl')
            ->andWhere('agl.lang = :idLang')
            ->andWhere('ags.id IN (:idShops)')
            ->orderBy('ag.position', 'ASC')
            ->setParameters([
                'idLang' => $idLang,
                'idShops' => $idShops,
            ])->getQuery()->getResult();
    }
}
