-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
183 lines (163 loc) · 4.83 KB
/
functions.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
function isUser($fbid){
$q = mysql_query("SELECT * FROM users WHERE fbid = $fbid");
if(mysql_num_rows($q) > 0){
return true;
}else{
return false;
}
}
function createUser($fbid,$email,$fname,$lname,$location,$latlong,$timezone){
$q = mysql_query("INSERT INTO users(fbid, email, first_name, last_name, location, loc_lat_long, time_zone) VALUES('$fbid', '$email', '$fname', '$lname', '$location', '$latlong', '$timezone')");
}
function getUID($fbid){
$q = mysql_query("SELECT * FROM users WHERE fbid = $fbid");
$r = mysql_fetch_assoc($q);
$uid = $r['uid'];
return $uid;
}
function getEmail($uid){
$q = mysql_query("SELECT * FROM users WHERE uid = $uid");
$r = mysql_fetch_assoc($q);
$email = $r['email'];
return $email;
}
function setEmail($uid, $email){
$q = mysql_query("UPDATE users SET email = '$email' WHERE uid = $uid");
}
function getLocation($uid){
$q = mysql_query("SELECT * FROM users WHERE uid = $uid");
$r = mysql_fetch_assoc($q);
$location = $r['location'];
return $location;
}
function setLocation($uid, $location){
$q = mysql_query("UPDATE users SET location = '$location' WHERE uid = $uid");
}
function getFBLatLong($locID, $token){
$facebook = new Facebook(array(
'appId' => '294829963933906',
'secret' => '7337ff3d6e2b1c9999e638773fa51880',
));
$q = "SELECT latitude, longitude FROM place WHERE page_id = $locID";
$latlong = $facebook->api(array(
'method' => 'fql.query',
'query' => $q
)
);
$lat = $latlong[0]['latitude'];
$long = $latlong[0]['longitude'];
$latlong = "$lat,$long";
return $latlong;
}
function getLatLong($uid){
$q = mysql_query("SELECT * FROM users WHERE uid = $uid");
$r = mysql_fetch_assoc($q);
$d = $r['loc_lat_long'];
return $d;
}
function setLatLong($uid, $latlong){
$q = mysql_query("UPDATE users SET loc_lat_long = '$latlong' WHERE uid = $uid");
}
if($loginstate){
//user is logged in.
//check to see if this is their first time:
$fbinfo = $facebook->api('/me');
// error_log(print_r($fbinfo));
if(!isUser($fbid)){
$email = $fbinfo['email'];
$fname = $fbinfo['first_name'];
$lname = $fbinfo['last_name'];
$timezone = $fbinfo['timezone'];
$location = $fbinfo['location']['name'];
createUser($fbid, $email, $fname, $lname, $loction, $latlong, $timezone);
$uid = getUID($fbid);
}else{
$uid = getUID($fbid);
$email = getEmail($uid);
$location = getLocation($uid);
if(empty($email)){
$email = $fbinfo['email'];
setEmail($uid, $email);
}
if(empty($location)){
$locID = $fbinfo['location']['id'];
if(!empty($locID)){
$latlong = getFBLatLong($locID, $token);
$location = $fbinfo['location']['name'];
setLocation($uid, $location);
setLatLong($uid, $latlong);
}
}
$fname = $fbinfo['first_name'];
}
}
function doneToday($uid){
if(!empty($uid)){
$q = mysql_query("SELECT * FROM daily_goals WHERE uid = $uid AND completed != 0 ORDER BY goal_date DESC LIMIT 1");
if(mysql_num_rows($q) > 0){
return true;
}else{
return false;
}
}
}
function myGoalToday($uid){
if(!empty($uid)){
$q = mysql_query("SELECT * FROM daily_goals WHERE uid = $uid and DATE(goal_date) = DATE(NOW())");
$r = mysql_fetch_assoc($q);
$gid = $r['ugid'];
return $gid;
}
}
function getGoalText($ugid){
if(!empty($ugid)){
$q = mysql_query("SELECT * FROM goals NATURAL JOIN daily_goals WHERE ugid = $ugid");
// echo "SELECT * FROM goals NATURAL JOIN daily_goals WHERE ugid = $ugid";
$r = mysql_fetch_assoc($q);
$text = $r['goal'];
return $text;
}
}
function todayStatus($uid){
$q = mysql_query("SELECT * FROM daily_goals WHERE uid = $uid AND DATE(goal_date) = DATE(NOW())");
$r = mysql_fetch_assoc($q);
if($r > 0){
return true;
}else{
return false;
}
}
function getTodayStatus($uid){
$q = mysql_query("SELECT * FROM daily_goals WHERE uid = $uid AND DATE(goal_date) = DATE(NOW())");
$r = mysql_fetch_assoc($q);
$status = $r['completed'];
return $status;
}
function getAllTodaysAwesome(){
$q = mysql_query("SELECT * FROM daily_goals NATURAL JOIN goals WHERE DATE(goal_date) = DATE(NOW()) GROUP BY gid ORDER BY goal_date DESC");
$goals = array();
while($r = mysql_fetch_assoc($q)){
$goals[] = array('uid' => $r['uid'], 'ugid' => $r['ugid'], 'goal' => $r['goal']);
}
return $goals;
}
function hasActiveGoal($uid){
$q = mysql_query("SELECT * FROM daily_goals WHERE uid = $uid AND completed = 0 ORDER BY goal_date DESC LIMIT 1");
// echo "SELECT * FROM daily_goals WHERE uid = $uid AND completed = 0 ORDER BY goal_date DESC LIMIT 1";
if(mysql_num_rows($q) > 0){
return true;
}else{
return false;
}
}
function getActiveGoal($uid){
if(!empty($uid)){
$q = mysql_query("SELECT * FROM daily_goals WHERE uid = $uid AND completed = 0 ORDER BY goal_date DESC LIMIT 1");
// echo "SELECT * FROM daily_goals WHERE uid = $uid AND completed = 0 ORDER BY goal_date DESC LIMIT 1";
$r = mysql_fetch_assoc($q);
$ugid = $r['ugid'];
return $ugid;
}
}
?>