<?php

/**
 * This Software is the property of Data Development and is protected
 * by copyright law - it is NOT Freeware.
 * Any unauthorized use of this software without a valid license
 * is a violation of the license agreement and will be prosecuted by
 * civil and criminal law.
 * http://www.shopmodule.com
 *
 * @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
 * @author        D3 Data Development - Daniel Seifert <support@shopmodule.com>
 * @link          http://www.oxidmodule.com
 */

namespace D3\DataWizard\Application\Model\Exports;

use D3\DataWizard\Application\Model\ExportBase;
use OxidEsales\Eshop\Application\Model\Article;
use OxidEsales\Eshop\Application\Model\Category;
use OxidEsales\Eshop\Application\Model\Object2Category;
use OxidEsales\Eshop\Core\Registry;

class InactiveCategories extends ExportBase
{
    /**
     * Kategorien -deaktiviert, mit aktiven Artikel
     */

    /**
     * @return string
     */
    public function getTitle() : string
    {
        return Registry::getLang()->translateString('D3_DATAWIZARD_EXPORTS_INACTIVECATEGORIES');
    }

    /**
     * @return array
     */
    public function getQuery() : array
    {
        $categoryTableName          = oxNew(Category::class)->getCoreTableName();
        $object2categoryTableName   = oxNew(Object2Category::class)->getCoreTableName();
        $articleTableName           = oxNew(Article::class)->getCoreTableName();

        $treeTitle  = Registry::getLang()->translateString('D3_DATAWIZARD_EXPORTS_INACTIVECATEGORIES_TREE');
        $titleTitle = Registry::getLang()->translateString('D3_DATAWIZARD_EXPORTS_INACTIVECATEGORIES_TITLE');
        $countTitle = Registry::getLang()->translateString('D3_DATAWIZARD_EXPORTS_INACTIVECATEGORIES_COUNT');

        return [
            "SELECT
                oc.OXID,
                oc.OXSHOPID,
                oc.oxtitle as :titleTitle,
                (
                    SELECT GROUP_CONCAT(oxtitle ORDER BY oxleft ASC SEPARATOR ' > ') 
                    from ".$categoryTableName." 
                    WHERE OXLEFT < oc.oxleft AND OXRIGHT > oc.oxright AND OXROOTID = oc.OXROOTID AND OXSHOPID = oc.OXSHOPID
                ) as :treeTitle,
                COUNT(oa.oxid) as :countTitle
                FROM ".$categoryTableName." oc
                LEFT JOIN ".$object2categoryTableName." o2c ON oc.OXID = o2c.OXCATNID
                LEFT JOIN ".$articleTableName." oa ON o2c.OXOBJECTID = oa.OXID
                WHERE oc.OXACTIVE = :categoryActive AND oa.OXACTIVE = :articleActive
                GROUP BY oc.oxid
                ORDER BY oc.oxleft ASC",
            [
                'categoryActive'    => 0,
                'articleActive'     => 1,
                'titleTitle'        => $titleTitle,
                'treeTitle'         => $treeTitle,
                'countTitle'        => $countTitle
            ]
        ];
    }
}