OXID eShop CE  4.8.10
 All Classes Files Functions Variables Pages
voucherserie_export.php
Go to the documentation of this file.
1 <?php
2 
7 {
13  public $sClassDo = "voucherserie_export";
14 
20  public $sExportFileType = "csv";
21 
26  protected $_sThisTemplate = "voucherserie_export.tpl";
27 
33  public $iExportPerTick = 1000;
34 
40  public function __construct()
41  {
43 
44  // export file name
45  $this->sExportFileName = $this->_getExportFileName();
46 
47  // set generic frame template
48  $this->_sFilePath = $this->_getExportFilePath();
49  }
50 
56  public function getDownloadUrl()
57  {
58  $myConfig = $this->getConfig();
59 
60  // override cause of admin dir
61  $sUrl = $myConfig->getConfigParam( 'sShopURL' ). $myConfig->getConfigParam( 'sAdminDir' );
62  if ( $myConfig->getConfigParam( 'sAdminSSLURL' ) ) {
63  $sUrl = $myConfig->getConfigParam( 'sAdminSSLURL' );
64  }
65 
66  $sUrl = oxRegistry::get("oxUtilsUrl")->processUrl( $sUrl.'/index.php' );
67  return $sUrl . '&amp;cl='.$this->sClassDo.'&amp;fnc=download';
68  }
69 
75  protected function _getExportFileName()
76  {
77  $sSessionFileName = oxSession::getVar( "sExportFileName" );
78  if ( !$sSessionFileName ) {
79  $sSessionFileName = md5( $this->getSession()->getId() . oxUtilsObject::getInstance()->generateUId() );
80  oxSession::setVar( "sExportFileName", $sSessionFileName );
81  }
82  return $sSessionFileName;
83  }
84 
90  protected function _getExportFilePath()
91  {
92  return $this->getConfig()->getConfigParam( 'sShopDir' ) . "/export/". $this->_getExportFileName();
93  }
94 
100  public function download()
101  {
102  $oUtils = oxRegistry::getUtils();
103  $oUtils->setHeader( "Pragma: public" );
104  $oUtils->setHeader( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
105  $oUtils->setHeader( "Expires: 0" );
106  $oUtils->setHeader( "Content-Disposition: attachment; filename=vouchers.csv");
107  $oUtils->setHeader( "Content-Type: application/csv" );
108  $sFile = $this->_getExportFilePath();
109  if ( file_exists( $sFile ) && is_readable( $sFile ) ) {
110  readfile( $sFile );
111  }
112  $oUtils->showMessageAndExit( "" );
113  }
114 
120  public function run()
121  {
122  $blContinue = true;
123  $iExportedItems = 0;
124 
125  $this->fpFile = @fopen( $this->_sFilePath, "a");
126  if ( !isset( $this->fpFile) || !$this->fpFile) {
127  // we do have an error !
128  $this->stop( ERR_FILEIO);
129  } else {
130  // file is open
131  $iStart = oxConfig::getParameter("iStart");
132  if (!$iStart) {
133  ftruncate($this->fpFile, 0);
134  }
135 
136  if ( ( $iExportedItems = $this->exportVouchers( $iStart ) ) === false ) {
137  // end reached
138  $this->stop( ERR_SUCCESS );
139  $blContinue = false;
140  }
141 
142  if ( $blContinue ) {
143  // make ticker continue
144  $this->_aViewData['refresh'] = 0;
145  $this->_aViewData['iStart'] = $iStart + $iExportedItems;
146  $this->_aViewData['iExpItems'] = $iStart + $iExportedItems;
147  }
148  fclose( $this->fpFile);
149  }
150  }
151 
159  public function exportVouchers( $iStart )
160  {
161  $iExported = false;
162 
163  if ( $oSerie = $this->_getVoucherSerie() ) {
164 
165  $oDb = oxDb::getDb( oxDB::FETCH_MODE_ASSOC );
166 
167  $sSelect = "select oxvouchernr from oxvouchers where oxvoucherserieid = ".$oDb->quote( $oSerie->getId() );
168  $rs = $oDb->selectLimit( $sSelect, $this->iExportPerTick, $iStart );
169 
170  if ( !$rs->EOF ) {
171  $iExported = 0;
172 
173  // writing header text
174  if ( $iStart == 0 ) {
175  $this->write( oxRegistry::getLang()->translateString("VOUCHERSERIE_MAIN_VOUCHERSTATISTICS", oxRegistry::getLang()->getTplLanguage(), true ));
176  }
177  }
178 
179  // writing vouchers..
180  while ( !$rs->EOF ) {
181  $this->write( current( $rs->fields ) );
182  $iExported++;
183  $rs->moveNext();
184  }
185  }
186 
187  return $iExported;
188  }
189 
197  public function write( $sLine )
198  {
199  if ( $sLine ) {
200  fwrite( $this->fpFile, $sLine."\n");
201  }
202  }
203 }