Drupal: Import Vebra Property using Vebra API

This CodeLet allows you programatically import property to content type:

1. Install module.
2. Create content type with the fields to which you want to import.
3. you have to update KEY/PASSWORD for Vebra API and URL of property on Vebra.

Module Info

name = Custom Vebra Property Import
description = Custom feature for import vebra property to node type
core = 7.x
package = custom

CodeLet: 


<?php
/**
* Implementation of hook_menu
*
* @author saru1683
*/
function vebra_import_menu() {
$item = array();

$item['vebra_import_property/%'] = array(
'type' => MENU_CALLBACK,
'page callback' => '_get_vebra_property_detail',
'page arguments' => array(1),
'access callback' => TRUE,
);

return $item;
}

/**
* menu callback
*/
function _get_vebra_property_detail($arg = NULL) {

//ADD VEBRA PROPERTY URL FOR IMPORT TO NODE

$url = 'http://webservices.vebra.com/export/APIKEY/v4/branch/BRANCHID/property/PROPERTYID';
$property_detail_xml = _get_response($url, false);
if($property_detail_xml) {
if(_xml_vebra_import_process($property_detail_xml)) {
//watchlog true;
}else {
//watchlog false;
}
}
drupal_goto('');
}

/**
* property import process
*/
function _xml_vebra_import_process($xml) {
$om__result = new StdClass();

$p = xml_parser_create();
xml_parse_into_struct($p, $xml, $vals, $index);
xml_parser_free($p);

$ar__index = $index;
$ar__value = $vals;
$om__result->field_property_id = isset($vals[0]['attributes']['ID']) ? $vals[0]['attributes']['ID'] : 0;
$om__result->field_rent_pm = isset($ar__value[$ar__index['PRICE'][0]]['value']) ? $ar__value[$ar__index['PRICE'][0]]['value'] : '';
$om__result->field_property_type = isset($ar__value[$ar__index['TYPE'][0]]['value']) ? $ar__value[$ar__index['TYPE'][0]]['value'] : '';

$ss__field_property_images = NULL;
// allow six files
$sn__property_file_count = isset($ar__index['FILE']) ? count($ar__index['FILE']) : 0;
if($sn__property_file_count > 10) {
$sn__property_file_count = 10;
}

//
for($i = 0; $i < $sn__property_file_count; $i++) {
$attr = isset($ar__value[$ar__index['FILE'][$i]]['attributes']) ? $ar__value[$ar__index['FILE'][$i]]['attributes'] : array();

if(!isset($attr) || !isset($attr['TYPE']) || $attr['TYPE'] != 9) {
$sn__url_index = isset($ar__index['URL'][$i]) ? $ar__index['URL'][$i] : '';
$sn__value_index = isset($sn__url_index) && isset($ar__value[$sn__url_index]['value']) ? $ar__value[$sn__url_index]['value'] : '';
if($sn__value_index != '') {
if (strpos($ar__value[$ar__index['URL'][$i]]['value'],'home/hip/') === false) {//check is not energy certificate
$ss__field_property_images .= $ar__value[$ar__index['URL'][$i]]['value'] . ', ';
}
}
}
}

$om__result->field_property_images = $ss__field_property_images;

$om__result->field_reference = isset($ar__value[$ar__index['AGENTS'][0]]['value']) ? $ar__value[$ar__index['AGENTS'][0]]['value'] : '';
$om__result->field_bedrooms = isset($ar__value[$ar__index['BEDROOMS'][0]]['value']) ? $ar__value[$ar__index['BEDROOMS'][0]]['value'] : '';
$om__result->field_description = isset($ar__value[$ar__index['DESCRIPTION'][0]]['value']) ? addslashes($ar__value[$ar__index['DESCRIPTION'][0]]['value']) : '';
$om__result->field_lat_long_lat = isset($ar__value[$ar__index['LATITUDE'][0]]['value']) ? $ar__value[$ar__index['LATITUDE'][0]]['value'] : '';
$om__result->field_lat_long_lon = isset($ar__value[$ar__index['LONGITUDE'][0]]['value']) ? $ar__value[$ar__index['LONGITUDE'][0]]['value'] : '';
$om__result->field_area = isset($ar__value[$ar__index['AREA'][0]]['value']) ? trim($ar__value[$ar__index['AREA'][0]]['value']) : '';
if(isset($ar__index['BULLET']) && !empty($ar__index['BULLET'])) {
$attr = isset($ar__value[$ar__index['BULLET'][0]]['attributes']) ? $ar__value[$ar__index['BULLET'][0]]['attributes'] : '';
if($attr && $attr['ID'] == 1)
$om__result->field_area = isset($ar__value[$ar__index['BULLET'][0]]['value']) ? $ar__value[$ar__index['BULLET'][0]]['value'] : $ss__field_area;
}

$ss__field_energy_certificate = NULL;
$ss__certificate = '';
$ar__index['FILE'] = isset($ar__index['FILE']) ? $ar__index['FILE'] : array();
for($i = 0; $i < count($ar__index['FILE']); $i++) {
$attr = isset($ar__value[$ar__index['FILE'][$i]]['attributes']) ? $ar__value[$ar__index['FILE'][$i]]['attributes'] : '';
if($attr && $attr['TYPE'] == 9) {
$ar__index_certificate = $ar__index['FILE'][$i] + 2;
$ss__certificate .= (trim($ar__value[$ar__index_certificate]['value']) ? trim($ar__value[$ar__index_certificate]['value']) : trim($ar__value[$ar__index_certificate+1]['value'])) . ',';
}
}
$om__result->field_energy_certificate = $ss__certificate;
$om__result->field_data_available = isset($ar__value[$ar__index['AVAILABLE'][0]]['value']) ? $ar__value[$ar__index['AVAILABLE'][0]]['value'] : '';
$om__result->field_facilities2 = NULL;

$ar__index['BULLET'] = isset($ar__index['BULLET']) ? $ar__index['BULLET'] : array();
for($i = 0; $i < count($ar__index['BULLET']); $i++) {
$ss__facility = isset($ar__value[$ar__index['BULLET'][$i]]['value']) ? $ar__value[$ar__index['BULLET'][$i]]['value'] : '' ;
$ss__field_facilities2 .= $ss__facility . '#|#';
}
$om__result->field_facilities2 = $ss__field_facilities2;

$om__result->field_city_or_town = isset($ar__value[$ar__index['TOWN'][0]]['value']) ? $ar__value[$ar__index['TOWN'][0]]['value'] : '';
$om__result->field_postcodeprop = isset($ar__value[$ar__index['STREET'][0]]['value']) ? $ar__value[$ar__index['STREET'][0]]['value'] : '';
$om__result->field_street = isset($ar__value[$ar__index['STREET'][0]]['value']) ? $ar__value[$ar__index['STREET'][0]]['value'] : '';
$om__result->field_property_name_or_number = isset($ar__value[$ar__index['NAME'][0]]['value']) ? $ar__value[$ar__index['NAME'][0]]['value'] : '';
$om__result->field_locality = isset($ar__value[$ar__index['LOCALITY'][0]]['value']) ? $ar__value[$ar__index['LOCALITY'][0]]['value'] : '';

$ss__field_map_from_address = NULL;

$ss_display = isset($ar__value[$ar__index['DISPLAY'][0]]['value']) ? $ar__value[$ar__index['DISPLAY'][0]]['value'] : '';

$om__result->title = $ss_title = $ss_display;
$om__result->field_display_title = $ss_display;

//Create node
$node = new StdClass();
$node->type = 'import';//content type
$node->uid = 1;
$node->title = $om__result->title;
$node->status = 1;
$node->language = 'und';
//price
$node->field_rent_pm['und']['0']['value'] = $om__result->field_rent_pm;

//property type
$node->field_property_type['und'][0]['value'] = $om__result->field_property_type;

$ss__destination = file_build_uri('csv_import_property');
$ss__pro_images = explode(',', $om__result->field_property_images);

if(count($ss__pro_images)) {

for($i = 0; $i < count($ss__pro_images); $i++) {
$file_url = trim($ss__pro_images[$i]);
if($file_url != '') {
$file = _save_property_images($file_url, 1,'properties');

$node->field_property_images['und'][$i] = (array)$file;
}
}
}

//reference
$node->field_reference['und']['0']['value'] = $om__result->field_reference;
//field_bedrooms
$node->field_bedrooms['und']['0']['value'] = $om__result->field_bedrooms;
//field_description
$node->field_description['und']['0']['value'] = $om__result->field_description;
//field_lat_long:lat
$node->field_lat_long['und'][0] = array(
'lat' => $om__result->field_lat_long_lat,
'lon' => $om__result->field_lat_long_lon,
);

//field_area
$node->field_area['und']['0']['value'] = $om__result->field_area;
//field_energy_certificate

$ss__certificate_images = explode(',', $om__result->field_energy_certificate);
if(count($ss__certificate_images)) {
//if (file_prepare_directory($ss__destination, FILE_CREATE_DIRECTORY)) {

for($i = 0; $i < count($ss__certificate_images); $i++) {
$file_url = trim($ss__certificate_images[$i]);

if($file_url != '') {
$file = _save_property_images($file_url);
$node->field_energy_certificate['und'][$i] = (array)$file;
}
}
//}
}
//field_data_available
$arr__data_available = explode('/',$om__result->field_data_available);
$sn__yy = $arr__data_available[2];
$sn__mm = $arr__data_available[1];
$sn__dd = $arr__data_available[0];
$node->field_data_available['und']['0']['value'] = $sn__yy . '-' . $sn__mm . '-' . $sn__dd .' 00:00:00';//str_replace('/', '-', $om__result->field_data_available);//date('Y-m-d H:i:s');
//field_facilities2
$ss__facilities = explode('#|#', $om__result->field_facilities2);
$jj = 0;
for($ii = 0; $ii < count($ss__facilities); $ii++) {
if(trim($ss__facilities[$ii])) {
$node->field_facilities2['und'][$jj]['value'] = $ss__facilities[$ii];
$jj++;
}
}
//field_city_or_town
$node->field_city_or_town['und']['0']['value'] = $om__result->field_city_or_town;
//field_postcodeprop
$node->field_postcodeprop['und']['0']['value'] = $om__result->field_postcodeprop;
//field_street

$node->field_street['und']['0']['value'] = $om__result->field_street;

//field_city_or_town
$node->field_property_name_or_number['und']['0']['value'] = $om__result->field_property_name_or_number;

$node->field_locality['und']['0']['value'] = $om__result->field_locality;
//field_map_from_address field not available on property listing content type

//field_display_title
$node->field_display_title['und']['0']['value'] = $om__result->field_display_title;
$node->field_property_id['und'][0]['value'] = $om__result->field_property_id;

// update existing property

$query = db_select('field_data_field_property_id', 'p');
$query->join('node', 'n', 'p.entity_id = n.nid');
$query->fields('n', array('nid'))
->condition('p.field_property_id_value', $om__result->field_property_id, '=')
->condition('p.bundle', 'property_listing', '=');
$result = $query->execute();

if($result->rowCount()) {
$record = $result->fetchAssoc();
if(!empty($record['nid']))
$node->nid = $record['nid'];
}
// end of update existing

node_submit($node);

$nid = node_save($node);
if($node->nid) {
drupal_set_message(t('@node has been created', array('@node' => $node->title)));
}else {
drupal_set_message('DB:Error ', 'error');
}

unset($node, $om__result, $r__result);

return true;
}

function _get_response($url = '', $request_token = false){
if($url == '')
return false;

$ss__token = variable_get('csv_import_token',0);

$username = 'XXXXXXXXXX';
$password = 'XXXXXXXXXX';

$ch = curl_init();
if(isset($_SESSION['VAR']) && $_SESSION['VAR'] > 3 ) {

drupal_set_message('We are failed to fetch Token please try after some times', 'error');
unset($_SESSION['VAR']);
watchdog('import', 'failed to fetch Token ',array(), WATCHDOG_NOTICE);
//drupal_goto('');
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);

if($request_token == false ) {
watchdog('import property', 'Authorize using old token',array(), WATCHDOG_NOTICE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic '.base64_encode($ss__token) )); // I found this to be the only way of consistently authorising between user-password combo and using a token
}
else {
watchdog('csv_import', 'Authorize using User:Pass',array(), WATCHDOG_NOTICE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic '.base64_encode("$username:$password") )); // I found this to be the only way of consistently authorising between user-password combo and using a token
}

$response = curl_exec($ch);
list($headers, $body) = explode("\r\n\r\n", $response);
$headers = nl2br($headers);
$headers = explode('', $headers);
$status = explode(' ', $headers[0]);
$status = $status[1];
if($status == '200'){
foreach($headers as $header) {
$components = explode(': ', trim($header));
$headers[$components[0]] = isset($components[1]) ? $components[1] : '';
}
$ss__token = isset($headers['Token']) ? $headers['Token'] : variable_get('csv_import_token', 0);
if(isset($headers['Token'])){
variable_set('csv_import_token', $ss__token);
watchdog('import', 'Token updated', array(), WATCHDOG_NOTICE);
}
return $body;
}
else if($status == '401') {
$_SESSION['VAR'] = isset($_SESSION['VAR']) ? ($_SESSION['VAR'] + 1) : 1;
return _get_response($url, true);
drupal_set_message('Unauthorized');
}
else if($status == '204') {
drupal_set_message('Import Property: Provided branch has no property list');
}
else if($status == '403') {
drupal_set_message('Import Property: Forbidden The access property that is not available to the client');
}
else if($status == '404') {
drupal_set_message('Import Property: incorrect URL');
}
else if($status == '410') {
drupal_set_message('Import Property: The property/branch has been deleted OR is not currently available');
}
else if($status == '500') {
drupal_set_message('Import Property: Something bad happened to the API');
}
else if($status == '501') {
drupal_set_message('Import Property: The API version in the request is not supported');
}
else
drupal_set_message('Import Property: Property Import fail');
}

By saru1683 on Sat, 15/03/2014 - 11:44am

Add comment

Log in or register to post comments
QR code for this page URL This page URL
Feedback