Location: PHPKode > projects > MembersGear > membersgear/includes/export_class.php
<?php
 //echo date("Y-m-d",1243728000)  ;echo strtotime("2009-05-26"); die;   
 
 require_once "master_class.php";
 
 class export_class
 {   
        function SetMemberFields($pQuery,$pFields)
        {
             foreach($pFields as $fields)
             {
                 if($fields == 'createdon' or $fields == 'modifiedon')
                 {
                        $pQuery .= 'FROM_UNIXTIME('.$fields.',"%Y-%m-%d %H:%i:%s") as '.$fields.', ';
                 }
                 else
                 {
                        $pQuery .= $fields.', ';   
                 }
             }
             return $pQuery;
        }
        
        function SetSubscriptionFields($pQuery,$pFields)
        {
             foreach($pFields as $fields)
             {
                 if($fields == 'fromdate' or $fields == 'uptodate')
                 {
                        $pQuery .= 'FROM_UNIXTIME('.$fields.',"%Y-%m-%d %H:%i:%s") as '.$fields.', ';
                 }
                 else
                 {
                        $pQuery .= $fields.', ';   
                 }  
             }
             $pQuery .= 'productid, ';
             return $pQuery;
        }
        
        function SetRecordSelections($pQuery,$pCriteria)
        {
            $loPaygearMasterClass = new master_class();
            $masterID = $loPaygearMasterClass->getMasterId('productmembershipstatus');  
            if(is_array($pCriteria) and sizeof($pCriteria) > 0)
            {
                    $pQuery .= ' AND (';
                    $condition = ''; 
                    foreach($pCriteria as $criteria)
                     {
                         //$pQuery .= $criteria.' OR ';   
                         switch ($criteria)
                         {
                              case 'any':
                                    
                                    $pQuery .= 'tpu.productmembershipstatusid = tpu.productmembershipstatusid ';
                                    $condition = ' OR ';        
                                    break;
                                    
                              case 'active':
                                    $mastervalueid = $loPaygearMasterClass->getMasterValuesId('Active',$masterID);
                                    $pQuery .= $condition.'tpu.productmembershipstatusid = '.$mastervalueid.' ';
                                    $condition = ' OR ';
                                    break;
                                    
                              case 'not_completed':
                                    $mastervalueid = $loPaygearMasterClass->getMasterValuesId('Pending',$masterID);
                                    $pQuery .= $condition.'tpu.productmembershipstatusid = '.$mastervalueid.' ';
                                    $condition = ' OR ';
                                    break;
                              
                              case 'expired':
                                    $mastervalueid = $loPaygearMasterClass->getMasterValuesId('Expire',$masterID);
                                    $pQuery .= $condition.'tpu.productmembershipstatusid = '.$mastervalueid.' ';
                                    $condition = ' OR ';
                                    break;
                         }
                         
                     }
                     $pQuery .= ') ';
            }
             
             return $pQuery;
        }
        
        function SetDateRange($pQuery,$pFromDate,$pUpToDate)
        {
               $pQuery .= '( tpu.fromdate < '.$pFromDate.' AND tpu.uptodate > '.$pUpToDate.')';
               return $pQuery;
        }
        
        function SetProductList($pQuery,$pProductID)
        {
            if(is_array($pProductID) and count($pProductID) > 0)
                {$pQuery .= '(';}
            foreach($pProductID as $fields)
             {
                    $pQuery .= 'tpu.productid = '.$fields.' OR ';   
             }
             if(is_array($pProductID) and count($pProductID) > 0)
             {$pQuery .= ')';}

             return $pQuery;
        }
        
        function exportMysqlToCsv($Query,$filename,$pCSVseprator,$pCSVenclosed,$pIsDownload)
        {
                    $csv_terminated = "\n";
                    $csv_separator = $pCSVseprator;//";";
                    $csv_enclosed = $pCSVenclosed;//'"';
                    $csv_escaped = "\\";
                    $laResponseData = array();
                    $sql_query=$Query;
                    
                    // Gets the data from the database
                    $result = mysql_query($sql_query) or die(mysql_error());
                    $fields_cnt = mysql_num_fields($result);


                    $schema_insert = '';
                    
                    $passflag = '';
                    $passindex = 0;
                    
                    $countryflag = '';
                    $countryindex = 0;
                    
                    $stateflag = '';
                    $stateindex = 0;
                    
                    $shippingcountryflag = '';
                    $shippingcountryindex = 0;
                    
                    $shippingstateflag = '';
                    $shippingstateindex = 0;
                    
                    for ($i = 0; $i < $fields_cnt; $i++)
                    {

                        $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
                            stripslashes(mysql_field_name($result, $i))) . $csv_enclosed;
                        $schema_insert .= $l;
                        $schema_insert .= $csv_separator;
                        if(mysql_field_name($result,$i) == "password")
                        {
                            $passflag  = 'true';
                            $passindex = $i;
                        }
                        
                        if(mysql_field_name($result,$i) == "country")
                        {
                            $countryflag  = 'true';
                            $countryindex = $i;
                        }
                        
                        if(mysql_field_name($result,$i) == "state")
                        {
                            $stateflag  = 'true';
                            $stateindex = $i;
                        }
                        
                        if(mysql_field_name($result,$i) == "billing_country")
                        {
                            $shippingcountryflag  = 'true';
                            $shippingcountryindex = $i;
                        }
                        
                        if(mysql_field_name($result,$i) == "billing_state")
                        {
                            $shippingstateflag  = 'true';
                            $shippingstateindex = $i;
                        }
                        
                    } // end for

                      

                    $out = trim(substr($schema_insert, 0, -1));

                    $out .= $csv_terminated;
                    $laResponseData[] = $out;
                    
                    // Format the data
                    while ($row = mysql_fetch_array($result))
                    {
                        $schema_insert = '';
                        for ($j = 0; $j < $fields_cnt; $j++)
                        {
                            if ($row[$j] == '0' || $row[$j] != '')
                            {

                                if ($csv_enclosed == '')
                                {
                                    if($passflag == 'true' and $passindex == $j)
                                      {
                                          $query = 'SELECT secretsalt FROM '.TABLE_PREFIX.'users WHERE password=\''.$row[$j].'\'';
                                          $link = mysql_query($query) or die(mysql_error());
                                          $secretKey = mysql_fetch_array($link) or die; 
                                          $row[$j] = decrypt($row[$j],$secretKey[0]); 
                                      }
                                      if(($countryflag == 'true' or $stateflag == 'true' or $shippingcountryflag == 'true' or $shippingstateflag == 'true') and ($countryindex == $j or $stateindex == $j or $shippingcountryindex == $j or $shippingstateindex == $j))
                                      {
                                            if($row[$j] != '' and $row[$j] > 0)
                                            {
                                                $query = 'SELECT name FROM '.TABLE_PREFIX.'location WHERE id='.$row[$j];
                                                $link = mysql_query($query) or die(mysql_error());
                                                $locationname = mysql_fetch_array($link); //or die(mysql_error());
                                                if($locationname['name'] != '')
                                                {
                                                    $row[$j] = $locationname['name'];
                                                }
                                                else
                                                {
                                                    $row[$j] = 'Not Specified';   
                                                }
                                            }
                                            else
                                            {
                                                $row[$j] = 'Not Specified';   
                                            }
                                      }
                                      $schema_insert .= $row[$j];
                                } 
                                else
                                {
                                      if($passflag == 'true' and $passindex == $j)
                                      {
                                          $query = 'SELECT secretsalt FROM '.TABLE_PREFIX.'users WHERE password=\''.$row[$j].'\'';
                                          $link = mysql_query($query) or die(mysql_error());
                                          $secretKey = mysql_fetch_array($link) or die; 
                                          $row[$j] = decrypt($row[$j],$secretKey[0]); 
                                      }
                                      
                                      if(($countryflag == 'true' and $countryindex == $j) or ($stateflag == 'true' and $stateindex==$j) or( $shippingcountryflag == 'true' and $shippingcountryindex == $j) or ($shippingstateflag == 'true' and  $shippingstateindex == $j))
                                      {
                                            if($row[$j] != '' and $row[$j] > 0)
                                            {
                                                $query = 'SELECT name FROM '.TABLE_PREFIX.'location WHERE id='.$row[$j];
                                                $link = mysql_query($query) or die(mysql_error());
                                                $locationname = mysql_fetch_array($link); //or die(mysql_error());
                                                if($locationname['name'] != '')
                                                {
                                                    $row[$j] = $locationname['name'];
                                                }
                                                else
                                                {
                                                    $row[$j] = 'Not Specified';   
                                                }
                                            }
                                            else
                                            {
                                                $row[$j] = 'Not Specified';   
                                            }
                                      }
                                      
                                           $schema_insert .= $csv_enclosed .
                                            str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed;
                                }
                            } 
                            else
                            {
                                if($countryindex == $j or $stateindex == $j or $shippingcountryindex == $j or $shippingstateindex == $j)
                                {
                                    $schema_insert .= '"Not Specified"';                                          
                                }
                                else
                                {
                                    $schema_insert .= '';
                                }
                            }

                            if ($j < $fields_cnt - 1)
                            {
                                $schema_insert .= $csv_separator;
                            }
                        } // end for

                        $out .= $schema_insert;
                        $out .= $csv_terminated;
                        $laResponseData[] = $schema_insert;
                        //echo $out."<br>";
                    } // end while 
                   if($pIsDownload > 0)
                   {
                            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
                            header("Content-Length: " . strlen($out));
                            // Output to browser with appropriate mime type, you choose ;)
                            header("Content-type: text/x-csv");
                            //header("Content-type: text/csv");
                            //header("Content-type: application/csv");
                            header("Content-Disposition: attachment; filename=$filename");
                           echo $out;
                           //exit;
                   }
                   else
                   {
                        return $laResponseData;   
                   }
                   
        }
 }   
?>
Return current item: MembersGear