-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.php
144 lines (131 loc) · 4.47 KB
/
database.php
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
<?php
namespace PostgreSQL;
/**
* Represent the Connection
*/
class Connection {
/**
* Connection
* @var type
*/
private static $conn;
/**
* Connect to the database and return an instance of \PDO object
* @return \PDO
* @throws \Exception
*/
function __construct() {}
public function connect() {
// read parameters in the ini configuration file
$params = parse_ini_file('database.ini');
if ($params === false) {
throw new Exception("Error reading database configuration file");
}
// connect to the postgresql database
$conStr = sprintf("pgsql:host=%s;port=%d;dbname=%s;user=%s;password=%s",
$params['host'],
$params['port'],
$params['database'],
$params['user'],
$params['password']);
$pdo = new \PDO($conStr);
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
return $pdo;
}
public function AllMark()
{
$connection = $this->get()->connect();
$query = 'SELECT
pits."ID",
pits."Address",
pits."Time_Create",
pits."X",
pits."Y",
pits."About"
FROM
yama.pits
WHERE
pits."ID" > 0
ORDER BY
pits."ID" ASC';
$res = $connection->query($query);
$json = ["type" => "FeatureCollection",
"features" => []];
$res->setFetchMode(\PDO::FETCH_NUM);
while ($row = $res->fetch())
{
$temp["type"] = "Feature";
$temp["id"] = $row[0];
$temp["geometry"]["type"] = "Point";
$temp["geometry"]["coordinates"] = [(double)$row[3], (double)$row[4]];
//$temp["properties"]["balloonContent"] = 'Дата добавления: '.date('m.d.Y H:i:s', strtotime($row[2])).' <br />'.$row[5]."<br /> Фото: <br /><img height='100%' width='300px' src='img_pit/foto.jpg'>";
//$temp["properties"]["balloonContent"] =
//'Дата добавления: '.date('m.d.Y H:i:s', strtotime($row[2])).'
//<br />'.$row[5]."<br /> Фото: <br /><img height='100%' width='300px' src='img_pit/foto.jpg'>";
$temp["properties"]["balloonContent"] =
"<img style='width: 100%' src='img_pit/pit_$row[0].jpg'><br /><small>".
date('m.d.Y H:i', strtotime($row[2])).'</small><br />'.$row[5].'<br /><b>'.$row[1].'</b>';
$temp["properties"]["clusterCaption"] = '№'.$row[0];
$temp["properties"]["hintContent"] = 'Яма №'.$row[0];
$json['features'][] = $temp;
}
$json = json_encode($json, JSON_UNESCAPED_UNICODE);
return $json;
}
public function AddMark($cords, $about, $address)
{
if($cords != '') {
$data['cords'] = explode(', ', $cords);
} else {
$json = 'https://geocode-maps.yandex.ru/1.x/?format=json&geocode=Ульяновск, '.$address;
$json = json_decode(file_get_contents($json));
$data['cords'] = explode(' ', $address->response->GeoObjectCollection->featureMember[0]->GeoObject->Point->pos);
}
$data['about']=$about;
$connection = $this->get()->connect();
$query = 'INSERT INTO "yama"."pits" ("Address","X","Y","About") '.
'VALUES (\''.$address.'\', '.(double)$data['cords'][0].' ,'.(double)$data['cords'][1].', \''.$about.'\');';
$connection->query($query);
$id = $connection->query('SELECT pits."ID" FROM yama.pits ORDER BY pits."ID" DESC LIMIT 1')->fetch()[0];
require_once 'upload.php';
return $data;
}
public function LastMark()
{
$connection = $this->get()->connect();
$query = 'SELECT
pits."ID",
pits."Address",
pits."Time_Create",
pits."X",
pits."Y",
pits."About"
FROM
yama.pits
WHERE
pits."ID" > 0
ORDER BY
pits."ID" DESC LIMIT 5';
$res = $connection->query($query);
$res->setFetchMode(\PDO::FETCH_NUM);
$array;
while ($row = $res->fetch()) {
#... вывод последних 4 ям
# ABOUT, ID
$array[]=$row;
}
return $array;
}
/**
* return an instance of the Connection object
* @return type
*/
public static function get() {
if (null === static::$conn) {
static::$conn = new static();
}
return static::$conn;
}
private function __clone() {}
private function __wakeup() {}
}