Skip to content

Commit 46c7280

Browse files
committed
new currency api layer interface.. move currency calls to api to api class
* the new currency api class will handle multicalls to vrious backend api, so we can configure various api backends and if some of them fails, we will try next other.. * move and sanitize button look based on data available, if the data is present renders a stop icon, so indicates to user that is not necesary to call api cos we already has data stored. But still able to call it, we need to manange this in new behaviour by permission * check curren user and current api calls on each call, so user_id and codkey must be parsed on api calls * provide and fix the missing json response on api call from older broken controller Currency_Manager when moves to Currency_Api closes https://gitlab.com/codeigniterpower/codeigniter-currencylib/-/issues/6 * remove common calls for jquery and datatables javascripts libraries andmoves to header loading, the delayed ones moved to footer * footer was remade to only be loaded by internal views, the footer showed at login and index are hardcoded.. this must be managed refers https://gitlab.com/codeigniterpower/codeigniter-currencylib/-/issues/7 so we just try to fit but still is an issue
1 parent 0ed7384 commit 46c7280

File tree

7 files changed

+281
-173
lines changed

7 files changed

+281
-173
lines changed

cweb/elcurrencyweb/config/currencyweb.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
66
*/
77

8-
$config['codkey'] = 'SHAR265ql-23krjhnou2q34rhi2';//';
8+
$config['codkey'] = 'SHAR265ql-23krjhnou2q34rhi2';//' this is a key that our own clients can use to retrieve secrelty data from this app.. so we can avoid using external paids to others apis;
9+
$config['telegramnews'] = 'https://t.me/s/open_tecnologies';//' https://t.me/s/open_tecnologies the 's' uri section means can be used in a widget.. witout then the phone will load the app ;
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2+
3+
/**
4+
* THIS CONTROLLER IS INTERFACE TO PROVIDE CURRENCY FUNCTIONALITY TO OHERS SYSTEMS
5+
* remmembered that each controller is a request calll in the web app
6+
*
7+
* @author PICCORO Lenz McKAY
8+
* @copyright Copyright (c) 2018, 2019
9+
* @version ab - 1.0
10+
*/
11+
class Currency_Api extends CP_Controller {
12+
13+
/**
14+
* name: default constructor
15+
*/
16+
function __construct()
17+
{
18+
parent::__construct();
19+
$this->load->helper(array('form', 'url','html'));
20+
$this->output->enable_profiler(ENVIRONMENT !== 'production');
21+
}
22+
23+
24+
/**
25+
* uri CALL to uptate the amount of a specific rate currency by code using api call
26+
*
27+
* @access public
28+
* @param $codkey mixed and authentication string key to check api
29+
*/
30+
public function updateRateAmount()
31+
{
32+
$this->load->model('Currency_m','dbcm');
33+
$this->load->library('Userlib');
34+
$this->load->library('form_validation');
35+
36+
$mon_tasa_moneda = $this->input->post('mon_tasa_moneda', FALSE);
37+
$cod_tasa = $this->input->post('cod_tasa', FALSE);
38+
$user_id = $this->input->post('user_id', FALSE);
39+
$codkey = $this->input->post('codkey', FALSE);
40+
41+
$validfields = $this->form_validation->required($cod_tasa);
42+
$validfields = preg_match('/^[0-9]{1,14}+$/i', $cod_tasa);
43+
if($validfields == FALSE){
44+
log_message('error', __METHOD__ .' POST : ' . print_r($cod_tasa, TRUE) . ' why field (numeric/lenght) is : '.var_export($validfields, TRUE));
45+
$error = 2;
46+
echo json_encode(array('result' =>'cod_tasa invalid format or no data provided'));
47+
return $error;
48+
}
49+
50+
$validfields = $this->form_validation->required($mon_tasa_moneda);
51+
$validfields = preg_match('/^[0-9\.]{1,}+$/i', $mon_tasa_moneda);
52+
if($validfields == FALSE){
53+
log_message('error', __METHOD__ .' POST : ' . print_r($mon_tasa_moneda, TRUE) . ' moneda is not numeric/decmal : '.print_r($validfields, TRUE));
54+
$error = json_encode(array('result' =>'mon_tasa_moneda invalida format or no data provided'));
55+
echo $error;
56+
return $error;
57+
}
58+
59+
$validfields = $this->form_validation->required($mon_tasa_moneda);
60+
$validfields = preg_match('/^[0-9\.]{1,}+$/i', $mon_tasa_moneda);
61+
if($validfields == FALSE){
62+
log_message('error', __METHOD__ .' POST : ' . print_r($mon_tasa_moneda, TRUE) . ' moneda is not numeric/decmal : '.print_r($validfields, TRUE));
63+
$error = json_encode(array('result' =>'mon_tasa_moneda invalida format or no data provided'));
64+
echo $error;
65+
return $error;
66+
}
67+
68+
$this->userlib->initialize($user_id);
69+
if($this->userlib->isActive()){
70+
$result = $this->dbcm->updateCurrencyMount($cod_tasa, $mon_tasa_moneda);
71+
}
72+
73+
log_message('info', __METHOD__ .' DB result : ' . print_r($result, TRUE) );
74+
$result = json_encode(array('result' =>$result));
75+
echo $result;
76+
return $result;
77+
// TODO: use this only with desesperate end of time : $this->listcurrencies();
78+
}
79+
80+
/**
81+
* uri CALL to invoke the api and store the data into db
82+
*
83+
* @access public
84+
* @param $codkey mixed and authentication string key to check api
85+
*/
86+
public function callApisAndSaveDB($codkey = NULL)
87+
{
88+
89+
$this->load->model('Currency_m','dbcm');
90+
$this->load->library('Userlib');
91+
$this->load->library('form_validation');
92+
93+
$user_id = $this->input->post('user_id', FALSE);
94+
$codkey = $this->input->post('codkey', FALSE);
95+
96+
$validfields = $this->form_validation->required($user_id);
97+
$validfields = preg_match('/^[a-zA-Z0-9\.]{1,}+$/i', $user_id);
98+
if($validfields == FALSE){
99+
log_message('error', __METHOD__ .' POST : ' . print_r($user_id, TRUE) . ' user_id is not valid : '.print_r($validfields, TRUE));
100+
$error = json_encode(array('result' =>'user_id is not valid'));
101+
echo $error;
102+
return $error;
103+
}
104+
105+
$validfields = $this->form_validation->required($codkey);
106+
$validfields = preg_match('/^[a-zA-Z0-9\.]{1,}+$/i', $codkey);
107+
if($validfields == FALSE){
108+
log_message('error', __METHOD__ .' POST : ' . print_r($codkey, TRUE) . ' codkey is not valid : '.print_r($validfields, TRUE));
109+
$error = json_encode(array('result' =>'codkey is not valid'));
110+
echo $error;
111+
return $error;
112+
}
113+
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"
117+
$currencyDate = $this->input->get_post('dateapi', FALSE);
118+
$currencyBase = $this->input->get_post('curbase', FALSE);
119+
$currencyDest = $this->input->get_post('curdest', FALSE);
120+
$this->load->library('form_validation');
121+
$missdest = $this->form_validation->required($currencyDest);
122+
$validcurren = $this->form_validation->min_length($currencyDest,3);
123+
$missdate = $this->form_validation->required($currencyDate);
124+
$validfields = $this->form_validation->exact_length($currencyDate,10);
125+
$missbase = $this->form_validation->required($currencyBase);
126+
$validfields = $this->form_validation->exact_length($currencyBase,3);
127+
if($missdate == FALSE)
128+
$currencyDate = date('Y-m-d');
129+
if($missbase == FALSE)
130+
$currencyBase = 'USD';
131+
if($validfields == FALSE)
132+
$currencyBase = 'USD';
133+
if($validcurren == FALSE OR $missdest == FALSE)
134+
{
135+
log_message('info', __METHOD__ .' missing currency rates to convert, we will use all availables from api results ');
136+
$currencyDest = NULL;
137+
}
138+
if(mb_strlen($currencyDest) > 3)
139+
{
140+
if(stripos($currencyDest,',') == FALSE)
141+
{
142+
log_message('error', __METHOD__ .' invalid currency set, more than one but missing separator, we will use all');
143+
$currencyDest = NULL;
144+
}
145+
}
146+
147+
$this->userlib->initialize($user_id);
148+
149+
log_message('debug', __METHOD__ .' getting the data from internet API for : '.print_r($currencyDate, TRUE));
150+
$currency_list_apiarray = array();
151+
$this->load->library('Currencylib');
152+
$currencyDate = date('Y-m-d',strtotime($currencyDate));
153+
$currency_list_apiarray = $this->currencylib->getAllCurrencyByApi($currencyBase,$currencyDest,$currencyDate);
154+
155+
log_message('debug', __METHOD__ .' API already called, now try to store the result into DB for : '.print_r($currencyDate, TRUE));
156+
$this->load->model('Currency_m','dbcm');
157+
$currencyDate = date('Ymd',strtotime($currencyDate)).date('H');
158+
$createdbresult = $this->dbcm->createCurrencyFromApi($currency_list_apiarray, $currencyDate, $currencyBase);
159+
log_message('debug', __METHOD__ .' saved? : ' .print_r($createdbresult,TRUE). ' from parameter: '.print_r($currencyDate, TRUE));
160+
$result = json_encode(array('result'=>$createdbresult));
161+
echo $result;
162+
return $result;
163+
}
164+
165+
}

cweb/elcurrencyweb/controllers/Currency_Manager.php

Lines changed: 13 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function __construct()
2929
*/
3030
public function index()
3131
{
32-
$this->listcurrencies();
32+
$this->listTodayCurrencies();
3333
}
3434

3535
/**
@@ -39,12 +39,12 @@ public function index()
3939
*
4040
* @access public
4141
*/
42-
public function listcurrencies()
42+
public function listTodayCurrencies()
4343
{
4444
// load user preferences, lib must be init after loading
4545
$this->load->library('Userlib');
46-
// $this->userlib->initialize('gonzalez_angel');
47-
$this->userlib->initialize('lenz_gerardo');
46+
$this->userlib->initialize('gonzalez_angel');
47+
//$this->userlib->initialize('lenz_gerardo');
4848

4949
$this->userlib->getID();
5050
$this->load->model('Currency_m','dbcm');
@@ -57,9 +57,17 @@ public function listcurrencies()
5757
// ver si id y status es valido ejemplo $this->userlib->isActive(); solo edita si esta activo
5858
$data['active'] = $active = $this->userlib->isActive();
5959
$currency_list_dbarraynow = $this->dbcm->readCurrenciesTodayStored();
60-
if($active){
60+
if($active)
61+
{
6162
$currency_list_dbarraypre = $this->dbcm->readCurrenciesTodayStored($cur_monedas_dest,NULL,$cur_monedas_base);
6263
}
64+
$buttonicon = '<i class="bi bi-x-octagon-fill" style="font-size: 30px;"></i>';
65+
if(is_array($currency_list_dbarraypre))
66+
{
67+
if(count($currency_list_dbarraypre) > 0 );
68+
$buttonicon = '<i class="bi bi-currency-exchange" style="font-size: 30px;"></i>';
69+
}
70+
$data['buttonicon'] = $buttonicon;
6371
$data['user_preferences'] = $user_preferences;
6472
$data['currency_list_dbarraynow'] = $currency_list_dbarraynow;
6573
$data['currency_list_dbarraypre'] = $currency_list_dbarraypre;
@@ -71,136 +79,6 @@ public function listcurrencies()
7179
$this->load->view('footer',$data);
7280
}
7381

74-
public function updatecurrency()
75-
{
76-
$this->load->library('Userlib');
77-
$this->userlib->initialize('lenz_gerardo');
78-
79-
80-
$this->load->model('Currency_m','dbcm');
81-
$this->load->library('form_validation');
82-
$mon_tasa_moneda = $this->input->post('mon_tasa_moneda', FALSE);
83-
$cod_tasa = $this->input->post('cod_tasa', FALSE);
84-
$validfields = $this->form_validation->required($cod_tasa);
85-
if($validfields == FALSE){
86-
log_message('error', __METHOD__ .' POST : ' . print_r($_POST, TRUE) . ' why: '.print_r($cod_tasa, TRUE));
87-
return $error = 0;
88-
}
89-
$validfields = $this->form_validation->exact_length($cod_tasa,14);
90-
if($validfields == FALSE){
91-
log_message('error', __METHOD__ .' POST : ' . print_r($_POST, TRUE) . ' why: '.print_r("No cumple con la longitud", TRUE));
92-
return $error = 0;
93-
}
94-
$validfields = $this->form_validation->numeric($cod_tasa);
95-
if($validfields == FALSE){
96-
log_message('error', __METHOD__ .' POST : ' . print_r($_POST, TRUE) . ' why: '.print_r("No es numerico 1", TRUE));
97-
echo json_encode(array('result' =>'Debe ser un numero entero o decimal'));
98-
return $error = 0;
99-
}
100-
$validfields = $this->form_validation->required($mon_tasa_moneda);
101-
if($validfields == FALSE){
102-
log_message('error', __METHOD__ .' POST : ' . print_r($_POST, TRUE) . ' why: '.print_r($mon_tasa_moneda, TRUE));
103-
echo json_encode(array('result' =>'Debe ingresar un monto'));
104-
return $error = 0;
105-
}
106-
$validfields = $this->form_validation->numeric($mon_tasa_moneda);
107-
if($validfields == FALSE){
108-
log_message('error', __METHOD__ .' POST : ' . print_r($_POST, TRUE) . ' why: '.print_r("No es numerico", TRUE));
109-
echo json_encode(array('result' =>'Debe ser de tipo numerico'));
110-
return $error = 0;
111-
}
112-
113-
if($this->userlib->isActive()){
114-
$result = $this->dbcm->updateCurrencyMount($cod_tasa, $mon_tasa_moneda);
115-
}
116-
117-
log_message('info', __METHOD__ .' DB result : ' . print_r($result, TRUE) );
118-
119-
// TODO: use this only with desesperate end of time : $this->listcurrencies();
120-
echo json_encode(array('result' =>$result));
121-
}
122-
123-
/**
124-
* uri CALL to invoke the api and store the data into db
125-
*
126-
* @access public
127-
* @param $codkey mixed and authentication string key to check api
128-
*/
129-
public function callapitodb($codkey = NULL)
130-
{
131-
$this->load->model('Usuario_m','users');
132-
$this->load->library('Userlib');
133-
$this->userlib->initialize('gonzalez_angel');
134-
135-
if(!$this->userlib->isActive())
136-
{
137-
log_message('debug', __METHOD__ .' saved? : ' .' Not authorized ');
138-
return json_encode(array('result'=>'unauthorized access, user must be a valid activated user'));
139-
}
140-
141-
log_message('info', __METHOD__ .' calltoapi codkey argument method '.print_r($codkey, TRUE));
142-
if($codkey == NULL)
143-
{
144-
log_message('error', __METHOD__ .' missing codkey, checking POST');
145-
$codkey = $this->input->get_post('codkey', FALSE);
146-
if( is_null($codkey) OR empty($codkey) )
147-
return json_encode(array('result'=>'unauthorized access'));
148-
}
149-
150-
$this->load->config('currencyweb');
151-
$codkeyconf = $this->config->item('codkey');
152-
if( $codkey != $codkeyconf)
153-
{
154-
log_message('error', __METHOD__ .' invalid codkey ' .print_r($codkey,TRUE). ' from config: '.print_r($codkeyconf, TRUE));
155-
// return json_encode(array('result'=>'unauthorized access'));
156-
}
157-
$config['language'] = 'spanish';
158-
$data = array();
159-
// example invokation http://localhost/~general/codeigniter-currencylib/cweb/index.php/Currency_Manager/callapitodb/SHAR265ql-23krjhnou2q34rhi2?dateapi=2023-02-03&curbase=USD
160-
// example shot base "index.php/Currency_Manager/callapitodb/SHAR265ql-23krjhnou2q34rhi2?dateapi=2023-02-03&curbase=USD"
161-
$currencyDate = $this->input->get_post('dateapi', FALSE);
162-
$currencyBase = $this->input->get_post('curbase', FALSE);
163-
$currencyDest = $this->input->get_post('curdest', FALSE);
164-
$this->load->library('form_validation');
165-
$missdest = $this->form_validation->required($currencyDest);
166-
$validcurren = $this->form_validation->min_length($currencyDest,3);
167-
$missdate = $this->form_validation->required($currencyDate);
168-
$validfields = $this->form_validation->exact_length($currencyDate,10);
169-
$missbase = $this->form_validation->required($currencyBase);
170-
$validfields = $this->form_validation->exact_length($currencyBase,3);
171-
if($missdate == FALSE)
172-
$currencyDate = date('Y-m-d');
173-
if($missbase == FALSE)
174-
$currencyBase = 'USD';
175-
if($validfields == FALSE)
176-
$currencyBase = 'USD';
177-
if($validcurren == FALSE OR $missdest == FALSE)
178-
{
179-
log_message('info', __METHOD__ .' missing currency rates to convert, we will use all availables from api results ');
180-
$currencyDest = NULL;
181-
}
182-
if(mb_strlen($currencyDest) > 3)
183-
{
184-
if(stripos($currencyDest,',') == FALSE)
185-
{
186-
log_message('error', __METHOD__ .' invalid currency set, more than one but missing separator, we will use all');
187-
$currencyDest = NULL;
188-
}
189-
}
190-
log_message('debug', __METHOD__ .' getting the data from internet API for : '.print_r($currencyDate, TRUE));
191-
$currency_list_apiarray = array();
192-
$this->load->library('Currencylib');
193-
$currencyDate = date('Y-m-d',strtotime($currencyDate));
194-
$currency_list_apiarray = $this->currencylib->getAllCurrencyByApi($currencyBase,$currencyDest,$currencyDate);
195-
log_message('debug', __METHOD__ .' API already called, now try to store the result into DB for : '.print_r($currencyDate, TRUE));
196-
$this->load->model('Currency_m','dbcm');
197-
$currencyDate = date('Ymd',strtotime($currencyDate)).date('H');
198-
$createdbresult = $this->dbcm->createCurrencyFromApi($currency_list_apiarray, $currencyDate, $currencyBase);
199-
log_message('debug', __METHOD__ .' saved? : ' .print_r($createdbresult,TRUE). ' from parameter: '.print_r($currencyDate, TRUE));
200-
return json_encode(array('result'=>$createdbresult));
201-
}
202-
203-
20482
}
20583

20684
/* End of file Currency_Manager.php */

0 commit comments

Comments
 (0)