Odyssey
getcustatus.prg
1 <?php
2 // vim: tabstop=2 expandtab syn=php
3 
4 //for each cu in the cuadmin table
5 //return results in the form: (read straight from the database table)
6 // {
7 // "cruisecu":{"livebatch":"L","offlinestat":""},
8 // "scrubcu":{"livebatch":"B","offlinestat":"N"}
9 // }
10 
11 require_once 'logging.i';
12 require_once 'hcuEnv.i';
13 
14 $logger = new ColorStandardLogger('getcustatus');
15 
16 // * SETUP WORKING ENVIRONMENT
17 // require_once(dirname(__FILE__) . '/../../shared/library/hcuEnv.i');
18 // require_once(dirname(__FILE__) . '/../../shared/library/hcuCommon.i');
19 
20 $env = LoadSystemEnv('version');
21 if ($env['devmode']) {
22  $env['logger']->warning("Showing /getcustatus info - DEVMODE.");
23 }
24 
25 $HB_ENV = LoadSystemEnv("banking");
26 $db = $HB_ENV['db'];
27 
28 $connect_to = sprintf('host=%s port=%d dbname=%s', $db['host'],
29  $db['port'], $db['dbname']);
30 if (!is_null($db['user'])) {
31  $connect_to .= ' user=' . $db['user'];
32 }
33 if (!is_null($db['connect_timeout'])) {
34  $connect_to .= ' connect_timeout=' . $db['connect_timeout'];
35 }
36 if (!is_null($db['password'])) {
37  $connect_to .= ' password=' . $db['password'];
38 }
39 $r = pg_connect($connect_to);
40 
41 if (!$r) {
42  throw new Exception('DB connect error');
43 }
44 if (pg_connection_status($r) !== PGSQL_CONNECTION_OK) {
45  throw new Exception('DB status error');
46 }
47 if (!pg_ping($r)) {
48  throw new Exception('DB ping error');
49 }
50 
51 
52 //custatus from cuadmin table
53 $result = pg_query($r, "select user_name, livebatch, offlinestat from cuadmin order by user_name");
54 
55 $fields = [ "name", "livebatch", "offlinestat" ];
56 
57 while ($row = pg_fetch_row($result)) {
58  $row[0] = trim($row[0]);
59  $name = $row[0];
60  //echo "echo name= ", $name, "<br>";
61  $entry = array();
62  // $entry = $res[$name]; //get the existing values for this cu
63  //echo "existing entry", json_encode($entry), "<br>";
64  for( $i=1; $i<count($fields); $i++) { //skip the name field in result
65  $entry[$fields[$i]] = trim($row[$i]); //match the values to field names
66  };
67  $res[$name] = $entry; //replace with updated values
68 };
69 
70 echo json_encode($res); //output everything
71 
72 ?>