voucherserie_export.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class VoucherSerie_Export extends VoucherSerie_Main
00007 {
00008 
00014     public $sClassDo = "voucherserie_export";
00015 
00021     public $sExportFileType = "csv";
00022 
00028     protected $_sThisTemplate = "voucherserie_export.tpl";
00029 
00035     public $iExportPerTick = 1000;
00036 
00042     public function __construct()
00043     {
00044         parent::__construct();
00045 
00046         // export file name
00047         $this->sExportFileName = $this->_getExportFileName();
00048 
00049         // set generic frame template
00050         $this->_sFilePath = $this->_getExportFilePath();
00051     }
00052 
00058     public function getDownloadUrl()
00059     {
00060         $myConfig = $this->getConfig();
00061 
00062         // override cause of admin dir
00063         $sUrl = $myConfig->getConfigParam('sShopURL') . $myConfig->getConfigParam('sAdminDir');
00064         if ($myConfig->getConfigParam('sAdminSSLURL')) {
00065             $sUrl = $myConfig->getConfigParam('sAdminSSLURL');
00066         }
00067 
00068         $sUrl = oxRegistry::get("oxUtilsUrl")->processUrl($sUrl . '/index.php');
00069 
00070         return $sUrl . '&amp;cl=' . $this->sClassDo . '&amp;fnc=download';
00071     }
00072 
00078     protected function _getExportFileName()
00079     {
00080         $sSessionFileName = oxRegistry::getSession()->getVariable("sExportFileName");
00081         if (!$sSessionFileName) {
00082             $sSessionFileName = md5($this->getSession()->getId() . oxUtilsObject::getInstance()->generateUId());
00083             oxRegistry::getSession()->setVariable("sExportFileName", $sSessionFileName);
00084         }
00085 
00086         return $sSessionFileName;
00087     }
00088 
00094     protected function _getExportFilePath()
00095     {
00096         return $this->getConfig()->getConfigParam('sShopDir') . "/export/" . $this->_getExportFileName();
00097     }
00098 
00102     public function download()
00103     {
00104         $oUtils = oxRegistry::getUtils();
00105         $oUtils->setHeader("Pragma: public");
00106         $oUtils->setHeader("Cache-Control: must-revalidate, post-check=0, pre-check=0");
00107         $oUtils->setHeader("Expires: 0");
00108         $oUtils->setHeader("Content-Disposition: attachment; filename=vouchers.csv");
00109         $oUtils->setHeader("Content-Type: application/csv");
00110         $sFile = $this->_getExportFilePath();
00111         if (file_exists($sFile) && is_readable($sFile)) {
00112             readfile($sFile);
00113         }
00114         $oUtils->showMessageAndExit("");
00115     }
00116 
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 = oxRegistry::getConfig()->getRequestParameter("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 
00195     public function write($sLine)
00196     {
00197         if ($sLine) {
00198             fwrite($this->fpFile, $sLine . "\n");
00199         }
00200     }
00201 }