<?php

/******************************************************************************
 *
 *	PROJECT: Flynax Classifieds Software
 *	VERSION: 4.X
 *	LICENSE: FL0M2SG681CQ - http://www.flynax.com/license-agreement.html
 *	PRODUCT: Pets Classifieds
 *	DOMAIN: pupsnshop.com
 *	FILE: LISTING_TYPE.INC.PHP
 *
 *	The software is a commercial product delivered under single, non-exclusive, 
 *	non-transferable license for one domain or IP address. Therefore distribution, 
 *	sale or transfer of the file in whole or in part without permission of Flynax 
 *	respective owners is considered to be illegal and breach of Flynax License End 
 *	User Agreement. 
 *
 *	You are not allowed to remove this information from the file without permission
 *	of Flynax respective owners.
 *
 *	Flynax Classifieds Software 2014 |  All copyrights reserved. 
 *
 *	http://www.flynax.com/
 *
 ******************************************************************************/

$reefless -> loadClass('Categories');
$reefless -> loadClass('Listings');

$category = (int)$_POST['category'];
$category = $rlCategories -> getCategory($category);
$type = $_POST['type'];
$category['ID'] = empty($category) ? 0 : $category['ID'];
$subCategory = isset($_POST['sub']) ? true : false;
$noListings = isset($_POST['no_listings']) ? true : false;
$pInfo['current'] = (int)$_POST['pg'];
$nextPage = 0;

$tmp_data = array();
$data = array();

// get listings
if ( $category['ID'] && !$noListings )
{
	
	$listings = $rlListings -> getListings($category['ID'], null, null, $pInfo['current'], $config['iFlynaxConnect_listings_per_page'], true);

	if ( !empty($listings) )
	{
		// build next page
		$pInfo['calc'] = $rlListings -> calc;
		$pInfo['total'] = count($listings);
		$pInfo['per_page'] = $config['iFlynaxConnect_listings_per_page'];
		$nextPage = $iPhone -> paging($pInfo);

		$tmp_data['listings'] = $iPhone -> buildListingsShortForm($listings);

		if ( $nextPage > 0 )
		{
			$tmp_data['loadMore'] = array(
				0 => $iPhone -> getTextLoadMore($nextPage)
			);
		}
		unset($listings);
	}
}

// get current category children
if ( $pInfo['current'] == 0 )
{
	$categories = $rlCategories -> getCategories($category['ID'], $type);
}

if ( !empty($categories) )
{
	$listing_type = $rlListingTypes -> types[$type];

	if ( $tmp_data['listings'] )
	{
		$countListingsInSubcaterories = 0;
		$shortCategoriesName = array();

		foreach( $categories as $key => $entry )
		{
			array_push($shortCategoriesName, $entry['name']);
			$countListingsInSubcaterories += (int)$entry['Count'];
		}

		$tmp_data['subCategories'][0] = array(
			'id' => (int)$category['ID'],
			'name' => implode(', ', $shortCategoriesName) .'...'
		);

		if ( $listing_type['Cat_listing_counter'] ) {
			$tmp_data['subCategories'][0]['counter'] = $countListingsInSubcaterories;
		}
	}
	else
	{
		foreach( $categories as $key => $entry)
		{
			$cIndex = count($tmp_data['categories']);
			$tmp_data['categories'][$cIndex] = array(
				'id' => (int)$entry['ID'],
				'name' => $entry['name']
			);

			if ( $listing_type['Cat_listing_counter'] ) {
				$tmp_data['categories'][$cIndex]['counter'] = (int)$entry['Count'];
			}
		}
	}
	unset($categories);
}

// adapt output data
if ( $tmp_data['subCategories'] )
{
	array_push($data, array(
			'key' => 'subCategories',
			'rows' => $tmp_data['subCategories']
		)
	);
}

if ( $tmp_data['listings'] )
{
	array_push($data, array(
			'key' => 'listings',
			'rows' => $tmp_data['listings']
		)
	);
}

if ( $tmp_data['categories'] )
{
	array_push($data, array(
			'key' => 'categories',
			'rows' => $tmp_data['categories']
		)
	);
}

if ( $tmp_data['loadMore'] )
{
	array_push($data, array(
			'key' => 'loadMore',
			'rows' => $tmp_data['loadMore']
		)
	);
}
unset($tmp_data);

$notice = false;
if ( empty($data) )
{
	$notice = array(
		'emptyMessage' => $category['ID'] == 0 ? $lang['iflynax+empty+categories'] : $lang['iflynax+empty+listings']
	);
}

$iPhone -> printAsXml($data, $notice);