<?php

/******************************************************************************
 *
 *	PROJECT: Flynax Classifieds Software
 *	VERSION: 4.0
 *	LISENSE: http://www.flynax.com/license-agreement.html
 *	PRODUCT: General Classifieds
 *	
 *	FILE: SAVED_SEARCHES.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/
 *
 ******************************************************************************/

/* ext js action */
if ($_GET['q'] == 'ext')
{
	/* system config */
	require_once( '../../includes/config.inc.php' );
	require_once( RL_ADMIN_CONTROL . 'ext_header.inc.php' );
	require_once( RL_LIBS . 'system.lib.php' );
	
	/* date update */
	if ($_GET['action'] == 'update' )
	{
		$reefless -> loadClass( 'Actions' );
		
		$type = $rlValid -> xSql( $_GET['type'] );
		$field = $rlValid -> xSql( $_GET['field'] );
		$value = $rlValid -> xSql( nl2br($_GET['value']) );
		$id = $rlValid -> xSql( $_GET['id'] );
		$key = $rlValid -> xSql( $_GET['key'] );

		$updateData = array(
			'fields' => array(
				$field => $value
			),
			'where' => array(
				'ID' => $id
			)
		);
		
		/* update cache */
		$reefless -> loadClass('Cache');
		$rlCache -> updateForms();
		
		$rlHook -> load('apExtSavedSearchesUpdate');
		
		$rlActions -> updateOne( $updateData, 'saved_search');
		exit;
	}
	
	/* data read */
	$limit = $rlValid -> xSql( $_GET['limit'] );
	$start = $rlValid -> xSql( $_GET['start'] );

	$sql = "SELECT SQL_CALC_FOUND_ROWS `T1`.*, `T2`.`Username` ";
	$sql .= "FROM `". RL_DBPREFIX ."saved_search` AS `T1` ";
	$sql .= "LEFT JOIN `". RL_DBPREFIX ."accounts` AS `T2` ON `T1`.`Account_ID` = `T2`.`ID` ";
	$sql .= "WHERE `T1`.`Status` <> 'trash' ";
	$sql .= "LIMIT {$start}, {$limit}";
	
	$rlHook -> load('apExtSavedSearchesSql');
	
	$data = $rlDb -> getAll($sql);
	
	foreach ($data as $key => $value)
	{
		$data[$key]['Status'] = $lang[$value['Status']];
		$data[$key]['name'] = $lang['listing_types+name+'. $value['Listing_type']] .' ('. $lang['search_forms+name+'. $value['Form_key']] .')';
		
		$rlHook -> load('apExtSavedSearchesData');
	}

	$count = $rlDb -> getRow( "SELECT FOUND_ROWS() AS `count`" );
	$count = $count['count'];
	
	$reefless -> loadClass( 'Json' );
	
	$output['total'] = $count['count'];
	$output['data'] = $data;

	echo $rlJson -> encode( $output );
}
/* ext js action end */

else 
{
	/* additional bread crumb step */
	if ( $_GET['action'] == 'view' )
	{
		$reefless -> loadClass('Message');
		
		$rlXajax -> registerFunction( array( 'contactOwner', $rlMessage, 'ajaxContactOwnerAP' ) );
		$rlXajax -> registerFunction( array( 'checkSavedSearch', $rlAdmin, 'ajaxCheckSavedSearch' ) );
		
		$bcAStep = $lang['view_details'];
		$id = (int)$_GET['id'];
		
		/* get saved search details */
		$saved_search = $rlDb -> fetch('*', array('ID' => $id), null, 1, 'saved_search', 'row');
		$saved_search['Content'] = unserialize($saved_search['Content']);
		
		if ( $saved_search['Content'] )
		{
			$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[$v['Key']] = $v;
			}
			unset($tmp_fields);
			
			foreach ( $saved_search['Content'] as $cKey => $field )
			{
				$tmp_content = array();
				
				if ( isset($fields[$cKey]) )
				{
					$tmp_content['Type'] = $fields[$cKey]['Type'];
					$tmp_content['Default'] = $fields[$cKey]['Default'];
					$tmp_content['Condition'] = $fields[$cKey]['Condition'];
					$tmp_content['name'] = $fields[$cKey]['name'];
					
					if ( $fields[$cKey]['Type'] == 'mixed' )
					{
						$tmp_content['value'] = $field;
						if ( empty($fields[$cKey]['Condition']) )
						{
							$tmp_content['value']['df'] = $lang['listing_fields+name+'. $field['df']];
						}
						else
						{
							$tmp_content['value']['df'] = $lang['data_formats+name+'. $field['df']];
						}
					}
					elseif ( $fields[$cKey]['Type'] == 'date' )
					{
						$tmp_content['value'] = $field;
					}
					elseif ( $fields[$cKey]['Type'] == 'price' )
					{
						$tmp_content['value'] = $field;
						$tmp_content['value']['currency'] = $lang['data_formats+name+'. $field['currency']];
					}
					elseif ( $fields[$cKey]['Type'] == 'unit' )
					{
						$tmp_content['value'] = $field;
						$tmp_content['value']['unit'] = $lang['data_formats+name+'. $field];
					}
					elseif ( $fields[$cKey]['Type'] == 'checkbox' )
					{
						$tmp_content['value'] = $rlCommon -> adaptValue( $fields[$cKey], implode(',', $field) );
					}
					elseif ( $fields[$cKey]['Key'] == 'Category_ID' )
					{
						$cat_name = $rlDb -> fetch( array('Key'), array( 'ID' => $field ), null, 1, 'categories', 'row' );
						$tmp_content['value'] = $lang['categories+name+'. $cat_name['Key']];
					}
					else
					{
						$tmp_content['value'] = $rlCommon -> adaptValue( $fields[$cKey], $field );
					}
				}
				
				$saved_search['fields'][] = $tmp_content;
			}
			
			unset($tmp_content, $fields);
			$rlSmarty -> assign_by_ref('saved_search', $saved_search);
		}
		
		/* get profile details */
		$reefless -> loadClass('Account');
		$profile_data = $rlAccount -> getProfile((int)$saved_search['Account_ID']);
		$rlSmarty -> assign_by_ref('profile_data', $profile_data);
	}
	
	/* register ajax methods */
	$rlXajax -> registerFunction( array( 'deleteSavedSearch', $rlAdmin, 'ajaxDeleteSavedSearch' ) );
	
	$rlHook -> load('apPhpSavedSearchesBottom');
}