-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
170 lines (159 loc) · 7.09 KB
/
api.php
File metadata and controls
170 lines (159 loc) · 7.09 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/**
* Created by PhpStorm.
* User: mmd
* Date: 16.10.14
* Time: 13:38
*/
class API
{
private $DB;
public function __construct ()
{
include_once('dbconfig.php');
$this->DB = $DBH;
}
function connectToTMS ()
{
// include_once('tms_dbconfig.php');
// $this->TMS = $TMS_DBH;
}
public function handleMobileRequest ()
{
$result = array();
switch ($_SERVER['REQUEST_METHOD']) {
case 'GET': {
if (isset($_GET['cmd'])) {
$result['cmd'] = $_GET['cmd'];
switch ($_GET['cmd']) {
case 'grp':
{
$sql = 'SELECT name FROM groups ORDER BY name';
$groupsArray = array();
foreach($this->DB->query($sql) as $row){
array_push($groupsArray,$row['name']);
}
$result['groups'] = $groupsArray;
$result['result'] = 'true';
}
break;
case 'tt':
{
$user_group = $_GET['user_group'];
$sql = "SELECT timetable.date,
timetable.type,
timetable.offset,
rooms.name as room,
lecturers.name,
lecturers.surname,
lecturers.patronymic,
disciplines.name as discipline
FROM timetable JOIN disciplines ON timetable.id_discipline=disciplines.id
JOIN lecturers ON timetable.id_lecturer=lecturers.id
JOIN rooms ON timetable.id_room = rooms.id
JOIN groups ON timetable.id_group = groups.id
WHERE groups.name LIKE '%$user_group%' ORDER BY timetable.date, timetable.offset";
$respArr = array();
foreach($this->DB->query($sql) as $row){
$tt_array = array();
$tt_array['name'] = $row['name'];
$tt_array['patronymic'] = $row['patronymic'];
$tt_array['type'] = $row['type'];
$tt_array['room'] = $row['room'];
$tt_array['offset'] = $row['offset'];
$tt_array['surname'] = $row['surname'];
$tt_array['discipline'] = $row['discipline'];
$tt_array['date'] = $row['date'];
array_push($respArr,$tt_array);
}
$result['tt'] = $respArr;
$result['result'] = 'true';
}
break;
case 'tt_g':
{
$room = $_GET['room'];
$sql = "SELECT timetable.date,
timetable.type,
timetable.offset,
rooms.name as room,
lecturers.name,
lecturers.surname,
lecturers.patronymic,
disciplines.name as discipline
FROM timetable JOIN disciplines ON timetable.id_discipline=disciplines.id
JOIN lecturers ON timetable.id_lecturer=lecturers.id
JOIN rooms ON timetable.id_room = rooms.id
JOIN groups ON timetable.id_group = groups.id
WHERE rooms.name LIKE '%$room%' ORDER BY timetable.date, timetable.offset";
$respArr = array();
foreach($this->DB->query($sql) as $row){
$tt_array = array();
$tt_array['name'] = $row['name'];
$tt_array['patronymic'] = $row['patronymic'];
$tt_array['type'] = $row['type'];
$tt_array['room'] = $row['room'];
$tt_array['offset'] = $row['offset'];
$tt_array['surname'] = $row['surname'];
$tt_array['discipline'] = $row['discipline'];
$tt_array['date'] = $row['date'];
array_push($respArr,$tt_array);
}
$result['tt'] = $respArr;
$result['result'] = 'true';
}
break;
}
}
else
$result['result'] = 'false';
echo json_encode($result);
}
break;
case
'POST': {
if (isset($_POST['cmd'])) {
$result['cmd'] = $_POST['cmd'];
switch ($_POST['cmd']) {
case 'apns':
{
if ($this->userExists($_POST['user_id']) && isset($_POST['user_apns_token'])) {
$user_id = $_POST['user_id'];
$data = array($_POST['user_apns_token'],$user_id);
$STH = $this->DB->prepare('UPDATE users SET user_apns_token = (?) WHERE user_id = (?)');
$STH->execute($data);
$result['result'] = 'true';
}
else
$result['result'] = 'false';
}
break;
case 'gcm':
{
if ($this->userExists($_POST['user_id']) && isset($_POST['user_gcm_token'])) {
$user_id = $_POST['user_id'];
$data = array($_POST['user_gcm_token'],$user_id);
$STH = $this->DB->prepare('UPDATE users SET user_gcm_token = (?) WHERE user_id = (?)');
$STH->execute($data);
$result['result'] = 'true';
}
else
$result['result'] = 'false';
}
break;
default: { //неизвестная команда
$result['result'] = "false";
}
break;
}
echo json_encode($result);
} else
http_response_code(400); //bad request
}
break;
}
}
}
$API = new API();
$API->handleMobileRequest();
?>