Skip to content

Instantly share code, notes, and snippets.

@arpitr
Created March 23, 2012 23:21
Show Gist options
  • Save arpitr/2176277 to your computer and use it in GitHub Desktop.
Save arpitr/2176277 to your computer and use it in GitHub Desktop.
<?php
/*
* Doubts
* 1.there is no more need of lookupListing as we are having standardizeListingFormat()
* 2.Will the implementation of applyListingDataToLocalRecord() will go like this..
* 3.staticfunction of a class can access non static class variables?....
* 4.Need documentation for getListingData()
*/
// Including citygrid API
include "citygrid-places-api-class.php";
/*
* class ExternalLisitngService with methods to call citysearch API,update nodes on the basis of data
* from listing site,to count reviews from users,return all reviews form users
*
*/
class ExternalListingService {
private $node;
public function __construct($node) {
$this->node = $node;
}
public function determineEmptyNodeFields() {
$empty_fields = array();
$listing = BusinessListing::fromNode($node);
if (empty($listing->locationStreet)) {
$empty_fields[] = 'locationStreet';
}
// check other fields here
return $empty_fields;
}
//method to get data from different listing sites API
public function lookupListing();
//method to count all reviews from users to a business firm
public function getReviewCount();
//method to return the direct URL to view the listing on the external website
public function getListingURL();
//return all reviews from users in a common format that is Review class object type
public function getAllReviews();
//????? to ask about documentation ????????
public function getListingData();
//method to post data from node to external listing sites
public function postListing();
//method to post reviews from nodes to external listing sites
public function postReview();
//method to update the nodes field on the basis of data from listing sites
public function applyListingDataToLocalRecord() {
$comparison = $this->compareListingDataToLocalRecord();
$listing = new BusinessListing();
foreach($comparison as $element){
if($element[comparison] == 'empty_in_node'){
$listing->element[field_name] = $element[webservice_value];
}
}
$listing->saveToNode();
/* if (empty($listing->contactPhone)) {
$listing->contactPhone = $listingData->contactPhone;
}
if (empty($listing->companyName)) {
$listing->companyName = $listingData->companyName;
}
if (empty($listing->locationName)) {
$listing->locationName = $listingData->companyName;
}
if (empty($listing->locationStreet)) {
$listing->locationStreet = $listingData->locationStreet;
}
if (empty($listing->locationCity)) {
$listing->locationCity = $listingData->locationCity;
}
if (empty($listing->locationState)) {
$listing->locationState = $listingData->locationState;
}
if (empty($listing->locationPostalCode)) {
$listing->locationPostalCode = $listingData->locationPostalCode;
}
if (empty($listing->locationLatitude)) {
$listing->locationLatitude = $listingData->locationLatitude;
}
if (empty($listing->locationLongitude)) {
$listing->locationLongitude = $listingData->locationLongitude;
}
if (empty($listing->locationPhone)) {
$listing->locationPhone = $listingData->contactPhone;
}
if (empty($listing->contactUrl)) {
$listing->contactUrl = $listingData->contactUrl;
}
if (empty($listing->hoursOfOperation)) {
$listing->hoursOfOperation = $listingData->hoursOfOperation;
}*/
//noad_save($this->node);
}
}
/*
* Class citysearch which extends ExternalListingService class and implement its method
* so to update client listing node on the basis of citysearch listing website
*
*/
/* lookupListing a base class method implemented to call api call and get data from it and
* then calling applyListingDataToLocalRecord() passing citysearch listing site data
* as argument.
*/
class CitySearch extends ExternalListingService {
public $search;
public $searchWhere;
public function lookupListing() {
$lang = LANGUAGE_NONE;
$phone = $node->field_phone[$lang][0]['value'];
$publishercode = "test";
$customer_only = null;
$placement = null;
$all_results = null;
$review_count = null;
$i = null;
$format = 'json';
$callback = null;
$id_type = 'cg';
//Get Place Detail
$citygrid = new citygrid($publishercode);
$this->search = $citygrid->places_detail($id, $id_type, $phone, $customer_only, $all_results, $review_count, $placement, $format, $callback, $i);
if ($this->search) {
applyListingDataToLocalRecord($this->search);
}
$what = $node->title;
$type = null;
$where = str_replace('', '+', $node->location['street']) . ',' . $node->location['city'] . ',' . $node->location['state'];
$format = 'json';
$placement = null;
$has_offers = false;
$histograms = false;
$i = null;
$citygrid = new citygrid($publishercode);
$this->searchWhere = $citygrid->srch_places_where($what, $type, $where, $page, $rpp, $sort, $format, $placement, $has_offers, $i);
}
}
/*
*
* a common format i.e of type class BusinessListing object so that method
* applyListingDataToLocalRecord() can get a common format data from all listing site
*/
public function standardizeListingFormat() {
$listing = new BusinessListing();
if ($this->search) {
$this->listing->companyName = $this->search->locations->name;
$this->listing->contactPhone = $this->search->locations->contact_info->display_phone;
$this->listing->contactUrl = $this->search->locations->contact_info->display_url;
$this->listing->hoursOfOperation = $this->search->locations->business_hours;
$this->listing->locationCity = $this->search->locations->address->city;
$this->listing->locationLatitude = $this->search->locations->address->latitude;
$this->listing->locationLongitude = $this->search->locations->address->longitude;
$this->listing->locationPostalCode = $this->search->locations->address->postal_code;
$this->listing->locationState = $this->search->locations->address->state;
$this->listing->locationStreet = $this->$search->locations->address->street;
$this->listing->locationPhone = $this->search->locations->contact_info->display_phone;
$this->listing->locationName = $this->search->locations->name;
}
if ($this->searchWhere) {
$this->listing->companyName = $this->searchWhere->locations->name;
$this->listing->contactPhone = $this->searchWhere->locations->phone_number;
$this->listing->contactUrl = $this->searchWhere->locations->website;
$this->listing->locationCity = $this->searchWhere->locations->address->city;
$this->listing->locationLatitude = $this->searchWhere->locations->latitude;
$this->listing->locationLongitude = $this->searchWhere->locations->longitude;
$this->listing->locationPostalCode = $this->searchWhere->locations->address->postal_code;
$this->listing->locationState = $this->searchWhere->locations->address->state;
$this->listing->locationStreet = $this->searchWhere->locations->address->street;
$this->listing->faxNumber = $this->searchWhere->locations->fax_number;
$this->listing->locationPhone = $this->searchWhere->locations->phone_number;
$this->listing->locationName = $this->searchWhere->locations->name;
}
return $this->listing;
}
/*
* method to compare data between nodes fields and data from listing site
*
*
*/
public function compareListingDataToLocalRecord() {
$node_listing = BusinessListing::fromNode($this->node);
$fields_to_compare = BusinessListing::fieldNames();
$comparison = array();
foreach ($fields_to_compare as $field_name) {
//doubt from where are we gettin object of BusinessListing class here
$ws_value = $this->listing->{$field_name};
$node_value = $node_listing->listing->{$field_name};
if (empty($ws_value) && empty($node_value)) {
$comparison = 'empty_in_both';
} elseif (!empty($ws_value) && empty($node_value)) {
$comparison = 'empty_in_node';
} else {
if ($node_value == $ws_value) {
$comparison = 'equal';
} else {
$comparison = 'different';
}
}
$comparison[$field_name] = array(
'webservice_value' => $ws_value,
'node_value' => $node_value,
'comparison' => $comparison,
);
return $comparison;
}
}
/*
* a base class method implemented to return the business profile url on citysearch website
*/
public function getListingURL() {
$listingUrl;
if ($this->search)
$listingUrl = $this->search->urls->profile_url;
elseif ($this->searchWhere) {
$listingUrl = $this->searchWhere->locations->profile;
}
return $listingUrl;
}
/* getAllReviews a base class method implemented to return all reviews for a business firm
* from all users and return them in a common format i.e. of type class Review object.
*/
public function getAllReviews() {
$review = new Review();
foreach ($this->search->review_info->reviews->review as $reviewSearch) {
$review->reviewerName[] = $reviewSearch->review_author;
$review->rating[] = $reviewSearch->review_rating;
$review->reviewText[] = $reviewSearch->review_text;
$review->subject[] = $reviewSearch->review_title;
$review->reviewURL[] = $reviewSearch->review_url;
return review;
}
}
/*
* getReviewCount()a base class method implemented to count all reviews for a business firm
* on Citysearch listing site
*/
public function getReviewCount() {
if ($this->searchWhere) {
$reviewCount = $this->searchWhere->locations->user_reviews_count;
} elseif ($this->search) {
$reviewCount = sizeof($this->search->review_info->reviews->review);
}
return $reviewCount;
}
}
/*
* Class review is to get all reviews in a common format i.e object type of this class
* An instance of this class has been created in the method getAllReviews() to return
* reviews in the common format.
*
*/
class Review {
public $revireviewerName;
public $rating;
public $reviewText;
public $reviewURL;
public $subject;
}
/*
* BusinessListing class is there to ensure common data format for data from all
* listing site APIS
*
*/
class BusinessListing {
public $nid;
public $companyName;
public $locationStreet;
public $locationCity;
public $locationState;
public $locationPostalCode;
public $locationLatitude;
public $locationLongitude;
public $contactPhone;
public $contactUrl;
public $hoursOfOperation;
public $photos = array();
public $faxNumber;
public $locationName;
public $locationPhone;
public static function fieldNames() {
return array(
'nid',
'companyName',
'locationStreet',
'locationCity',
'locationState',
'locationPostalCode',
'locationLatitude',
'locationLongitude',
'contactPhone',
'hoursOfOperation',
'faxNumber',
'locationName',
'locationPhone'
);
}
/*
* method to will fetch the data from nodes
* will be needed to post our node data to listing sites...
* !!!!!working on it!!!!
*/
public static function fromNode($node) {
$listing = new BusinessListing();
$listing->nid = $node->nid;
$listing->companyName = $node->title;
$listing->locationStreet = $node->field_location[$lang][0]['street'];
$listing->locationCity = $node->field_location[$lang][0]['city'];
$listing->locationState = $node->field_location[$lang][0]['province'];
$listing->locationPostalCode = $node->field_location[$lang][0]['postal_code'];
$listing->locationLatitude = $node->field_location[$lang][0]['latitude'];
$listing->locationLongitude = $node->field_location[$lang][0]['longitude'];
$listing->contactPhone = $node->field_phone[$lang][0]['value'];
$listing->hoursOfOperation = $node->field_hours[$lang][0]['value'];
$listing->faxNumber = $node->field_fax[$lang][0]['value'];
$listing->locationName = $node->field_location[$lang][0]['name'];
$listing->locationPhone = $node->field_location[$lang][0]['phone'];
$listing->companyWebsite = $node->field_website_url[$lang][0]['url'];
return $listing;
}
public function saveToNode() {
$node->title = $listing->companyName;
$node->field_location[$lang][0]['street'] = $this->locationStreet;
$node->field_location[$lang][0]['city'] = $this->locationCity;
$node->field_location[$lang][0]['province'] = $this->locationState;
$node->field_location[$lang][0]['postal_code'] = $this->locationPostalCode;
$node->field_location[$lang][0]['latitude'] = $this->locationLatitude;
$node->field_phone[$lang][0]['value'] = $this->contactPhone;
$node->field_hours[$lang][0]['value'] = $this->hoursOfOperation;
$node->field_fax[$lang][0]['value'] = $this->faxNumber;
$node->field_location[$lang][0]['name'] = $this->locationName;
$node->field_location[$lang][0]['phone'] = $this->locationPhone;
$node->field_website_url[$lang][0]['url'] = $this->companyWebsite;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment