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 = oxRegistry::get("oxUtilsUrl")->processUrl( $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( 'sShopDir' ) . "/export/". $this->_getExportFileName();
00093     }
00094 
00100     public function download()
00101     {
00102         $oUtils = oxRegistry::getUtils();
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             if (!$iStart) {
00133                 ftruncate($this->fpFile, 0);
00134             }
00135 
00136             if ( ( $iExportedItems = $this->exportVouchers( $iStart ) ) === false ) {
00137                 // end reached
00138                 $this->stop( ERR_SUCCESS );
00139                 $blContinue = false;
00140             }
00141 
00142             if ( $blContinue ) {
00143                 // make ticker continue
00144                 $this->_aViewData['refresh']   = 0;
00145                 $this->_aViewData['iStart']    = $iStart + $iExportedItems;
00146                 $this->_aViewData['iExpItems'] = $iStart + $iExportedItems;
00147             }
00148             fclose( $this->fpFile);
00149         }
00150     }
00151 
00159     public function exportVouchers( $iStart )
00160     {
00161         $iExported = false;
00162 
00163         if ( $oSerie = $this->_getVoucherSerie() ) {
00164 
00165             $oDb = oxDb::getDb( oxDB::FETCH_MODE_ASSOC );
00166 
00167             $sSelect = "select oxvouchernr from oxvouchers where oxvoucherserieid = ".$oDb->quote( $oSerie->getId() );
00168             $rs = $oDb->selectLimit( $sSelect, $this->iExportPerTick, $iStart );
00169 
00170             if ( !$rs->EOF ) {
00171                 $iExported = 0;
00172 
00173                 // writing header text
00174                 if ( $iStart == 0 ) {
00175                     $this->write( oxRegistry::getLang()->translateString("VOUCHERSERIE_MAIN_VOUCHERSTATISTICS", oxRegistry::getLang()->getTplLanguage(), true ));
00176                 }
00177             }
00178 
00179             // writing vouchers..
00180             while ( !$rs->EOF ) {
00181                 $this->write( current( $rs->fields ) );
00182                 $iExported++;
00183                 $rs->moveNext();
00184             }
00185         }
00186 
00187         return $iExported;
00188     }
00189 
00197     public function write( $sLine )
00198     {
00199         if ( $sLine ) {
00200            fwrite( $this->fpFile, $sLine."\n");
00201         }
00202     }
00203 }