Odyssey
aBankingCalendar.i
1 <?php
2 /**
3  * Reply:
4  * @uses return data to client view print to json
5  * @param assoc $pResult: array contining results of search
6  * @param assoc $pReply: empty array to return to client
7  * @param assoc $pOperation: requested search operation
8  */
9 function BankingCalendarReplay($pResult, $pReply, $pOperation) {
10  $pReply['operation'] = $pOperation;
11  if (isset($pResult['data']) && count($pResult['data'])) $pReply['data'] = $pResult['data'];
12  if (isset($pResult['info']) && count($pResult['info'])) $pReply['info'] = $pResult['info'];
13 
14  print json_encode(array("Results" => $pReply));
15 }
16 
17 /**
18  * @uses return calendar data for specified year each calendar
19  * year has 5 fields named after each day of the week.
20  *
21  * these fileds are null or array,
22  * array -> if empty , user has selected to make all
23  * of this specific weekday CLOSED for business.
24  * array -> has data , user has selected to make all
25  * of this specific weekday CLOSED for business
26  * EXCEPT the dates in this array.
27  * null -> no dates have been selected as CLOSED
28  * or EXCEPTIONS to CLOSED.
29  *
30  * there is a single field for UNIQUE date selections
31  * which are not related to being EXCEPTIONS for a
32  * particular weekday.
33  *
34  * these UNIQUE dates are just single hand-picked dates
35  * to be CLOSE for business.
36  *
37  * @param #pEnv array: environment variable for debugging
38  * @param $pDbh object: database access variable
39  * @param $pCu string: credit union code to be used as variable or table in sql
40  * @param $pYear string: calendar year to search
41  *
42  * @param list of CLOSED for business dates for the specified year
43  */
44 function BankingCalendarRead($pEnv, $pDbh, $pCu, $pYear) {
45  $sqlReturn = array();
46  $sql = "
47  SELECT dates FROM cu_calendar
48  WHERE cu = '$pCu'
49  AND year = '$pYear'";
50  $sqlRs = db_query($sql, $pDbh);
51  if (!$sqlRs) {
52  throw new Exception("Failed to read calendar for " . $pYear);
53  }
54  $sqlReturn['calendar'] = db_fetch_assoc($sqlRs);
55  return $sqlReturn;
56 }
57 
58 /**
59  * @uses create calendar data for displaying CLOSED
60  * for business dates.
61  *
62  * @param #pEnv array: environment variable for debugging
63  * @param $pDbh object: database access variable
64  * @param $pCu string: credit union code to be used as variable or table in sql
65  * @param $pYear string: calendar year to create
66  * @param $pDates json: list of CLOSED for business dates
67  * refer to (BankingCalendarRead description)
68  *
69  * @return newly created CLOSE for business dates for the specified year
70  */
71 function BankingCalendarCreate($pEnv, $pDbh, $pCu, $pYear, $pDates) {
72  $sqlReturn = array();
73  $sql = "
74  INSERT INTO cu_calendar
75  (cu, year, dates) VALUES ('$pCu', '$pYear', '$pDates')";
76  $sqlRs = db_query($sql, $pDbh);
77  if (!$sqlRs) {
78  throw new Exception("Failed to create calendar for " . $pYear);
79  }
80 
81  $sqlReturn['calendar'] = db_fetch_assoc($sqlRs);
82  $sqlReturn['message'] = "Calendar created successfully for ". $pYear;
83  return $sqlReturn;
84 }
85 
86 /**
87  * @uses update calendar data for displaying CLOSED
88  * for business dates.
89  *
90  * @param #pEnv array: environment variable for debugging
91  * @param $pDbh object: database access variable
92  * @param $pCu string: credit union code to be used as variable or table in sql
93  * @param $pYear string: calendar year to create
94  * @param $pDates json: list of CLOSED for business dates
95  * refer to (BankingCalendarRead description)
96  *
97  * @return updated CLOSE for business dates for the specified year
98  */
99 function BankingCalendarUpdate($pEnv, $pDbh, $pCu, $pYear, $pDates) {
100  $sqlReturn = array();
101  $sql = "
102  UPDATE cu_calendar
103  SET dates = '$pDates'
104  WHERE cu = '$pCu'
105  AND year = '$pYear'";
106  $sqlRs = db_query($sql, $pDbh);
107  if (!$sqlRs) {
108  throw new Exception("Failed to update calendar for " . $pYear);
109  }
110 
111  $sqlReturn['calendar'] = db_fetch_assoc($sqlRs);
112  $sqlReturn['message'] = "Calendar updated successfully for ". $pYear;
113  return $sqlReturn;
114 }
115 
116 /**
117  * @uses validate the required parameters for creating and updating
118  * CLOSED for business dates for a specified calendar year.
119  *
120  * @param $pEnv array: evironment object for debugging
121  * @param $pCalendar array/json:
122  * holds calendar year and list of CLOSED for business dates
123  *
124  * @return $vParameters array: array of data valid for use in the database
125  */
126 function BankingCalendarValidate($pEnv, $pCalendar, $pJson = false) {
127  $cValidate = array();
128  $cCalendar = array();
129 
130  if ($pCalendar === null) {
131  throw new Exception("Calendar data not found");
132  }
133 
134  // dcode json, if needed
135  if ($pJson) {
136  $cCalendar = trim($pCalendar);
137  $cCalendar = html_entity_decode($cCalendar);
138  $cCalendar = HCU_JsonDecode($cCalendar);
139  } else {
140  $cCalendar = $pCalendar;
141  }
142 
143  if (!is_array($cCalendar)) {
144  throw new Exception("Calendar data is invalid");
145  }
146 
147  // validate calendar year is 4 characters
148  if (isset($cCalendar['year'])) {
149  $cYear = trim($cCalendar['year']);
150 
151  if (strlen($cYear) !== 4) {
152  throw new Exception("Calendar year is invalid");
153  }
154 
155  $cValidate['validate']['year'] = $cYear;
156  } else {
157  throw new Exception("Calendar year is missing");
158  }
159 
160  // validate dates list
161  if (isset($cCalendar['dates'])) {
162  $cDates = $cCalendar['dates'];
163 
164 
165  // must be an array
166  if (!is_array($cDates)) {
167  throw new Exception("Calendar date list is invalid");
168  }
169 
170  // set as json string for database
171  $cDates = HCU_JsonEncode($cDates);
172  $cDates = trim($cDates);
173 
174  // safety check
175  if ($cDates === "") {
176  throw new Exception("Calendar dates list is missing");
177  }
178 
179  $cValidate['validate']['dates'] = $cDates;
180  } else {
181  throw new Exception("Calendar dates list is missing");
182  }
183 
184  return $cValidate;
185 }
186 ?>