Skip to content

Instantly share code, notes, and snippets.

@arpitr
Created March 22, 2012 14:50
Show Gist options
  • Save arpitr/2158760 to your computer and use it in GitHub Desktop.
Save arpitr/2158760 to your computer and use it in GitHub Desktop.
<?php
// 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;
}
//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($search) {
$lang = LANGUAGE_NONE;
if (!$this->node->field_phone[$lang][0]['value']) {
$this->node->field_phone[$lang][0]['value'] = $listing->contactPhone;
}
if (!$this->node->field_location[$lang][0]['name']) {
$this->node->field_location[$lang][0]['name'] = $listing->companyName;
}
if (!$this->node->field_location[$lang][0]['street']) {
$this->node->field_location[$lang][0]['street'] = $listing->locationStreet;
}
if (!$this->node->field_location[$lang][0]['city']) {
$this->node->field_location[$lang][0]['city'] = $listing->locationCity;
}
if (!$this->node->field_location[$lang][0]['province']) {
$this->node->field_location[$lang][0]['province'] = $listing->locationState;
}
if (!$this->node->field_location[$lang][0]['postal_code']) {
$this->node->field_location[$lang][0]['postal_code'] = $listing->locationPostalCode;
}
if (!$this->node->field_location[$lang][0]['latitude']) {
$this->node->field_location[$lang][0]['latitude'] = $listing->locationLatitude;
}
if (!$this->node->field_location[$lang][0]['longitude']) {
$this->node->field_location[$lang][0]['longitude'] = $listing->locationLongitude;
}
if (!$this->node->field_location[$lang][0]['phone']) {
$this->node->field_location[$lang][0]['phone'] = $listing->contactPhone;
}
if (!$this->node->field_website_url[$lang][0]['url']) {
$this->node->field_website_url[$lang][0]['url'] = $listing->contactUrl;
}
if (!$this->node->field_hours[$lang][0]['value']) {
$this->node->field_hours[$lang][0]['value'] = $listing->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
*
*/
class CitySearch extends ExternalListingService {
public $search;
public $searchWhere;
/*
* method to call Citysearch API and get data from it and then convert it into
* 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();
$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);
//var_dump($search);
if ($this->search) {
$listing->companyName = $this->search->locations->name;
$listing->contactPhone = $this->search->locations->contact_info->display_phone;
$listing->contactUrl = $this->search->locations->contact_info->display_url;
$listing->hoursOfOperation = $this->search->locations->business_hours;
$listing->locationCity = $this->search->locations->address->city;
$listing->locationLatitude = $this->search->locations->address->latitude;
$listing->locationLongitude = $this->search->locations->address->longitude;
$listing->locationPostalCode = $this->search->locations->address->postal_code;
$listing->locationState = $this->search->locations->address->state;
$listing->locationStreet = $this->$search->locations->address->street;
}
$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);
$searchWhere = $citygrid->srch_places_where($what, $type, $where, $page, $rpp, $sort, $format, $placement, $has_offers, $i);
if ($searchWhere) {
$listing->companyName = $this->searchWhere->locations->name;
$listing->contactPhone = $this->searchWhere->locations->phone_number;
$listing->contactUrl = $this->searchWhere->locations->website;
$listing->locationCity = $this->searchWhere->locations->address->city;
$listing->locationLatitude = $this->searchWhere->locations->latitude;
$listing->locationLongitude = $this->searchWhere->locations->longitude;
$listing->locationPostalCode = $this->searchWhere->locations->address->postal_code;
$listing->locationState = $this->searchWhere->locations->address->state;
$listing->locationStreet = $this->searchWhere->locations->address->street;
$listing->faxNumber = $this->searchWhere->locations->fax_number;
}
applyListingDataToLocalRecord($listing);
}
/* 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.
*/
/* 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);
//var_dump($search);
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);
$searchWhere = $citygrid->srch_places_where($what, $type, $where, $page, $rpp, $sort, $format, $placement, $has_offers, $i);
//var_dump($search);
if ($searchWhere) {
applyListingDataToLocalRecord($searchWhere);
}
} */
/*
* compareListingDataToLocalRecord()this will identify missing data from the local
* listing node record and see where we can apply data from the API to fill the missing data
* ??????? doubts ????????
*/
public function compareListingDataToLocalRecord() {
$empty = array();
if (!$node->field_phone[$lang][0]['value']) {
$empty[field_phone][value] = "";
}if (!$node->field_location[$lang][0]['name']) {
$empty[field_location][name] = "";
} if (!$this->node->field_location[$lang][0]['street']) {
$empty[field_location][street] = "";
} if (!$this->node->field_location[$lang][0]['city']) {
$empty[field_location][city] = "";
}if (!$this->node->field_location[$lang][0]['province']) {
$empty[field_location][province] = "";
}if (!$this->node->field_location[$lang][0]['postal_code']) {
$empty[field_location][postal_code] = "";
}if (!$this->node->field_location[$lang][0]['latitude']) {
$empty[field_location][latitude] = "";
}if (!$this->node->field_location[$lang][0]['longitude']) {
$empty[field_location][longitude] = "";
}if (!$this->node->field_location[$lang][0]['phone']) {
$empty[field_location][phone] = "";
}if (!$this->node->field_website_url[$lang][0]['url']) {
$empty[field_website_url][url] = "";
}if (!$this->node->field_hours[$lang][0]['value']) {
$empty[filed_hours][value] = "";
}
return $empty;
//incomplete !! problem in implementation...... :(
}
/*
* 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;
/*
* 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 formNode($node) {
$listing = new BusinessListing();
$listing->nid = $node->nid;
$listing->companyName = $node->title;
// etc
return $listing;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment