Skip to content

Instantly share code, notes, and snippets.

@leetempest
Created August 4, 2015 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leetempest/068edf1f06f8ccbb3ff3 to your computer and use it in GitHub Desktop.
Save leetempest/068edf1f06f8ccbb3ff3 to your computer and use it in GitHub Desktop.
<?php
//@unlink('importxml.xml');
$_SERVER['HTTP_HOST'] = '127.0.0.1';
define('_JEXEC', 1);
define('JPATH_BASE', realpath(__DIR__ . '/../'));
require_once '../includes/defines.php';
require_once '../includes/framework.php';
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Instantiate the application.
$mainframe = JFactory::getApplication('site');
copy('./bk.table.php','../libraries/joomla/table/table.php');
require 'bfXML.php';
require '../administrator/components/com_iproperty/tables/property.php';
require '../administrator/components/com_iproperty/tables/image.php';
define('BF_CLI', TRUE);
try {
define('BF_ROOT', realpath(dirname(__FILE__)));
define('ZF2_PATH', BF_ROOT . '/library/Zend/');
// noot to include models here to get them to work
set_include_path(BF_ROOT . '/library/' . get_include_path());
require_once 'Zend/Loader/StandardAutoloader.php';
$loader = new Zend\Loader\StandardAutoloader(array('autoregister_zf' => TRUE));
$loader->registerNamespace('Imagine', BF_ROOT . '/library/Imagine');
$loader->register();
class log
{
static public function _($msg)
{
file_put_contents('log', $msg . PHP_EOL, FILE_APPEND);
}
}
if (!file_exists('importxml.xml')) {
log::_('No cache found so downloading now...');
$xml = file_get_contents('http://www.propertypilot.co.uk/cgi-bin/db2www.exe/BKxml.d2w/report');
file_put_contents('importxml.xml', $xml);
log::_('importxml downloaded and stored locally');
} else {
$xml = file_get_contents('importxml.xml');
log::_('using Cached file');
}
$db = JFactory::getDBO();
$db->setQuery('TRUNCATE #__iproperty');
$db->query();
$db->setQuery('TRUNCATE #__iproperty_images');
$db->query();
$db->setQuery('TRUNCATE #__iproperty_agentmid');
$db->query();
$db->setQuery('TRUNCATE #__iproperty_propmid');
$db->query();
libxml_use_internal_errors(TRUE);
$xml = simplexml_load_string(trim(file_get_contents('importxml.xml')));
if ($xml === FALSE) {
echo "Failed loading XML\n";
foreach (libxml_get_errors() as $error) {
echo "\t", $error->message;
}
}
$oo = 0;
foreach ($xml->property as $prop) {
if ($prop['id'] == "2315") {
// continue;
}
echo PHP_EOL . strval($prop['id']) . ' = ' . '+';
$oo++;
log::_(strval($prop['id']));
$property = new stdClass();
$property->id = strval($prop['id']);
$property->mls_id = (string)$prop->fileref;
$property->stype = getSType((string)$prop->status);
$property->stype_freq = getFreq($prop);;
$property->listing_office = 1;
$property->street_num = (string)$prop->building_name;
$property->street = (string)$prop->street_name;
$property->street2 = (string)$prop->secondary_street_name;
//$property->title = (string)$prop->brochure_header;
$property->title = (string)$prop->building_name;
$property->alias = BKURLSafe((string)$prop->building_name);
$property->short_description = NULL;
$property->description = getDescription($prop);
$property->agent_notes = NULL; //1.5.4
$property->city = (string)$prop->city;
$property->locstate = getCounty((string)$prop->county);
$property->province = ((string)$prop->county);
$property->postcode = (string)$prop->postcode;
$property->county = ((string)$prop->county);
$property->country = 193;
$property->latitude = (string)$prop->latitude;
$property->longitude = (string)$prop->longitude;
$property->gbase_address = NULL;
$property->concat_address = NULL;
$property->price = getPrice($prop);
$property->call_for_price = NULL; //1.5.4
$property->beds = NULL;
$property->lot_acres = getSizeValue($prop, 'baths'); // Acreage
$property->sqft = getSizeValue($prop, 'sqft'); // (string)$prop->maxsize;
$property->lotsize = getSizeValue($prop, 'lotsize');
$property->lot_acres = NULL;
$property->yearbuilt = NULL;
$property->heat = NULL;
$property->cool = NULL; //1.5.4
$property->fuel = NULL;
$property->garage_type = NULL;
$property->garage_size = NULL;
$property->zoning = NULL;
$property->frontage = NULL;
$property->siding = NULL;
$property->roof = NULL;
$property->propview = NULL;
$property->school_district = NULL;
$property->lot_type = getSizeValue($prop, 'lot_type'); // (string)$prop->othermaxsize;
$property->style = NULL;
$property->hoa = NULL;
$property->reo = NULL;
$property->gbase_url = NULL;
$property->gbase_timestamp = NULL;
$property->hits = NULL;
$property->featured = ((string)$prop->featured == 'No' ? 0 : 1);
$property->metadesc = NULL;
$property->metakey = NULL;
$property->created = (string)$prop->date_registered;
$property->created_by = NULL;
$property->modified = (string)$prop->date_updated;
$property->modified_by = NULL; //1.5.4
$property->access = NULL;
$property->publish_up = NULL;
$property->publish_down = NULL;
$property->state = 1;
$property->approved = 1; //1.5.4
$property->checked_out = NULL;
$property->checked_out_time = NULL;
$mmm2 = new IpropertyTableProperty($db);
$mmm2->bind((array)$property);
$mmm2->_tbl_key = '34q97nwo';
$mmm2->_tbl_keys = array('34q97nwo');
$mmm2->store();
$email = (string)$prop->contactEmail;
resolveAgent(strval($prop['id']), $email);
addCats(strval($prop['id']), $prop);
$propertyId = strval($prop['id']);
log::_('Processing: ' . (string)$prop->brochure_header);
if (is_string($prop->photos->photo)) {
$propPhotos = array($prop->photos->photo);
} else {
$propPhotos = $prop->photos->photo;
}
$ordering = 1;
$propertyId = $propertyId . '_';
foreach ($propPhotos as $photo) {
log::_('Photo for property ' . strval($prop['id']));
$parts = explode('/', $photo);
$filename = $parts[count($parts) - 1];
echo '.';
if ($photo == 'http://bk.agencypilot.com/store/property/254+2228.jpg') {
continue;
}
if ($photo == 'http://bk.agencypilot.com/store/property/254+2229.jpg') {
continue;
}
if ($photo == 'http://bk.agencypilot.com/store/property/254+2230.jpg') {
continue;
}
$file = '../media/com_iproperty/pictures/' . $propertyId . '600x449' . $filename;
$origin = $propertyId . $filename;
if (!file_exists('../media/com_iproperty/pictures/' . $propertyId . $filename)) {
log::_('Getting image ' . $photo);
$photoContentss = file_get_contents($photo);
if (preg_match('/\%pdf/i', $photoContentss) || !trim($photoContentss)) {
// file_put_contents('errors', print_R($photo, true) . PHP_EOL, FILE_APPEND);
// echo 'e='.$photo;
// continue;
echo PHP_EOL . 'SKIPPING Photo: ' . $tt;
continue;
}
file_put_contents('../media/com_iproperty/pictures/' . $origin, $photoContentss);
$imagine = new Imagine\Gd\Imagine();
$mode = Imagine\Image\ImageInterface::THUMBNAIL_OUTBOUND;
// Get original
$img = $imagine->open('../media/com_iproperty/pictures/' . $origin);
$originalSize = $img->getSize();
$file1 = '../media/com_iproperty/pictures/'
. $propertyId
. $originalSize->getWidth()
. 'x' . $originalSize->getHeight()
. '_' . $filename;
$img->save($file1);
log::_('Saving image File1 = ' . $file1);
// Lee Changed resize figure to below $height = number_format(($originalSize->getHeight() * 0.32), 0);
$height = number_format(($originalSize->getHeight() * 0.2375), 0);
//$height = number_format(($originalSize->getHeight() * 0.72), 0);
$size = new Imagine\Image\Box(200, ($originalSize->getHeight() * (200 / $originalSize->getWidth())));
$file2 = '../media/com_iproperty/pictures/'
. $propertyId . $originalSize->getWidth()
. 'x'
. $originalSize->getHeight() . '_' . str_replace(array('.jpg', '.png', '.gif'), array('_thumb.jpg',
'_thumb.png',
'_thumb.gif'), $filename);
log::_('Saving image File2 = ' . $file2);
$img->thumbnail($size, $mode)
->save($file2);
// unlink('img/' . $origin);
$parts = explode('.', $file1);
$extension = array_pop($parts);
$newImage = new stdClass();
$newImage->id = NULL;
$newImage->propid = strval($prop['id']);
$newImage->title = strval($photo['name']);
$newImage->description = NULL;
log::_('fname = ' . $file2);
$newImage->fname = str_replace('../media/com_iproperty/pictures/', '', implode('.', $parts));
$newImage->type = '.' . $extension;
$newImage->path = '/media/com_iproperty/pictures/';
$newImage->remote = NULL;
$newImage->owner = 63;
$newImage->ordering = $ordering;
$newImage->state = 1;
$newImage->ip_source = NULL;
$mmm2 = new IpropertyTableImage($db);
$mmm2->bind((array)$newImage);
$mmm2->_tbl_key = '34q97nwo;';
$mmm2->_tbl_keys = array('34q97nwo');
if (!$mmm2->store()) {
var_dump($mmm2->getError());
log::_(print_R($mmm2->getError(), TRUE));
die;
} else {
log::_('STORED');
}
$ordering++;
} else {
log::_('Image Exists' . $file);
$imagine = new Imagine\Gd\Imagine();
$mode = Imagine\Image\ImageInterface::THUMBNAIL_OUTBOUND;
$tt = '../media/com_iproperty/pictures/' . $origin;
$photoContentss = file_get_contents($tt);
if (preg_match('/\%pdf/i', $photoContentss) || !trim($photoContentss)) {
echo PHP_EOL . 'SKIPPING Photo: ' . $tt;
continue;
}
// Get original
$origin = $propertyId . $filename;
$img = $imagine->open('../media/com_iproperty/pictures/' . $origin);
$originalSize = $img->getSize();
$file1 = '../media/com_iproperty/pictures/'
. $propertyId
. $originalSize->getWidth()
. 'x' . $originalSize->getHeight()
. '_' . $filename;
$parts = explode('.', $file1);
$extension = array_pop($parts);
$newImage = new stdClass();
$newImage->id = NULL;
$newImage->propid = strval($prop['id']);
$newImage->title = strval($photo['name']);
$newImage->description = NULL;
log::_('fname = ' . $file1);
$newImage->fname = str_replace('../media/com_iproperty/pictures/', '', implode('.', $parts));
$newImage->type = '.' . $extension;
$newImage->path = '/media/com_iproperty/pictures/';
$newImage->remote = NULL;
$newImage->owner = 63;
$newImage->ordering = $ordering;
$newImage->state = 1;
$newImage->ip_source = NULL;
$mmm2 = new IpropertyTableImage($db);
$mmm2->bind((array)$newImage);
$mmm2->_tbl_key = '34q97nwo;';
$mmm2->_tbl_keys = array('34q97nwo');
$mmm2->store();
$ordering++;
}
}
}
echo PHP_EOL . $oo . ' Properties imported';
} catch (Exception $e) {
log::_('================EXCEPTION===============');
log::_($e->getMessage());
log::_(print_R($e->getTrace(), TRUE));
file_put_contents('errors', print_R($photo, TRUE) . PHP_EOL, FILE_APPEND);
echo 'E';
}
copy('./original.table.php','../libraries/joomla/table/table.php');
function resolveCategory($name)
{
$db = JFactory::getDBO();
$db->setQuery('SELECT id FROM #__iproperty_categories WHERE title = "' . $name . '"');
return $db->loadResult();
}
function resolveAgent($property_id, $email)
{
$db = JFactory::getDBO();
$db->setQuery("SELECT id FROM `#__iproperty_agents` WHERE email = '" . $email . "'");
$agent_id = $db->loadResult();
// @todo fix this
if (!$agent_id) {
$agent_id = 2;
}
$sql = sprintf("INSERT INTO `#__iproperty_agentmid` (`id`, `prop_id`, `agent_id`, `agent_type`, `ip_source`)
VALUES (NULL, %s, %s, 0, NULL);", $property_id, $agent_id);
$db->setQuery($sql);
$db->query();
}
function addCats($property_id, $xml)
{
foreach ($xml->propertyTypes->propertyType as $propertyType) {
$cat_id = strval($propertyType['id']);
$db = JFactory::getDBO();
$sql = sprintf("INSERT INTO `#__iproperty_propmid` (`id`, `prop_id`, `cat_id`, `amen_id`, `ip_source`)
VALUES
(NULL, %s, %s, 0, NULL);", $property_id, $cat_id);
$db->setQuery($sql);
$db->query();
}
}
function getCounty($county)
{
$db = JFactory::getDBO();
$db->setQuery('SELECT id FROM #__iproperty_states WHERE title = "' . $county . '"');
return $db->loadResult();
}
function getSType($string)
{
switch (strtoupper($string)) {
case strtoupper('For Sale'):
$id = 1;
break;
case strtoupper('To Let'):
$id = 2;
break;
case strtoupper('FOR SALE/TO LET'):
//case strtoupper('For Sale or To Let'):
$id = 3;
break;
case strtoupper('Sold'):
$id = 4;
break;
case strtoupper('Let'):
$id = 5;
break;
case strtoupper('Under Offer'):
$id = 6;
break;
case strtoupper('For Promotion Only'):
$id = 7;
break;
}
return $id;
}
function getDescription($prop)
{
$html = (string)$prop->description;
$brochure = trim((string)$prop->brochure);
if ($brochure) {
$html = $html . sprintf('<p>View <a href="%s">Brochure</a></p>', $brochure);
echo 'B';
}
//var_dump($prop->epcs);
//$epcs = trim((string)$prop->epcs);
//foreach ($prop->epcs as $epc){
// $html = $html . sprintf('<p><a href="%s">View EPC</a></p>',(string)$epc->epc);
//}
foreach ($prop->epcs as $epc) {
if (!empty($epc->epc)) {
$html = $html . sprintf('<p><a href="%s">View EPC</a></p>', (string)$epc->epc);
}
}
$otherdocs = $prop->otherdocs;
if (count($otherdocs->otherdoc)) {
$html = $html . '<p>';
}
$c = 0;
foreach ($otherdocs->otherdoc as $doc) {
$suffix = sprintf('<br/>View <a href="%s">%s</a><br/>', $doc, $otherdocs->otherdoctitle[$c++]);
$html = $html . $suffix;
echo 'D';
}
if (count($otherdocs->otherdoc)) {
$html = $html . '</p>';
}
return trim($html);
}
/**
* @param $prop
* @param $attribute
* Acerage = baths
* Hectares = lotsize
* sq ft = sqft
* m2 = lot_type
*
* <minsize unit="Hectares">0.14</minsize>
* <otherminsize unit="Acres">0.35</otherminsize>
* <maxsize unit="Hectares">0.14</maxsize>
* <othermaxsize unit="Acres">0.35</othermaxsize>
*
*/
function getSizeValue($prop, $attribute)
{
$units = strval($prop->maxsize['unit']);
$units2 = strval($prop->othermaxsize['unit']);
switch ($attribute) {
case 'baths':
if ($units == 'Acres') {
return strval($prop->maxsize);
} else if ($units2 == 'Acres') {
return strval($prop->othermaxsize);
}
break;
case 'lotsize':
if ($units == 'Hectares') {
return strval($prop->maxsize);
} else if ($units2 == 'Hectares') {
return strval($prop->othermaxsize);
}
break;
case 'sqft':
case 'Sq Ft':
if ($units == 'Sq Ft') {
return strval($prop->maxsize);
} else if ($units2 == 'Sq Ft') {
return strval($prop->othermaxsize);
}
break;
case 'lot_type':
if ($units == 'Sq M') {
return strval($prop->maxsize);
} else if ($units2 == 'Sq M') {
return strval($prop->othermaxsize);
}
break;
}
}
/**
* <Leasehold_Min_Price term="">0.00</Leasehold_Min_Price>
* <Leasehold_Max_Price term="">0.00</Leasehold_Max_Price>
* <Freehold_Min_Price term="plus VAT">350000.00</Freehold_Min_Price>
* <Freehold_Max_Price term="plus VAT">0.00</Freehold_Max_Price>
* <License_Min_Price term="">0.00</License_Min_Price>
* <License_Max_Price term="">0.00</License_Max_Price>
*
* @param $prop
*
* @return string
*/
function getPrice($prop)
{
$res = '';
if ((string)$prop->Leasehold_Min_Price != '0.00') {
$res = (string)$prop->Leasehold_Min_Price;
}
if ((string)$prop->Freehold_Min_Price != '0.00') {
$res = (string)$prop->Freehold_Min_Price;
}
if ((string)$prop->License_Min_Price != '0.00') {
$res = (string)$prop->License_Min_Price;
}
return $res;
}
function getFreq($prop)
{
$res = '';
if ((string)trim($prop->Leasehold_Min_Price) != '0.00') {
$res = (string)$prop->Leasehold_Min_Price['term'];
}
if ((string)trim($prop->Freehold_Min_Price) != '0.00') {
$res = (string)$prop->Freehold_Min_Price['term'];
}
if ((string)trim($prop->License_Min_Price) != '0.00') {
$res = (string)$prop->License_Min_Price['term'];
}
return $res;
}
function BKURLSafe($string)
{
//remove any '-' from the string they will be used as concatonater
$str = str_replace('-', ' ', $string);
$str = str_replace('_', ' ', $string);
//$lang =& JFactory::getLanguage();
//$str = $lang->transliterate($str);
// remove any duplicate whitespace, and ensure all characters are alphanumeric
$str = preg_replace(array('/\s+/','/[^A-Za-z0-9\-]/'), array('-',''), $str);
// lowercase and trim
$str = trim(strtolower($str));
return $str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment