Skip to content

Instantly share code, notes, and snippets.

@markupboy
Created November 1, 2011 20:03
Show Gist options
  • Select an option

  • Save markupboy/1331737 to your computer and use it in GitHub Desktop.

Select an option

Save markupboy/1331737 to your computer and use it in GitHub Desktop.
<?php
$plugin_info = array(
'pi_name' => 'Property Info',
'pi_version' => '1.0',
'pi_author' => 'Blake Walters, Viget Labs',
'pi_description' => 'Returns general info about a property',
'pi_usage' => Property_info::usage()
);
class Property_info {
var $return_data;
function Property_info() {}
// ----------------------------------------
// Parse single template vars from an array
// ----------------------------------------
private function parse_variables($tagdata, $variables) {
global $TMPL;
foreach ($TMPL->var_single as $key => $val) {
if(isset($variables[$val])) {
$tagdata = $TMPL->swap_var_single($val, $variables[$val], $tagdata);
}
}
return $tagdata;
}
// ----------------------------------------
// Get vaultware xml file and convert to simplexml object
// ----------------------------------------
private function get_xml($filename = "http://kettler.dev/xml/kettler.xml") {
return simplexml_load_file($filename);
}
// ----------------------------------------
// DB query for property information
// ----------------------------------------
private function query_property_info($id, $cat_id) {
global $DB;
return $DB->query("SELECT exp_categories.* FROM exp_relationships
LEFT JOIN exp_category_posts
ON exp_category_posts.entry_id = exp_relationships.rel_parent_id
LEFT JOIN exp_categories
ON exp_category_posts.cat_id = exp_categories.cat_id
WHERE exp_relationships.rel_child_id = $id
AND exp_categories.group_id = $cat_id
GROUP BY exp_categories.cat_name;"
);
}
// ----------------------------------------
// Parse a query into number of beds and baths
// ----------------------------------------
private function get_basic_property_info($id, $cat_id) {
$query = $this->query_property_info($id, $cat_id);
$results = array();
if ($query->num_rows > 0) {
foreach($query->result as $info) {
$results[] = intval($info['cat_name']);
}
sort($results);
} else {
$results[] = "0";
}
return $results;
}
// ----------------------------------------
// Get the model id from a vaultware url
// ----------------------------------------
private function get_model_id($url) {
$query = split('\?', $url);
parse_str($query[1]);
return $fid;
}
// ----------------------------------------
// Get the property id from a vaultware url
// ----------------------------------------
private function get_property_id($url) {
$query = split('\?', $url);
parse_str($query[1]);
return $propid;
}
// ----------------------------------------
// Provide a formatted rent string
// ----------------------------------------
private function format_rent($min, $max) {
return $min == $max ? "$$min" : "$$min&mdash;$$max";
}
// ----------------------------------------
// Get a models prices from EE when no vaultware data is present
// ----------------------------------------
private function get_model_default_prices($entry_id) {
global $DB;
$prices = array();
$query_rent_range = $DB->query("SELECT field_id_97 as rent_range FROM exp_weblog_data
WHERE entry_id = $entry_id");
if($query_rent_range->num_rows > 0) {
foreach ($query_rent_range->result as $index => $floorplan_rent_range) {
foreach($floorplan_rent_range as $index => $rent_range) {
$rent_range = split("-", $rent_range);
foreach ($rent_range as $index => $rent) {
$rent = preg_replace('/[^\d\s]/', '', $rent);
$prices[] = (int)$rent;
}
}
}
}
return $prices;
}
// ----------------------------------------
// Get a models prices from vaultware or EE
// ----------------------------------------
private function get_model_prices($vaultware_url, $entry_id, $xml) {
global $DB;
$prices = array();
if(trim($vaultware_url) != "") {
$model_id = $this->get_model_id($vaultware_url);
$model = $xml->xpath("Property/Floorplan[@id=$model_id]/MarketRent");
if(count($model) > 0) {
$rent = $model[0]->attributes();
$prices[] = (int)$rent['min'];
$prices[] = (int)$rent['max'];
} else {
$prices = $this->get_model_default_prices($entry_id);
}
} else {
$prices = $this->get_model_default_prices($entry_id);
}
sort($prices);
return $prices;
}
// ----------------------------------------
// public plugin method for getting basic info about a property
// ----------------------------------------
function basic() {
global $TMPL, $FNS, $DB;
$id = $TMPL->fetch_param('id');
$tagdata = $TMPL->tagdata;
$bedrooms = $this->get_basic_property_info($id, 18);
$bathrooms = $this->get_basic_property_info($id, 19);
$this->return_data = $this->parse_variables($tagdata, array(
'beds_low' => $bedrooms[0] == 0 ? "Studio" : $bedrooms[0],
'beds_high' => end($bedrooms),
'baths_low' => $bathrooms[0],
'baths_high' => end($bathrooms)
));
return $this->return_data;
}
// ----------------------------------------
// public plugin method for looping through all types of properties
// ----------------------------------------
function floorplan_groups() {
global $TMPL, $DB;
$id = $TMPL->fetch_param('id');
$query = $this->query_property_info($id, 18);
$this->return_data = "";
$price_range = "";
if($query->num_rows > 0) {
foreach ($query->result as $info) {
$query_floorplans = $DB->query("SELECT exp_weblog_data.field_id_106 as vaultware_url,
exp_weblog_data.entry_id as entry_id
FROM exp_relationships
LEFT JOIN exp_category_posts
ON exp_category_posts.entry_id = exp_relationships.rel_parent_id
LEFT JOIN exp_categories
ON exp_category_posts.cat_id = exp_categories.cat_id
LEFT JOIN exp_weblog_data
ON exp_weblog_data.entry_id = exp_relationships.rel_parent_id
WHERE exp_relationships.rel_child_id = $id
AND exp_categories.cat_url_title = '".$info['cat_url_title'] . "'");
if($query_floorplans->num_rows > 0) {
$xml = $this->get_xml();
$prices = array();
foreach ($query_floorplans->result as $index => $floorplan) {
$prices = array_merge($prices,
$this->get_model_prices($floorplan['vaultware_url'], $floorplan['entry_id'], $xml)
);
}
sort($prices);
$price_range = $this->format_rent($prices[0], end($prices));
}
$this->return_data .= $this->parse_variables($TMPL->tagdata, array(
'beds' => $info['cat_name'],
'beds_url' => $info['cat_url_title'],
'price_range' => $price_range
));
}
}
return $this->return_data;
}
// ----------------------------------------
// public plugin method for getting a specific model's price
// ----------------------------------------
function model_price() {
global $TMPL, $DB;
$vaultware_url = $TMPL->fetch_param('vaultware_url');
$entry_id = $TMPL->fetch_param('entry_id');
$xml = $this->get_xml();
$prices = $this->get_model_prices($vaultware_url, $entry_id, $xml);
return $this->format_rent($prices[0], end($prices));
}
// ----------------------------------------
// public plugin method for getting a property's price
// ----------------------------------------
function property_price() {
global $TMPL, $DB;
$vaultware_url = $TMPL->fetch_param('vaultware_url');
$rent_range = $TMPL->fetch_param('rent_range');
if(trim($vaultware_url) == '') {
$this->return_data = $rent_range;
} else {
$xml = $this->get_xml();
$property_id = $this->get_property_id($vaultware_url);
$property = $xml->xpath("Property[Identification/PrimaryID=$property_id]");
if(count($property) > 0) {
$prices = array();
$market_rents = $property[0]->xpath("Floorplan/MarketRent");
foreach ($market_rents as $index => $price) {
$prices[] = (int)$price['min'];
$prices[] = (int)$price['max'];
}
sort($prices);
$this->return_data = $this->format_rent($prices[0], end($prices));
} else {
$this->return_data = $rent_range;
}
}
return $this->return_data;
}
/* END */
// ----------------------------------------
// Plugin Usage
// ----------------------------------------
// This function describes how the plugin is used.
// Make sure and use output buffering
function usage()
{
ob_start();
?>
<?php
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
/* END */
}
// END CLASS
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment