OXID eShop CE  4.10.4
 All Classes Namespaces Files Functions Variables Pages
voucherserie_export.php
Go to the documentation of this file.
1 <?php
2 
7 {
8 
14  public $sClassDo = "voucherserie_export";
15 
21  public $sExportFileType = "csv";
22 
28  protected $_sThisTemplate = "voucherserie_export.tpl";
29 
35  public $iExportPerTick = 1000;
36 
42  public function __construct()
43  {
45 
46  // export file name
47  $this->sExportFileName = $this->_getExportFileName();
48 
49  // set generic frame template
50  $this->_sFilePath = $this->_getExportFilePath();
51  }
52 
58  public function getDownloadUrl()
59  {
60  $myConfig = $this->getConfig();
61 
62  // override cause of admin dir
63  $sUrl = $myConfig->getConfigParam('sShopURL') . $myConfig->getConfigParam('sAdminDir');
64  if ($myConfig->getConfigParam('sAdminSSLURL')) {
65  $sUrl = $myConfig->getConfigParam('sAdminSSLURL');
66  }
67 
68  $sUrl = oxRegistry::get("oxUtilsUrl")->processUrl($sUrl . '/index.php');
69 
70  return $sUrl . '&amp;cl=' . $this->sClassDo . '&amp;fnc=download';
71  }
72 
78  protected function _getExportFileName()
79  {
80  $sSessionFileName = oxRegistry::getSession()->getVariable("sExportFileName");
81  if (!$sSessionFileName) {
82  $sSessionFileName = md5($this->getSession()->getId() . oxUtilsObject::getInstance()->generateUId());
83  oxRegistry::getSession()->setVariable("sExportFileName", $sSessionFileName);
84  }
85 
86  return $sSessionFileName;
87  }
88 
94  protected function _getExportFilePath()
95  {
96  return $this->getConfig()->getConfigParam('sShopDir') . "/export/" . $this->_getExportFileName();
97  }
98 
102  public function download()
103  {
104  $oUtils = oxRegistry::getUtils();
105  $oUtils->setHeader("Pragma: public");
106  $oUtils->setHeader("Cache-Control: must-revalidate, post-check=0, pre-check=0");
107  $oUtils->setHeader("Expires: 0");
108  $oUtils->setHeader("Content-Disposition: attachment; filename=vouchers.csv");
109  $oUtils->setHeader("Content-Type: application/csv");
110  $sFile = $this->_getExportFilePath();
111  if (file_exists($sFile) && is_readable($sFile)) {
112  readfile($sFile);
113  }
114  $oUtils->showMessageAndExit("");
115  }
116 
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 = oxRegistry::getConfig()->getRequestParameter("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 
195  public function write($sLine)
196  {
197  if ($sLine) {
198  fwrite($this->fpFile, $sLine . "\n");
199  }
200  }
201 }