Odyssey
hcuHistory.data
1 <?php
2 /*
3  * File: history.data
4  * Purpose: To return the requested data in a JSON format, for the client to display
5  * accordingly
6  * This is script is limited to what can be sorted/returned
7  *
8  * My goal is to return 7 days at a time for any specific account
9  *
10  *
11  * Call this script with the following parameters
12  * a - This is the value of the Account Key {Acctnbr|AcctType|Sfx|CertNo}
13  * r1 - This is the starting date - IF empty, I will prime it with Todays date - 30
14  * r2 - This is the ending date - If empty, I will prime with today
15  * r3 - When dates are being specified, this will be used to calculate the next
16  * segment of data that will be retrieved. It can be blank when priming data.
17  *
18  * TO PRIME THE HISTORY DATA --
19  * pass in an empty r1 and r2 --
20  * the Get_History function will determine the correct default days starting
21  * backwards from today.
22  * At this point the Get_History function will also return the default days
23  * to use in future queries
24  *
25  * OTHERWISE
26  * pass in r1 (Starting Date) and r2 (Ending Date) in the format
27  * of MM/DD/YYYY
28  * The Get_History function will then return the rows that match that range
29  *
30  *
31  *
32  * Returned JSON OBJECT
33  * Here are the pieces in the JSON object that are returned
34  * 'title' => Simple title for the data --
35  * 'status' => Success status of the query results can be:
36  * 000 -- SUCCESS
37  * 'datastartdate' => This is returned by Get_History and is the official start
38  * date that was used in the query
39  * 'dataenddate' => This is returned by Get_History and is the official END
40  * date that was used in the query
41  * 'nextstartdate' => This script will use the default days which is supplied either
42  * by r3 or the Get_History function. This will calculate the
43  * next segment history START date to use
44  * 'nextenddate' => This script will use the default days which is supplied either
45  * by r3 or the Get_History function. This will calculate the
46  * next segment history END date to use
47  * 'defaultdays' => This is the days of history that are retrieved at a time. It
48  * is set when the data is primed, but can be overridden with the
49  * r3 parameter.
50  * 'items' => $Get_History_ary[$history_param]
51  */
52 
53  $serviceViewFromCUAdmin = (isset($serviceViewFromCUAdmin) ? $serviceViewFromCUAdmin : false);
54 
55  if (!$serviceViewFromCUAdmin) {
56 
57  // ** SET HOMECU FLAGS
58  $serviceShowInfo = true;
59  $serviceLoadMenu = true;
60  $serviceShowMenu = true;
61  $serviceAllowReadonly = true;
62  $serviceLiveCheck = true;
63 
64 
65  // ** INCLUDE MAIN GLOBAL SCRIPT -- Handles security / global variable values
66  require_once(dirname(__FILE__) . '/../library/hcuService.i');
67  }
68 
69  // ** IMPORT FORM VALUES
70  $string= array("filter" => FILTER_SANITIZE_STRING);
71  $digits= array("filter" => FILTER_SANITIZE_NUMBER_INT);
72  $dms_ok = array('cu'=>$string, 'a'=>$string, 'r1'=>$string, 'r2'=>$string, 'r3'=>$digits,"action"=>$string);
73  HCU_ImportVars($HB_ENV, 'HCUPOST', $dms_ok);
74 
75 try {
76  // ** First check the refer script -- This will be used to ensure proper usage later
77  // * as errors may be returned differently
78 
79  $parseRefer = parse_url($_SERVER['HTTP_REFERER']);
80  $referScript = basename($parseRefer['path']);
81 
82  if (!in_array($referScript, array('hcuHistory.prg', 'main'))) {
83  // ** Wrong script calling this data routine
84  throw new Exception ($MC->msg('Feature Not Set', HCU_DISPLAY_AS_HTML));
85  }
86 
87  /*
88  * ** CHECK USER FEATURE PERMISSIONS **
89  * NOTE: DO NOT AUTO-REDIR. Handle perm error here
90  */
91  if (!PermCheckFeatureScreen($dbh, $HB_ENV, $MC, FEATURE_BASIC, '', false)) {
92  throw new Exception (HCU_JsonEncode(Array($MC->msg('Rights not set', HCU_DISPLAY_AS_HTML))));
93  }
94 
95 
96  // print_r($HB_ENV);
97  // ** We have the values
98  $Get_History_ary = Array();
99  $retJson_ary = Array();
100  if (!is_null($HB_ENV['HCUPOST']['a'])) {
101  // * Have a row value
102  // ** Set the value
103  $history_param = hcu_decrypturl($HB_ENV['HCUPOST']['a'], $HB_ENV['historyHash']);
104  // * Now break apart on the | symbols that should be there
105 
106  $history_acct = explode('|', $history_param);
107  // * we should have 4 pieces
108 
109  // ** Set the r1 & r2
110  if ($HB_ENV['HCUPOST']['r1'] == '' || $HB_ENV['HCUPOST']['r2'] == '') {
111  // ** Always look for 3 days in advance when we prime
112  $HB_ENV['HCUPOST']['r2'] = date('m/d/Y', strtotime('+3 days'));
113  $HB_ENV['HCUPOST']['r1'] = date('m/d/Y', strtotime('-5 days'));
114  }
115 
116  $advSearch = array();
117  // ** import the values for the advanced search
118  $dms_ok = array('searchAmt'=>$string, 'searchDesc'=>$string, 'searchCheckNum'=>$string);
119  HCU_ImportVars($HB_ENV, 'HCUPOST', $dms_ok);
120 
121  if (HCU_array_key_exists('searchAmt', $HB_ENV["HCUPOST"]) && strlen($HB_ENV["HCUPOST"]["searchAmt"]) > 0 ) {
122  $findamt = preg_replace("/[^0-9.]/","",$HB_ENV["HCUPOST"]["searchAmt"]);
123  $advSearch["findamt"] = $findamt;
124  }
125  if (HCU_array_key_exists('searchDesc', $HB_ENV["HCUPOST"]) && strlen($HB_ENV["HCUPOST"]["searchDesc"]) > 0 ) {
126  $fdesc = preg_replace("/[^a-zA-Z0-9 ]/","",$HB_ENV["HCUPOST"]["searchDesc"]);
127  $advSearch["fdesc"] = $fdesc;
128  }
129  if (HCU_array_key_exists('searchCheckNum', $HB_ENV['HCUPOST'])) {
130  if ( strlen($HB_ENV["HCUPOST"]["searchCheckNum"]) > 0 ) {
131  $check = (preg_replace("/[^0-9.]/","",$HB_ENV["HCUPOST"]["searchCheckNum"]));
132  $advSearch["check"] = $check;
133  }
134  }
135  // * okay -- try passing the account to the Get_History and get the results
136 
137  $Get_History_ary = Get_History($dbh, $HB_ENV, $history_param, $HB_ENV['HCUPOST']['r1'], $HB_ENV['HCUPOST']['r2'], $advSearch);
138 
139  // ** Do I need to update the packetStatus with latest values??
140  if ($HB_ENV['live'] && $serviceLiveCheck && HCU_array_key_exists('packetStatus', $HB_ENV)) {
141  savePacketStatusCookie($HB_ENV);
142  }
143 
144 // TO TEST IN SMALL FRAGMENTS
145  $ret_defaultdays = intval(5);
146 
147  if ($Get_History_ary['status']['code'] == '000') {
148 
149  // * TESTING ONLY REMOVE FOR PRODUCTION
150  if ($Get_History_ary['status']['startdate'] == '') {
151  $Get_History_ary['status']['startdate'] = date('m/d/Y', strtotime('now - 90 days'));
152  $Get_History_ary['status']['stopdate'] = date('m/d/Y', strtotime('now'));
153  }
154 
155  // * CALCULATE THE NEXT Start End Date
156  // Subtract the number of days to fetch from the current starting date for the new start
157  $ret_nextstartdate = date('m/d/Y', strtotime("- $ret_defaultdays days", strtotime($Get_History_ary['status']['startdate'])));
158  // * NEXT END Date is ONE day previous to the current start
159  $ret_nextenddate = date('m/d/Y', strtotime("- 1 days", strtotime($Get_History_ary['status']['startdate'])));
160 
161  // ** QUICKLY GO through and format values for presentation
162  $retJson_ary = array(
163  'title' => 'Account History',
164  'status' => '000',
165  'datastartdate' => $Get_History_ary['status']['startdate'],
166  'dataenddate' => $Get_History_ary['status']['stopdate'],
167  'nextstartdate' => $ret_nextstartdate,
168  'nextenddate' => $ret_nextenddate,
169  'defaultdays' => $ret_defaultdays,
170  );
171 
172  if (HCU_array_key_exists($history_param, $Get_History_ary)) {
173  if (count($Get_History_ary[$history_param]) > 0) {
174  foreach ($Get_History_ary[$history_param] as $key => $value) {
175  $Get_History_ary[$history_param][$key]['date'] = mobile_formatdate($value['date']);
176 
177  // return same date as a number so it can sort easily
178  $Get_History_ary[$history_param][$key]['datesort'] = returnJsonDateValue($value['date']);
179  }
180  }
181 
182  $retJson_ary['itemcount'] = count($Get_History_ary[$history_param]);
183  $retJson_ary['items'] = is_array($Get_History_ary[$history_param]) ?
184  array_values($Get_History_ary[$history_param]) :
185  '';
186  }
187 
188  $retJson_value = HCU_JsonEncode($retJson_ary);
189 
190  // ** Try the following to check for Gzip compression
191  // * if it is accepted then set the headers and return to table
192  if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {
193  if (stristr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip,deflate')) {
194  $retJson_value = gzencode($retJson_value);
195  header('Content-Encoding: gzip');
196  header('Content-Length: '.strlen($retJson_value));
197  }
198  }
199  // ** TEST ONLY
200  // ** Evaluate browser, if it accepts gzip, then use this
201 
202  }
203  } else {
204 
205  // ** NO ROW EVEN ATTEMPTED TO BE SET
206  // ** ERROR OF SOME SORT
207  }
208 }
209 catch(Exception $ex)
210 {
211  // ** Make sure the results being returned are empty
212 
213  $retStatus_ary['homecuErrors'][] = Array('id' => '', 'message' => $ex->getMessage());
214  $retJson_value = HCU_JsonEncode(Array("Results" => Array($retStatus_ary)));
215 }
216 
217  header('Content-type: application/json');
218  print $retJson_value;