<?php

/******************************************************************************
 *
 *	PROJECT: Flynax Classifieds Software
 *	VERSION: 4.X
 *	LICENSE: FL0M2SG681CQ - http://www.flynax.com/license-agreement.html
 *	PRODUCT: Pets Classifieds
 *	DOMAIN: pupsnshop.com
 *	FILE: MY_LISTINGS.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('Listings');
$data = array();

// delete listing
if ( isset($_POST['del_id']) && !empty($_POST['del_id']) )
{
	$reefless -> loadClass('Actions');
	$data['deleted'] = $iPhone -> deleteListing((int)$_POST['del_id']);
	if ( $data['deleted'] ) {
		$data['message'] = $lang['notice_listing_deleted'];
	}
}
// delete listing photo
else if ( isset($_POST['del_photo_id']) && !empty($_POST['del_photo_id']) )
{
	$photoId = (int)$_POST['del_photo_id'];
	$sql = "SELECT `Photo`, `Thumbnail`, `Original` FROM `". RL_DBPREFIX ."listing_photos` WHERE `ID` = '{$photoId}'";
	$photoInfo = $rlDb -> getRow($sql);

	// remove photo files
	@unlink(RL_FILES . $photoInfo['Photo']);
	@unlink(RL_FILES . $photoInfo['Thumbnail']);
	@unlink(RL_FILES . $photoInfo['Original']);

	// remove from db
	$rlDb -> query("DELETE FROM `". RL_DBPREFIX ."listing_photos` WHERE `ID` = '{$photoId}'");

	// remove related directory if it is empty
	$del_dir = explode('/', $photoInfo['Photo']);
	array_pop($del_dir);
	$reefless -> deleteDirectory(implode(RL_DS, $del_dir) . RL_DS, true);

	// return response
	$data['deleted'] = true;
}
// save listing photo
else if ( isset($_POST['save_photo']) )
{
	$options = array(
		'large' => array(
			'max_width'  => $config['pg_upload_large_width'] ? $config['pg_upload_large_width'] : 640,
			'max_height' => $config['pg_upload_large_height'] ? $config['pg_upload_large_height'] : 480,
		),
		'thumbnail' => array(
			'max_width'  => $config['pg_upload_thumbnail_width'] ? $config['pg_upload_thumbnail_width'] : 120,
			'max_height' => $config['pg_upload_thumbnail_height'] ? $config['pg_upload_thumbnail_height'] : 90,
		)
	);

	$file_tmp_name = $_FILES['userfile']['name'];
	$file_type = $_FILES['userfile']['type'];
	$file_tmp_dir = $_FILES['userfile']['tmp_name'];
	$file_ext = pathinfo($file_tmp_name, PATHINFO_EXTENSION);

	if ( false == $rlValid -> isImage($file_ext) )
	{
		$iPhone -> printAsXml(array(
				'uploaded' => false,
				'message' => str_replace(array('{file}', '{ext}'), array('', $file_ext), $lang['notice_bad_file_ext'])
			)
		);
	}

	$file_name = 'orig_'. time() . mt_rand() .'.'. $file_ext;
	$file_dir = RL_UPLOAD . $file_name;

	$large_name = 'large_'. time() . mt_rand() .'.'. $file_ext;
	$thumb_name = 'thumb_'. time() . mt_rand() .'.'. $file_ext;

	// upload file
	if ( move_uploaded_file($file_tmp_dir, $file_dir) )
	{
		$reefless -> rlChmod($file_dir);
	}

	if ( is_readable($file_dir) )
	{
		$reefless -> loadClass('Resize');
		$reefless -> loadClass('Crop');
		$reefless -> loadClass('Actions');

		$listing_id = (int)$_POST['listing_id'];

		// file directories handler
		$cur_photo = $rlDb -> getOne('Photo', "`Listing_ID` = '{$listing_id}'", 'listing_photos');
		if ( $cur_photo )
		{
			$exp_dir = explode('/', $cur_photo);
			if ( count($exp_dir) > 1 )
			{
				array_pop($exp_dir);
				$dir = RL_FILES . implode(RL_DS, $exp_dir) . RL_DS;
				$dir_name = implode('/', $exp_dir) .'/';
			}
		}

		if ( !$dir )
		{
			$dir = RL_FILES . date('m-Y') . RL_DS .'ad'. $listing_id . RL_DS;
			$dir_name = date('m-Y') .'/ad'. $listing_id .'/';
		}
		$reefless -> rlMkdir($dir);

		//
		$original_destination = $dir . $file_name;
		$large_destination = $dir . $large_name;
		$thumb_destination = $dir . $thumb_name;

		// make large image
		$rlCrop -> loadImage($file_dir);
		$rlCrop -> cropBySize($options['large']['max_width'], $options['large']['max_height'], ccCENTER);
		$rlCrop -> saveImage($file_dir, $config['img_quality']);
		$rlCrop -> flushImages();
		$rlResize -> resize($file_dir, $large_destination, 'C', array($options['large']['max_width'], $options['large']['max_height']), null, true);

		if ( is_readable($large_destination) )
		{
			chmod($large_destination, 0644);
		}

		// make thumbnail
		$rlCrop -> loadImage($file_dir);
		$rlCrop -> cropBySize($options['thumbnail']['max_width'], $options['thumbnail']['max_height'], ccCENTER);
		$rlCrop -> saveImage($file_dir, $config['img_quality']);
		$rlCrop -> flushImages();
		$rlResize -> resize($file_dir, $thumb_destination, 'C', array($options['thumbnail']['max_width'], $options['thumbnail']['max_height']), null, false);

		if ( is_readable($thumb_destination) )
		{
			chmod($thumb_destination, 0644);
		}

		// remove original image if crop user interface disabled
		if ( $config['img_crop_interface'] )
		{
			copy($file_dir, $original_destination);
			if ( is_readable($original_destination) )
			{
				chmod($original_destination, 0644);
			}
		}
		unlink($file_dir);

		// save to base
		$max_pos = $rlDb -> getRow("SELECT MAX(`Position`) AS `Max` FROM `". RL_DBPREFIX ."listing_photos` WHERE `Listing_ID` = {$listing_id}");
		$insert = array(
			'Listing_ID' => $listing_id,
			'Position' => $max_pos['Max'] + 1,
			'Photo' => $dir_name . $large_name,
			'Thumbnail' => $dir_name . $thumb_name,
			'Original' => $config['img_crop_interface'] ? $dir_name . $file_name : '',
			'Description' => '',
			'Type' => 'photo',
			'Status' => 'active'
		);
		$rlActions -> insertOne($insert, 'listing_photos');

		// update photos data
		if ( version_compare($config['rl_version'], '4.0.1', '>') )
		{
			$rlListings -> updatePhotoData($listing_id);
		}

		$iPhone -> printAsXml(array(
			'uploaded' => true,
			'id' => mysql_insert_id()
			)
		);
	}
	$iPhone -> printAsXml(array('uploaded' => false));
}
// get listing photos
else if ( isset($_POST['photos_id']) && !empty($_POST['photos_id']) )
{
	$listingId = (int)$_POST['photos_id'];

	$sql  = "SELECT `T2`.`Image` FROM `". RL_DBPREFIX ."listings` AS `T1` ";
	$sql .= "LEFT JOIN `". RL_DBPREFIX ."listing_plans` AS `T2` ON `T1`.`Plan_ID` = `T2`.`ID` ";
	$sql .= "WHERE `T1`.`ID` = '{$listingId}' LIMIT 1";
	$listingInfo = $rlDb -> getRow($sql);

	if ( false !== $photos = $iPhone -> getListingPhotos($listingId, (int)$listingInfo['Image']) )
	{
		$data['photos'] = $photos;
	}
	$data['maxAllow'] = (int)$listingInfo['Image'];
}
// get listings list
else
{
	$pInfo['current'] = (int)$_POST['pg'];
	$myPage = $_POST['my_page'];

	// define listings type
	$listingsType = $rlListingTypes -> types[substr($myPage, 3)];
	$listings = $rlListings -> getMyListings($listingsType['Key'], 'Pay_date', 'desc', $pInfo['current'], $config['iFlynaxConnect_listings_per_page']);

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

	$data = array();
	if ( !empty($listings) )
	{
		$data = $iPhone -> buildListingsShortForm($listings, false, true);

		if ( $nextPage > 0 )
		{
			array_push($data, $iPhone -> getTextLoadMore($nextPage));
		}
	}
}

$iPhone -> printAsXml($data);