-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistribution_controller.php
177 lines (163 loc) · 8.31 KB
/
distribution_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
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
<?php
/*
All Emoncms code is released under the GNU Affero General Public License.
See COPYRIGHT.txt and LICENSE.txt.
---------------------------------------------------------------------
Emoncms - open source energy visualisation
Part of the OpenEnergyMonitor project: http://openenergymonitor.org
*/
// no direct access
defined('EMONCMS_EXEC') or die('Restricted access');
function distribution_controller() {
global $mysqli, $user, $session, $route;
require_once "Modules/distribution/distribution_model.php";
$distribution = new Distribution($mysqli, $user);
if (!$session['read']) {// Login
if ($route->format == 'html') {
if ($route->action == 'login') {
$result = view("Modules/distribution/Views/distribution_login_view.php", array());
return array('content' => $result);
}
if ($route->action == 'tokenlogin') {
$day_token = $distribution->get_day_token();
$token_login = post('day_token');
if ($day_token == $token_login) {
$orgs = $distribution->get_organizations();
$organizationid = 0;
session_regenerate_id();
//$_SESSION['userid'] = $userData_id;
$_SESSION['read'] = 1;
$_SESSION['write'] = 1;
$_SESSION['admin'] = 0;
$_SESSION['distribution_day_access'] = true;
$result = view("Modules/distribution/Views/preparation_view.php", array('organizations' => $orgs, 'organizationid' => $organizationid));
return array('content' => $result);
}
}
}
}
// There are no actions in the distribution module that can be performed with less than write privileges
if (!$session['write'])
return array('content' => false);
$result = false;
if ($session['admin'] == 1) {
$role = Roles::SUPERADMINISTRATOR;
$organizationid = 0;
}
else if (isset($_SESSION['distribution_day_access']) && $_SESSION['distribution_day_access'] == true) {
$role = Roles::DAYVOL;
$organizationid = 0;
}
else {
$distro_user = $distribution->get_user($session['userid']);
if (!$distro_user)
return array('content' => false);
$role = $distro_user['role'];
$organizationid = $distro_user['organizationid'];
}
if ($route->format == 'html') {
if ($route->action == 'admin') {
if ($role == Roles::SUPERADMINISTRATOR || $role == Roles::ADMINISTRATOR) {
$result = view("Modules/distribution/Views/admin_view.php", array());
}
}
if ($route->action == 'preparation') { // Everybody can get to the preparation page
if ($role == Roles::SUPERADMINISTRATOR || $role == Roles::DAYVOL) { // the day vol logged with day token can see all the distributions
$orgs = $distribution->get_organizations();
}
else {
$orgs = array($distribution->get_organization($organizationid)); //We put it in an array so it has the same structure than the one returned by $distribution->get_organizations()
}
$result = view("Modules/distribution/Views/preparation_view.php", array('organizations' => $orgs, 'organizationid' => $organizationid));
}
if ($route->action == "daytoken") {
if ($role == Roles::SUPERADMINISTRATOR || $role == Roles::ADMINISTRATOR) {
$day_token = $distribution->get_day_token();
if (isset($day_token['error']))
return array('content' => $day_token['error']);
else
$result = view("Modules/distribution/Views/day_token.php", array('day_token' => $day_token));
}
}
if ($route->action == 'items') {
if ($role == Roles::SUPERADMINISTRATOR || $role == Roles::ADMINISTRATOR) {
$result = view("Modules/distribution/Views/items_view.php", array());
}
}
if ($route->action == 'editdistributions') {
if ($role == Roles::SUPERADMINISTRATOR || $role == Roles::ADMINISTRATOR) {
$orgs = $distribution->get_organizations();
$result = view("Modules/distribution/Views/edit_distributions_view.php", array('organizations' => $orgs, 'organizationid' => $organizationid));
}
}
}
else if ($route->format == 'json') {
if ($route->action == "listorganizations") {
if ($role == Roles::SUPERADMINISTRATOR)
$result = $distribution->get_organizations();
if ($role == Roles::ADMINISTRATOR) {
$org = $distribution->get_organization($organizationid);
$result = array($org);
}
}
if ($route->action == 'createorganization') {
if ($role == Roles::SUPERADMINISTRATOR || $role == Roles::ADMINISTRATOR)
$result = $distribution->create_organization(get('name'));
}
if ($route->action == 'createuser') {
if ($role == Roles::SUPERADMINISTRATOR || ($role == Roles::ADMINISTRATOR && $distribution->user_is_in_organization($session['userid'], $organizationid)))
$result = $distribution->create_user(get('name'), get('organizationid'), get('email'), post('password'), get('role'));
}
if ($route->action == 'createdistributionpoint') {
if ($role == Roles::SUPERADMINISTRATOR || ($role == Roles::ADMINISTRATOR && $distribution->user_is_in_organization($session['userid'], $organizationid)))
$result = $distribution->create_distribution_point(get('name'), get('organizationid'));
}
if ($route->action == 'deletedistributionpoint') {
if ($role == Roles::SUPERADMINISTRATOR || ($role == Roles::ADMINISTRATOR && $distribution->user_is_in_organization($session['userid'], $organizationid)))
$result = $distribution->delete_distribution_point(get('distributionid'));
}
// Items
if ($route->action == 'getitems') {
$result = $distribution->get_items();
}
if ($route->action == 'getitemsnotdeleted') {
$result = $distribution->get_items_not_deleted();
}
if ($route->action == 'createitem') {
$result = $distribution->create_item(get('name'), get('regular'));
}
if ($route->action == 'deleteitem') {
if ($role == Roles::SUPERADMINISTRATOR || $role == Roles::ADMINISTRATOR)
$result = $distribution->delete_item(get('id'));
}
// Distribution preparation
$distributionid = get('distributionid');
$organizationid = $distribution->get_distribution_organization($distributionid);
if ($role == Roles::SUPERADMINISTRATOR || $role == Roles::DAYVOL || $distribution->user_is_in_organization($session['userid'], $organizationid)) {
if ($route->action == 'getyesterdaypreparation') {
$result = $distribution->get_yesterday_preparation($distributionid);
}
if ($route->action == 'savereturneditem') {
$result = $distribution->save_returned_item(get('value'), get('itemid'), get('distributionid'));
}
if ($route->action == 'savegoingoutitem') {
$result = $distribution->save_going_out_item(get('value'), get('itemid'), get('distributionid'));
}
if ($route->action == 'gettodaypreparation') {
$result = $distribution->get_today_preparation(get('distributionid'));
}
if ($route->action == 'getlastweekpreparation') {
$result = $distribution->get_last_week_preparation(get('distributionid'));
}
if ($route->action == 'getweekpreparation') {
$result = $distribution->get_week_preparation(get('distributionid'), get('date'));
}
}
if ($role == Roles::SUPERADMINISTRATOR || $role == Roles::ADMINISTRATOR) {
if ($route->action == 'savedistributeditem') {
$result = $distribution->save_distributed_item(get('value'), get('itemid'), get('distributionid'), get('date'));
}
}
}
return array('content' => $result);
}