/** receives a new item or item update, returns the delay of the change getting here */ function remoteItemUpdate($item) { global $nodeId; $db = $this->db; $id = new Id($item['station'], $item['entry_date'], $item['track']); $where = $id->whereClause(); $where2 = $id->whereClause2('i'); // get list of db fields $tableInfo = $db->tableInfo('sotf_items', DB_TABLEINFO_ORDER); $fields = $tableInfo['order']; // TODO: needs synchronization if two different requests try to insert the same thing parallelly $existing = $db->getRow("SELECT i.*, s.node FROM sotf_items i, sotf_stations s WHERE i.station=s.station AND {$where2}"); if (empty($existing)) { foreach ($item as $key => $value) { if ($value && array_key_exists($key, $fields)) { $value = addslashes(stripslashes($value)); $sql1 .= "{$key}, "; $sql2 .= " '{$value}', "; } } $sql1 = substr($sql1, 0, strlen($sql1) - 2); // chop trailing comma $sql2 = substr($sql2, 0, strlen($sql2) - 2); // chop trailing comma $sql = "INSERT INTO sotf_items ( {$sql1} ) VALUES ( {$sql2} )"; $res = $db->query($sql); if (DB::isError($res)) { die($res->getMessage()); //return -1; } } else { // need UPDATE instead of INSERT if ($existing['node'] == $nodeId) { return 0; } $t1 = strtotime($item['last_change']); $t2 = strtotime($existing['last_change']); if ($t1 <= $t2) { return 0; } $sql = "UPDATE sotf_items SET "; foreach ($item as $key => $value) { if ($value && array_key_exists($key, $fields) && $key != 'station' && $key != 'entry_date' && $key != 'track' && $key != 'id') { $value = addslashes(stripslashes($value)); $sql .= " {$key}='{$value}', "; } } $sql = substr($sql, 0, strlen($sql) - 2); // chop trailing comma $sql = $sql . " WHERE {$where}"; $res = $db->query($sql); if (DB::isError($res)) { die($res->getMessage()); //return -1; } $updated = true; } debug("item changed?", $item); $t1 = strtotime($item['last_change']); $now = time(); return $now - $t1; }