-
Notifications
You must be signed in to change notification settings - Fork 37
/
lines.php
186 lines (158 loc) · 5.8 KB
/
lines.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
184
185
186
<?php
include 'inc/init.php';
fAuthorization::requireLoggedIn();
fRequest::overrideAction();
$action = fRequest::getValid('action', array('list', 'add', 'edit', 'delete', 'view', 'clone', 'reorder'));
$line_id = fRequest::get('line_id', 'integer');
$graph_id = fRequest::get('graph_id', 'integer');
if ('delete' == $action) {
$class_name = 'Line';
try {
$obj = new Line($line_id);
$graph = new Graph($obj->getGraphId());
$delete_text = 'Are you sure you want to delete the line : <strong>' . $obj->getAlias() . '</strong>?';
if (fRequest::isPost()) {
fRequest::validateCSRFToken(fRequest::get('token'));
$obj->delete();
fMessaging::create('success', Graph::makeUrl('edit',$graph),
'The line for ' . $graph->getName() . ' was successfully deleted');
fURL::redirect(Graph::makeUrl('edit',$graph));
}
} catch (fNotFoundException $e) {
fMessaging::create('error', Graph::makeUrl('edit',$graph),
'The line requested could not be found');
fURL::redirect(Graph::makeUrl('edit',$graph));
} catch (fExpectedException $e) {
fMessaging::create('error', fURL::get(), $e->getMessage());
}
include VIEW_PATH . '/delete.php';
// --------------------------------- //
} elseif ('edit' == $action) {
try {
$line = new Line($line_id);
$graph = new Graph($line->getGraphId());
if (fRequest::isPost()) {
$line->populate();
fRequest::validateCSRFToken(fRequest::get('token'));
$line->store();
fMessaging::create('affected', fURL::get(), $graph->getName());
fMessaging::create('success', fURL::get(),'The Line ' . $line->getAlias(). ' was successfully updated');
}
} catch (fNotFoundException $e) {
fMessaging::create('error', Graph::makeUrl('edit',$graph),
'The Line requested, ' . fHTML::encode($line_id) . ', could not be found');
fURL::redirect(Graph::makeUrl('edit',$graph));
} catch (fExpectedException $e) {
fMessaging::create('error', fURL::get(), $e->getMessage());
}
include VIEW_PATH . '/add_edit_line.php';
// --------------------------------- //
} elseif ('add' == $action) {
$line = new Line();
$graph = new Graph($graph_id);
if (fRequest::isPost()) {
try {
$line->populate();
fRequest::validateCSRFToken(fRequest::get('token'));
$line->store();
$graph_url = Graph::makeUrl('edit',$graph);
fMessaging::create('affected', "/graphs.php", $line->getAlias());
fMessaging::create('success', "/graphs.php",
'The Line ' . $line->getAlias() . ' was successfully created');
fURL::redirect($graph_url);
} catch (fExpectedException $e) {
fMessaging::create('error', fURL::get(), $e->getMessage());
}
}
include VIEW_PATH . '/add_edit_line.php';
} elseif ('clone' == $action) {
$line_to_clone = new Line($line_id);
$graph_id = $line_to_clone->getGraphId();
$graph = new Graph($graph_id);
if (fRequest::isPost()) {
try {
fRequest::validateCSRFToken(fRequest::get('token'));
Line::cloneLine($line_id);
$graph_url = Graph::makeUrl('edit',$graph);
fMessaging::create('affected', $graph_url, $line_to_clone->getAlias());
fMessaging::create('success', $graph_url,
'The Line ' . $line_to_clone->getAlias() . ' was successfully cloned');
fURL::redirect($graph_url);
} catch (fExpectedException $e) {
fMessaging::create('error', fURL::get(), $e->getMessage());
}
}
$dashboard = new Dashboard($graph->getDashboardId());
$dashboard_id = $graph->getDashboardId();
$lines = Line::findAll($graph_id);
include VIEW_PATH . '/add_edit_graph.php';
} elseif ('reorder' == $action) {
$drag_order = fRequest::get('drag_order');
$error = false;
if (empty($drag_order)) {
// In this case, the user clicks on the arrow
$move = fRequest::getValid('move', array('previous', 'next'));
$line_to_move = new Line($line_id);
$graph_id = $line_to_move->getGraphId();
$lines_in_graph = Line::findAll($graph_id);
$number_of_lines = $lines_in_graph->count(TRUE);
$skip_next = false;
for ($i=0; $i < $number_of_lines; $i++) {
if (!$skip_next) {
$current_line = $lines_in_graph[$i];
if ($current_line->getLineId() != $line_id) {
// This isn't the concerned line
$current_line->setWeight($i);
} else {
if ('previous' == $move) {
if ($i > 0) {
$current_line->setWeight($i-1);
$previous_line = $lines_in_graph[$i-1];
$previous_line->setWeight($i);
}
} else if ('next' == $move) {
if ($i < $number_of_lines -1) {
$current_line->setWeight($i+1);
$next_line = $lines_in_graph[$i+1];
$next_line->setWeight($i);
$skip_next = true;
}
}
}
} else {
$skip_next = false;
}
}
} else {
// In this case the user has used the drag and drop functionnality
$array_of_weights = explode(",", $drag_order);
$lines_in_graph = array();
foreach ($array_of_weights as $new_weight) {
$expl = explode(":",$new_weight);
$current_line = new Line($expl[0]);
if (empty($graph_id)) {
$graph_id = $current_line->getGraphId();
} else {
// Check if all the lines are in the same graph
if ($graph_id != $current_line->getGraphId()) {
$error = true;
break;
}
}
$current_line->setWeight($expl[1]);
$lines_in_graph[] = $current_line;
}
}
if (!$error) {
foreach ($lines_in_graph as $line_to_store) {
$line_to_store -> store();
}
$graph = new Graph($graph_id);
$url_redirect = Graph::makeURL('edit',$graph);
fMessaging::create("success", "/graphs.php","The lines have been successfully reordered");
} else {
$url_redirect = Dashboard::makeURL('list');
fMessaging::create("success", "/dashboard.php","An error occured and the lines couldn't be reordered");
}
fURL::redirect($url_redirect);
}