<?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');

class SPMapUserMarkers extends SPMapMarkersItem
{
    protected $context = 'com_users.user';
    
    public function getMarkers()
    {
        $this->applyGroup();

        $this->applyStatus();

        return $this->getDBResults();
    }

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

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

    private function applyGroup()
    {
        $groupsEnabled = $this->params->get('filters_user_group_enabled', '0');
        if ($groupsEnabled !== '1')
        {
            return;
        }

        if (!$groups = $this->params->get('filters_user_group_value', []))
        {
            return;
        }
        
        $inc_children = $this->params->get('filters_user_group_inc_children', '0');

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

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

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

        if (!$groups)
        {
            return;
        }

        $this->query->join('LEFT', $this->db->quoteName('#__user_usergroup_map', 'g') . ' ON g.user_id = a.id');
        $this->query->where($this->db->quoteName('g.group_id') . ' IN (' . (is_string($groups) ? $groups : implode(',', $groups)) . ')');
    }

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

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

    /**
     * Return all group child ids.
     * 
     * @param   array  $groups
     * 
     * @return  array
     */
    protected function getGroupsChildIds($groups = [])
    {
        $query = $this->db->getQuery(true)
            ->select('ug.id')
            ->from($this->db->quoteName('#__usergroups', 'ug'));

        $children = [];

        while (!empty($groups))
        {
            $query
                ->clear('where')
                ->where($this->db->quoteName('ug.parent_id') . ' IN (' . implode(',', $groups) . ')');
            
            $this->db->setQuery($query);

            $groups = $this->db->loadColumn();

            $children = array_merge($children, $groups);
        }

        return $children;
    }
}