voucherserie_export.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class VoucherSerie_Export extends VoucherSerie_Main
00007 {
00013     public $sClassDo = "voucherserie_export";
00014 
00020     public $sExportFileType = "csv";
00021 
00026     protected $_sThisTemplate = "voucherserie_export.tpl";
00027 
00033     public $iExportPerTick = 1000;
00034 
00040     public function __construct()
00041     {
00042         parent::__construct();
00043 
00044         // export file name
00045         $this->sExportFileName = $this->_getExportFileName();
00046 
00047         // set generic frame template
00048         $this->_sFilePath = $this->_getExportFilePath();
00049     }
00050 
00056     public function getDownloadUrl()
00057     {
00058         $myConfig = $this->getConfig();
00059 
00060         // override cause of admin dir
00061         $sUrl = $myConfig->getConfigParam( 'sShopURL' ). $myConfig->getConfigParam( 'sAdminDir' );
00062         if ( $myConfig->getConfigParam( 'sAdminSSLURL' ) ) {
00063             $sUrl = $myConfig->getConfigParam( 'sAdminSSLURL' );
00064         }
00065 
00066         $sUrl = $this->getSession()->url( $sUrl.'/index.php' );
00067         return $sUrl . '&amp;cl='.$this->sClassDo.'&amp;fnc=download';
00068     }
00069 
00075     protected function _getExportFileName()
00076     {
00077         $sSessionFileName = oxSession::getVar( "sExportFileName" );
00078         if ( !$sSessionFileName ) {
00079             $sSessionFileName = md5( $this->getSession()->getId() . oxUtilsObject::getInstance()->generateUId() );
00080             oxSession::setVar( "sExportFileName", $sSessionFileName );
00081         }
00082         return $sSessionFileName;
00083     }
00084 
00090     protected function _getExportFilePath()
00091     {
00092         return $this->getConfig()->getConfigParam( 'sCompileDir' ) . "/". $this->_getExportFileName();
00093     }
00094 
00100     public function download()
00101     {
00102         $oUtils = oxUtils::getInstance();
00103         $oUtils->setHeader( "Pragma: public" );
00104         $oUtils->setHeader( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
00105         $oUtils->setHeader( "Expires: 0" );
00106         $oUtils->setHeader( "Content-Disposition: attachment; filename=vouchers.csv");
00107         $oUtils->setHeader( "Content-Type: application/csv" );
00108         $sFile = $this->_getExportFilePath();
00109         if ( file_exists( $sFile ) && is_readable( $sFile ) ) {
00110             readfile( $sFile );
00111         }
00112         $oUtils->showMessageAndExit( "" );
00113     }
00114 
00120     public function run()
00121     {
00122         $blContinue = true;
00123         $iExportedItems = 0;
00124 
00125         $this->fpFile = @fopen( $this->_sFilePath, "a");
00126         if ( !isset( $this->fpFile) || !$this->fpFile) {
00127             // we do have an error !
00128             $this->stop( ERR_FILEIO);
00129         } else {
00130             // file is open
00131             $iStart = oxConfig::getParameter("iStart");
00132 
00133             if ( ( $iExportedItems = $this->exportVouchers( $iStart ) ) === false ) {
00134                 // end reached
00135                 $this->stop( ERR_SUCCESS );
00136                 $blContinue = false;
00137             }
00138 
00139             if ( $blContinue ) {
00140                 // make ticker continue
00141                 $this->_aViewData['refresh']   = 0;
00142                 $this->_aViewData['iStart']    = $iStart + $iExportedItems;
00143                 $this->_aViewData['iExpItems'] = $iStart + $iExportedItems;
00144             }
00145             fclose( $this->fpFile);
00146         }
00147     }
00148 
00156     public function exportVouchers( $iStart )
00157     {
00158         $iExported = false;
00159 
00160         if ( $oSerie = $this->_getVoucherSerie() ) {
00161 
00162             $oDb = oxDb::getDb(true);
00163 
00164             $sSelect = "select oxvouchernr from oxvouchers where oxvoucherserieid = ".$oDb->quote( $oSerie->getId() );
00165             $rs = $oDb->selectLimit( $sSelect, $this->iExportPerTick, $iStart );
00166 
00167             if ( !$rs->EOF ) {
00168                 $iExported = 0;
00169 
00170                 // writing header text
00171                 if ( $iStart == 0 ) {
00172                     $this->write( "Gutschein" );
00173                 }
00174             }
00175 
00176             // writing vouchers..
00177             while ( !$rs->EOF ) {
00178                 $this->write( current( $rs->fields ) );
00179                 $iExported++;
00180                 $rs->moveNext();
00181             }
00182         }
00183 
00184         return $iExported;
00185     }
00186 
00194     public function write( $sLine )
00195     {
00196         if ( $sLine ) {
00197            fwrite( $this->fpFile, $sLine."\n");
00198         }
00199     }
00200 }