<?php

/**
 * @package         Smile Pack
 * @version         1.2.3 Pro
 * 
 * @author          Tassos Marinos <info@tassos.gr>
 * @link            http://www.tassos.gr
 * @copyright       Copyright © 2024 Tassos Marinos All Rights Reserved
 * @license         GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
*/

defined('_JEXEC') or die('Restricted access');

use Joomla\CMS\Factory;

class SPMapContentMarkers extends SPMapMarkersItem
{
    protected $context = 'com_content.article';

    public function getMarkers()
    {
        $this->applyCategory();

        $this->applyTag();

        $this->applyAuthor();

        $this->applyStatus();

        return $this->getDBResults();
    }

    protected function setJoin()
    {
        $this->query->select('a.catid, a.language');
        $this->query->join('LEFT', $this->db->quoteName('#__content', 'a') . ' ON a.id = fv.item_id');
    }

    protected function getFieldID()
    {
        return $this->params->get('acf_content_map');
    }

    protected function applyCategory()
    {
        $categoriesEnabled = $this->params->get('filters_content_category_enabled', '0');
        if ($categoriesEnabled !== '1')
        {
            return;
        }

        if (!$categories = $this->params->get('filters_content_category_value', []))
        {
            return;
        }

        $inc_children = $this->params->get('filters_content_category_inc_children', '0');

        if (in_array($inc_children, ['1', '2']))
        {
            $categories = is_string($categories) ? array_map('trim', explode(',', $categories)) : $categories;
            
            $children_categories = $this->getCategoriesChildIds($categories);

            // Also include children categories
            if ($inc_children === '1')
            {
                $categories = array_unique(array_filter(array_merge($categories, $children_categories)));
            }
            // Use only children categories
            else
            {
                $categories = $children_categories;
            }

            $categories = implode(',', $categories);
        }

        if (!$categories)
        {
            return;
        }

        $this->query->where($this->db->quoteName('a.catid') . ' IN (' . (is_string($categories) ? $categories : implode(',', $categories)) . ')');
    }

    protected function applyTag()
    {
        $tagsEnabled = $this->params->get('filters_content_tag_enabled', '0');
        if ($tagsEnabled !== '1')
        {
            return;
        }

        if (!$tags = $this->params->get('filters_content_tag_value', []))
        {
            return;
        }

        $this->query
            ->join('INNER', $this->db->quoteName('#__contentitem_tag_map', 't') . ' ON t.content_item_id = a.id')
            ->where($this->db->quoteName('t.tag_id') . ' IN (' . (is_string($tags) ? $tags : implode(',', $tags)) . ')')
            ->group($this->db->quoteName('a.id'));
    }

    protected function applyAuthor()
    {
        $authorEnabled = $this->params->get('filters_content_author_enabled', '0');
        if ($authorEnabled !== '1')
        {
            return;
        }

        $authors = $this->params->get('filters_content_author_value', []);

        $this->query->where($this->db->quoteName('a.created_by') . ' IN (' . (is_string($authors) ? $authors : implode(',', $authors)) . ')');
    }

    protected function applyStatus()
    {
        $status = $this->params->get('filters_content_status_value', ['1']);

        $this->query->where($this->db->quoteName('a.state') . ' IN (' . (is_string($status) ? $status : implode(',', $status)) . ')');
    }
}