<?php

/**
 * @version        1.14.0
 * @package        Joomla
 * @subpackage     EDocman
 * @author         Tuan Pham Ngoc
 * @copyright      Copyright (C) 2011 - 2024 Ossolution Team
 * @license        GNU/GPL, see LICENSE.php
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Pagination\Pagination;

class EDocmanModelManageCategories extends OSModelList
{
	/**
	 * Instantiate the model.
	 *
	 * @param array $config configuration data for the model
	 *
	 */
	public function __construct($config = array())
	{
        $config['table']            = '#__edocman_categories';
		parent::__construct($config);
        $app                        = Factory::getApplication();
        $context                    = $this->option . '.' . $this->name . '.';
        $this->state->insert('filter_category_id', 'int', $app->getUserStateFromRequest($context . 'filter_category_id', 'filter_category_id', 0, 'int'));
        $this->state->insert('filter_level', 'int', $app->getUserStateFromRequest($context . 'filter_level', 'filter_level', 0, 'int'));
        $this->state->insert('filter_full_ordering', 'word');
	}

    /**
     * Get a list of items
     *
     * @return array
     */
    public function getData()
    {
        if (empty($this->data))
        {
            $db    = $this->getDbo();
            $query = $db->getQuery(true);
            $this->_buildQueryColumns($query)
                ->_buildQueryFrom($query)
                ->_buildQueryJoins($query)
                ->_buildQueryWhere($query)
                ->_buildQueryGroup($query)
                ->_buildQueryOrder($query);

            $db->setQuery($query);
            $rows     = $db->loadObjectList();
            $children = array();
            // first pass - collect children
            if (count($rows))
            {
                foreach ($rows as $v)
                {
                    $pt   = $v->parent_id;
                    $list = @$children[$pt] ? $children[$pt] : array();
                    array_push($list, $v);
                    $children[$pt] = $list;
                }
            }
            if($this->state->filter_category_id > 0)
            {
                $list  = HTMLHelper::_('menu.treerecurse', $this->state->filter_category_id, '', array(), $children, 9999);
            }
            elseif($this->state->filter_category_id == 0 && $this->state->filter_search == '')
            {
                $list  = HTMLHelper::_('menu.treerecurse', $this->state->filter_category_id, '', array(), $children, 9999);
            }
            else
            {
                $keys  = array_keys($children);
                $count = count($children);
                $temp  = array();
                $list  = array();
                $prgarr= array();
                foreach($keys as $key)
                {
                    $temp = EdocmanHelper::treerecurse($key, '', array() , $children , $prgarr , 9999);
                    $list = array_merge($list,$temp);
                }
            }
            $total = count($list);
            jimport('joomla.html.pagination');
            $this->pagination = new Pagination($total, $this->state->limitstart, $this->state->limit);
            // slice out elements based on limits
            $this->data = array_slice($list, $this->pagination->limitstart, $this->pagination->limit);
        }
        return $this->data;
    }

    /**
     * Builds SELECT columns list for the query
     *
     * @param JDatabaseQuery $query
     *
     * @return $this
     */
    protected function _buildQueryColumns(JDatabaseQuery $query)
    {
        $query->select('tbl.*, uc.name AS editor, vl.title AS access_level, COUNT(b.id) AS total_documents');

        return $this;
    }

    /**
     * Builds JOIN clauses for the query
     *
     * @param JDatabaseQuery $query
     *
     * @return $this
     */
    protected function _buildQueryJoins(JDatabaseQuery $query)
    {
        $query->leftJoin('#__users AS uc ON uc.id=tbl.checked_out')
            ->leftJoin('#__viewlevels AS vl ON tbl.access = vl.id')
            ->leftJoin('#__edocman_document_category AS b ON (tbl.id = b.category_id AND b.is_main_category = 1)');

        return $this;
    }

    /**
     * Builds WHERE clauses for the query
     *
     * @param JDatabaseQuery $query
     *
     * @return $this
     */
    protected function _buildQueryWhere(JDatabaseQuery $query)
    {
        parent::_buildQueryWhere($query);

        if ($this->state->filter_category_id)
        {
            $query->where('tbl.parent_id =' . (int) $this->state->filter_category_id);
        }

        if ($this->state->filter_level)
        {
            $query->where('tbl.level <=' . (int) $this->state->filter_level);
        }

        return $this;
    }

    /**
     * Builds GROUP BY clause for the query
     *
     * @param JDatabaseQuery $query
     *
     * @return $this
     */
    protected function _buildQueryGroup(JDatabaseQuery $query)
    {
        $query->group('tbl.id');
        return $this;
    }

	public function saveCategoryOrder($pks = null, $order = null)
	{
		$table      = $this->getTable('Category');
		$conditions = array();

		// Update ordering values
		foreach ($pks as $i => $pk)
		{
			$table->load((int) $pk);
			if ($table->ordering != $order[$i])
			{
				$table->ordering = $order[$i];
				if (!$table->store())
				{
					$this->setError($table->getError());

					return false;
				}

				// Remember to reorder within position and client_id
				//$condition = $this->getReorderConditions($table);
				$found     = false;

				foreach ($conditions as $cond)
				{
					if ($cond[1] == $condition)
					{
						$found = true;
						break;
					}
				}

				if (!$found)
				{
					$conditions[] = array($table->id, $condition);
				}
			}
		}

		// Execute reorder for each category.
		foreach ($conditions as $cond)
		{
			$table->load($cond[0]);
			$table->reorder($cond[1]);
		}

		// Clear the component's cache
		$this->cleanCache();

		return true;
	}
}
