@@ -23,6 +23,8 @@ function __construct()
2323
2424 /**
2525 * uri CALL to uptate the amount of a specific rate currency by code using api call
26+ * it will receive the user_id, cod_base, mon_rate currency and codkey as post parameters,
27+ * and later will return 1 when sucess and any other string when fails
2628 *
2729 * @access public
2830 * @param $codkey mixed and authentication string key to check api
@@ -78,12 +80,14 @@ public function updateRateAmount()
7880 }
7981
8082 /**
81- * uri CALL to invoke the api and store the data into db
83+ * uri CALL to invoke the api and store the data into db,
84+ * it will receive the user_id and codkey as post parameters,
85+ * and later will return 1 when sucess and any other string when fails
8286 *
8387 * @access public
84- * @param $codkey mixed and authentication string key to check api
88+ * @param $extradata mixed and authentication string key to check api
8589 */
86- public function callApisAndSaveDB ($ codkey = NULL )
90+ public function callApisAndSaveDB ($ extradata = NULL )
8791 {
8892
8993 $ this ->load ->model ('Currency_m ' ,'dbcm ' );
@@ -111,9 +115,6 @@ public function callApisAndSaveDB($codkey = NULL)
111115 return $ error ;
112116 }
113117
114- $ data = array ();
115- // example invokation http://localhost/~general/codeigniter-currencylib/cweb/index.php/Currency_Manager/callapitodb/SHAR265ql-23krjhnou2q34rhi2?dateapi=2023-02-03&curbase=USD
116- // example shot base "index.php/Currency_Manager/callapitodb/SHAR265ql-23krjhnou2q34rhi2?dateapi=2023-02-03&curbase=USD"
117118 $ currencyDate = $ this ->input ->get_post ('dateapi ' , FALSE );
118119 $ currencyBase = $ this ->input ->get_post ('curbase ' , FALSE );
119120 $ currencyDest = $ this ->input ->get_post ('curdest ' , FALSE );
@@ -157,7 +158,208 @@ public function callApisAndSaveDB($codkey = NULL)
157158 $ currencyDate = date ('Ymd ' ,strtotime ($ currencyDate )).date ('H ' );
158159 $ createdbresult = $ this ->dbcm ->createCurrencyFromApi ($ currency_list_apiarray , $ currencyDate , $ currencyBase );
159160 log_message ('debug ' , __METHOD__ .' saved? : ' .print_r ($ createdbresult ,TRUE ). ' from parameter: ' .print_r ($ currencyDate , TRUE ));
160- $ result = json_encode (array ('result ' =>$ createdbresult ));
161+ $ result = $ createdbresult ;
162+
163+ $ data = array ();
164+ $ data ['result ' ] = $ createdbresult ;
165+ $ data ['currency_list_apiarray ' ] = $ currency_list_apiarray ;
166+ $ data ['currenturl ' ] = $ this ->currenturl ;
167+ log_message ('debug ' , __METHOD__ .' saved? : ' .print_r ($ data ,TRUE ). ' from parameter: ' .print_r ($ currencyDate , TRUE ));
168+ $ result = json_encode ($ data );
169+ echo $ result ;
170+ return $ result ;
171+ }
172+
173+ /**
174+ * uri CALL to read rates from db event call from an external api (if there is such rates already saved)
175+ * it will receive the user_id, currency base, mount currency and codkey as post parameters,
176+ * and later will return 1 when sucess and any other string when fails
177+ * the data when success will be in second string after the result
178+ *
179+ * @access public
180+ * @param $codkey mixed and authentication string key to check api
181+ */
182+ public function callRatesFromDB ($ extradata = NULL )
183+ {
184+
185+ $ this ->load ->model ('Currency_m ' ,'dbcm ' );
186+ $ this ->load ->library ('Userlib ' );
187+ $ this ->load ->library ('form_validation ' );
188+
189+ $ user_id = $ this ->input ->get_post ('user_id ' , FALSE );
190+ $ codkey = $ this ->input ->get_post ('codkey ' , FALSE );
191+ $ currencyBase = $ this ->input ->get_post ('curbase ' , FALSE );
192+ $ currencyDest = $ this ->input ->get_post ('curdest ' , FALSE );
193+ $ currencyDate = $ this ->input ->get_post ('dateapi ' , FALSE );
194+
195+ $ validfields = $ this ->form_validation ->required ($ user_id );
196+ $ validfields = preg_match ('/^[a-zA-Z0-9_\.]{1,}+$/i ' , $ user_id );
197+ if ($ validfields == FALSE ){
198+ log_message ('error ' , __METHOD__ .' POST : ' . print_r ($ user_id , TRUE ) . ' user_id is not valid : ' .print_r ($ validfields , TRUE ));
199+ $ error = json_encode (array ('result ' =>'user_id is not valid ' ));
200+ echo $ error ;
201+ return $ error ;
202+ }
203+
204+ $ validfields = $ this ->form_validation ->required ($ codkey );
205+ $ validfields = preg_match ('/^[a-zA-Z0-9\.]{1,}+$/i ' , $ codkey );
206+ if ($ validfields == FALSE ){
207+ log_message ('error ' , __METHOD__ .' POST : ' . print_r ($ codkey , TRUE ) . ' codkey is not valid : ' .print_r ($ validfields , TRUE ));
208+ $ error = json_encode (array ('result ' =>'codkey is not valid ' ));
209+ echo $ error ;
210+ return $ error ;
211+ }
212+
213+ $ missdest = $ this ->form_validation ->required ($ currencyDest );
214+ $ validcurren = $ this ->form_validation ->exact_length ($ currencyDest ,3 );
215+ $ missdate = $ this ->form_validation ->required ($ currencyDate );
216+ $ validfields = $ this ->form_validation ->exact_length ($ currencyDate ,10 );
217+ $ missbase = $ this ->form_validation ->required ($ currencyBase );
218+ $ validfields = $ this ->form_validation ->exact_length ($ currencyBase ,3 );
219+
220+ if ($ missdate == FALSE )
221+ $ currencyDate = date ('Y-m-d ' );
222+ if ($ missbase == FALSE )
223+ $ currencyBase = 'USD ' ;
224+ if ($ validfields == FALSE )
225+ $ currencyBase = 'USD ' ;
226+ if ($ validcurren == FALSE OR $ missdest == FALSE )
227+ {
228+ log_message ('info ' , __METHOD__ .' missing currency rates to convert, we will use all availables from api results ' );
229+ $ currencyDest = NULL ;
230+ }
231+
232+ $ this ->userlib ->initialize ($ user_id );
233+ $ this ->userlib ->getID ();
234+
235+ log_message ('debug ' , __METHOD__ .' API already called, now try to retrieve the result from DB for : ' .print_r ($ currencyDate , TRUE ));
236+ $ user_preferences = $ this ->userlib ->getUser ();
237+ $ cur_monedas_base = $ this ->userlib ->getBaseCurrency ();
238+ $ cur_monedas_dest = $ this ->userlib ->getDestCurrency ();
239+ $ currency_list_dbarraynow = array ();
240+ $ currency_list_dbarraypre = array ();
241+ // ver si id y status es valido ejemplo $this->userlib->isActive(); solo edita si esta activo
242+ $ active = $ this ->userlib ->isActive ();
243+ if ($ active )
244+ {
245+ $ currency_list_dbarraynow = $ this ->dbcm ->readCurrenciesTodayStored ($ currencyDest ,$ currencyDate ,$ currencyBase );
246+ $ currency_list_dbarraypre = $ this ->dbcm ->readCurrenciesTodayStored ($ cur_monedas_dest ,NULL ,$ cur_monedas_base );
247+ }
248+ $ result = 0 ;
249+ if (is_array ($ currency_list_dbarraypre ))
250+ {
251+ if (count ($ currency_list_dbarraypre ) > 0 );
252+ $ result = 1 ;
253+ }
254+ if (is_array ($ currency_list_dbarraynow ))
255+ {
256+ if (count ($ currency_list_dbarraynow ) > 0 );
257+ $ result = 1 ;
258+ }
259+ $ data = array ();
260+ $ data ['result ' ] = $ result ;
261+ $ data ['currency_list_dbarraynow ' ] = $ currency_list_dbarraynow ;
262+ $ data ['currency_list_dbarraypre ' ] = $ currency_list_dbarraypre ;
263+ $ data ['currenturl ' ] = $ this ->currenturl ;
264+ log_message ('debug ' , __METHOD__ .' saved? : ' .print_r ($ data ,TRUE ). ' from parameter: ' .print_r ($ currencyDate , TRUE ));
265+ $ result = json_encode ($ data );
266+ echo $ result ;
267+ return $ result ;
268+ }
269+
270+ /**
271+ * uri CALL to convert one currency to another currency TODO
272+ * it will receive the user_id, currency base/dest, mount currency and codkey as post parameters,
273+ * and later will return 1 when sucess and any other string when fails
274+ * the data when success will be in second string after the result
275+ *
276+ * @access public
277+ * @param $codkey mixed and authentication string key to check api
278+ */
279+ public function convertCurrency ($ extradata = NULL )
280+ {
281+
282+ $ this ->load ->model ('Currency_m ' ,'dbcm ' );
283+ $ this ->load ->library ('Userlib ' );
284+ $ this ->load ->library ('form_validation ' );
285+
286+ $ user_id = $ this ->input ->get_post ('user_id ' , FALSE );
287+ $ codkey = $ this ->input ->get_post ('codkey ' , FALSE );
288+ $ currencyBase = $ this ->input ->get_post ('curbase ' , FALSE );
289+ $ currencyDest = $ this ->input ->get_post ('curdest ' , FALSE );
290+ $ currencyDate = $ this ->input ->get_post ('dateapi ' , FALSE );
291+ $ currencyMont = $ this ->input ->get_post ('curmont ' , FALSE );
292+
293+ $ validfields = $ this ->form_validation ->required ($ user_id );
294+ $ validfields = preg_match ('/^[a-zA-Z0-9_\.]{1,}+$/i ' , $ user_id );
295+ if ($ validfields == FALSE ){
296+ log_message ('error ' , __METHOD__ .' POST : ' . print_r ($ user_id , TRUE ) . ' user_id is not valid : ' .print_r ($ validfields , TRUE ));
297+ $ error = json_encode (array ('result ' =>'user_id is not valid ' ));
298+ echo $ error ;
299+ return $ error ;
300+ }
301+
302+ $ validfields = $ this ->form_validation ->required ($ codkey );
303+ $ validfields = preg_match ('/^[a-zA-Z0-9\.]{1,}+$/i ' , $ codkey );
304+ if ($ validfields == FALSE ){
305+ log_message ('error ' , __METHOD__ .' POST : ' . print_r ($ codkey , TRUE ) . ' codkey is not valid : ' .print_r ($ validfields , TRUE ));
306+ $ error = json_encode (array ('result ' =>'codkey is not valid ' ));
307+ echo $ error ;
308+ return $ error ;
309+ }
310+
311+ $ missdest = $ this ->form_validation ->required ($ currencyDest );
312+ $ validcurren = $ this ->form_validation ->exact_length ($ currencyDest ,3 );
313+ $ missdate = $ this ->form_validation ->required ($ currencyDate );
314+ $ validfields = $ this ->form_validation ->exact_length ($ currencyDate ,10 );
315+ $ missbase = $ this ->form_validation ->required ($ currencyBase );
316+ $ validfields = $ this ->form_validation ->exact_length ($ currencyBase ,3 );
317+
318+ if ($ missdate == FALSE )
319+ $ currencyDate = date ('Y-m-d ' );
320+ if ($ missbase == FALSE )
321+ $ currencyBase = 'USD ' ;
322+ if ($ validfields == FALSE )
323+ $ currencyBase = 'USD ' ;
324+ if ($ validcurren == FALSE OR $ missdest == FALSE )
325+ {
326+ log_message ('info ' , __METHOD__ .' missing currency rates to convert, we will use all availables from api results ' );
327+ $ currencyDest = NULL ;
328+ }
329+
330+ $ this ->userlib ->initialize ($ user_id );
331+ $ this ->userlib ->getID ();
332+
333+ log_message ('debug ' , __METHOD__ .' API already called, now try to retrieve the result from DB for : ' .print_r ($ currencyDate , TRUE ));
334+ $ user_preferences = $ this ->userlib ->getUser ();
335+ $ cur_monedas_base = $ this ->userlib ->getBaseCurrency ();
336+ $ cur_monedas_dest = $ this ->userlib ->getDestCurrency ();
337+ $ currency_list_dbarraynow = array ();
338+ $ currency_list_dbarraypre = array ();
339+ // ver si id y status es valido ejemplo $this->userlib->isActive(); solo edita si esta activo
340+ $ active = $ this ->userlib ->isActive ();
341+ if ($ active )
342+ {
343+ $ currency_list_dbarraynow = $ this ->dbcm ->readCurrenciesTodayStored ($ currencyDest ,$ currencyDate ,$ currencyBase );
344+ $ currency_list_dbarraypre = $ this ->dbcm ->readCurrenciesTodayStored ($ cur_monedas_dest ,NULL ,$ cur_monedas_base );
345+ }
346+ $ result = 0 ;
347+ if (is_array ($ currency_list_dbarraypre ))
348+ {
349+ if (count ($ currency_list_dbarraypre ) > 0 );
350+ $ result = 1 ;
351+ }
352+ if (is_array ($ currency_list_dbarraynow ))
353+ {
354+ if (count ($ currency_list_dbarraynow ) > 0 );
355+ $ result = 1 ;
356+ }
357+ $ data = array ();
358+ $ data ['result ' ] = $ result ;
359+ $ data ['currency_mont ' ] = '30.90 ' ;
360+ $ data ['currenturl ' ] = $ this ->currenturl ;
361+ log_message ('debug ' , __METHOD__ .' saved? : ' .print_r ($ data ,TRUE ). ' from parameter: ' .print_r ($ currencyDate , TRUE ));
362+ $ result = json_encode ($ data );
161363 echo $ result ;
162364 return $ result ;
163365 }
0 commit comments