<?php

namespace KupShop\ContentBundle\Sitemap;

use KupShop\I18nBundle\Translations\ArticlesSectionsTranslation;
use KupShop\KupShopBundle\Util\StringUtil;
use Query\Operator;
use Query\QueryBuilder;
use Query\Translation;
use Symfony\Component\Routing\Router;

class ArticleSectionSitemap extends BaseSitemap
{
    protected static $type = 'articles_sections';
    protected static $module = \Modules::ARTICLES;

    public function getData(): \Generator
    {
        $articleSectionList = sqlQueryBuilder()->select('ab.id', 'ab.name')->from('articles_branches', 'ab')
            ->andWhere(Translation::coalesceTranslatedFields(ArticlesSectionsTranslation::class))
            ->andWhere(Translation::joinTranslatedFields(ArticlesSectionsTranslation::class,
                function (QueryBuilder $qb, $columnName, $translatedField) {
                    $qb->andWhere(Operator::coalesce($translatedField, 'ab.figure').' = "Y" ');

                    return false;
                }, ['figure']))
            ->orderBy('name', 'ASC')->execute();

        foreach ($articleSectionList as $section) {
            yield [
                'loc' => path('kupshop_content_articles_articles_2', ['IDb' => $section['id'], 'slug' => StringUtil::slugify($section['name'])], Router::ABSOLUTE_URL),
                'priority' => '0.7',
                'changefreq' => 'monthly',
            ];
        }
    }
}
