<?php

namespace KupShop\I18nBundle\Admin\lists;

use Doctrine\DBAL\Query\QueryBuilder;
use KupShop\ContentBundle\Util\PageLocator;
use KupShop\I18nBundle\Translations\SysPagesFragmentsTranslation;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use Query\Operator;

class TranslateSysPagesFragmentsList extends TranslateList
{
    protected $template = 'list/translateSysPagesFragments.tpl';

    protected $listType = 'translateSysPagesFragments';

    /** @var PageLocator */
    public $pageLocator;

    public function __construct()
    {
        parent::__construct();
        $this->translation = ServiceContainer::getService(SysPagesFragmentsTranslation::class);
        $this->pageLocator = ServiceContainer::getService(PageLocator::class);
    }

    protected function createQueryBuilder(): QueryBuilder
    {
        $tableAlias = $this->translation->getTableAlias();
        $qb = sqlQueryBuilder()->select($this->translation->getTableAlias().'.*')
            ->from($this->translation->getTableName(), $tableAlias)
            ->groupBy($this->translation->getTableAlias().'.'.$this->translation->getKeyColumn());

        return $qb;
    }

    protected function applyFilters(QueryBuilder $qb)
    {
        $qb = parent::applyFilters($qb);

        $qb->andWhere(Operator::inStringArray($this->pageLocator->getTypes(), 'pg.type'));

        return $qb;
    }
}

$main_class = TranslateSysPagesFragmentsList::class;
