/
Hotpads.php
216 lines (181 loc) · 5.92 KB
/
Hotpads.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
/**
* @package Hotpads
* @author Michael Botsko, Trellis Development, LLC
* @version Git-Revision
*
* This PHP object allows you to generate the required xml documents for
* a listing feed for the Hotpads.com web service. You primarily must
* fill in the data accepted by the HotpadProperty class, and pass an array
* of all property objects to the Hotpads object.
*
* You should cache the resulting xml file and provide this cached version
* as the download option to Hotpads.
*/
require('Xml.php');
/**
* @package Hotpads
* @subpackage HotpadProperty
*/
class HotpadProperty {
public $id;
public $type;
public $property_type;
public $name;
public $unit_no;
public $address_1;
public $street;
public $city;
public $state;
public $zip;
public $country= 'US'; // http://www.iso.org/iso/iso3166_en_code_lists.txt
public $contact_name;
public $contact_email;
public $contact_phone;
public $contact_fax;
public $preview_message;
public $description;
public $terms;
public $website_url;
public $vr_url;
public $photos = array();
public $price;
public $price_freq;
public $hoa_maint;
public $bedrooms;
public $full_bath;
public $half_bath;
public $sqft;
}
/**
* @package Hotpads
*/
class Hotpads {
/**
* @var arrays Holds an array of all HotpadProperty objects
* @access private
*/
protected $properties = array();
/**
* Adds a new property object to the properties array
* @param object $prop_obj
* @access public
*/
public function addProperty($prop_obj){
$this->properties[] = $prop_obj;
}
/**
* Validates that all required property values have been provided.
* @param object $prop
* @return boolean
* @access private
* This is implented assuming all fields of prop object are required
*/
protected function validate($prop){
// $errors = array();
//
// foreach($prop as $var => $p){
// if(is_array($p)){
// if(empty($p)){
// $errors[$var] = 'Field may not contain an empty array.';
// }
// } else {
// if(is_null($p)){
// $errors[$var] = 'Field must contain a value.';
// }
// }
// }
//
// return (empty($errors) ? true : $errors);
return true;
}
/**
* Builds the complete xml document.
* @return string
* @access private
*/
protected function buildXML(){
$hp = new Xml('1.0', 'utf-8');
$hp->formatOutput = true;
// begin hp items
$hpitems = $hp->createElement('hotPadsItems');
$hpitems->setAttribute("version", '2.1');
$comm = $hp->createComment('Generated at ' . gmdate(DATE_RFC822));
$hpitems->appendChild($comm);
if(is_array($this->properties)){
foreach($this->properties as $prop){
$noerrors = $this->validate($prop);
if($noerrors === true){
// Build listing
$listing = $hp->createElement('Listing');
// Listing attributes
$listing->setAttribute("id", $prop->id);
$listing->setAttribute("type", $prop->type);
$listing->setAttribute("propertyType", $prop->property_type);
// Property details
$listing->appendChild( $this->hpTextNode('name', false, $hp, $prop) );
$listing->appendChild( $this->hpTextNode('unit', false, $hp, $prop) );
$listing->appendChild( $this->hpTextNode('street', false, $hp, $prop) );
$listing->appendChild( $this->hpTextNode('city', false, $hp, $prop) );
$listing->appendChild( $this->hpTextNode('state', false, $hp, $prop) );
$listing->appendChild( $this->hpTextNode('zip', false, $hp, $prop) );
$listing->appendChild( $this->hpTextNode('country', false, $hp, $prop) );
// Contact details
$listing->appendChild( $this->hpTextNode('contactName', 'contact_name', $hp, $prop) );
$listing->appendChild( $this->hpTextNode('contactEmail', 'contact_email', $hp, $prop) );
$listing->appendChild( $this->hpTextNode('contactPhone', 'contact_phone', $hp, $prop) );
$listing->appendChild( $this->hpTextNode('contactFax', 'contact_fax', $hp, $prop) );
// Descriptions and urls
$listing->appendChild( $this->hpTextNode('previewMessage', 'preview_message', $hp, $prop) );
$listing->appendChild( $this->hpTextNode('description', false, $hp, $prop) );
$listing->appendChild( $this->hpTextNode('terms', false, $hp, $prop) );
$listing->appendChild( $this->hpTextNode('website', 'website_url', $hp, $prop) );
$listing->appendChild( $this->hpTextNode('virtualTourUrl', 'vr_url', $hp, $prop) );
// Photos
if($prop->photos){
foreach($prop->photos as $img){
$photo = $hp->createElement('ListingPhoto');
$photo->setAttribute("source", $img['source']);
$listing->appendChild( $photo );
}
}
// Additional data
$listing->appendChild( $this->hpTextNode('price', false, $hp, $prop) );
$listing->appendChild( $this->hpTextNode('pricingFrequency', 'price_freq', $hp, $prop) );
$listing->appendChild( $this->hpTextNode('HOA-Fee', 'hoa_maint', $hp, $prop) );
$listing->appendChild( $this->hpTextNode('numBedrooms', 'bedrooms', $hp, $prop) );
$listing->appendChild( $this->hpTextNode('numFullBaths', 'full_bath', $hp, $prop) );
$listing->appendChild( $this->hpTextNode('numHalfBaths', 'half_bath', $hp, $prop) );
$listing->appendChild( $this->hpTextNode('squareFeet', 'sqft', $hp, $prop) );
$hpitems->appendChild($listing);
}
}
$hp->appendChild($hpitems);
return $hp->saveXML();
}
}
/**
* Inserts a new text node using the property object field provided.
* @param string $element
* @param string $var
* @param object $hp
* @param object $prop
* @return object
* @access private
*/
protected function hpTextNode($element = 'node', $var = false, &$hp, $prop){
$var = $var ? $var : $element;
$node = $hp->createElement($element);
$node->appendChild( $hp->createTextNode( $hp->encode_for_xml( $prop->{$var} ) ) );
return $node;
}
/**
* Returns the completed xml document.
* @return string
* @access public
*/
public function xml(){
return $this->buildXML();
}
}
?>