/
rets_import.php
142 lines (127 loc) · 3.07 KB
/
rets_import.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
<?php
class RetsImport extends RetsAppModel {
/**
* Additional find methods available
*
* @var string
*/
public $_findMethods = array('latest' => true);
/**
* find('latest') defaults
*
* @see Model::__construct()
* @var array
*/
public $findLatestDefaults = array();
/**
* Default time to start importing from, strtotime() compatible string
*
* @see http://us3.php.net/manual/en/function.strtotime.php
* @see http://us3.php.net/manual/en/datetime.formats.relative.php
* @var string
*/
public $startImport = '1 year ago';
/**
* Default range to use for each import, strtotime() compatible string
*
* @see http://us3.php.net/manual/en/function.strtotime.php
* @see http://us3.php.net/manual/en/datetime.formats.relative.php
* @var string
*/
public $defaultRange = '1 week';
/**
* Number of listings to import in one query
*
* @var integer
*/
public $batchSize = 500;
/**
* Construct the model
*
* @param string $id
* @param string $table
* @param string $ds
* @author David Kullmann
*/
public function __construct( $id = false, $table = NULL, $ds = NULL ) {
parent::__construct($id, $table, $ds);
$this->findLatestDefaults = array(
'order' => array(
sprintf('%s.finished', $this->alias) => 'ASC',
sprintf('%s.created', $this->alias) => 'DESC'
)
);
}
/**
* Find the latest import
*
* @param string $state
* @param string $query
* @param string $resutls
* @return void
* @author David Kullmann
*/
protected function _findLatest($state, $query, $results = array()) {
if ($state == 'before') {
return array_merge($query, $this->findLatestDefaults);
} elseif($state == 'after') {
if(!empty($results[0])) {
return $results[0];
}
return false;
}
}
/**
* Start a new import
*
* @param array $data Start time, end time, and record count for this import
* @return array This import record
* @author David Kullmann
*/
public function startImport($data) {
$data['finished'] = false;
$this->set($data);
if(!$this->validates() || !$this->save($data)){
throw new OutOfBoundsException('Unable to start import');
}
return $this->read();
}
/**
* Mark an import as finished
*
* @param array $data Import data
* @return array Import record
* @author David Kullmann
*/
public function finishImport($data) {
$data['finished'] = true;
if(!empty($data['modified'])) {
unset($data['modified']);
}
$this->set($data);
if(!$this->validates() || !$this->save($data)){
throw new OutOfBoundsException('Unable to start import');
}
return $this->read();
}
/**
* Calculate the dates to use for this import
*
* @param string $lastImport
* @return array First value is start time, second value is end time
* @author David Kullmann
*/
public function getImportDates($lastImport = null) {
if ($lastImport == null) {
$lastImport = date('Y-m-d H:i:s', strtotime($this->startImport));
}
$time = strtotime($this->defaultRange, strtotime($lastImport));
$now = strtotime('now');
if($time > $now) {
$time = $now;
}
$endDate = date('Y-m-d H:i:s', $time);
return array($lastImport, $endDate);
}
}
?>