#!/usr/bin/env ruby
#coding:utf-8
print"Content-type: text/html charset=UTF-8\n\n"
require'sqlite3'
require'cgi'
cgi = CGI.new(:accept_charaset => "UTF-8")
db = SQLite3::Database.new("sql/test.sq3")
db.results_as_hash = true #DBの値をハッシュとして受け取る
cookieuke = Hash.new # cookie値保存用のHash
if (c=ENV['HTTP_COOKIE']) # 環境変数 HTTP_COOKIE にcookieリストがある
c.split(/[;,]\s+/).each do |exp| # (セミコロンかカンマ)+空白 で分解
if /(.*)=(.*)/ =~ exp # =の前後で分解 「変数=値;」
key = CGI::unescape($1) # 変数も値もエスケープされているので
value = CGI::unescape($2) # 元に戻す
cookieuke[key] = CGI::unescape(value)
end
end
end
name = cookieuke["name"]
id = cookieuke["id"].to_i
image = ["","face","angry","sad"]
if ARGV[0] == "error" || ARGV[1] == nil || (name == "" && id == 0)
message = ""
redirect = "redirect"
if name == "" && id == 0
redirect = "add"
message = "登録されていないため登録ページへ飛びます"
elsif ARGV[0] == "error"
message = "位置情報が正しく取れなかったため、もう一度取り直します"
elsif ARGV[1] == nil
message = "想定されないアクセスがされたため登録ページに戻ります"
redirect = "add"
end
printf(<<_EOS_,redirect,message,redirect)
<!DOCTYPE html>
<html>
<head>
<title>MISS</title>
<meta http-equiv="refresh" content="3;%s.html">
</head>
<body>
<p>%s</p>
<p>移動しない場合は<a href="%s.html">こちら</a>から移動してください</p>
</body>
</html>
_EOS_
else
lon = ARGV[0].to_f
lat = ARGV[1].to_f
data = Hash.new
begin
db.execute("select * from main");
rescue
db.execute("create table main(id INTEGER PRIMARY KEY AUTOINCREMENT,name,lon,lat,sta)");
db.execute("insert into main(name,lon,lat,sta) values('#{name}','#{lon}','#{lat}','#{sta}')");
end
sta = cgi["v_radio"].to_i
if sta != 0
bun = "good"
sql = "update main set name=?,lon=?,lat=?,sta=? where id=?"
begin
db.execute(sql,name,lon,lat,sta,id)
rescue
#db.execute("insert into main(name,lon,lat,sta) values('#{name}','#{lon}','#{lat}','#{sta}')");
end
else
bun = "boo"
sql = "update main set name=?,lon=?,lat=? where id=?"
begin
db.execute(sql,name,lon,lat,id)
rescue
#db.execute("insert into main(name,lon,lat,sta) values('#{name}','#{lon}','#{lat}','#{sta}')");
end
end
db.execute("select * from main") do |load|
data[load['id']] = [load['name'],load['lon'],load['lat'],load['sta']]
end
printf(<<_EOS_,lon.to_f,lat.to_f,lon.to_f,lat.to_f)
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="leaflet/leaflet.css"/>
<script src="leaflet/leaflet.js"></script>
<style>
html,body{
height: 100%
}
#map{
height: 95%
}
body{
padding: 0;
margin: 0;
}
form{
font-size:5mm;
}
</style>
<title>Getting Started with Leaflet</title>
<meta http-equiv="refresh" content="20;redirect.html">
</head>
<body>
<form method="POST" action="./leaf.rb?%f+%f">
<input name="v_radio" type="radio" value="1">順調
<input name="v_radio" type="radio" value="2">トラブル
<input name="v_radio" type="radio" value="3">ヘルプ
<input type="submit" value="OK">
</form>
<div id="map"></div>
<script type="text/javascript">
var hmap = L.map('map',{
center: [%f,%f],
zoom: 17 //1~18
});
L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',{
attribution: '© OpenStreetMap contributors'
}).addTo(hmap);
_EOS_
for id in data.keys
if data[id][1] == "LON"
next
else
gazo = data[id][3]
printf(<<_EOS_,data[id][1].to_f,data[id][2].to_f,image[gazo],data[id][0])
L.marker([%f,%f],{
clickable:true,
draggable:true,
icon:L.icon({
iconUrl: 'leaflet/images/%s.png',
iconAnchor: [0,0],
popupAnchor: [10,5]
})
})
.bindPopup('%s')
.addTo(hmap);
_EOS_
end
end
print"
</script>
</body>
</html>\n"
end