forked from bolingresearch/idx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tehama-county.php
435 lines (380 loc) · 11.3 KB
/
tehama-county.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
<?php
/*
* Tehama County, California IDX Data Import Script.
*/
require_once("../wp-load.php");
/**
* FTP Server Credentials
*/
$host = "idx.fnismls.com";
$user = "tehamamls_djboling";
$pass = "2dk9j2ipcz";
$dir = "IDX/";
/**
* Local directory details
*/
$local_temp_dir = dirname(__FILE__)."/temp/";
$local_images_dir = ABSPATH."wp-content/listing-images/";
// Database
$db_table = 'wp_rplistings';
/**
* Connect to the FTP Server
*/
$conn = ftp_connect($host) or die("ERROR: Cannot connect to FTP server.");
ftp_login($conn, $user, $pass) or die("ERROR: Cannot login to FTP server");
ftp_chdir($conn, $dir);
/**
* Retrieve list of files on the server.
*/
$list = ftp_nlist($conn, ".");
natsort($list);
/**
* Separate image archives from data files.
*/
$listingdata_files = array();
$image_archives = array();
$i = $j = 0;
foreach ($list as $remote_file) {
if (preg_match("/listings/", $remote_file)) {
$listingdata_files[$i] = $remote_file;
$i++;
}
else if (preg_match("/pics/", $remote_file)) {
$image_archives[$j] = $remote_file;
$j++;
}
}
/**
* Files to download.
*/
$date = new DateTime(date("Y-m-d")); // today
$date->modify("-1 day"); // yesterday
$formatted_date = $date->format("Ymd");
$files2download = $listingdata_files;
foreach ($image_archives as $image_archive) {
if ( preg_match("/$formatted_date/", $image_archive)) {
$files2download[] = $image_archive;
}
}
/**
* Download and extract data files and image archives.
*/
get_idxfiles( $files2download );
/**
* Disconnect from the FTP server.
*/
ftp_close($conn);
/**
* Update the database with IDX listing data.
*/
update_database();
assign_images();
/**
* Downloads and extracts data files and image archives from
* the IDX server.
*
* @global FTP $conn
* @global string $local_temp_dir
* @global string $local_images_dir
* @param string[] $files Filenames to download and extract.
*/
function get_idxfiles( $files = array() ) {
global $conn;
global $local_temp_dir;
global $local_images_dir;
// include Tar class
require_once("Archive/Tar.php");
echo "<div style='height:150px; width:400px; overflow:auto; border: solid 1px #222;'>";
foreach ($files as $remote_file) {
echo "<p>";
$local_file = $local_temp_dir.$remote_file;
ftp_get($conn, $local_file, $remote_file, FTP_BINARY) or die("ERROR: Cannot download $remote_file");
echo "$remote_file successfully downloaded.<br />";
// if text file
if (preg_match("/txt.gz/", $remote_file) == 1) {
if (file_exists(substr($local_file, 0, -3))) {
unlink(substr($local_file, 0, -3));
}
`gunzip $local_file`;
// check if file was successfully unzipped
if (file_exists(substr($local_file, 0, -3))) {
echo "$remote_file unzipped.";
}
}
// if images archive
if (preg_match("/.tar/", $remote_file) == 1) {
// use tar file
$tar = new Archive_Tar($local_file);
if (is_writable($local_images_dir)) {
if ($tar->extract($local_images_dir)) {
echo "Images extracted.<br />";
}
if(file_exists($local_file)) {
if (unlink($local_file)) {
echo 'Archive file deleted.<br />';
} else {
echo 'Check your folder permissions. The archive file could not be deleted.<br />';
}
}
} else {
echo "Images directory is not writable.<br />";
}
}
echo "</p>";
}
echo "</div>";
}
/**
* Updates RealtyPress tables with IDX data. Scans image directory and
* updates listing table with image date.
*
* @global <type> $local_temp_dir
* @global <type> $local_images_dir
* @global <type> $wpdb
*/
function update_database() {
global $local_temp_dir;
global $local_images_dir;
global $wpdb;
global $db_table;
// array of lines from data file
$file = $local_temp_dir."listings-residential.txt";
if (!file_exists($file)) {
wp_die('<div class="updated fade"><p>The file: '.$file.' doesn\'t exist.</p></div>');
}
$lines = file($file, FILE_IGNORE_NEW_LINES);
$listing_count = 0;
foreach($lines as $line) {
// explode columns into $fields array
$fields = explode("\t", $line);
// make data safe
$fields[1] = $wpdb->escape(ucwords(strtolower($fields[1])));
$fields[2] = $wpdb->escape(ucwords(strtolower($fields[2])));
$fields[3] = $wpdb->escape(ucwords(strtolower($fields[3])));
$fields[4] = $wpdb->escape($fields[4]);
$fields[5] = $wpdb->escape($fields[5]);
$fields[6] = $wpdb->escape($fields[6]);
$fields[7] = $wpdb->escape(ucwords(strtolower($fields[7])));
$fields[8] = $wpdb->escape(ucwords(strtolower($fields[8])));
$fields[9] = $wpdb->escape(ucwords(strtolower($fields[9])));
$fields[10] = $wpdb->escape($fields[10]);
$fields[11] = $wpdb->escape($fields[11]);
$fields[12] = $wpdb->escape($fields[12]);
$fields[13] = $wpdb->escape($fields[13]);
$fields[14] = $wpdb->escape($fields[14]);
$fields[15] = $wpdb->escape($fields[15]);
$fields[16] = $wpdb->escape($fields[16]);
$fields[17] = $wpdb->escape($fields[17]);
$fields[19] = $wpdb->escape(ucwords(strtolower($fields[19])));
$fields[22] = $wpdb->escape(ucwords(strtolower($fields[22])));
$fields[24] = $wpdb->escape($fields[24]);
$fields[26] = $wpdb->escape(ucwords(strtolower($fields[26])));
$fields[27] = $wpdb->escape(ucwords(strtolower($fields[27])));
$fields[49] = $wpdb->escape($fields[49]);
$fields[54] = $wpdb->escape(ucwords(strtolower($fields[54])));
$fields[59] = $wpdb->escape($fields[59]);
$fields[101] = $wpdb->escape($fields[101]);
$fields[108] = $wpdb->escape(sentence_case($fields[108]));
$fields[123] = $wpdb->escape($fields[123]);
$fields[125] = $wpdb->escape($fields[125]);
$fields[126] = $wpdb->escape($fields[126]);
$price_change = $fields[101] - $fields[4];
// format date
$fields[47] = date("Y-m-d", strtotime($fields[47]));
// does listing already exist?
$wpdb->show_errors();
$found = $wpdb->get_results("SELECT listing_id FROM " . $db_table . " WHERE listing_id='$fields[0]'");
// if found, listing exists. UPDATE it.
$arr_size = count($found);
if ($arr_size != 0) {
$rows_affected = $wpdb->query(
"UPDATE ".$db_table." SET class='$fields[1]',
type='$fields[2]',
area='$fields[3]',
price='$fields[4]',
street_num='$fields[5]',
street_dir='$fields[6]',
street_name='$fields[7]',
street_line2='$fields[8]',
city='$fields[9]',
state='$fields[10]',
zipcode='$fields[11]',
status='$fields[12]',
sale_or_rent='$fields[13]',
beds='$fields[14]',
baths_full='$fields[15]',
baths_partial='$fields[16]',
garage_cap='$fields[17]',
stories='$fields[19]',
water_src='$fields[22]',
acres='$fields[24]',
lot_dimensions='$fields[26]',
lot_description='$fields[27]',
list_date='$fields[47]',
year_built='$fields[49]',
subdivision='$fields[54]',
total_rooms='$fields[59]',
orig_price='$fields[101]',
public_remarks='$fields[108]',
agent_id='$fields[123]',
office_id='$fields[125]',
sqft='$fields[126]',
price_change='$price_change'
WHERE listing_id='$fields[0]'");
if($rows_affected === 0) {
//echo "Listing #$fields[0] doesn't need updating.<br />";
//echo '<div class="updated fade"><p>Plugin settings saved.</p></div>';
} elseif($rows_affected === FALSE) {
echo '<div class="error fade"><p>There has been an error querying the database.</p></div>';
} else {
$listing_count++;
}
unset($found);
} else {
$affected = $wpdb->query("
INSERT INTO " . $db_table . " (listing_id,
class,
type,
area,
price,
street_num,
street_dir,
street_name,
street_line2,
city,
state,
zipcode,
status,
sale_or_rent,
beds,
baths_full,
baths_partial,
garage_cap,
stories,
water_src,
acres,
lot_dimensions,
lot_description,
list_date,
year_built,
subdivision,
total_rooms,
orig_price,
public_remarks,
agent_id,
office_id,
sqft,
price_change)
VALUES (
'$fields[0]',
'$fields[1]',
'$fields[2]',
'$fields[3]',
'$fields[4]',
'$fields[5]',
'$fields[6]',
'$fields[7]',
'$fields[8]',
'$fields[9]',
'$fields[10]',
'$fields[11]',
'$fields[12]',
'$fields[13]',
'$fields[14]',
'$fields[15]',
'$fields[16]',
'$fields[17]',
'$fields[19]',
'$fields[22]',
'$fields[24]',
'$fields[26]',
'$fields[27]',
'$fields[47]',
'$fields[49]',
'$fields[54]',
'$fields[59]',
'$fields[101]',
'$fields[108]',
'$fields[123]',
'$fields[125]',
'$fields[126]',
'$price_change')
");
} // end else
flush();
$wpdb->flush();
} // end foreach
echo '<div class="updated fade"><p>The listings database was updated successfully. Count: '.$listing_count.'</p></div>';
} // end rp_update_listing_data
/*
* Converts a string to sentence case.
* @returns string
*/
function sentence_case($string) {
$sentences = preg_split('/([.?!]+)/', $string, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
$new_string = '';
foreach ($sentences as $key => $sentence) {
$new_string .= ($key & 1) == 0?
ucfirst(strtolower(trim($sentence))) :
$sentence.' ';
}
return trim($new_string);
}
/**
*
* @global WPDB $wpdb
*/
function assign_images() {
global $wpdb;
global $db_table;
global $local_images_dir;
$wpdb->show_errors();
$listings = $wpdb->get_results("SELECT listing_id FROM $db_table");
$count = 0;
foreach($listings as $listing) {
// Grab all images associated with listing id.
$images = image_search($local_images_dir, $listing->listing_id);
if(!empty($images)) {
natsort($images);
// insert image data into database.
$img_string = "";
foreach($images as $image) {
$img_string .= $image . "\t";
}
$img_string = trim($img_string);
$affected = $wpdb->query("UPDATE $db_table SET images='$img_string' WHERE listing_id={$listing->listing_id}");
$count++;
}
}
echo '<div class="updated fade"><p>'.$count.' listings updated with images.</p></div>';
}
/*
* Searches recursively for filenames matching a given pattern.
* Outputs an array of the matching filenames.
*/
function image_search($dir, $id) {
// check if argument is a valid directory
if(!is_dir($dir)) {
wp_die("Argument '$dir' is not a valid directory!");
}
// open directory handle
$dh = opendir($dir) or wp_die("Cannot open directory '$dir'!");
$matches = array();
// iterate over files in directory
$i = 0;
while (($file = readdir($dh)) !== false) {
// filter out "." and ".."
if ($file != "." && $file != "..") {
// if this is a file
// check for a match
// add to $matchList if found
if (preg_match("/$id/", $file) == 1) {
$matches[$i] = "$file";
$i++;
}
}
}
// return the final list to the caller
return $matches;
}
?>