Completed
Push — master ( 8797d3...6ab92f )
by Yannick
27:52
created
require/class.Accident.php 1 patch
Braces   +87 added lines, -31 removed lines patch added patch discarded remove patch
@@ -10,7 +10,9 @@  discard block
 block discarded – undo
10 10
 	public function __construct($dbc = null) {
11 11
 		$Connection = new Connection($dbc);
12 12
 		$this->db = $Connection->db();
13
-		if ($this->db === null) die('Error: No DB connection. (Accident)');
13
+		if ($this->db === null) {
14
+			die('Error: No DB connection. (Accident)');
15
+		}
14 16
 	}
15 17
 
16 18
 	public function get() {
@@ -109,8 +111,11 @@  discard block
 block discarded – undo
109 111
 				$data = array();
110 112
 				if ($row['registration'] != '') {
111 113
 					$image_array = $Image->getSpotterImage($row['registration']);
112
-					if (count($image_array) > 0) $data = array_merge($data,array('image' => $image_array[0]['image'],'image_thumbnail' => $image_array[0]['image_thumbnail'],'image_copyright' => $image_array[0]['image_copyright'],'image_source' => $image_array[0]['image_source'],'image_source_website' => $image_array[0]['image_source_website']));
113
-					else $data = array_merge($data,array('image' => '','image_thumbnail' => '','image_copyright' => '','image_source' => '','image_source_website' => ''));
114
+					if (count($image_array) > 0) {
115
+						$data = array_merge($data,array('image' => $image_array[0]['image'],'image_thumbnail' => $image_array[0]['image_thumbnail'],'image_copyright' => $image_array[0]['image_copyright'],'image_source' => $image_array[0]['image_source'],'image_source_website' => $image_array[0]['image_source_website']));
116
+					} else {
117
+						$data = array_merge($data,array('image' => '','image_thumbnail' => '','image_copyright' => '','image_source' => '','image_source_website' => ''));
118
+					}
114 119
 					$aircraft_type = $Spotter->getAllAircraftTypeByRegistration($row['registration']);
115 120
 					$aircraft_info = $Spotter->getAllAircraftInfo($aircraft_type);
116 121
 					if (!empty($aircraft_info)) {
@@ -126,17 +131,30 @@  discard block
 block discarded – undo
126 131
 						$data['aircraft_base'] = $owner_data['base'];
127 132
 						$data['aircraft_date_first_reg'] = $owner_data['date_first_reg'];
128 133
 					}
129
-				} else $data = array_merge($data,array('image' => '','image_thumbnail' => '','image_copyright' => '','image_source' => '','image_source_website' => ''));
130
-				if ($row['registration'] == '') $row['registration'] = 'NA';
131
-				if ($row['ident'] == '') $row['ident'] = 'NA';
134
+				} else {
135
+					$data = array_merge($data,array('image' => '','image_thumbnail' => '','image_copyright' => '','image_source' => '','image_source_website' => ''));
136
+				}
137
+				if ($row['registration'] == '') {
138
+					$row['registration'] = 'NA';
139
+				}
140
+				if ($row['ident'] == '') {
141
+					$row['ident'] = 'NA';
142
+				}
132 143
 				$identicao = $Spotter->getAllAirlineInfo(substr($row['ident'],0,3));
133 144
 				if (isset($identicao[0])) {
134 145
 					if (substr($row['ident'],0,2) == 'AF') {
135
-						if (filter_var(substr($row['ident'],2),FILTER_VALIDATE_INT,array("flags"=>FILTER_FLAG_ALLOW_OCTAL))) $icao = $row['ident'];
136
-						else $icao = 'AFR'.ltrim(substr($row['ident'],2),'0');
137
-					} else $icao = $identicao[0]['icao'].ltrim(substr($row['ident'],2),'0');
146
+						if (filter_var(substr($row['ident'],2),FILTER_VALIDATE_INT,array("flags"=>FILTER_FLAG_ALLOW_OCTAL))) {
147
+							$icao = $row['ident'];
148
+						} else {
149
+							$icao = 'AFR'.ltrim(substr($row['ident'],2),'0');
150
+						}
151
+					} else {
152
+						$icao = $identicao[0]['icao'].ltrim(substr($row['ident'],2),'0');
153
+					}
138 154
 					$data = array_merge($data,array('airline_icao' => $identicao[0]['icao'],'airline_name' => $identicao[0]['name']));
139
-				} else $icao = $row['ident'];
155
+				} else {
156
+					$icao = $row['ident'];
157
+				}
140 158
 				$icao = $Translation->checkTranslation($icao,false);
141 159
 				//$data = array_merge($data,array('registration' => $row['registration'], 'date' => $row['date'], 'ident' => $icao,'url' => $row['url']));
142 160
 				if ($row['airline_name'] != '' && !isset($data['airline_name'])) {
@@ -151,10 +169,14 @@  discard block
 block discarded – undo
151 169
 					//else echo 'No data...'."\n";
152 170
 				}
153 171
 				$data = array_merge($row,$data);
154
-				if ($data['ident'] == null) $data['ident'] = $icao;
172
+				if ($data['ident'] == null) {
173
+					$data['ident'] = $icao;
174
+				}
155 175
 				if ($data['title'] == null) {
156 176
 					$data['message'] = $row['type'].' of '.$row['registration'].' at '.$row['place'].','.$row['country'];
157
-				} else $data['message'] = strtolower($data['title']);
177
+				} else {
178
+					$data['message'] = strtolower($data['title']);
179
+				}
158 180
 				$ids = $Spotter->getAllIDByRegistration($data['registration'],true);
159 181
 				$date = $data['date'];
160 182
 				if (isset($ids[$date])) {
@@ -173,8 +195,9 @@  discard block
 block discarded – undo
173 195
 		if (isset($result)) {
174 196
 			$result[0]['query_number_rows'] = $i;
175 197
 			return $result;
198
+		} else {
199
+			return array();
176 200
 		}
177
-		else return array();
178 201
 	}
179 202
 
180 203
 	/*
@@ -219,7 +242,9 @@  discard block
 block discarded – undo
219 242
 	*/
220 243
 	public function import($file) {
221 244
 		global $globalTransaction, $globalDebug;
222
-		if ($globalDebug) echo 'Import '.$file."\n";
245
+		if ($globalDebug) {
246
+			echo 'Import '.$file."\n";
247
+		}
223 248
 		$result = array();
224 249
 		if (file_exists($file)) {
225 250
 			if (($handle = fopen($file,'r')) !== FALSE) {
@@ -230,8 +255,11 @@  discard block
 block discarded – undo
230 255
 				}
231 256
 				fclose($handle);
232 257
 			}
233
-			if (!empty($result)) $this->add($result,true);
234
-			elseif ($globalDebug) echo 'Nothing to import';
258
+			if (!empty($result)) {
259
+				$this->add($result,true);
260
+			} elseif ($globalDebug) {
261
+				echo 'Nothing to import';
262
+			}
235 263
 		}
236 264
 	}
237 265
 
@@ -267,14 +295,23 @@  discard block
 block discarded – undo
267 295
 					}
268 296
 				}
269 297
 				fclose($handle);
270
-			} elseif ($globalDebug) echo "Can't open ".dirname(__FILE__).'/../install/tmp/cr-all.md5';
271
-		} elseif ($globalDebug) echo 'Download cr-all.md5 failed. '.dirname(__FILE__).'/../install/tmp/cr-all.md5 not here.';
298
+			} elseif ($globalDebug) {
299
+				echo "Can't open ".dirname(__FILE__).'/../install/tmp/cr-all.md5';
300
+			}
301
+		} elseif ($globalDebug) {
302
+			echo 'Download cr-all.md5 failed. '.dirname(__FILE__).'/../install/tmp/cr-all.md5 not here.';
303
+		}
272 304
 		$result = $Common->arr_diff($all_md5_new,$all_md5);
273
-		if (empty($result) && $globalDebug) echo 'Nothing to update';
305
+		if (empty($result) && $globalDebug) {
306
+			echo 'Nothing to update';
307
+		}
274 308
 		foreach ($result as $file => $md5) {
275 309
 			$Common->download('http://data.flightairmap.fr/data/cr/'.$file,dirname(__FILE__).'/../install/tmp/'.$file);
276
-			if (file_exists(dirname(__FILE__).'/../install/tmp/'.$file)) $this->import(dirname(__FILE__).'/../install/tmp/'.$file);
277
-			elseif ($globalDebug) echo 'Download '.$file.' failed';
310
+			if (file_exists(dirname(__FILE__).'/../install/tmp/'.$file)) {
311
+				$this->import(dirname(__FILE__).'/../install/tmp/'.$file);
312
+			} elseif ($globalDebug) {
313
+				echo 'Download '.$file.' failed';
314
+			}
278 315
 		}
279 316
 	}
280 317
 
@@ -291,13 +328,17 @@  discard block
 block discarded – undo
291 328
 		$Image = new Image($this->db);
292 329
 		$Spotter = new Spotter($this->db);
293 330
 
294
-		if (empty($crash)) return false;
331
+		if (empty($crash)) {
332
+			return false;
333
+		}
295 334
 		if (!$new) {
296 335
 			$query_delete = 'DELETE FROM accidents WHERE source = :source';
297 336
 			$sthd = $Connection->db->prepare($query_delete);
298 337
 			$sthd->execute(array(':source' => $crash[0]['source']));
299 338
 		}
300
-		if ($globalTransaction) $Connection->db->beginTransaction();
339
+		if ($globalTransaction) {
340
+			$Connection->db->beginTransaction();
341
+		}
301 342
 		$initial_array = array('ident' => null,'type' => 'accident','url' => null,'registration' => null, 'date' => null, 'place' => null,'country' => null, 'latitude' => null, 'longitude' => null, 'fatalities' => null, 'title' => '','source' => '','aircraft_manufacturer' => null,'aircraft_name' => null,'operator' => null);
302 343
 		$query_check = 'SELECT COUNT(*) as nb FROM accidents WHERE registration = :registration AND date = :date AND type = :type AND source = :source';
303 344
 		$sth_check = $Connection->db->prepare($query_check);
@@ -312,7 +353,9 @@  discard block
 block discarded – undo
312 353
 					return $value === "" ? NULL : $value;
313 354
 				}, $cr);
314 355
 				if ($cr['date'] != '' && $cr['registration'] != null && $cr['registration'] != '' && $cr['registration'] != '?' && $cr['registration'] != '-' && strtolower($cr['registration']) != 'unknown' && $cr['date'] < time() && !preg_match('/\s/',$cr['registration'])) {
315
-					if (strpos($cr['registration'],'-') === FALSE) $cr['registration'] = $Spotter->convertAircraftRegistration($cr['registration']);
356
+					if (strpos($cr['registration'],'-') === FALSE) {
357
+						$cr['registration'] = $Spotter->convertAircraftRegistration($cr['registration']);
358
+					}
316 359
 					$query_check_values = array(':registration' => $cr['registration'],':date' => date('Y-m-d',$cr['date']),':type' => $cr['type'],':source' => $cr['source']);
317 360
 					$sth_check->execute($query_check_values);
318 361
 					$result_check = $sth_check->fetch(PDO::FETCH_ASSOC);
@@ -322,13 +365,19 @@  discard block
 block discarded – undo
322 365
 						if ($cr['date'] > time()-(30*86400)) {
323 366
 							$imgchk = $Image->getSpotterImage($cr['registration']);
324 367
 							if (empty($imgchk)) {
325
-								if ($globalDebug) echo "\t".'Get image for '.$cr['registration'].'...';
368
+								if ($globalDebug) {
369
+									echo "\t".'Get image for '.$cr['registration'].'...';
370
+								}
326 371
 								$Image->addSpotterImage($cr['registration']);
327
-								if ($globalDebug) echo "\t".'Done'."\n";
372
+								if ($globalDebug) {
373
+									echo "\t".'Done'."\n";
374
+								}
328 375
 							}
329 376
 							// elseif ($globalDebug) echo 'Image already in DB'."\n";
330 377
 						}
331
-						if ($cr['title'] == '') $cr['title'] = $cr['registration'].' '.$cr['type'];
378
+						if ($cr['title'] == '') {
379
+							$cr['title'] = $cr['registration'].' '.$cr['type'];
380
+						}
332 381
 						$Spotter->setHighlightFlightByRegistration($cr['registration'],$cr['title'],date('Y-m-d',$cr['date']));
333 382
 					}
334 383
 				}
@@ -337,9 +386,13 @@  discard block
 block discarded – undo
337 386
 					$Connection->db->beginTransaction();
338 387
 				}
339 388
 			}
340
-			if ($globalTransaction) $Connection->db->commit();
389
+			if ($globalTransaction) {
390
+				$Connection->db->commit();
391
+			}
341 392
 		} catch(PDOException $e) {
342
-			if ($globalTransaction) $Connection->db->rollBack();
393
+			if ($globalTransaction) {
394
+				$Connection->db->rollBack();
395
+			}
343 396
 			echo $e->getMessage();
344 397
 		}
345 398
 		$sth_check->closeCursor();
@@ -378,8 +431,11 @@  discard block
 block discarded – undo
378 431
 			return "error : ".$e->getMessage();
379 432
 		}
380 433
 		$row = $sth->fetch(PDO::FETCH_ASSOC);
381
-		if ($row['nb'] > 0) return false;
382
-		else return true;
434
+		if ($row['nb'] > 0) {
435
+			return false;
436
+		} else {
437
+			return true;
438
+		}
383 439
 	}
384 440
 
385 441
 	public static function insert_last_accidents_update() {
Please login to merge, or discard this patch.