<?php
/**
 * @package DJ-Catalog2
 * @copyright Copyright (C) DJ-Extensions.com, All rights reserved.
 * @license http://www.gnu.org/licenses GNU/GPL
 * @author url: http://dj-extensions.com
 * @author email contact@dj-extensions.com
 */

defined( '_JEXEC' ) or die( 'Direct Access to this location is not allowed.' );

if (!defined('DS')) {
	define('DS', DIRECTORY_SEPARATOR);
}

require_once JPATH_SITE .'/components/com_djcatalog2/helpers/route.php';
require_once JPATH_SITE .'/administrator/components/com_djcatalog2/lib/categories.php';

class xmap_com_djcatalog2
{
	static function getTree( $xmap, $parent, &$params )
	{
		if ($xmap->isNews)
			return false;
			
			$link_vars = [];
			$link_query = parse_url( $parent->link );
			parse_str( html_entity_decode($link_query['query']), $link_vars);
			
			$view = self::getParam($link_vars,'view');
			
			$include_products = self::getParam($params,'include_products',1);
			$include_products = ( $include_products == 1
				|| ( $include_products == 2 && $xmap->view == 'xml')
				|| ( $include_products == 3 && $xmap->view == 'html')
				||   $xmap->view == 'navigator');
			$params['include_products'] = $include_products;
			
			$priority = self::getParam($params,'cat_priority',$parent->priority);
			$changefreq = self::getParam($params,'cat_changefreq',$parent->changefreq);
			if ($priority  == '-1')
				$priority = $parent->priority;
				if ($changefreq  == '-1')
					$changefreq = $parent->changefreq;
					
					$params['cat_priority'] = $priority;
					$params['cat_changefreq'] = $changefreq;
					
					$priority = self::getParam($params,'link_priority',$parent->priority);
					$changefreq = self::getParam($params,'link_changefreq',$parent->changefreq);
					if ($priority  == '-1')
						$priority = $parent->priority;
						
						if ($changefreq  == '-1')
							$changefreq = $parent->changefreq;
							
							$params['link_priority'] = $priority;
							$params['link_changefreq'] = $changefreq;
							
							switch ($view) {
								case 'items' : {
									$catid = self::getParam($link_vars,'cid',0);
									$prodid = self::getParam($link_vars,'pid', null);
									$stid = self::getParam($link_vars,'stid', null);
									$cntid = self::getParam($link_vars,'cntid', null);
									$filter = self::getParam($link_vars,'filter', null);
									return self::getDJCatalog2Category($xmap,$parent,$params,$catid, ['pid' => $prodid, 'cntid' => $cntid, 'stid' => $stid, 'filter' => $filter]);
									break;
								}
								case 'item' : {
									$itemid = self::getParam($link_vars,'id',0);
									if ($itemid > 0) {
										return self::getDJCatalog2Item($xmap,$parent,$params,$itemid);
									} else {
										return false;
									}
									break;
								}
								case 'producer' : {
									$producerid = self::getParam($link_vars,'pid',0);
									if ($producerid > 0) {
										return self::getDJCatalog2Producer($xmap,$parent,$params,$producerid);
									} else {
										return false;
									}
									break;
								}
								
								case 'producers' : {
									return self::getDJCatalog2Producers($xmap,$parent,$params);
									break;
								}
							}
							
	}
	
	/* Returns URLs of all Categories and links in of one category using recursion */
	static function getDJCatalog2Category (&$xmap, &$parent, &$params, &$catid, $categoryParams = [] )
	{
		$database = JFactory::getDBO();
		
		$djc2categories = Djc2Categories::getInstance(array('state'=>'1'));
		$category = $djc2categories->get((int)$catid);
		if (!$category) {
			return false;
		}
		$categories = $category->getChildren();
		$xmap->changeLevel(1);
		foreach($categories as $row) {
			if( !$row->created ) {
				$row->created = $xmap->now;
			}
			
			$node = new stdclass;
			$node->name = $row->name;
			$node->link = DJCatalogHelperRoute::getCategoryRoute($row->id.':'.$row->alias);
			$node->id = $parent->id;
			$node->uid = 'com_djcatalog2c'.$row->id;
			
			if (!empty($categoryParams) && is_array($categoryParams)) {
				$node->link = DJCatalogHelperRoute::getCategoryRoute($row->id.':'.$row->alias, $categoryParams['pid'], $categoryParams['cntid'], $categoryParams['stid']);
				$node->uid .= '_'.md5(serialize($categoryParams));
			}
			
			$node->browserNav = $parent->browserNav;
			$node->modified = $row->created;
			$node->priority = $params['cat_priority'];
			$node->changefreq = $params['cat_changefreq'];
			$node->expandible = true;
			$node->secure = $parent->secure;
			
			if ( $xmap->printNode($node) !== FALSE) {
				self::getDJCatalog2Category($xmap,$parent,$params,$row->id);
			}
		}
		
		/* Returns URLs of all listings in the current category */
		if ($params['include_products']) {
			$query = " SELECT a.name, a.alias, a.id, a.cat_id, c.alias as cat_alias, UNIX_TIMESTAMP(a.created) as `created` \n".
				" FROM #__djc2_items AS a \n".
				" INNER JOIN #__djc2_categories as c on c.id = a.cat_id".
				" WHERE a.cat_id = ".(int)$catid.
				" AND a.published=1" .
				" ORDER BY a.ordering ASC, a.name ASC ";
			
			$database->setQuery($query);
			
			$rows = $database->loadObjectList();
			
			foreach($rows as $row) {
				$node = new stdclass;
				$node->name = $row->name;
				$node->link = DJCatalogHelperRoute::getItemRoute($row->id.':'.$row->alias, $row->cat_id.':'.$row->cat_alias);
				$node->id = $parent->id;
				$node->uid = 'com_djcatalog2i'.$row->id;
				if (!empty($categoryParams) && is_array($categoryParams)) {
					$node->uid .= '_'.md5(serialize($categoryParams));
				}
				$node->browserNav = $parent->browserNav;
				$node->modified = ($row->created);
				$node->priority = $params['link_priority'];
				$node->changefreq = $params['link_changefreq'];
				$node->expandible = false;
				$node->secure = $parent->secure;
				$xmap->printNode($node);
			}
		}
		$xmap->changeLevel(-1);
		return true;
	}
	
	static function getDJCatalog2Item (&$xmap, &$parent, &$params, &$itemid )
	{
		$database = JFactory::getDBO();
		$query = " SELECT a.name, a.alias, a.id, a.cat_id, c.alias as cat_alias, UNIX_TIMESTAMP(a.created) as `created` \n".
			" FROM #__djc2_items AS a \n".
			" INNER JOIN #__djc2_categories as c on c.id = a.cat_id".
			" WHERE a.id = ".(int)$itemid.
			" AND a.published=1"
				;
				
				$database->setQuery($query);
				
				$row = $database->loadObject();
				$node = new stdclass;
				$node->name = $row->name;
				$node->link = DJCatalogHelperRoute::getItemRoute($row->id.':'.$row->alias, $row->cat_id.':'.$row->cat_alias);
				$node->id = $parent->id;
				$node->uid = 'com_djcatalog2i'.$row->id;
				$node->browserNav = $parent->browserNav;
				$node->modified = ($row->created);
				$node->priority = $params['link_priority'];
				$node->changefreq = $params['link_changefreq'];
				$node->expandible = false;
				$node->secure = $parent->secure;
				$xmap->printNode($node);
				
				return true;
	}
	
	static function getDJCatalog2Producer (&$xmap, &$parent, &$params, &$producerid )
	{
		$database = JFactory::getDBO();
		$query = " SELECT a.name, a.alias, a.id, UNIX_TIMESTAMP(a.created) as `created` \n".
			" FROM #__djc2_producers AS a \n".
			" WHERE a.id = ".(int)$producerid.
			" AND a.published=1"
				;
				
				$database->setQuery($query);
				
				$row = $database->loadObject();
				$node = new stdclass;
				$node->name = $row->name;
				$node->link = DJCatalogHelperRoute::getProducerRoute($row->id.':'.$row->alias);
				$node->id = $parent->id;
				$node->uid = 'com_djcatalog2p'.$row->id;
				$node->browserNav = $parent->browserNav;
				$node->modified = ($row->created);
				$node->priority = $params['link_priority'];
				$node->changefreq = $params['link_changefreq'];
				$node->expandible = false;
				$node->secure = $parent->secure;
				$xmap->printNode($node);
				
				return true;
	}
	
	static function getDJCatalog2Producers (&$xmap, &$parent, &$params )
	{
		$database = JFactory::getDBO();
		$query = " SELECT a.name, a.alias, a.id, UNIX_TIMESTAMP(a.created) as `created` \n".
			" FROM #__djc2_producers AS a \n".
			" WHERE a.published=1"
				;
				
				$database->setQuery($query);
				
				$rows = $database->loadObjectList();
				if (empty($rows)) {
					return true;
				}
				
				$xmap->changeLevel(1);
				foreach($rows as $row) {
					$node = new stdclass;
					$node->name = $row->name;
					$node->link = DJCatalogHelperRoute::getProducerRoute($row->id.':'.$row->alias);
					$node->id = $parent->id;
					$node->uid = 'com_djcatalog2p'.$row->id;
					$node->browserNav = $parent->browserNav;
					$node->modified = ($row->created);
					$node->priority = $params['link_priority'];
					$node->changefreq = $params['link_changefreq'];
					$node->expandible = false;
					$node->secure = $parent->secure;
					$xmap->printNode($node);
				}
				
				return true;
	}
	
	static function prepareMenuItem($node, &$params)
	{
		$db = JFactory::getDbo();
		$link_query = parse_url($node->link);
		if (!isset($link_query['query'])) {
			return;
		}
		
		parse_str(html_entity_decode($link_query['query']), $link_vars);
		$view = Joomla\Utilities\ArrayHelper::getValue($link_vars, 'view', '');
		$layout = Joomla\Utilities\ArrayHelper::getValue($link_vars, 'layout', '');
		
		switch ($view) {
			case 'items':
				$id = Joomla\Utilities\ArrayHelper::getValue($link_vars, 'cid', 0);
				
				$prodid = Joomla\Utilities\ArrayHelper::getValue($link_vars,'pid', null);
				$stid = Joomla\Utilities\ArrayHelper::getValue($link_vars,'stid', null);
				$cntid = Joomla\Utilities\ArrayHelper::getValue($link_vars,'cntid', null);
				$filter = Joomla\Utilities\ArrayHelper::getValue($link_vars,'filter', null);
				
				$categoryParams = ['pid' => $prodid, 'cntid' => $cntid, 'stid' => $stid, 'filter' => $filter];
				
				$node->uid = 'com_djcatalog2c' . $id.'_' . md5(serialize($categoryParams));
				$expandible = true;
				foreach($categoryParams as $k=>$v) {
					if (!empty($v)) {
						$expandible = false;
						break;
					}
				}
				$node->expandible = $expandible;
				break;
			case 'item':
				$id = Joomla\Utilities\ArrayHelper::getValue($link_vars, 'id', 0);
				$node->uid = 'com_djcatalog2i' . $id;
				$node->expandible = false;
				break;
			case 'producer':
				$id = Joomla\Utilities\ArrayHelper::getValue($link_vars, 'pid', 0);
				$node->expandible = false;
				break;
		}
	}
	
	static function getParam($arr, $name, $def=null)
	{
		$var = Joomla\Utilities\ArrayHelper::getValue( $arr, $name, $def, '' );
		return $var;
	}
}
