OMGM Code
Status: Beta
Brought to you by:
ifkkiruna
--- a +++ b/omgm/db.php @@ -0,0 +1,168 @@ +<?php +include_once("define.php"); + +class Db{ + + private $dbLink; + + function __construct(){ + $this->connect(); + } + + function __destruct(){ + @mysql_close($this->dbLink); + } + + public function deleteMap($mapId){ + $sql = "DELETE FROM omgm_maps WHERE id=$mapId"; + $this->query($sql); + } + + public function deleteAllTiles(){ + $sql = "DELETE FROM omgm_tiles"; + $this->query($sql); + } + + public function deleteTilesByMap($mapId){ + $sql = "DELETE FROM omgm_tiles WHERE mapId=$mapId"; + $this->query($sql); + } + + public function deleteTilesAtZoom($zoom){ + $sql = "DELETE FROM omgm_tiles WHERE zoom=$zoom"; + $this->query($sql); + } + + public function setNumTiles(&$maps){ + $sql = "SELECT mapId, count(id) FROM omgm_tiles GROUP BY mapId"; + + $res = $this->query($sql); + + while( $row = mysql_fetch_row($res) ){ + $map = $maps[$row[0]]; + if($map){ + $map["totalTiles"] = $row[1]; + $maps[$map["id"]] = $map; + } + } + } + + public function getTilesByName($tileX, $tileY){ + $sql = "SELECT * FROM omgm_tiles WHERE tileX = $tileX AND tileY = $tileY ORDER BY mapId"; + + $res = $this->query($sql); + + $tiles = array(); + while( $row = mysql_fetch_assoc($res)){ + $tiles[] = $row; + } + return $tiles; + } + + public function getTiles($mapId){ + $sql = "SELECT * FROM omgm_tiles WHERE mapId = $mapId ORDER BY zoom, tileX, tileY"; + + $res = $this->query($sql); + + $tiles = array(); + while( $row = mysql_fetch_assoc($res)){ + $tiles[] = $row; + } + return $tiles; + } + + public function getMaps($sortDesc = false){ + + $sort = "ASC"; + if($sortDesc){ + $sort = "DESC"; + } + + $sql = "SELECT * FROM omgm_maps ORDER BY addedDate ".$sort; + + $res = $this->query($sql); + + $maps = array(); + while( $row = mysql_fetch_assoc($res)){ + $maps[$row["id"]] = $row; + } + return $maps; + } + + + public function setUnmerged($mapId, $tileX, $tileY){ + $sql = "UPDATE omgm_tiles SET duplicate=0 WHERE mapId=$mapId AND tileX=$tileX AND tileY=$tileY"; + + $res = $this->query($sql); + + if(!$res){ + $message = 'Invalid query: ' . mysql_error() . "\n"; + $message .= 'Whole query: ' . $query; + die($message); + } + } + + public function setMerged($mapId, $tileX, $tileY){ + $sql = "UPDATE omgm_tiles SET duplicate=1 WHERE mapId=$mapId AND tileX=$tileX AND tileY=$tileY"; + + $res = $this->query($sql); + + if(!$res){ + $message = 'Invalid query: ' . mysql_error() . "\n"; + $message .= 'Whole query: ' . $query; + die($message); + } + } + + public function addTile($mapId, $zoom, $tileX, $tileY){ + $sql = "INSERT INTO omgm_tiles "; + $sql .= "(mapId, zoom, tileX, tileY, duplicate) "; + $sql .= "VALUES($mapId,$zoom,$tileX,$tileY,0)"; + + $res = $this->query($sql); + + if(!$res){ + $message = 'Invalid query: ' . mysql_error() . "\n"; + $message .= 'Whole query: ' . $query; + die($message); + } + } + + public function addMap($bbox, $data){ + $sql = "INSERT INTO omgm_maps "; + $sql .= "(name,epsg,contactName,contactInfo,addedDate,addedIp,south,west,north,east) "; + $sql .= "VALUES('".$data["name"]."', ".$data["epsg"].", '".$data["contactName"]."', "; + $sql .= "'".$data["contactInfo"]."', '".date("Y-m-d H:i:s",time())."', "; + $sql .= "'".$_SERVER["REMOTE_ADDR"]."',".$bbox["LL"]["LAT"].",".$bbox["LL"]["LNG"].","; + $sql .= $bbox["UR"]["LAT"].",".$bbox["UR"]["LNG"].")"; + + + $res = $this->query($sql); + + if(!$res){ + $message = 'Invalid query: ' . mysql_error() . "\n"; + $message .= 'Whole query: ' . $query; + die($message); + } + + $id = mysql_insert_id($this->dbLink); + return $id; + } + + private function query($sql){ + return @mysql_query($sql, $this->dbLink); + } + + private function connect(){ + $this->dbLink = mysql_connect(OMGMDBHOST, OMGMDBUSER, OMGMDBPASSWD) + or die("Unable to connect to SQL server. " . mysql_error());; + + mysql_select_db(OMGMDB) or die("Unable to select database. " . mysql_error()); + $create_table_query = "CREATE TABLE IF NOT EXISTS `omgm_maps` ( `id` mediumint(8) unsigned NOT NULL auto_increment, `name` varchar(30) default NULL, `epsg` mediumint(8) unsigned NOT NULL, `contactName` varchar(35) default NULL, `contactInfo` varchar(50) default NULL, `addedDate` datetime NOT NULL, `addedIp` varchar(27) NOT NULL, `south` double NOT NULL, `west` double NOT NULL, `north` double NOT NULL, `east` double NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `name` (`name`)) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=48;"; + mysql_query($create_table_query); + $create_table_query = "CREATE TABLE IF NOT EXISTS `omgm_tiles` ( `id` bigint(20) unsigned NOT NULL auto_increment, `mapId` mediumint(8) unsigned NOT NULL, `zoom` tinyint(3) unsigned NOT NULL, `tileX` int(11) NOT NULL, `tileY` int(11) NOT NULL, `duplicate` bit(1) NOT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=19131;"; + mysql_query($create_table_query); + } + +} +?>