<?php

/******************************************************************************
 *
 *	PROJECT: Flynax Classifieds Software
 *	VERSION: 4.0
 *	LISENSE: http://www.flynax.com/license-agreement.html
 *	PRODUCT: General Classifieds
 *	
 *	FILE: SAVED_SEARCH.INC.PHP
 *
 *	This script is a commercial software and any kind of using it must be 
 *	coordinate with Flynax Owners Team and be agree to Flynax License Agreement
 *
 *	This block may not be removed from this file or any other files with out 
 *	permission of Flynax respective owners.
 *
 *	Copyrights Flynax Classifieds Software | 2012
 *	http://www.flynax.com/
 *
 ******************************************************************************/

/* get account saved search */
$rlDb -> setTable('saved_search');
$saved_search = $rlDb -> fetch( array('ID', 'Content', 'Date', 'Status'), array( 'Account_ID' => $account_info['ID'] ), "ORDER BY `ID`" );

if ( !empty($saved_search) )
{
	$tmp_fields = $rlDb -> fetch( array('Key', 'Type', 'Condition', 'Default'), array( 'Status' => 'active' ), null, null, 'listing_fields' );
	$tmp_fields = $rlLang -> replaceLangKeys( $tmp_fields, 'listing_fields', array( 'name' ) );
	
	foreach ( $tmp_fields as $k => $v )
	{
		$fields[$tmp_fields[$k]['Key']] = $tmp_fields[$k];
	}
	unset($tmp_fields);

	foreach ($saved_search as $key => $value)
	{
		$content = unserialize($saved_search[$key]['Content']);
		$saved_search[$key]['Content'] = $content;
		//unset($saved_search[$key]['Content']);

		$tmp_content = false;
		$step = 0;
		foreach ( $content as $cKey => $cVal )
		{
			if ( isset($fields[$cKey]) )
			{
				$tmp_content[$step]['Type'] = $fields[$cKey]['Type'];
				$tmp_content[$step]['Default'] = $fields[$cKey]['Default'];
				$tmp_content[$step]['Condition'] = $fields[$cKey]['Condition'];
				$tmp_content[$step]['name'] = $fields[$cKey]['name'];
				
				if ( $fields[$cKey]['Type'] == 'mixed' )
				{
					$tmp_content[$step]['value'] = $content[$cKey];
					if ( empty($fields[$cKey]['Condition']) )
					{
						$tmp_content[$step]['value']['df'] = $lang['listing_fields+name+'.$content[$cKey]['df']];
					}
					else
					{
						$tmp_content[$step]['value']['df'] = $lang['data_formats+name+'.$content[$cKey]['df']];
					}
				}
				elseif ( $fields[$cKey]['Type'] == 'date' )
				{
					$tmp_content[$step]['value'] = $content[$cKey];
				}
				elseif ( $fields[$cKey]['Type'] == 'price' )
				{
					$tmp_content[$step]['value'] = $content[$cKey];
					$tmp_content[$step]['value']['currency'] = $lang['data_formats+name+'.$content[$cKey]['currency']];
				}
				elseif ( $fields[$cKey]['Type'] == 'unit' )
				{
					$tmp_content[$step]['value'] = $content[$cKey];
					$tmp_content[$step]['value']['unit'] = $lang['data_formats+name+'.$content[$cKey]['unit']];
				}
				elseif ( $fields[$cKey]['Type'] == 'checkbox' )
				{
					$tmp_content[$step]['value'] = $rlCommon -> adaptValue( $fields[$cKey], implode(',', $content[$cKey]) );
				}
				elseif ( $fields[$cKey]['Key'] == 'Category_ID' )
				{
					$cat_name = $rlDb -> fetch( array('Key'), array( 'ID' => $content[$cKey] ), null, 1, 'categories', 'row' );
					$tmp_content[$step]['value'] = $lang['categories+name+'.$cat_name['Key']];
				}
				else
				{
					$tmp_content[$step]['value'] = $rlCommon -> adaptValue( $fields[$cKey], $content[$cKey] );
				}
			}
			$step++;
		}
		$saved_search[$key]['fields'] = $tmp_content;
		unset($tmp_content);
	}
	unset($fields);

	$rlSmarty -> assign_by_ref('saved_search', $saved_search);
	
	$reefless -> loadClass( 'Search' );
	$reefless -> loadClass( 'Actions' );
	
	$rlXajax -> registerFunction( array( 'deleteSavedSearch', $rlSearch, 'ajaxDeleteSavedSearch' ) );
	$rlXajax -> registerFunction( array( 'massSavedSearch', $rlSearch, 'ajaxMassSavedSearch' ) );
	$rlXajax -> registerFunction( array( 'checkSavedSearch', $rlSearch, 'ajaxCheckSavedSearch' ) );
}