<?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_DETAILS.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/
 *
 ******************************************************************************/

$listingID = (int)$_POST['id'];

$reefless -> loadClass('ListingTypes');
$reefless -> loadClass('Listings');
$reefless -> loadClass('Account');

// get listing info
$sql  = "SELECT `T1`.*, `T2`.`Path`, `T2`.`Type` AS `Listing_type`, `T2`.`Key` AS `Cat_key`, `T2`.`Type` AS `Cat_type`, ";
$sql .= "`T3`.`Image`, `T3`.`Image_unlim`, `T3`.`Video`, `T3`.`Video_unlim`, CONCAT('categories+name+', `T2`.`Key`) AS `Category_pName` ";
$sql .= "FROM `" . RL_DBPREFIX . "listings` AS `T1` ";
$sql .= "LEFT JOIN `" . RL_DBPREFIX . "categories` AS `T2` ON `T1`.`Category_ID` = `T2`.`ID` ";
$sql .= "LEFT JOIN `" . RL_DBPREFIX . "listing_plans` AS `T3` ON `T1`.`Plan_ID` = `T3`.`ID` ";
$sql .= "LEFT JOIN `" . RL_DBPREFIX . "accounts` AS `T5` ON `T1`.`Account_ID` = `T5`.`ID` ";
$sql .= "WHERE `T1`.`ID` = '{$listingID}' AND `T5`.`Status` = 'active' LIMIT 1";
$listingData = $rlDb -> getRow($sql);

// define listing type
$listingType = $rlListingTypes -> types[$listingData['Listing_type']];
$data = array();

if ( !empty($listingData) )
{
	// count visit
	if ( $config['count_listing_visits'] && $_POST['view'] == 'details' )
	{
		$rlListings -> countVisit( $listingData['ID'] );
	}

	$category_id = $listingData['Category_ID'];
	$listing = $rlListings -> getListingDetails($category_id, $listingData, $listingType);
	$skipFieldTypes = array('image', 'file');

	if ( in_array($_POST['view'], array('sellerInfo', 'location')) )
	{
		// get seller information
		$account_id = (int)$listingData['Account_ID'];
		$sellerInfo = $rlAccount -> getProfile($account_id);
	}

	switch($_POST['view'])
	{
		case 'details': 

			// remove fields if empty sections
			$iPhone -> adaptListingDetailSections($listing);

			foreach( $listing as $sKey => $entry )
			{
				$sIndex = count($data['sections']);
				$data['sections'][$sIndex]['name'] = $entry['name'];
				$data['sections'][$sIndex]['fields'] = array();

				foreach( $entry['Fields'] as $fKey => $field )
				{
					if ( !in_array($field['Type'], $skipFieldTypes) )
					{
						$fIndex = count($data['sections'][$sIndex]['fields']);
						$data['sections'][$sIndex]['fields'][$fIndex] = array(
							'id' => (int)$field['ID'],
							'name' => $field['name'],
							'value' => $iPhone -> pValid($field['value'])
						);
					}
				}
			}
			unset($listing);

			// get listing photos
			if ( false !== $photos = $iPhone -> getListingPhotos($listingID, $listing['Image']) )
			{
				$data['listingPhotos'] = $photos;
			}
			break;

		case 'sellerInfo':

		$data[0]['mapLocation'] = $rlAccount -> mapLocation;

			if ( !empty($sellerInfo) )
			{
				array_push($data, array(
						'key' => 'name',
						'title' => $lang['name'],
						'value' => $sellerInfo['Full_name']
					)
				);

				if ( !empty($sellerInfo['Photo']) )
				{
					array_push($data, array(
							'isPhoto' => true,
							'value' => RL_FILES_URL . $sellerInfo['Photo']
						)
					);
				}

				if ( $sellerInfo['Display_email'] && !empty($sellerInfo['Mail']) )
				{
					array_push($data, array(
							'key' => 'mail',
							'isEmail' => true,
							'title' => $lang['mail'],
							'value' => $sellerInfo['Mail']
						)
					);
				}

				foreach( $sellerInfo['Fields'] as $aKey => $aField )
				{
					if ( !in_array($aField['Type'], $skipFieldTypes) && !empty($aField['value']) && $aField['Key'] != 'address' )
					{
						$field_info = array(
							'key' => $aField['Key'],
							'title' => $aField['name'],
							'value' => $iPhone -> pValid($aField['value'])
						);

						if ( $aField['Key'] == 'phone' || $aField['Type'] == 'phone' )
						{
							$field_info['isPhone'] = true;
							$field_info['trim_phone'] = preg_replace('/\D+/', '', $aField['value']);
						}

						if ($aField['Condition'] == 'isUrl') {
							$field_info['isUrl'] = true;
						}
						else if ($aField['Condition'] == 'isEmail') {
							$field_info['isEmail'] = true;
						}

						array_push($data, $field_info);
					}
				}
				unset($sellerInfo);
			}
			break;

		case 'location':

			// build location fields
			$location = $data['location'] = false;
			if ( $config['address_on_map'] && $listingData['account_address_on_map'] )
			{
				// get location data from user account
				$location = $rlAccount -> mapLocation;

				if ( $sellerInfo['Loc_latitude'] && $sellerInfo['Loc_longitude'] )
				{
					$location['direct'] = $sellerInfo['Loc_latitude'] .','. $sellerInfo['Loc_longitude'];
				}
			}
			else
			{
				// get location data from listing
				$fieldsList = $rlListings -> fieldsList;

				$location = $data['location'] = false;
				foreach ( $fieldsList as $key => $value )
				{
					if ( $fieldsList[$key]['Map'] && !empty($listingData[$fieldsList[$key]['Key']]) )
					{
						$name = $iPhone -> pValid( $lang[$value['pName']] );
						$mValue = $iPhone -> pValid( $value['value'] );
						$location['search'] .= "{$mValue}, ";
						$location['show'] .= "{$name}: {$mValue} ";
					}
				}

				if ( $listingData['Loc_latitude'] && $listingData['Loc_longitude'] )
				{
					$location['direct'] = $listingData['Loc_latitude'] .','. $listingData['Loc_longitude'];
				}
			}

			if ( $location !== false )
			{
				$location = str_replace(array('<\/b><br />','<b>'), array(' ', ''), $location);
				$coord = trim(!empty($location['direct']) ? $location['direct'] : $location['search']);
				list($lat, $lng) = explode(',', $coord, 2);

				$data['location'] = true;
				$data['coord'] = array(
					'lat' => strval($lat),
					'lng' => strval($lng)
				);
				$data['title'] = $rlListings -> getListingTitle($category_id, $listingData, $listingType['Key']);
				$data['subTitle'] = trim($location['show']);
				unset( $location );
			}
			break;
	}
}
$iPhone -> printAsXml($data);