<?php
 include_once(LoadPHPFile('/RealConnect/Model/details_model.php'));
 include_once(LoadPHPFile('/RealConnect/Model/shared_model.php'));
  include_once(LoadPHPFile('/RealConnect/Model/properties_model.php'));
 include_once(LoadPHPFile('/RealConnect/Model/flexproperties_model.php'));
 include_once(LoadPHPFile('/RealConnect/Model/flexdetails_model.php'));
 include_once(LoadPHPFile('/RealConnect/controllers/vendor/autoload.php'));
 
?>
<?php
function details($property_type = 'residential', $mls_id = 0)
	{
		$model_Obj = new \stdClass();
		$model_Obj->details_model = new Details_model();
		$model_Obj->shared_model = new Shared_model();
		$model_Obj->properties_model = new Properties_model();
		$model_Obj->flexproperties_model = new Flexproperties_model();
		$model_Obj->flexdetails_model = new Flexdetails_model();
		
		
		if($property_type =='test')
		{
			$model_Obj->details_model->test();
		}
		//initialize the arrays for data that will be sent to the views
		$data = array();
		$headdata = array();
		//comment tpss
		//$this->content['page_title'] = "Property Details";
		$content['page_title'] = "Property Details";
		
		//get client information
		$site_url = get_site_url();
		$client_info = $model_Obj->shared_model->get_client_info($site_url);
		$mls_type = 'paragon';
		$office_id = '';
		
		$data['rotating_agent_show_contact_info'] = false;
		
		if (count($client_info) > 0) {
			$mls_type = $client_info['mls_feed'];
			$office_id = $client_info['office_id'];
			if($client_info['rotating_agents']){
				$data['rotating_agent_show_contact_info'] = true;
				
			}
		}
		
		//get details to send to the view
		if ($mls_id != 0) {
			if ($mls_type == 'paragon') {
				$data['details'] = $model_Obj->details_model->get_property_details($property_type, $mls_id, $site_url);
				if($data['details'])
				{
					if(count($data['details']['images']) > 0) {
						$image_count = 0;
						foreach ($data['details']['images'] as $image) {
							if ($image_count == 0) {
								$fbImage = $image['url'];
							}
							$image_count++;
						}
					} else {
						$fbImage = site_url('/images/placeholder.png');
					}
					$fbDescription = $data['details']['LR_remarks22'];
					$fbTitle = $data['details']['L_Address'].', '.$data['details']['L_City'].', '.$data['details']['L_State'].' '.$data['details']['L_Zip'];
				}
					
			} else {
				$data['details'] = $model_Obj->flexdetails_model->get_property_details($property_type, $mls_id, $site_url);
				if($data['details'])
				{
					if(count($data['details']['images']) > 0) {
						$image_count = 0;
						foreach ($data['details']['images'] as $image) {
							if ($image_count == 0) {
								$fbImage = $image['url'];
							} 
							$image_count++;
						}
					} else {
						$fbImage = site_url('/images/placeholder.png');
					}
					$fbDescription = $data['details']['LIST_78'];
					$fbTitle = $data['details']['address'];
				}
					
			}
		  $data['BrandedVideoUrl']= getBrandedVideoUrl($mls_id);	
		} else {
			$data['details'] = 'The property you are looking for is not in the MLS database.';
		}
		if(isset($fbDescription))
			$data['fbDescription'] = $fbDescription;
		if(isset($fbImage))
			$data['fbImage'] = $fbImage;
		// $headdata['headScript'] = '<meta name="title" content="'.$fbTitle.'" />';
		// $headdata['headScript'] = '<meta name="description" content="'.$fbDescription.' " />';
		//$headdata['headScript'] .= '<meta property="og:description" content="'.$fbDescription.'" />';
		//$headdata['headScript'] .= '<meta name="image" content="'.$fbImage.'" />';
		//$data['meta'] = array("og:title" => $fbTitle, "og:type" => "website", "og:description" => $fbDescription, "og:image" => $fbImage);
		$data['maintenance'] = '';
		
		//$data['maintenance'] = 'This page is currently under maintenance. Please check back shortly.';
		
		//get  realconnect user information
		global $wpdb;
		$data['rc_user_email'] = '';
		$data['rc_display_name'] = '';
	    
	    $contact_info_belong_to_our_client = $model_Obj->shared_model->contact_info_belong_to_our_client($mls_id,$property_type,$office_id);		
	    if(count($contact_info_belong_to_our_client) && isset($contact_info_belong_to_our_client['U_Email']) && $contact_info_belong_to_our_client['U_Email'] != ''){
			$data['rc_user_email'] = $contact_info_belong_to_our_client['U_Email'];
			$data['rc_display_name'] = $contact_info_belong_to_our_client['U_UserFirstName'].' '.$contact_info_belong_to_our_client['U_UserLastName'];
			$data['rotating_agent_show_contact_info'] = false;
		}
		else if (isset($client_info['email']) && $client_info['email'] != '') {
			$data['rc_user_email'] = $client_info['email'];
			$data['rc_display_name'] = $client_info['first_name'].' '.$client_info['last_name'];
		}
		//send some other data to the view
		$data['property_type'] = $property_type;
		$data['mls_id'] = $mls_id;
		
		//get the data for the search - search across all property tables
		/*custom featured listing */
		
		//load arrays with data that will be sent to the views
		/* end custom featured listing */
		if ($mls_type == 'paragon') {
			//get queries to prefill area and subdivision in the view
			$areas_autofill = $model_Obj->shared_model->get_areas();
			$subdivision_autofill = $model_Obj->shared_model->get_subdivisions();
			$data['areas_autofill'] = $areas_autofill;
			$data['subdivision_autofill'] = $subdivision_autofill;
			//get queries to prefill school in the view
			$elem_autofill = $model_Obj->shared_model->get_elem_schools();
			$middle_autofill = $model_Obj->shared_model->get_middle_schools();
			$high_autofill = $model_Obj->shared_model->get_high_schools();
			$data['elem_autofill'] = $elem_autofill;
			$data['middle_autofill'] = $middle_autofill;
			$data['high_autofill'] = $high_autofill;
			//load views
			// $this->load->view('common/header', $headdata);
			//$this->load->view('main/details_view', $data);
			// $this->load->view('common/footer');
			$data['recent_properties_listing'] = $model_Obj->details_model->rc_get_recent_properties();
			$data['feautred_listings'] = $model_Obj->properties_model->rc_get_featured_listings();
			$_SESSION['details_view_data'] = $data;
			$_template_file = ABSPATH.'wp-content/plugins/RealConnect/views/main/details_view.php';
			load_template( $_template_file);
			
		} else {
			//get queries to prefill area and subdivision in the view
			$areas_autofill = $model_Obj->shared_model->get_flex_areas();
			$subdivision_autofill = $model_Obj->shared_model->get_flex_subdivisions();
			$data['areas_autofill'] = $areas_autofill;
			$data['subdivision_autofill'] = $subdivision_autofill;
			//get queries to prefill school in the view
			$elem_autofill = $model_Obj->shared_model->get_flex_elem_schools();
			$middle_autofill = $model_Obj->shared_model->get_flex_middle_schools();
			$high_autofill = $model_Obj->shared_model->get_flex_high_schools();
			$data['elem_autofill'] = $elem_autofill;
			$data['middle_autofill'] = $middle_autofill;
			$data['high_autofill'] = $high_autofill;
			//get residential subcategry info
			$residential_autofill = $model_Obj->shared_model->get_flex_res_subcategory();
			$data['res_autofill'] = $residential_autofill;
			//get commercial subcategory info
			$commercial_autofill = $model_Obj->shared_model->get_flex_comm_subcategory();
			$data['comm_autofill'] = $commercial_autofill;
			//get land subcategory info
			$land_autofill = $model_Obj->shared_model->get_flex_land_subcategory();
			$data['land_autofill'] = $land_autofill;
			//load views
			//$this->load->view('common/header', $headdata);
			//$this->load->view('main/flexdetails_view', $data);
			//$this->load->view('common/footer');
			
			$_SESSION['flexdetails_view_data'] = $data;
			$_template_file = ABSPATH.'wp-content/plugins/RealConnect/views/main/flexdetails_view.php';
			load_template( $_template_file);
		}
		
	}
	
 function getBrandedVideoUrl($mls_id)
	{
		$BrandedVideoUrl="";
		try{
			$config = new \PHRETS\Configuration;
			$config->setLoginUrl('http://retsgw.flexmls.com/rets2_3/Login')
					->setUsername('lbr.rets.brownj')
					->setPassword('rykes-stasis57');
					//->setRetsVersion('1.7.2');
			//GetObject?Resource=Property&Type=BrandedVideo&ID=130578:*&Location=0

			$rets = new \PHRETS\Session($config);
			$connect = $rets->Login();
			//131664/
			//'130578'
			$objects = $rets->GetObject('Property', 'BrandedVideo', $mls_id, '*', 0);
			$BrandedVideoUrl="";
			if($objects[0]->getContentType()=="text/html")
			{
				$xml=$objects[0]->getContent();
				$doc = new DOMDocument();
				$doc->loadHTML($xml);
				$tags = $doc->getElementsByTagName('iframe');
				if($tags)
				{
					$BrandedVideoUrl=$tags->item(0)->getAttribute('src');
				}
			}
		}catch(Exception $e){
		
		}
		return $BrandedVideoUrl;
	}
	
