Advertisement
christiansalazarh

demo1-componente

Aug 29th, 2013
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.88 KB | None | 0 0
  1. <?php
  2. /***
  3.     install as component:
  4.        
  5.     protected/config/main.php
  6.     ..
  7.     'components'=>array(
  8.         'dsx'=>array(
  9.             'class'=>'ext.datasourcecampanametro.DataSourceCampanaMetro',
  10.             'datasource'=>'vpn',
  11.             'vpn_view_name'=>'nombre_de_la_vista_que_el_cliente_da',
  12.             'vpn_db'=>array(
  13.                 'connectionString' => 'mysql:host=localhost;dbname=vpndbname',
  14.                 'emulatePrepare' => true,
  15.                 'username' => 'bla',
  16.                 'password' => 'xxblaxx',
  17.                 'charset' => 'utf8',
  18.             ),
  19.         ),
  20.     ),
  21.  
  22.  * @author Christian Salazar H. <christiansalazarh@gmail.com>
  23. */
  24. class DataSourceCampanaMetro
  25.     extends CApplicationComponent
  26.     implements IDataSourceCampana {
  27.    
  28.     public $datasource; // "vpn" or "csv"
  29.     public $vpn_db; // db settings. required when datasource is "vpn"
  30.     public $vpn_view_name;
  31.     private $_db;
  32.     private $_last_key;
  33.     private $_last_record;
  34.        
  35.     public function leerCliente($cedula){
  36.         if($this->_last_key == $cedula)
  37.             return $this->_last_record;//LAZY
  38.         if($this->_last_record = $this->getDb()->select()->from($this->_Tablename())   
  39.             ->where("cedula=:c",array(":c"=>$cedula))->queryRow()){
  40.             return $this->_last_record;
  41.         }else{
  42.             $this->_last_record = null;
  43.             return null;
  44.         }
  45.     }
  46.     public function listarContratos($cedula){
  47.         if($row = $this->leerCliente($cedula)){
  48.             $contracts = array();
  49.             foreach($this->decodeQuotes($row['cuotas']) as $quote){
  50.                 list($qn, $contract, $val1, $val2, $date) = $quote;
  51.                 if(!in_array($contract, $contracts))
  52.                     $contracts[] = $contract;
  53.             }
  54.             return $contracts;
  55.         }else
  56.         return array();
  57.     }
  58.     public function listarCuotas($cedula, $numero_contrato){
  59.         if($row = $this->leerCliente($cedula)){
  60.             $quotes = array();
  61.             foreach($this->decodeQuotes($row['cuotas']) as $quote){
  62.                 list($qn, $contract, $val1, $val2, $date) = $quote;
  63.                 if($contract == $numero_contrato)
  64.                     $quotes[] = $quote;
  65.             }
  66.             return $quotes;
  67.         }else
  68.         return array();
  69.     }
  70.     public function importarCsv($filename){
  71.         if(!is_file($filename))
  72.             return "ERR_NOT_A_FILE";
  73.         // force db connection to be "CSV" when importing data
  74.         $this->_db = null;
  75.         $this->datasource = "csv";
  76.         $this->getDb()->createCommand()->delete($this->_Tablename());
  77.         $f = fopen($filename,"r");
  78.         while($data = fgetcsv($f, 0, ",")){
  79.             list($ced, $nom, $dir, $tel, $fec, $cuotas)=$data;
  80.             $this->getDb()->createCommand()->insert($this->_Tablename(),
  81.                 array(
  82.                     "cedula"=>$ced,
  83.                     "nombres"=>$nom,
  84.                     "direccion"=>$dir,
  85.                     "telefono"=>$tel,
  86.                     "fecha_act"=>$fec,
  87.                     "cuotas"=>$cuotas
  88.                 ));
  89.         }
  90.         fclose($f);
  91.         return "OK";
  92.     }
  93.  
  94.     private function getDb(){
  95.         if($this->_db == null){
  96.             if($this->datasource == "csv"){
  97.                 $this->_db = Yii::app()->db;
  98.             }else{
  99.                 // build a db connection.
  100.                 $this->_db = new CDbConnection(
  101.                     $this->vpn_db['connectionString'],
  102.                         $this->vpn_db['username'],$this->vpn_db['password']);
  103.                 if(isset($this->vpn_db['charset']))
  104.                     $this->_db->charset = $this->vpn_db['charset'];
  105.                 if(isset($this->vpn_db['emulatePrepare']))
  106.                     $this->_db->emulatePrepare = $this->vpn_db['emulatePrepare'];
  107.                 $this->_db->active = true;
  108.             }
  109.         }
  110.         if($this->_db == null)
  111.             throw new Exception("invalid database connection");
  112.         return $this->_db;
  113.     }
  114.     private function _Tablename(){
  115.         if($this->datasource=="csv")
  116.             return "dscampanametro";
  117.         if($this->datasource=="vpn")
  118.             return $this->vpn_view_name;
  119.         return "";
  120.     }
  121.     private function decodeQuotes($quotes){
  122.         $out=array();
  123.         foreach(explode(";",$quotes) as $quote){
  124.             if($quote != ""){
  125.             list($qn,$items) = explode("=",$quote);
  126.             list($contract, $val1, $val2, $date) = explode(",",$items);
  127.             $dt=explode("/",$date);
  128.             $out[] = array($qn,$contract, $val1, $val2,
  129.                 date("Y-m-d",strtotime(sprintf("%s-%s-%s",$dt[2],$dt[1],$dt[0]))));
  130.             }
  131.         }
  132.         return $out;
  133.     }
  134.     private function _sampleRecord(){
  135.         $quotes =
  136.              "Q1=021546,75.12,84.1344,26/12/2004;"
  137.             ."Q2=021546,88.17,98.7504,26/12/2005;"
  138.             ."Q3=021546,102,114.24,26/12/2006;"
  139.             ."Q4=021546,117,131.04,26/12/2007;"
  140.             ."Q5=021546,143.13,160.306,26/12/2008;"
  141.             ."Q6=021546,189.9,212.688,26/12/2009;"
  142.             ."Q1=021547,241.08,270.01,26/12/2010;"
  143.             ."Q2=021547,308.04,345.005,26/12/2011;"
  144.             ."Q1=021548,388.41,435.019,26/12/2012;"
  145.             ."Q2=021548,575.88,644.986,26/12/2013;";
  146.         return array(
  147.             "cedula"=>'TEST',
  148.             "nombres"=>'PORCUPINE TREE',
  149.             "direccion"=>'ARRIVING SOMEWHERE, BUT NOT HERE',
  150.             "telefono"=>'555-9-123456;555-8-876543',
  151.             "fecha_act"=>'2013-08-20',
  152.             "cuotas"=>$quotes
  153.         );
  154.     }
  155.     public function test($vpn_db){
  156.         // called by consolecommand to test features
  157.         $this->_db = null;
  158.         $this->datasource = "csv";
  159.         printf("tableName is: %s\n",$this->_Tablename());
  160.         printf("getDb call when datasource is: %s...",$this->datasource);
  161.         $this->getDb();
  162.         printf("OK\n");
  163.  
  164.         $this->_db = null;
  165.         $this->vpn_db = $vpn_db;
  166.         $this->datasource = "vpn";
  167.         printf("viewName is: %s\n",$this->_Tablename());
  168.         printf("getDb call when datasource is: %s...",$this->datasource);
  169.         $this->getDb();
  170.         printf("OK\n");
  171.  
  172.         $this->_last_key = 'TEST';
  173.         $this->_last_record = $this->_sampleRecord();
  174.  
  175.         $quotes = $this->_last_record['cuotas'];
  176.         printf("testing quote object decode:\n%s\ninto:\n",$quotes);
  177.         foreach($this->decodeQuotes($quotes) as $quote=>$fields){
  178.             list($contract, $val1, $val2, $date)=$fields;
  179.             printf("%s = %s, %s, %s, %s\n",$quote,$contract, $val1, $val2, $date);
  180.         }
  181.         printf("\ndone\n");
  182.  
  183.         printf("test leerCliente()\n%s\nOK\n",json_encode($this->leerCliente('TEST')));
  184.         printf("test listarContratos()\n%s\nOK\n",json_encode($this->listarContratos('TEST')));
  185.         printf("test listarCuotas(test,021546)\n%s\nOK\n",json_encode($this->listarCuotas('TEST','021546')));  
  186.         printf("test listarCuotas(test,021547)\n%s\nOK\n",json_encode($this->listarCuotas('TEST','021547')));  
  187.         printf("test listarCuotas(test,021548)\n%s\nOK\n",json_encode($this->listarCuotas('TEST','021548')));  
  188.  
  189.     }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement