-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.php
181 lines (164 loc) · 6.19 KB
/
config.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
<?php
// Start session.
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
// Python backend configuration.
$python_base_url = 'http://optilife.pacassi.ch:5000/api/';
// Require composer's autoloader.
require 'vendor/autoload.php';
// User data helper.
if (!function_exists('initUserData')) {
function initUserData() {
global $python_base_url;
// Don't load user data if not logged in.
if (!isset($_SESSION['uid'])) {
return;
}
// Get user statistics.
$python_url = 'food/log/' . $_SESSION['uid'];
$python_method = 'GET';
$python_client = new GuzzleHttp\Client([
'base_uri' => $python_base_url,
]);
$_SESSION['user_statistics'] = $python_client->get($python_url)->getBody();
// Get user health index.
$python_url = 'users/health-index/' . $_SESSION['uid'];
$python_method = 'GET';
$python_client = new GuzzleHttp\Client([
'base_uri' => $python_base_url,
]);
$python_response = $python_client->get($python_url)->getBody();
$response_object = json_decode($python_response, true);
$_SESSION['user_health_index'] = $response_object;
// Get user budget spending.
$python_url = 'users/' . $_SESSION['uid'];
$python_method = 'GET';
$python_client = new GuzzleHttp\Client([
'base_uri' => $python_base_url,
]);
$python_response = $python_client->get($python_url)->getBody();
$response_object = json_decode($python_response, true);
$_SESSION['user_budget'] = round(100 - ($response_object['actual_budget'] / $response_object['monthly_budget']) * 100);
}
}
// HOTFIX: Always load user data.
initUserData();
// Helper variables.
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
$base_ajax_url = $protocol . '://' . $_SERVER['SERVER_NAME'] . '/ajax.php';
// Define pages.
$page_links = [
'dashboard' => [
'hidden' => FALSE,
'link' => $protocol . '://' . $_SERVER['SERVER_NAME'] . '/?page=dashboard',
'ajax-link' => $base_ajax_url . '?page=dashboard',
'text' => 'Dashboard',
'template' => 'pages/dashboard.php',
'body_cls' => 'page-dashboard',
'nav_cls' => 'menu-item menu-item--dashboard',
'icon' => 'dashboard.svg',
],
'upload' => [
'hidden' => FALSE,
'link' => $protocol . '://' . $_SERVER['SERVER_NAME'] . '/?page=upload',
'ajax-link' => $base_ajax_url . '?page=upload',
'text' => 'Add data',
'template' => 'pages/upload.php',
'body_cls' => 'page-upload',
'nav_cls' => 'menu-item menu-item--upload',
'icon' => 'upload.svg',
],
'profile' => [
'hidden' => FALSE,
'link' => $protocol . '://' . $_SERVER['SERVER_NAME'] . '/?page=profile',
'ajax-link' => $base_ajax_url . '?page=profile',
'text' => 'Profile',
'template' => 'pages/profile.php',
'body_cls' => 'page-profile',
'nav_cls' => 'menu-item menu-item--profile',
'icon' => 'profile.svg',
],
'logout' => [
'hidden' => FALSE,
'link' => $protocol . '://' . $_SERVER['SERVER_NAME'] . '/?page=logout',
'ajax-link' => $base_ajax_url . '?action=logout',
'text' => 'Log out',
'template' => 'pages/profile.php',
'body_cls' => 'page-profile',
'nav_cls' => 'menu-item menu-item--logout',
'icon' => 'logout.svg',
],
// Hiden pages
'label_food' => [
'hidden' => TRUE,
'link' => $protocol . '://' . $_SERVER['SERVER_NAME'] . '/?page=labelFood',
'ajax-link' => $base_ajax_url . '?page=labelFood',
'text' => 'Label food',
'template' => 'pages/label_food.php',
'body_cls' => 'page-label-food',
'nav_cls' => 'menu-item menu-item--label-food',
'icon' => 'home.svg',
],
];
// Define navigation.
$nav_links = $page_links;
// Get user statistics.
if (!empty($_GET['page'])) {
if ($_GET['page'] == 'dashboard') {
// Get user statistics.
initUserData($python_base_url);
$python_url = 'food/log/' . $_SESSION['uid'];
$python_method = 'GET';
$python_client = new GuzzleHttp\Client([
'base_uri' => $python_base_url,
]);
$_SESSION['user_statistics'] = $python_client->get($python_url)->getBody();
// Get user health index.
$python_url = 'users/health-index/' . $_SESSION['uid'];
$python_method = 'GET';
$python_client = new GuzzleHttp\Client([
'base_uri' => $python_base_url,
]);
$python_response = $python_client->get($python_url)->getBody();
$response_object = json_decode($python_response, true);
$_SESSION['user_health_index'] = $response_object;
// Get user budget spending.
$python_url = 'users/' . $_SESSION['uid'];
$python_method = 'GET';
$python_client = new GuzzleHttp\Client([
'base_uri' => $python_base_url,
]);
$python_response = $python_client->get($python_url)->getBody();
$response_object = json_decode($python_response, true);
$_SESSION['user_budget'] = round(100 - ($response_object['actual_budget'] / $response_object['monthly_budget']) * 100);
}
}
// Define current page.
if (!empty($_GET['page'])) {
if (array_key_exists($_GET['page'], $page_links)) {
$page_title = $page_links[$_GET['page']]['text'];
$page_template = $page_links[$_GET['page']]['template'];
$page_body_cls = $page_links[$_GET['page']]['body_cls'];
}
}
// By default, show the log in page or the dashboard.
if (empty($page_template)) {
if (isset($_SESSION['uid'])) {
$page_title = 'Dashboard';
$page_template = 'pages/dashboard.php';
$page_body_cls = 'page-dashboard';
} else {
$page_title = 'Login';
$page_template = 'pages/login.php';
$page_body_cls = 'page-login';
}
}
// Set logo url.
if (isset($_SESSION['uid'])) {
$logo_url = $protocol . '://' . $_SERVER['SERVER_NAME'] . '/?page=dashboard';
$logo_data_url = $protocol . '://' . $_SERVER['SERVER_NAME'] . '/ajax.php?page=dashboard';
} else {
$logo_url = $protocol . '://' . $_SERVER['SERVER_NAME'] . '/?page=login';
$logo_data_url = $protocol . '://' . $_SERVER['SERVER_NAME'] . '/ajax.php?page=login';
}