Created
April 20, 2017 15:37
-
-
Save asanikovich/420e4b306eccd071c7529f3c68660b7a to your computer and use it in GitHub Desktop.
RETS implementation for CRMLS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Lib; | |
/** | |
* This class is customized specifically for the CRMLS | |
* ( California Regional Multiple Listing Service ) | |
*/ | |
class rets extends phRETS { | |
public $rets_url = 'http://rets.mrmlsmatrix.com/rets/login.ashx'; | |
public $rets_user = 'XXXXXX'; | |
public $rets_pass = 'xXxXxXx'; | |
public $broker_code = 'XXX000'; | |
private $rets = NULL; | |
private $connected = FALSE; | |
private $listing_types = array( | |
'residential' => 'listing_mrmls_resi', | |
'residential_income' => 'listing_mrmls_rinc', | |
'rentals' => 'listing_mrmls_rlse', | |
'commercial' => 'listing_mrmls_comm', | |
'lots_land' => 'listing_mrmls_land', | |
'mobile_homes' => 'listing_mrmls_mobhomes' | |
); | |
// ----------------------------------------------------------------------- | |
/** | |
* Get all rentals | |
* | |
* @param string The RETS table to check. | |
* @param bool If images should be returned with the listings data, the image type. | |
* Any images returned with listings data are base64 encoded. | |
* @param string The fields to select, can be "*" or comma delimited string. | |
* No whitespace is allowed in the string. | |
*/ | |
public function get_listings( $listing_type, $image_size = 'Thumbnail', $select = NULL ) | |
{ | |
$results = NULL; | |
if( $this->_connect() ) | |
{ | |
$search = $this->SearchQuery( | |
'Property', | |
$this->listing_types[ $listing_type ], | |
// Just this broker's active rentals | |
'((LO_Code=' . $this->broker_code . '),(Status=A))', | |
array( | |
'Select' => ( is_null( $select ) OR $select == 'default' ) ? implode(',', array( | |
'BathsTotal', | |
'Bedrooms', | |
'City', | |
'Cooling', | |
'County', | |
'DateListingContract', | |
'Fireplace', | |
'GarageAttached', | |
'Heating', | |
'LA_FirstName', | |
'LA_LastName', | |
'Laundry', | |
'ListPrice', | |
'Matrix_Unique_ID', | |
'MLnumber', | |
'Parking', | |
'PetsAllowed', | |
'Pool', | |
'PropertySubType', | |
'Spa', | |
'SquareFootageStructure', | |
'State', | |
'Status', | |
'StreetName', | |
'StreetNumber', | |
'StreetSuffix', | |
'TimestampModified', | |
'YearBuilt', | |
'ZipCode' | |
)) : $select | |
) | |
); | |
// If search returned results | |
if( $this->TotalRecordsFound() > 0 ) | |
{ | |
while( $data = $this->FetchRow( $search ) ) | |
{ | |
$temp = $data; | |
if( $image_size ) | |
{ | |
// Photo type can be LargePhoto, Photo, or Thumbnail | |
$photos = $this->get_images( $data['Matrix_Unique_ID'], $image_size, 0 ); | |
if( ! empty( $photos ) && is_array( $photos ) ) | |
{ | |
$temp['photo_urls'] = $this->process_photos( $photos, $image_size, $data['MLnumber'] ); | |
} | |
} | |
$results[] = $temp; | |
} | |
} | |
} | |
return $results; | |
} | |
// ----------------------------------------------------------------------- | |
/** | |
* Get all rentals | |
* | |
* @param string The RETS table to check. | |
* @param bool If images should be returned with the listings data, the image type. | |
* Any images returned with listings data are base64 encoded. | |
* @param string The fields to select, can be "*" or comma delimited string. | |
* No whitespace is allowed in the string. | |
*/ | |
public function get_listing( $listing_type, $image_size = 'Thumbnail', $select = NULL, $MLnumber ) | |
{ | |
$result = NULL; | |
if( $this->_connect() ) | |
{ | |
$search = $this->SearchQuery( | |
'Property', | |
$this->listing_types[ $listing_type ], | |
// Just this broker's active rentals | |
'((MLnumber=' . $MLnumber . '))', | |
array( | |
'Select' => ( is_null( $select ) OR $select == 'default' ) ? '*' : $select | |
) | |
); | |
// If search returned results | |
if( $this->TotalRecordsFound() == 1 ) | |
{ | |
while( $data = $this->FetchRow( $search ) ) | |
{ | |
$temp = $data; | |
if( $image_size ) | |
{ | |
// Photo type can be LargePhoto, Photo, or Thumbnail | |
$photos = $this->get_images( $data['Matrix_Unique_ID'], $image_size ); | |
if( ! empty( $photos ) && is_array( $photos ) ) | |
{ | |
$temp['photo_urls'] = $this->process_photos( $photos, $image_size, $data['MLnumber'] ); | |
} | |
} | |
$result = $temp; | |
} | |
} | |
} | |
return $result; | |
} | |
// ----------------------------------------------------------------------- | |
/** | |
* Get all or any images for a listing | |
* | |
* @params string The matrix unique ID from the listing data | |
*/ | |
public function get_images( $matrix_unique_id, $size = 'Thumbnail', $photo_numbers = '*' ) | |
{ | |
if( $this->_connect() ) | |
{ | |
return $this->GetObject( 'media', $size, $matrix_unique_id, $photo_numbers ); | |
} | |
return NULL; | |
} | |
// ----------------------------------------------------------------------- | |
/** | |
* Store the images on the filesystem, because using data URIs sucks | |
*/ | |
public function process_photos( $photos, $image_size, $MLnumber ) | |
{ | |
$temp = []; | |
foreach( $photos as $photo ) | |
{ | |
$img_extension = NULL; | |
switch( $photo['Content-Type'] ) | |
{ | |
case 'image/jpeg': | |
$img_extension = '.jpg'; | |
break; | |
case 'image/gif': | |
$img_extension = '.gif'; | |
break; | |
case 'image/png': | |
$img_extension = '.png'; | |
break; | |
} | |
if( ! is_null( $img_extension ) ) | |
{ | |
// Allow images to be saved to the filesystem | |
$dir = 'img/listings/' . $MLnumber . '/' . $image_size; | |
$photo_url = $dir . '/' . md5( $photo['Data'] ) . $img_extension; | |
$temp['/' . $photo_url] = 'NULL'; | |
// Save the file if it doesn't already exist | |
$file_path = FCPATH . $photo_url; | |
if( ! @is_file( $file_path ) ) | |
{ | |
// Make the dir if necessary | |
if( ! is_dir( $dir ) ) | |
{ | |
mkdir( $dir, 0777, TRUE ); | |
} | |
file_put_contents( $file_path, $photo['Data'] ); | |
} | |
} | |
} | |
return $temp; | |
} | |
// ----------------------------------------------------------------------- | |
/** | |
* Show the resources and classes from the CRMLS | |
*/ | |
public function show_resources_and_classes() | |
{ | |
if( $this->_connect() ) | |
{ | |
return $this->GetMetadataTypes(); | |
} | |
return NULL; | |
} | |
// ----------------------------------------------------------------------- | |
/** | |
* Get the fields in a CRMLS class | |
*/ | |
public function show_class_fields( $resource_name, $class_name ) | |
{ | |
if( $this->_connect() ) | |
{ | |
return $this->GetMetadataTable( $resource_name, $class_name ); | |
} | |
return NULL; | |
} | |
// ----------------------------------------------------------------------- | |
/** | |
* Show the values that are acceptable to search a specific field by | |
*/ | |
public function show_accepted_search_values( $resource_name, $field_name ) | |
{ | |
if( $this->_connect() ) | |
{ | |
return $this->GetLookupValues( $resource_name, $field_name ); | |
} | |
return NULL; | |
} | |
// ----------------------------------------------------------------------- | |
/** | |
* Connect to CRMLS RETS | |
*/ | |
private function _connect() | |
{ | |
if( ! $this->connected ) | |
{ | |
$this->connected = $this->Connect( | |
$this->rets_url, | |
$this->rets_user, | |
$this->rets_pass | |
); | |
} | |
return $this->connected; | |
} | |
/** | |
* Kill the connection | |
*/ | |
public function __destruct() | |
{ | |
if( $this->connected ) | |
{ | |
$this->Disconnect(); | |
} | |
} | |
// ----------------------------------------------------------------------- | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment