<?php

namespace KupShop\ElninoBundle\Old;

use Doctrine\DBAL\Connection;
use KupShop\CatalogBundle\ProductList\ProductCollection;

class ProductList extends \ProductListBase
{
    public function fetchAdditionalImages($additionalSize = null)
    {
        $this->resultModifiers[] = function (ProductCollection $products) use ($additionalSize) {
            $SQL = sqlQuery('SELECT ppr.id_product, p.id, p.descr, p.source, p.image_2,ppr.show_in_lead
					FROM '.getTableName('photos').' AS p, '.getTableName('photos-products')." AS ppr
					WHERE ppr.id_photo=p.id AND ppr.id_product IN (:product_ids) AND ppr.active='Y'
					GROUP BY p.id
					ORDER BY ppr.position ASC", ['product_ids' => $products->getProductIds()], ['product_ids' => Connection::PARAM_INT_ARRAY]);

            while (($image = sqlFetchAssoc($SQL)) !== false) {
                $products[$image['id_product']]->photos[] = array_merge(
                    getImage($image['id'], $image['image_2'], $image['source'], $additionalSize, $image['descr']),
                    ['show_in_lead' => $image['show_in_lead']]
                );
            }

            sqlFreeResult($SQL);
        };
    }

    protected function createQueryBuilder($useTotalCount)
    {
        $qb = parent::createQueryBuilder($useTotalCount);
        $qb->joinModelsOnProducts();
        $qb->andWhere('p.id_model IS NOT NULL');

        return $qb;
    }

    /**
     * @return array
     */
    public function getSelectFields($qb = null)
    {
        $fields = parent::getSelectFields();

        $fields['id_model'] = 'abm.id_abra';
        $fields['old_kupshop_id'] = 'abm.old_kupshop_id';

        return $fields;
    }
}
