/
ajax.php
131 lines (110 loc) · 3.23 KB
/
ajax.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
<?php
$taskId = "entry_299071204";
$categoryId = "entry_920947763";
$tagId = "entry_662564008";
$timeId = "entry_64794649";
/**
* Time Converter
*/
function timeConvert($time) {
$time = str_replace(" ", "", $time);
$time = str_replace(".", ",", $time);
$time = trim($time);
if(preg_match("/:/", $time)) {
// return "je tam";
if(substr($time, 0, 1)==":") {
$minutes = substr($time, 1);
} else {
$timeArr = explode(":", $time);
$minutes = ($timeArr[0]*60)+(isset($timeArr[1]) ? $timeArr[1] : 0);
}
$time = str_replace(".", ",", round($minutes/60,2));
}
return $time;
}
/**
* Data
*/
if(isset($_GET['get_data'])) {
$categories = array();
$tags = array();
/**
* HTML of Form
*/
$ch = curl_init();
$googleForm = "https://docs.google.com/forms/d/".$_GET['form_id']."/viewform";
curl_setopt($ch, CURLOPT_URL, $googleForm);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$htmlForm = curl_exec($ch);
curl_close($ch);
/**
* Get <select> box options
*/
$dom = new DOMDocument();
$dom->loadHTML($htmlForm);
$xpath = new DOMXPath($dom);
$htmlCategories = $xpath->evaluate("//select[@id='$categoryId']//option");
foreach($htmlCategories as $category) {
if($category->nodeValue!="") $categories[] = $category->nodeValue;
}
$htmlTags = $xpath->evaluate("//select[@id='$tagId']//option");
foreach($htmlTags as $tag) {
if($tag->nodeValue!="") $tags[] = $tag->nodeValue;
}
sort($categories);
sort($tags);
$return = array(
"categories" => $categories,
"tags" => $tags,
);
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
echo json_encode($return);
}
/**
* Data sending
*/
if(isset($_POST['send_data'])) {
$googleFormResponse = $googleForm = "https://docs.google.com/forms/d/".$_POST['form_id']."/formResponse";
$task = isset($_POST['task']) ? $_POST['task'] : "";
$category = isset($_POST['category']) ? $_POST['category'] : "";
$tag = isset($_POST['tag']) ? $_POST['tag'] : "";
$time = isset($_POST['time']) ? $_POST['time'] : "";
$taskId = str_replace("_", ".", $taskId);
$categoryId = str_replace("_", ".", $categoryId);
$tagId = str_replace("_", ".", $tagId);
$timeId = str_replace("_", ".", $timeId);
$postData = array(
$taskId => $task,
$categoryId => $category,
$tagId => $tag,
$timeId => timeConvert($time),
"submit" => true
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $googleFormResponse);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$result = curl_exec($ch);
}
/**
* Time Format
*/
if(isset($_GET['time_format'])) {
$time = isset($_GET['time_format']) ? $_GET['time_format'] : 0;
$time = str_replace(",", ".", $time);
$time = ($time*60*60)-3600;
$return = array();
$return['time'] = $time;
$return['time_format'] = date("H:i", $time);
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
echo json_encode($return);
}