forked from hugowetterberg/cbisimport
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCbisTransaction.php
More file actions
243 lines (219 loc) · 5.96 KB
/
CbisTransaction.php
File metadata and controls
243 lines (219 loc) · 5.96 KB
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
<?php
/**
* CBIS Transaction
*
* Represents one imported product from CBIS and it's different states
* through the import process. This data can be used to verify the imported
* data in it's various states and make sure that the end product (a Drupal
* node) is valid.
*/
class CbisTransaction {
// "Singleton" object
static $current_transaction;
// Transaction date
private $id;
private $time;
private $language;
private $states;
private $path;
private $nid;
private $transaction_started;
private $transaction_id; // From database
private $has_load_state;
/**
* Returns the current transaction
*
* @return CbisTransaction
* @throws Exception
* Throws an exception if no transaction is set to the current one
*/
public static function getCurrentTransaction() {
if (!self::$current_transaction) {
throw new Exception('No current transaction.');
}
return self::$current_transaction;
}
/**
* Sets the current transaction
*
* Usually a newly constructed instance
*
* @param CbisTransaction $t
* @return void
*/
public static function setCurrentTransaction(CbisTransaction $t) {
self::$current_transaction = $t;
}
/**
* Release the current transaction
*/
public static function releaseCurrentTransaction() {
self::$current_transaction = NULL;
}
/**
* Constructs a new transaction object with data from an old transaction
*
* Used to update a transaction with a valid node state
*
* @param $tid
* Transaction ID
* @return CbisTransaction
* @throws Exception
* Throws an exception if a transaction with the given tid can't be found.
*/
public static function loadTransaction($tid) {
$record = db_query(
"SELECT * FROM {cbisimport_transactions} WHERE id = %d",
array(':tid' => $tid)
);
if ($record) {
// Create the transaction object
$record = db_fetch_object($record);
$transaction = new self(
$record->pid,
$record->timestamp,
$record->language
);
// Set other data not possible through constructor
$transaction->setTransactionId($tid);
$transaction->restoreStates();
// Set our transaction to the current one and return it
CbisTransaction::setCurrentTransaction($transaction);
return $transaction;
} else {
throw new Exception('Transcation with id ' . $tid . ' not found.');
}
}
/**
* Constructs a new transaction
*
* @param $id
* The CBIS product ID
* @param $time
* Modification date of the product as a UNIX timestamp
* @param $language
* The language of this product
*/
public function __construct($id, $time, $language) {
$this->id = (int) $id;
$this->time = $time;
$this->language = $language;
$this->states = array();
$this->transaction_started = time();
// Create a new directory for this transaction
$df = file_directory_path();
$this->path = $df . "/cbis_transactions/{$id}/{$time}_{$language}";
if (!file_exists($this->path)) {
mkdir($this->path, 0777, TRUE);
}
}
/**
* Adds a state to the transaction
*
* @param $name
* A name for the transaction, will be used as the filename etc.
* @param $state
* The state to save
* @return this
* Allows method chaining
*/
public function addState($name, $state) {
$this->states[] = $name;
if (!is_string($serialize)) {
$state = json_encode($state);
}
$nr_states = count($this->states);
file_put_contents("{$this->path}/{$nr_states}-{$name}", $state);
return $this;
}
/**
* Set the related products nid
*
* @param $nid
* @return void
*/
public function setNid($nid) {
$this->nid = $nid;
}
/**
* Set the transaction id
*
* @param $id
* @return void
*/
public function setTransactionId($id) {
$this->transaction_id = $id;
}
/**
* Update load status
*
* @param $has_load_state
* @return void
*/
public function setHasLoadState($has_load_state) {
$this->has_load_state = (bool) $has_load_state;
}
/**
* Restore a states from disk
*
* @return $path
*/
public function restoreStates() {
try {
$states = array();
$di = new DirectoryIterator(realpath(getcwd() . '/' . $this->path));
foreach ($di as $file) {
if ($file->isFile() && strpos($file->getFilename(), 'index.json') === FALSE) {
$parts = explode('-', $file->getFilename());
$number = array_shift($parts);
$state_name = join('-', $parts);
$states[$number] = $state_name;
}
}
// Make sure states are loaded in correct order..
} catch (Exception $e) {
// TODO: Do some useful logging or something
}
}
/**
* Writes transaction information to index file
*
* @access private
* @return void
*/
private function writeIndex() {
$index = array(
'id' => $this->id,
'nid' => $this->nid,
'time' => $this->time,
'language' => $this->language,
'transaction_started' => $this->transaction_started,
'states' => $this->states,
'has_load_state' => $this->has_load_state,
);
file_put_contents("{$this->path}/0-index.json", json_encode($index));
}
/**
* Saves the transaction to disk and logs a transaction record in the database
* Or updates an already logged transaction.
*
* @return void
*/
public function persist() {
$this->writeIndex();
// Save database record of this transaction
$record = (object) array(
'pid' => $this->id,
'language' => $this->language,
'timestamp' => $this->time,
'has_load_state' => (int) $this->has_load_state
);
if ($this->transaction_id) {
$update = array('id');
$record->id = $this->transaction_id;
} else {
$update = array();
}
drupal_write_record('cbisimport_transactions', $record, $update);
}
}