-
Notifications
You must be signed in to change notification settings - Fork 2
/
Controller.php
129 lines (124 loc) · 4.49 KB
/
Controller.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
<?php
include 'ReviewModel.php';
include 'WeatherModel.php';
if(isset($_POST['submit'])){
$con = new Controller;
$con->insertReview();
}
Class Controller{
public function getReview($id){
$models = new ReviewModel;
$topics = $models->getAllReview();
$result = array();
if (is_array($topics) || is_object($topics))
{
foreach ($topics['rows'] as $topic) {
$field = $topic['doc'];
if($field['_id'] === $id){
return $field;
}
}
}
return null;
}
public function getTopic(){
$models = new ReviewModel;
$topics = $models->getAllReview();
$result = array();
if (is_array($topics) || is_object($topics))
{
//make array of all topic
foreach($topics['rows'] as $topic) {
$field = $topic['doc'];
array_push($result, '{ "id":"' . $field['_id'] . '","images":"' . $field['image'] . '","topic":"' . $field['topic'] . '","timestamp":"' . $field['timestamp'] . '","tag":"' . $field['tag'] . '"}' );
}
return json_encode($result);
}
}
public function getWeather($la,$lo,$tag){
$weatherModel = new WeatherModel;
$json = file_get_contents('https://ad46647d-1b77-4644-b8c6-78eed562b7a3:NAgf7Zkmlo@twcservice.au-syd.mybluemix.net:443/api/weather/v2/forecast/daily/10day?units=m&geocode='.$la.','.$lo.'&language=en-US');
$obj = json_decode($json);
$rain_day = array();
$rain_night = array();
$result = array();
$forcasts = $obj->{'forecasts'};
$s = 0;
$c = 0;
$t = 0;
$cn = 0;
$tn = 0;
// count stat of forcast each day
foreach ($forcasts as $forcast) {
$day = $forcast->{'day'} ;
$night = $forcast->{'night'};
if($day != NULL){
if(strpos($day->{'shortcast'}, 'sun' ) !== false || strpos($day->{'shortcast'}, 'Sun' ) !== false)
$s++;
else if(strpos($day->{'shortcast'}, 'thunderstorm' ) !== false){
//add date
array_push($rain_day,$day->{'fcst_valid_local'});
$t++;
}
else{
$c++;
}
}
if($night != NULL){
if(strpos($night->{'shortcast'}, 'thunderstorm' ) !== false || strpos($night->{'shortcast'}, 'rain' ) !== false){
//add date
array_push($rain_night,$night->{'fcst_valid_local'});
$tn++;
}
else{
$cn++;
}
}
}
//calculate prop
//day
$rain_day_print = "";
foreach ($rain_day as $value) {
$rain_day_print = $rain_day_print . " " . $value;
}
array_push($result,'{"period":"day","temp":"sun","percent":"'. round(($s/($s+$c+$t))*100,2) .'%","date":null,"description" : "' . $weatherModel->getWeatherByKeys($tag,"day","sun").'"}');
array_push($result,'{"period":"day","temp":"clear","percent":"'. round(($c/($s+$c+$t))*100,2) .'%","date":null,"description" : "' . $weatherModel->getWeatherByKeys($tag,"day","clear").'"}');
array_push($result,'{"period":"day","temp":"rain","percent":"'. round(($t/($s+$c+$t))*100,2) .'%","date":"'. $rain_day_print .'","description" : "' . $weatherModel->getWeatherByKeys($tag,"day","rain").'"}');
//night
$rain_night_print = "";
foreach ($rain_night as $value) {
$rain_night_print = $rain_night_print . " " . $value;
}
array_push($result,'{"period":"night","temp":"clear","percent":"'. round(($cn/($cn+$tn))*100,2) .'%","date":null,"description" : "' . $weatherModel->getWeatherByKeys($tag,"night","clear").'"}');
array_push($result,'{"period":"night","temp":"rain","percent":"'. round(($tn/($cn+$tn))*100,2) .'%","date":"'. $rain_night_print .'","description" : "' . $weatherModel->getWeatherByKeys($tag,"night","rain").'"}');
return json_encode($result);
}
public function insertReview(){
$models = new ReviewModel;
$image = $_POST['image'];
$topic = $_POST['topic'];
$detail = $_POST['detail'];
$tag = $_POST['tag'];
$location = $_POST['location'];
$json = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.$location.'&key=AIzaSyDjBCeazLnByvNQlC8nvbXf-p4hm15MaBo');
$obj = json_decode($json);
$results = $obj->{'results'};
$results = $results[0];
$geo = $results->{'geometry'};
$geo = $geo->{'location'};
$lat = $geo->{'lat'};
$long = $geo->{'lng'};
$succ = $models->insertDB($image,$topic,$detail,$tag,$lat,$long);
$succ = json_decode($succ);
//var_dump($detail);
if($succ->{'ok'} == 'true'){
header( "location: Topic.php" );
exit(0);
}
else{
echo "Submit error pls try again";
echo '<br><a href="PostReview.php"> <button> try again </button> </a>';
}
}
}
?>