37 if ($line === '') |
36 if ($line === '') |
38 break; |
37 break; |
39 $answer .= $line; |
38 $answer .= $line; |
40 } |
39 } |
41 socket_close($sock); |
40 socket_close($sock); |
|
41 |
|
42 /* |
|
43 * $arr contains the complete reply of the LIST command. |
|
44 */ |
42 $arr = explode("\r\n", $answer); |
45 $arr = explode("\r\n", $answer); |
43 |
46 |
44 |
47 |
45 $outstr = build_header(); |
48 if (isset($_GET['action'])) { |
46 $outstr .= ' <div id="errors">'.PHP_EOL; |
49 switch ($_GET['action']) { |
47 $outstr .= ' </div> <!-- errors -->'.PHP_EOL; |
50 case 'edit': unit_edit(); |
48 $outstr .= ' <div id="etable">'.PHP_EOL; |
51 break; |
49 |
52 default: break; |
50 $outstr .= ' <table class="setup">'.PHP_EOL; |
53 } |
51 $outstr .= ' <tr class="trhead">'.PHP_EOL; |
54 } elseif (isset($_POST['action'])) { |
52 $outstr .= ' <td class="setup" style="width: 300px;">UUID</td>'.PHP_EOL; |
55 switch ($_POST['action']) { |
53 $outstr .= ' <td class="setup" style="width: 300px;">Name</td>'.PHP_EOL; |
56 case 'testdata': testdata(); |
54 $outstr .= ' <td class="setup" style="width: 60px;">Mode</td>'.PHP_EOL; |
57 break; |
55 $outstr .= ' </tr>'.PHP_EOL; |
58 default: break; |
56 |
59 } |
57 if (startsWith($arr[0], "212")) { |
60 } else { |
58 $j = 1; |
61 unit_list(); |
59 while (1) { |
62 } |
60 if (strcmp($arr[$j], ".") == 0) |
63 |
61 break; |
64 exit; |
62 $f = explode(",", $arr[$j]); |
65 |
63 $outstr .= ' <tr class="setup"><td class="setup">'.$f[0].'</td><td class="setup">'.$f[1].'</td><td class="setup">'.$f[2].'</td></tr>'.PHP_EOL; |
66 /**************************************************************************** |
64 $j++; |
67 * |
65 } |
68 */ |
66 } |
69 |
67 |
70 /* |
68 $outstr .= ' </table>'.PHP_EOL; |
71 * Unit add |
69 $outstr .= ' </div> <!-- etable -->'.PHP_EOL; |
72 * |
70 $outstr .= build_footer(); |
73 * @param string $_POST['Name'] The rpofile name |
71 |
74 */ |
72 echo $outstr; |
75 function unit_add() { |
73 |
76 |
|
77 if ($_POST['key'] == 'Add') { |
|
78 |
|
79 $cmd = "ADD UNIT ".$_POST['Name']; |
|
80 |
|
81 $sock = open_socket(); |
|
82 if ($sock != false) { |
|
83 /* |
|
84 * Send command and absorb the result. |
|
85 */ |
|
86 socket_write($sock, $cmd, 4096); |
|
87 while (1) { |
|
88 $line = socket_read($sock, 4096); |
|
89 if ($line === '') |
|
90 break; |
|
91 } |
|
92 socket_close($sock); |
|
93 } |
|
94 } |
|
95 |
|
96 unset($_POST['UUID']); |
|
97 unset($_POST['Name']); |
|
98 unset($_POST['key']); |
|
99 unset($_POST['command']); |
|
100 load('units.php'); |
|
101 } |
|
102 |
|
103 |
|
104 |
|
105 /* |
|
106 * Unit update |
|
107 * |
|
108 * @param string $_POST['UUID'] The unit UUID |
|
109 * @param string $_POST['Name'] The unit name |
|
110 * @param string $_POST['key'] The button pressed. |
|
111 */ |
|
112 function unit_update() { |
|
113 /* |
|
114 * Build the update command |
|
115 */ |
|
116 if ($_POST['key'] == 'Delete') |
|
117 $update_cmd = "DEL UNIT ".$_POST['UUID']; |
|
118 else |
|
119 $update_cmd = "XXXXXXX ".$_POST['UUID'].",".$_POST['Name']; |
|
120 |
|
121 $sock = open_socket(); |
|
122 if ($sock != false) { |
|
123 /* |
|
124 * Send command and absorb the result. |
|
125 */ |
|
126 socket_write($sock, $update_cmd, 4096); |
|
127 while (1) { |
|
128 $line = socket_read($sock, 4096); |
|
129 if ($line === '') |
|
130 break; |
|
131 } |
|
132 socket_close($sock); |
|
133 } |
|
134 |
|
135 unset($_POST['UUID']); |
|
136 unset($_POST['Name']); |
|
137 unset($_POST['key']); |
|
138 unset($_POST['command']); |
|
139 load('units.php'); |
|
140 } |
|
141 |
|
142 |
|
143 |
|
144 /* |
|
145 * Test input of a modified or new profile. |
|
146 * |
|
147 * @param string $_POST['UUID'] Unique record UUID |
|
148 * @param string $_POST['Name'] Profile name |
|
149 * @param string $_POST['key'] Key choice, Save or Cancel |
|
150 * @param string $_POST['command'] Command used, 'add' or 'update' |
|
151 * |
|
152 * Return: 0 = Ok |
|
153 * 1 = Missing data |
|
154 * 2 = Name field too short |
|
155 * 3 = Name already in use |
|
156 * 99 = Cancel key |
|
157 */ |
|
158 function test_thedata() { |
|
159 |
|
160 global $arr; |
|
161 |
|
162 if (isset($_POST['UUID']) && isset($_POST['Name']) && isset($_POST['key']) && isset($_POST['command'])) { |
|
163 |
|
164 if ($_POST['key'] == 'Cancel') |
|
165 return 99; |
|
166 |
|
167 if (strlen($_POST['Name']) < 2) |
|
168 return 2; |
|
169 |
|
170 if (startsWith($arr[0], "212")) { |
|
171 $j = 1; |
|
172 while (1) { |
|
173 if (strcmp($arr[$j], ".") == 0) |
|
174 break; |
|
175 $f = explode(",", $arr[$j]); |
|
176 if (strcmp($f[0], $_POST['UUID']) && (strcmp($f[1], $_POST['Name']) == 0)) { |
|
177 return 3; |
|
178 } |
|
179 $j++; |
|
180 } |
|
181 } |
|
182 |
|
183 } else { |
|
184 return 1; |
|
185 } |
|
186 |
|
187 return 0; |
|
188 } |
|
189 |
|
190 |
|
191 |
|
192 /* |
|
193 * Test result from edit screen and do next action |
|
194 */ |
|
195 function testdata() { |
|
196 |
|
197 $result = test_thedata(); |
|
198 $error = ''; |
|
199 |
|
200 switch ($result) { |
|
201 case 0: if ($_POST['command'] == 'add') { |
|
202 unit_add(); |
|
203 return; |
|
204 } else if ($_POST['command'] == 'update') { |
|
205 unit_update(); |
|
206 return; |
|
207 } |
|
208 break; |
|
209 case 1: $error = 'Missing data'; |
|
210 break; |
|
211 case 2: $error = 'The name is too short'; |
|
212 break; |
|
213 case 3: $error = 'The name is already in use, choose another one'; |
|
214 break; |
|
215 case 99: |
|
216 load('units.php'); |
|
217 break; |
|
218 } |
|
219 |
|
220 if ($_POST['command'] == 'add') { |
|
221 $heading = 'ThermFerm - Add Unit'; |
|
222 } else { |
|
223 $heading = 'ThermFerm - Edit Unit'; |
|
224 } |
|
225 |
|
226 edit_screen($_POST['UUID'], $_POST['Name'], $_POST['command'], $heading, $error); |
|
227 } |
|
228 |
|
229 |
|
230 |
|
231 /* |
|
232 * Unit edit screen. Used by unit_edit(), unit_add() and testdata() |
|
233 * |
|
234 * @param string $UUID The record UUID (fixed). |
|
235 * @param string $Name The Unit Name. |
|
236 * @param string $command 'add' or 'update' |
|
237 * @param string $heading Page heading title. |
|
238 * @Param string $error_message Blank or previous error. |
|
239 */ |
|
240 function edit_screen($UUID, $Name, $command, $heading, $error_message) { |
|
241 |
|
242 $outstr = build_header($heading); |
|
243 $outstr .= ' <div id="errors">'.PHP_EOL; |
|
244 $outstr .= ' '.$error_message.PHP_EOL; |
|
245 $outstr .= ' </div> <!-- errors -->'.PHP_EOL; |
|
246 $outstr .= ' <div id="etable">'.PHP_EOL; |
|
247 $outstr .= ' <form method="POST" action="units.php">'.PHP_EOL; |
|
248 $outstr .= ' <table class="editor">'.PHP_EOL; |
|
249 $outstr .= ' <tr class="editor">'.PHP_EOL; |
|
250 $outstr .= ' <td class="editname">UUID</td>'.PHP_EOL; |
|
251 $outstr .= ' <td class="editfield">'.$UUID.'</td>'.PHP_EOL; |
|
252 $outstr .= ' </tr>'.PHP_EOL; |
|
253 $outstr .= ' <tr class="editor">'.PHP_EOL; |
|
254 $outstr .= ' <td class="editname">Unit Name</td>'.PHP_EOL; |
|
255 $outstr .= ' <td class="editfield"><input type="text" name="Name" size="50" value="'.$Name.'"></td>'.PHP_EOL; |
|
256 $outstr .= ' </tr>'.PHP_EOL; |
|
257 $outstr .= ' <tr class="editor">'.PHP_EOL; |
|
258 $outstr .= ' <td class="editname"><input type="submit" value="Save" name="key"></td>'.PHP_EOL; |
|
259 $outstr .= ' <td class="editfield"><input type="submit" value="Cancel" name="key">'; |
|
260 $outstr .= '<input type="submit" value="Delete" name="key" style="margin-left: 100px;">'; |
|
261 $outstr .= '<input type="hidden" value="testdata" name="action">'; |
|
262 $outstr .= '<input type="hidden" value="'.$command.'" name="command">'; |
|
263 $outstr .= '<input type="hidden" value="'.$UUID.'" name="UUID"></td>'.PHP_EOL; |
|
264 $outstr .= ' </tr>'.PHP_EOL; |
|
265 $outstr .= ' </table>'.PHP_EOL; |
|
266 $outstr .= ' </form>'.PHP_EOL; |
|
267 $outstr .= ' </div> <!-- etable -->'.PHP_EOL; |
|
268 $outstr .= build_footer(); |
|
269 echo $outstr; |
|
270 } |
|
271 |
|
272 |
|
273 |
|
274 /* |
|
275 * Edit a Unit. Fetches the record data and shows the edit screen. |
|
276 * |
|
277 * @param string $_GET['action'] Must be 'edit'. |
|
278 * @param string $_GET['UUID'] The UUID of the Unit. |
|
279 */ |
|
280 function unit_edit() { |
|
281 |
|
282 global $arr; |
|
283 |
|
284 if ($_GET['action'] == 'edit') { |
|
285 |
|
286 if (startsWith($arr[0], "212")) { |
|
287 $j = 1; |
|
288 while (1) { |
|
289 if (strcmp($arr[$j], ".") == 0) |
|
290 break; |
|
291 $f = explode(",", $arr[$j]); |
|
292 if (strcmp($f[0], $_GET['UUID']) == 0) { |
|
293 edit_screen($f[0], $f[1], 'update', 'ThermFerm - Edit Unit', ''); |
|
294 return; |
|
295 } |
|
296 $j++; |
|
297 } |
|
298 } else { |
|
299 load('units.php'); |
|
300 } |
|
301 |
|
302 } else { |
|
303 load('units.php'); |
|
304 } |
|
305 } |
|
306 |
|
307 |
|
308 |
|
309 /* |
|
310 * @link edit unit |
|
311 * @link add unit |
|
312 */ |
|
313 function unit_list() { |
|
314 |
|
315 global $arr; |
|
316 |
|
317 $outstr = build_header('ThermFerm - Units Maintenance'); |
|
318 $outstr .= ' <div id="errors">'.PHP_EOL; |
|
319 $outstr .= ' </div> <!-- errors -->'.PHP_EOL; |
|
320 $outstr .= ' <div id="etable">'.PHP_EOL; |
|
321 $outstr .= ' <table class="setup">'.PHP_EOL; |
|
322 $outstr .= ' <tr class="trhead">'.PHP_EOL; |
|
323 $outstr .= ' <td class="setup" style="width: 300px;">UUID</td>'.PHP_EOL; |
|
324 $outstr .= ' <td class="setup" style="width: 300px;">Name</td>'.PHP_EOL; |
|
325 $outstr .= ' <td class="setup" style="width: 60px;">Mode</td>'.PHP_EOL; |
|
326 $outstr .= ' <td class="setup" style="width: 40px;">Edit</td>'.PHP_EOL; |
|
327 $outstr .= ' </tr>'.PHP_EOL; |
|
328 |
|
329 if (startsWith($arr[0], "212")) { |
|
330 $j = 1; |
|
331 while (1) { |
|
332 if (strcmp($arr[$j], ".") == 0) |
|
333 break; |
|
334 $f = explode(",", $arr[$j]); |
|
335 $outstr .= ' <tr class="setup">'.PHP_EOL; |
|
336 $outstr .= ' <td class="setup">'.$f[0].'</td>'.PHP_EOL; |
|
337 $outstr .= ' <td class="setup">'.$f[1].'</td>'.PHP_EOL; |
|
338 $outstr .= ' <td class="setup">'.$f[2].'</td>'.PHP_EOL; |
|
339 if (strcmp($f[2], "OFF")) { |
|
340 $outstr .= ' <td class="setup">Busy</td>'.PHP_EOL; |
|
341 } else { |
|
342 $outstr .= ' <td class="setup"><a href="units.php?action=edit&UUID='.$f[0].'">Edit</a></td>'.PHP_EOL; |
|
343 } |
|
344 $outstr .= ' </tr>'.PHP_EOL; |
|
345 $j++; |
|
346 } |
|
347 } |
|
348 |
|
349 $outstr .= ' </table>'.PHP_EOL; |
|
350 $outstr .= ' </div> <!-- etable -->'.PHP_EOL; |
|
351 |
|
352 $outstr .= ' <div id="atable">'.PHP_EOL; |
|
353 $outstr .= ' <form method="POST" action="units.php">'.PHP_EOL; |
|
354 $outstr .= ' <table class="editor">'.PHP_EOL; |
|
355 $outstr .= ' <tr class="trhead"><td colspan="3">Add new unit</td></tr>'.PHP_EOL; |
|
356 $outstr .= ' <tr class="editor">'.PHP_EOL; |
|
357 $outstr .= ' <td class="editname">Unit Name</td>'.PHP_EOL; |
|
358 $outstr .= ' <td class="editfield"><input type="text" name="Name" size="50" value=""></td>'.PHP_EOL; |
|
359 $outstr .= ' <td class="editsub"><input type="submit" value="Add" name="key"></td>'.PHP_EOL; |
|
360 $outstr .= '<input type="hidden" value="testdata" name="action">'; |
|
361 $outstr .= '<input type="hidden" value="add" name="command">'; |
|
362 $outstr .= '<input type="hidden" value="00000000-0000-0000-0000-000000000000" name="UUID">'; |
|
363 $outstr .= ' </tr>'.PHP_EOL; |
|
364 $outstr .= ' </table>'.PHP_EOL; |
|
365 $outstr .= ' </form>'.PHP_EOL; |
|
366 $outstr .= ' </div> <!-- atable -->'.PHP_EOL; |
|
367 $outstr .= build_footer(); |
|
368 echo $outstr; |
|
369 } |
|
370 |