-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.php
executable file
·288 lines (266 loc) · 8.53 KB
/
export.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<!doctype html><html><head>
<?php include'imports.php'?>
<style>
table#table{width:90%}
table#table td {max-width:100px}
button.button{margin:1px}
button.l2{font-size:10px}
table.ww th{background:#bf5050}
</style>
<script>
function updateTable(obj,name) {
//make highlight button enabled
document.querySelector('#highlight').removeAttribute('disabled');
document.querySelector('#createCSV').removeAttribute('disabled');
//get the table t
var t=document.querySelector('#table');
t.rows[0].cells[0].innerHTML=name;
//set the th color to red
if([
Global.Waste,
Global.Waste.Collection,
Global.Waste.Treatment,
Global.Waste.Discharge
].indexOf(obj)+1
){
t.classList.add('ww');
}else{
t.classList.remove('ww');
}
while(t.rows.length>2){t.deleteRow(-1);}
for(var field in obj) {
//type of the field
var type=typeof(obj[field]);
//only this object
if(type=='object')continue;
//new row
var newRow=t.insertRow(-1);
//code
newRow.insertCell(-1).innerHTML=field
//name
newRow.insertCell(-1).innerHTML=translate(field+'_descr');
//type
newRow.insertCell(-1).innerHTML=(function() {
if(field.search("c_")>=0) return "Calculated variable";
return { "number":"Input", "function":"Output", }[type];
})();
//value
newRow.insertCell(-1).innerHTML=(function() {
switch(type) {
case "number":
return format(obj[field]);
break;
case "function":
return format(obj[field]());
break;
}
})();
//formula or default value
newRow.insertCell(-1).innerHTML=(function() {
switch(type) {
case "number":return "--";break;;
case "function":
var formula=getVariable(field).toString();
formula=Formulas.prettify(formula);
return formula;
break;
}
})();
//unit
newRow.insertCell(-1).innerHTML=Info[field].unit;
//description
newRow.insertCell(-1).innerHTML=translate(field+'_expla');
}
//bottom line with the color of W/WW
var newRow=t.insertRow(-1);
var newTh=document.createElement('th');
newTh.setAttribute('colspan',7)
newTh.style.borderBottom='none';
newTh.style.borderTop='none';
newRow.appendChild(newTh);
}
function selectText(el) {
var body = document.body, range, sel;
if (document.createRange && window.getSelection) {
range = document.createRange();
sel = window.getSelection();
sel.removeAllRanges();
try {
range.selectNodeContents(el);
sel.addRange(range);
} catch (e) {
range.selectNode(el);
sel.addRange(range);
}
} else if (body.createTextRange) {
range = body.createTextRange();
range.moveToElementText(el);
range.select();
}
}
function createCSV() {
//get the table "table"
var t=document.getElementById('table');
//string where we will write the entire file
var str="";
//go over the table
for(var i=0; i<t.rows.length; i++)
{
for(var j=0; j<t.rows[i].cells.length; j++)
{
str += t.rows[i].cells[j].textContent.replace(/,/g,' ')+",";
}
str+='\r\n';
}
//generate clickable link
var a = document.createElement('a');
a.href = 'data:text/csv;charset=utf-8,' + encodeURI(str);
a.target = '_blank';
a.download = 'export.csv';
//click the link
document.body.appendChild(a);
a.click();
}
function exportAll() {
//make highlight button enabled
document.querySelector('#highlight').removeAttribute('disabled');
document.querySelector('#createCSV').removeAttribute('disabled');
//get the table t
var t=document.querySelector('#table');
t.rows[0].cells[0].innerHTML=translate("All stages");
while(t.rows.length>2){t.deleteRow(-1);}
//all stages objects
[
Global.Water,
Global.Water.Abstraction,
Global.Water.Treatment,
Global.Water.Distribution,
Global.Waste,
Global.Waste.Collection,
Global.Waste.Treatment,
Global.Waste.Discharge,
Global.Energy
].forEach( obj => {
for(var field in obj)
{
//type of the field
var type=typeof(obj[field]);
//only this object
if(type=='object')continue;
//new row
var newRow=t.insertRow(-1);
//code
newRow.insertCell(-1).innerHTML=field
//name
newRow.insertCell(-1).innerHTML=translate(field+'_descr');
//type
newRow.insertCell(-1).innerHTML=(function() {
if(field.search("c_")>=0) return "Output (Calculated variable)";
switch(type) {
case "number": return "Input"; break;
case "function": return "Output"; break;
}
})();
//value
newRow.insertCell(-1).innerHTML=(function() {
switch(type) {
case "number":
return format(obj[field]);
break;
case "function":
return format(obj[field]());
break;
}
})();
//formula or default value
newRow.insertCell(-1).innerHTML=(function() {
switch(type) {
case "number":return "--";break;;
case "function":
var formula=getVariable(field).toString();
formula=Formulas.prettify(formula)
return formula;
break;
}
})();
//unit
newRow.insertCell(-1).innerHTML=Info[field] ? Info[field].unit : "";
//description
newRow.insertCell(-1).innerHTML=translate(field+'_expla');
}
});
}
</script>
</head><body><center>
<!--sidebar--><?php include'sidebar.php'?>
<!--NAVBAR--><?php include"navbar.php"?>
<!--linear--> <?php include'linear.php'?>
<!--TITLE--><h1><?php write('#export_title')?></h1>
<h4>
<?php write('#You can export individual stages to excel using this menu')?>
</h4><h4>
<?php write('#export_explanation')?>
</h4>
<div id=main style=margin-bottom:3em>
<!--buttons to select stage-->
<div class=flex style=justify-content:center>
<div>
<table>
<tr>
<td rowspan=4>
<button onclick="exportAll()" class=button style="font-size:18px">
<?php write('#All stages')?>
</button>
</td>
<td><?php write('#ghg_assessment')?><td><?php write('#energy_performance')?>
<tr>
<td>
<button onclick="updateTable(Global.Water, '<?php write("#Water")?>')" class="button"><?php write('#Water')?></button>
</td>
<td>
<button onclick="updateTable(Global.Water.Abstraction, '<?php write('#energy_performance')?> - Abstraction')" class="button l2"><?php write('#Abstraction')?></button>
<button onclick="updateTable(Global.Water.Treatment, '<?php write('#energy_performance')?> - Treatment')" class="button l2"><?php write('#Treatment')?></button>
<button onclick="updateTable(Global.Water.Distribution, '<?php write('#energy_performance')?> - Distribution')" class="button l2"><?php write('#Distribution')?></button>
</td>
</tr>
<tr>
<td>
<button onclick="updateTable(Global.Waste, '<?php write("#Waste")?>')" class="button"><?php write('#Waste')?></button>
</td>
<td>
<button onclick="updateTable(Global.Waste.Collection, '<?php write('#energy_performance')?> - <?php write('#Collection')?>')" class="button l2"><?php write('#Collection')?></button>
<button onclick="updateTable(Global.Waste.Treatment, '<?php write('#energy_performance')?> - <?php write('#Treatment')?>')" class="button l2"><?php write('#Treatment')?></button>
<button onclick="updateTable(Global.Waste.Discharge, '<?php write('#energy_performance')?> - <?php write('#Discharge')?>')" class="button l2"><?php write('#Discharge')?></button>
</td>
</tr>
<tr>
<td colspan=2 align=center>
<button onclick="updateTable(Global.Energy, '<?php write('#energy_summary')?>')" class="button l2"><?php write('#energy_summary')?></button>
</td>
</tr>
</table>
</div>
<div>
<button id=highlight disabled class=button style="margin:1em;font-size:18px" onclick=selectText(document.querySelector('#table'))>
<?php write('#export_highlight_button')?>
</button>
<button class=button id=createCSV disabled onclick=createCSV() style="font-size:18px">
<?php write('#Create CSV from current table')?>
</button>
</div>
</div>
<!--info table-->
<table id=table style="margin-top:0.5em;width:95%">
<tr><th colspan=7 style=font-size:18px><?php write('#export_no_stage_selected')?>
<tr>
<th><?php write('#export_code')?>
<th><?php write('#export_name')?>
<th><?php write('#export_type')?>
<th><?php write('#Current value')?>
<th><?php write('#Formula')?>
<th><?php write('#export_unit')?>
<th><?php write('#export_desc')?>
<tr><td colspan=7 style="text-align:center;">
<?php write('#export_click_on_a_stage')?>
</table>
</div>