<?php
//error_reporting('E_All'); 
//error_reporting('~E_notice'); 
//show_error('message' [, int $status_code= 500 ] )
class Agency_mod extends Model{
 	function Agency_mod(){
		parent::Model();
	}
	


/*
	For manage agent profile page. need agency detauls
*/
	
	function get_agencyDetails_mod($ses_id){

		$select_query='select ag_email, ag_phone, ag_fax from agency where agency_id='.$ses_id;
		$query_result = $this->db->query($select_query);
		foreach ($query_result->result_array() as $row){
			$agencyAddressDetails['ag_email'] = $row['ag_email'];
			$agencyAddressDetails['ag_phone'] = $row['ag_phone'];
			$agencyAddressDetails['ag_fax'] = $row['ag_fax'];
		}//end foreach
		return $agencyAddressDetails;
   }
/*
	For manage agent profile page.show agent list
*/   
   function get_agentList_mod($ses_id){

		
		$select_query='select user_id, us_activated ,us_image_id_2,us_first_name,
		us_surname,us_phn_num,us_fax_num,us_email from users where us_agency_id = '.$ses_id;
		$query_result = $this->db->query($select_query);
		if(count($query_result->result_array())!=0){
			$i = 0;
			foreach ($query_result->result_array() as $row){
			
				$agent_details[$i]['user_id'] =$row['user_id'];
				$agent_details[$i]['us_activated'] =$row['us_activated'];
				$agent_details[$i]['us_image_id_2'] =$row['us_image_id_2'];
				$agent_details[$i]['us_first_name'] =$row['us_first_name'];
				$agent_details[$i]['us_surname'] =$row['us_surname'];
				$agent_details[$i]['us_phn_num'] =$row['us_phn_num'];
				$agent_details[$i]['us_fax_num'] =$row['us_fax_num'];
				$agent_details[$i]['us_email'] =$row['us_email']; 
				$agent_details[$i]['user_id'] =$row['user_id']; 
				//echo $agent_details[$i];
				$i++;
			}//end foreach
		}
		else {
			$agent_details	='no';
		}
		
   		return $agent_details;
   }
/* 
	UPDATE AGENT PROFILE -- show
*/   
	function show_AgentProfile_mod($agent_id){
	
		$select_query='select us_first_name, us_address, us_suburb_id, us_address2,us_post_code,   
						us_phn_num, us_mobile_num, us_fax_num, us_email,us_image_id_2
						from users where  `user_id`= '.$agent_id;
		$query_result = $this->db->query($select_query);
		if($query_result->num_rows()> 0){	
			$rs = $query_result->result_array();
			foreach ( $rs as $row){
				$agent_info['us_first_name']=$row['us_first_name'];
				$agent_info['us_address'] = $row['us_address'];
				$agent_info['us_address2'] = $row['us_address2'];
				$agent_info['us_suburb_id'] = $row['us_suburb_id'];
				//$agent_info['us_post_code'] = $row['us_post_code'];
				$agent_info['us_phn_num'] = $row['us_phn_num'];
				$agent_info['us_mobile_num'] = $row['us_mobile_num'] ;
				$agent_info['us_fax_num'] = $row['us_fax_num'];
				$agent_info['us_email'] = $row['us_email'];
				$agent_info['us_image_id_2'] = $row['us_image_id_2'];
				
			}
		}else{
			$agent_info = -1;
		}
		return $agent_info;
	}
/*UPDATE AGENT PROFILE--update*/
	function update_agentProfile_mod($agent_id,$orig_name,$orig_name_thumb){
		
		$flag=0;
		$old_pass = trim($this->input->xss_clean($this->input->post('old_password')));

		if($old_pass!=''){
			$select_pass='select us_password from users where user_id ='.$agent_id;
			$query_result = $this->db->query($select_pass);
			foreach ($query_result->result_array() as $row){
					if(md5($old_pass)!= $row['us_password']){
						return 'pass';
						
					}else{
					 	$flag=1;
						
					}
					
			}
		}
		$update_array['us_first_name'] = $this->input->xss_clean($this->input->post('name'));
		$ses_user_type = $this->session->userdata('type');
		if($ses_user_type>0){
			$this->session->set_userdata('name',$update_array['us_first_name']);
		}
		$update_array['us_address'] = $this->input->xss_clean($this->input->post('address1'));
//$address_dummy = $this->input->xss_clean($this->input->post('address1'));
//$update_array['us_address'] = $address_dummy;
//echo "From the Screen 1st".'<br />';
//echo "update_array['us_address']=".$update_array['us_address'].'<br />';

		$update_array['us_address2'] = $this->input->xss_clean($this->input->post('address2'));
		//$update_array['us_post_code'] = $this->input->xss_clean($this->input->post('postCode'));
		$update_array['us_suburb_id'] = $this->input->xss_clean($this->input->post('suburb_name'));
		$update_array['us_phn_num'] = $this->input->xss_clean($this->input->post('phone'));
		$update_array['us_mobile_num'] = $this->input->xss_clean($this->input->post('mobile'));
		$update_array['us_fax_num'] = $this->input->xss_clean($this->input->post('fax'));


//Added by ALex for users table - country, state, and city

//          include ('admin/config.php');
          include ('config.php');
          $link = mysql_connect(hostname,username,password);
          if (!$link) {
             die('Could not connect: ' . mysql_error());
          }
          mysql_select_db(databasename, $link); 
	  $country_id = $this->input->xss_clean($this->input->post('country'));
          $query="SELECT cd_country_name FROM code_country WHERE country_id='$country_id'";
          $result=mysql_query($query);
          while($row=mysql_fetch_array($result)) {
             $update_array['us_country_name'] = $row['cd_country_name'];
          }
	  $state_id = $this->input->xss_clean($this->input->post('state'));
          $query="SELECT cds_state_name FROM code_state WHERE state_id='$state_id'";
          $result=mysql_query($query);
          while($row=mysql_fetch_array($result)) {
             $update_array['us_state_name'] = $row['cds_state_name'];
          }
          $city_id = $this->input->xss_clean($this->input->post('city'));
          $query="SELECT ps_suburb FROM postcode WHERE postcode_id='$city_id'";
          $result=mysql_query($query);
          while($row=mysql_fetch_array($result)) {
             $update_array['us_suburb'] = $row['ps_suburb'];
          }
          mysql_close($link);
//End added by ALex

		if($orig_name!='' && $orig_name_thumb!=''){
			$update_array['us_image_id_1'] = $orig_name;
			$update_array['us_image_id_2'] = $orig_name_thumb;
		}
		if($flag){
		
			$pass1 = trim($this->input->xss_clean($this->input->post('password')));
			$pass2 = trim($this->input->xss_clean($this->input->post('new_password')));
			if($pass1!='' && $pass2!=''){
				if($pass1 == $pass2 ){
					
					$update_array['us_password'] = md5($pass1);
					
				}
			}	
		}


$update_array['user_id']=$agent_id;

echo "".'<br />';
echo "".'<br />';
echo "".'<br />';
echo "".'<br />';
echo "".'<br />';
echo "agent_id=".$agent_id.'<br />';

echo "From the Screen 2nd".'<br />';
$us_address=$update_array['us_address'];
echo "screen - us_address=".$us_address.'<br />'; 
echo "us_address Hexdec=".hexdec ($us_address).'<br />';
//$update_array['us_address']="56 George Street";
print_r($update_array); // print_r($_REQUEST);

//echo "Inserted in the program".'<br />';	

//print_r($update_array); print_r($_REQUEST);
//echo "update_array['us_address']=".$update_array['us_address'].'<br />';



//$us_address=$update_array['us_address'];
echo "BBBB*************************update_array['us_address']=".$update_array['us_address'].'<br />';

          include ('config.php');
          $link = mysql_connect(hostname,username,password);
          if (!$link) {
             die('Could not connect: ' . mysql_error());
          }

		$this->db->where('user_id', $agent_id);
		$result = $this->db->update('users', $update_array); 


          mysql_close($link);
echo "result=".$result.'<br />';
		return $result;
	}
/*
	count property
*/
	function count_propertyList_mod($ses_id, $activation_option){
		$select_query="SELECT count( property_id ) as total_property
		FROM property WHERE pr_user_type = $ses_id 
		&& pr_active".$activation_option." ";
						
		$query_result = $this->db->query($select_query);
		if(count($query_result->result_array())!=0){
		
			foreach ( $query_result->result_array() as $row){
				$total_property = $row['total_property'];
				return $total_property;
			}
		}else{
			$total_property =-1;
			return $total_property;
		}
	}
/*
	show list of property for user/agent
*/	
	function show_propertyList_mod($ses_id, $activation_option){
		$i=0;
		//$select_query="SELECT property_id, pr_expired_date, property_code_id, sb_name, sb_postcode, 
		$select_query="SELECT property_id, pr_expired_date, property_code_id, ps_suburb, ps_postcode, 
						pr_price_weekly_rent, pr_street_no,  pr_street_name,
/* pr_country_id added */
						pr_street_id, pr_bedroom, pr_bathroom, pr_car_space,pr_active, pr_country_id
						FROM property, postcode
						WHERE pr_user_type = $ses_id AND property.pr_suburb_id = postcode_id 
						&& pr_active".$activation_option." ORDER BY pr_create_date DESC";
						
		$query_result = $this->db->query($select_query);
		if(count($query_result->result_array())!=0){
			$rs = $query_result->result_array();
			foreach ( $rs as $row){
				$property_details[$i]['property_id'] = $row['property_id'];
				$property_details[$i]['pr_expired_date'] = $row['pr_expired_date'];
				$property_details[$i]['sb_name'] = $row ['pr_suburb_name'];
			      //$property_details[$i]['sb_name'] = $row ['sb_name'];
				$property_details[$i]['sb_name'] = $row ['ps_suburb'];
			      //$property_details[$i]['sb_postcode'] = $row['sb_postcode'];
				$property_details[$i]['sb_postcode'] = $row['ps_postcode'];
				$property_details[$i]['pr_price_weekly_rent'] =	$row['pr_price_weekly_rent'];
				$property_details[$i]['pr_street_no'] = $row['pr_street_no'];
				$property_details[$i]['pr_street_name'] = $row['pr_street_name'];
				$pr_street_id = $row['pr_street_id'];
				$select_query1 = "select `street_name` from street_list where street_id = ".$pr_street_id;
				$query_result1 = $this->db->query($select_query1);
				$rs1 = $query_result1->result_array();
				foreach ( $rs1 as $row1){
					$property_details[$i]['pr_street_type'] =  $row1['street_name'];
				}
				$property_details[$i]['pr_bedroom'] = $row['pr_bedroom'];
				$property_details[$i]['pr_bathroom'] = $row['pr_bathroom'];
				$property_details[$i]['pr_car_space'] = $row['pr_car_space'];
				//$property_details[$i]['pr_ensuits'] = $row['pr_ensuits'];
/* pr_country_id added */
				$property_details[$i]['pr_country_id'] = $row['pr_country_id'];
				$property_code_id = $row ['property_code_id'];
				if($property_code_id!=0){
					$select_query1='SELECT pr_cd_transaction_type, pr_cd_type FROM `property_code` WHERE `property_code_id`= '.$property_code_id;
					$query_result1 = $this->db->query($select_query1);
					foreach ($query_result1->result_array() as $row1){
						$property_details[$i]['pr_cd_transaction_type'] = $row1['pr_cd_transaction_type'];
						$property_details[$i]['pr_cd_type'] = $row1['pr_cd_type'];
					}
				}
				$property_details[$i]['pr_active'] = $row['pr_active'];
			$i++;
			}//end foreach
		}else{
			$property_details='no';
		}
   		return $property_details;
	}
/*
Delete agent profile
*/
	function deleteAgent_mod($agent_id){
		$this->db->where('pr_user_id', $agent_id);
		$result1 = $this->db->delete('property');
		if($result1){
			$this->db->where('user_id', $agent_id);
			$result=$this->db->delete('users');
			return $result;
		}	
		return $result1; 
		
	}	
/* 
	show property details for AGENCY
*/	
	function get_property_details_forAgency($property_id,$ses_id){

		$select_query="SELECT pr_suburb_id,pr_expired_date, pr_title,pr_user_id, property_code_id, pr_comments,
						pr_price_weekly_rent, pr_street_no, pr_street_name,edit_counter,
						pr_street_id, pr_bedroom, pr_bathroom, pr_car_space, pr_ensuits,
						pr_energyRate ,pr_uvValue,pr_block_size, pr_active, 
						pr_auction_date,pr_profile_pic,pr_country_id,pr_suburb_name
						FROM property,postcode
						WHERE property_id = $property_id AND pr_suburb_id=postcode_id AND pr_user_type=$ses_id";
		$query_result = $this->db->query($select_query);
			if(count($query_result->result_array())!=0){
			foreach ($query_result->result_array() as $row){
			
				$property_details['pr_suburb_id'] = $row ['pr_suburb_id'];
				$property_details['pr_expired_date'] = $row ['pr_expired_date'];
				$property_code_id = $row ['property_code_id'];

				if($property_code_id!=0){
					$select_query1='SELECT pr_cd_transaction_type, pr_cd_type FROM `property_code` WHERE `property_code_id`= '.$property_code_id;
					$query_result1 = $this->db->query($select_query1);
					foreach ($query_result1->result_array() as $row1){
						$property_details['pr_cd_transaction_type'] = $row1['pr_cd_transaction_type'];
						$property_details['pr_cd_type'] = $row1['pr_cd_type'];
					}
				}else{
				}
				//---------------
				$pr_user_id = $row['pr_user_id'];
				if($pr_user_id!=0){
					$select_query2='SELECT us_first_name FROM users WHERE `user_id`= '.$pr_user_id;
					$query_result2 = $this->db->query($select_query2);
					foreach ($query_result2->result_array() as $row2){
						$property_details['us_first_name'] = $row2['us_first_name'];
					}
				}	
				else{
					$property_details='noAgent';
					return $property_details;
				}
				//----------------
				
				$property_details['pr_price_weekly_rent'] =	$row['pr_price_weekly_rent'];
				$property_details['pr_title'] =	$row['pr_title'];
				$property_details['pr_street_no'] = $row['pr_street_no'];
				$property_details['pr_street_name'] = $row['pr_street_name'];
				//$property_details['pr_postcode'] = $row['pr_postcode'];
				$property_details['pr_street_id'] = $row['pr_street_id'];
				$select_query1 = "select `street_name` from street_list where street_id = ".$property_details['pr_street_id'];
					$query_result1 = $this->db->query($select_query1);
					$rs1 = $query_result1->result_array();
					foreach ( $rs1 as $row1){
						$property_details['pr_street_type'] =  $row1['street_name'];
					}
				$property_details['pr_bedroom'] = $row['pr_bedroom'];
				$property_details['pr_bathroom'] = $row['pr_bathroom'];
				$property_details['pr_car_space'] = $row['pr_car_space'];
				$property_details['pr_ensuits'] = $row['pr_ensuits'];
				$property_details['pr_energyRate'] = $row['pr_energyRate'];
				$property_details['pr_uvValue'] = $row['pr_uvValue'];
				$property_details['pr_block_size'] = $row['pr_block_size'];
				$property_details['pr_comments'] = $row['pr_comments'];
				$property_details['sb_name'] = $row['ps_suburb'];
			//	$property_details['sb_name'] = $row['pr_suburb_name'];
				$property_details['edit_counter'] = $row['edit_counter']; 
				$property_details['pr_active'] = $row['pr_active']; 
				$property_details['pr_auction_date'] = $row['pr_auction_date']; 
				$property_details['pr_profile_pic'] = $row['pr_profile_pic']; 
/* pr_country_id added */
				$property_details['pr_country_id'] = $row['pr_country_id']; 
			}//end foreach
		}
		else{
			$property_details='no';
		}	
   		return $property_details;
	}
/*
update property by agency
*/
	function update_property_details_forAgency($property_id,$ses_id){
	$transaction_type = $this->input->xss_clean($this->input->post('transaction_type'));
		$property_type = $this->input->xss_clean($this->input->post('property_type'));
		
		$select_query='SELECT property_code_id FROM `property_code` 
						WHERE  `pr_cd_type`= "'.$property_type.'" AND 
						`pr_cd_transaction_type`= "'.$transaction_type.'"';
		$query_result = $this->db->query($select_query);
		foreach ($query_result->result_array() as $row){
			$update_array['property_code_id'] = $row['property_code_id'];
		}
		
		$update_array['pr_title'] = $this->input->xss_clean($this->input->post('property_title'));
		$update_array['pr_street_id'] = $this->input->xss_clean($this->input->post('street_id'));
		$update_array['pr_street_name'] = $this->input->xss_clean($this->input->post('street_name'));
		$update_array['pr_street_no'] = $this->input->xss_clean($this->input->post('street_number'));
		$update_array['pr_suburb_id'] = $this->input->xss_clean($this->input->post('suburb_name'));
		//$update_array['pr_postcode'] = $this->input->xss_clean($this->input->post('postcode'));
		$update_array['pr_price_weekly_rent'] = $this->input->xss_clean($this->input->post('price'));
		$update_array['pr_comments']= $this->input->xss_clean($this->input->post('pr_comments'));
		$update_array['pr_bedroom'] = $this->input->xss_clean($this->input->post('bedroom'));
		$update_array['pr_bathroom'] = $this->input->xss_clean($this->input->post('bathroom'));
		$update_array['pr_ensuits'] = $this->input->xss_clean($this->input->post('ensuites'));
		$update_array['pr_car_space'] = $this->input->xss_clean($this->input->post('carSpace'));
		$update_array['pr_energyRate'] = $this->input->xss_clean($this->input->post('energyRate'));
		$update_array['pr_block_size'] = $this->input->xss_clean($this->input->post('block_size'));
		$update_array['pr_uvValue'] = $this->input->xss_clean($this->input->post('uvValue'));
/* pr_country_id added */
		$update_array['pr_country_id'] = $this->input->xss_clean($this->input->post('pr_country_id'));
		$auction_date = $this->input->xss_clean($this->input->post('date'));
		if($auction_date!='Month/Date/Year')
			$update_array['pr_auction_date'] = $auction_date;
		$select_query1 ="SELECT edit_counter FROM property where property_id=".$property_id;
		$query_result1 = $this->db->query($select_query1);

		foreach ($query_result1->result_array() as $row1){
			$update_array['edit_counter'] = $row1['edit_counter']+1;
			
		}
		//profile PIC
			$update_array['pr_profile_pic'] = $this->input->post('profilePic');
				$this->session->set_userdata('profilePhoto', false);
		/// _UPDATE FOR PHOTOS BY MAHADEB
		$photos= $this->input->xss_clean($this->input->post('photos'));
		$update_array['pr_photos'] =(is_array($photos))?'|'.implode('|', $photos).'|':'';
		$this->session->set_userdata('property_photos', false);
		/// _UPDATE MODIFICATION END
		$this->db->where('property_id', $property_id);
		$result = $this->db->update('property', $update_array); 
		return $result;
	}
/*
Remove property
*/

	function remove_property_byAgency($property_id){
		$this->db->where('property_id', $property_id);
		$result=$this->db->delete('property'); 
		//$result='--';
		return $result;
	}
	///by zakir
/*
	For manage agent profile page.show agent list
*/   
   function get_agency_by_user($user_id){
		
		$select_query='select us_agency_id from users where user_id = '.$user_id;
		$query_result = $this->db->query($select_query);
		$agency=$query_result->result_array();
   		return $agency[0]['us_agency_id'];
   }
	/*
		Function: generate Random Password
	*/ 
	function gen_md5_password($len = 12) {
	// function calculates 32-digit hexadecimal md5 hash
	// of some random data
	return substr(md5(rand().rand()), 0, $len);
	}
	/*
count sell Rent property.
*/
	function count_sellRental_propertyList($agency_id,$user_id,$report_type){
			if($report_type=='sell' && $user_id!='NULL'){
			
			$select_query = 'select count(property_id) as total_sell_property
							from property where pr_user_id  = '.$user_id.' and 
			( property_code_id >= 18 and property_code_id <= 25 ) && 
			( pr_published_date!="0000-00-00 00:00:00" && pr_expired_date >= "'.date("Y-m-d h:i:s").'")'; 
			
			$query_result = $this->db->query($select_query);
			
			if($query_result->num_rows()==0){
				$total_sell_property = -1;
				return $total_sell_property;
			}else{
				$rs = $query_result->result_array();
				foreach ($rs as $row){
					$total_sell_property = $row['total_sell_property'];
				}//end for
				return $total_sell_property;
			}//end //end num_rows		
		}//end main if

	//---------- RENT PROPERTY DETAILS --
		else if($report_type=='rent' && $user_id!='NULL'){
			$select_query = 'select count( property_id) as total_rent_property from property 
			where pr_user_id  = '.$user_id.' and 
			( property_code_id >= 26 and property_code_id <= 33 )  && 
			( pr_published_date!="0000-00-00 00:00:00" && pr_expired_date >= "'.date("Y-m-d h:i:s").'")';
			
			$query_result = $this->db->query($select_query);

			if($query_result->num_rows()==0){
				$total_rent_property = -1;
				return $total_rent_property;
			}else{

				foreach ($query_result->result_array() as $row){
					$total_rent_property = $row['total_rent_property'];
				}//end foreach
				return $total_rent_property;	
			}//end num_rows		
		}//end main if

	}//end function



	
}
	

?>
