00001 <?php
00002
00006 DEFINE("ERR_SUCCESS", -2);
00007 DEFINE("ERR_GENERAL", -1);
00008 DEFINE("ERR_FILEIO", 1);
00009
00010
00016 class DynExportBase extends oxAdminDetails
00017 {
00018 public $sClass_do = "";
00019 public $sClass_main = "";
00020
00021
00022 public $sExportPath = "export/";
00023 public $sExportFileType = "txt";
00024 public $sExportFileName = "dynexport";
00025 public $fpFile = null;
00026 public $iExportPerTick = 30;
00027
00028
00029 protected $_sFilePath = null;
00030 protected $_aExportResultset = array();
00031
00032 protected $_sThisTemplate = "dynexportbase.tpl";
00033
00034
00039 public function __construct()
00040 {
00041 parent::__construct();
00042
00043
00044 $this->_sFilePath = $this->getConfig()->getConfigParam( 'sShopDir' ) . "/". $this->sExportPath . $this->sExportFileName . "." . $this->sExportFileType;
00045
00046 }
00047
00054 public function render()
00055 {
00056 parent::render();
00057
00058
00059 $aClass_vars = get_object_vars( $this);
00060 while (list($name, $value) = each($aClass_vars)) {
00061 $this->_aViewData[$name] = $value;
00062 }
00063
00064 $this->_aViewData['sOutputFile'] = $this->_sFilePath;
00065 $this->_aViewData['sDownloadFile'] = $this->getConfig()->getConfigParam( 'sShopURL' ) . $this->sExportPath . $this->sExportFileName . "." . $this->sExportFileType;
00066
00067 return $this->_sThisTemplate;
00068 }
00069
00075 public function createMainExportView()
00076 {
00077 $myConfig = $this->getConfig();
00078
00079 $oCatTree = oxNew( "oxCategoryList" );
00080 $oCatTree->buildList($myConfig->getConfigParam( 'bl_perfLoadCatTree' ));
00081 $this->_aViewData["cattree"] = $oCatTree;
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 }
00101
00107 public function start()
00108 {
00109
00110 $this->fpFile = @fopen( $this->_sFilePath, "w");
00111 if ( !isset( $this->fpFile) || !$this->fpFile) {
00112
00113 $this->stop( ERR_FILEIO);
00114 } else {
00115 $this->_aViewData['refresh'] = 0;
00116 $this->_aViewData['iStart'] = 0;
00117 fclose( $this->fpFile);
00118
00119
00120 $iEnd = $this->prepareExport();
00121 oxSession::setVar( "iEnd", $iEnd);
00122 $this->_aViewData['iEnd'] = $iEnd;
00123 }
00124 }
00125
00133 public function stop( $iError = 0)
00134 {
00135 if ( $iError) {
00136 $this->_aViewData['iError'] = $iError;
00137 }
00138
00139
00140 $sHeapTable = $this->_getHeapTableName();
00141 oxDb::getDb()->Execute( "drop TABLE if exists $sHeapTable ");
00142 }
00143
00151 public function nextTick( $iCnt)
00152 {
00153 return false;
00154 }
00155
00163 public function write( $sLine)
00164 {
00165
00166 $sLine = $this->removeSID( $sLine);
00167 $sLine = str_replace( array("\r\n","\n"), "", $sLine);
00168 fwrite( $this->fpFile, $sLine."\r\n");
00169 }
00170
00176 public function run()
00177 {
00178 $blContinue = true;
00179
00180 $this->fpFile = @fopen( $this->_sFilePath, "a");
00181 if ( !isset( $this->fpFile) || !$this->fpFile) {
00182
00183 $this->stop( ERR_FILEIO);
00184 } else {
00185
00186 $iStart = oxConfig::getParameter("iStart");
00187
00188 $this->_aExportResultset = oxConfig::getParameter( "aExportResultset");
00189
00190 for ( $i = $iStart; $i < $iStart + $this->iExportPerTick; $i++) {
00191 if ( !$this->nextTick( $i)) {
00192
00193 $this->stop( ERR_SUCCESS);
00194 $blContinue = false;
00195 break;
00196 }
00197 }
00198 if ( $blContinue) {
00199
00200 $this->_aViewData['refresh'] = 0;
00201 $this->_aViewData['iStart'] = $i;
00202 $this->_aViewData['iEnd'] = oxConfig::getParameter("iEnd");
00203 }
00204 fclose( $this->fpFile);
00205 }
00206 }
00207
00215 public function removeSID( $sInput)
00216 {
00217
00218 $mySession = $this->getSession();
00219
00220
00221 $sOutput = str_replace( "sid=".$mySession->getId()."/", "", $sInput);
00222 $sOutput = str_replace( "sid/".$mySession->getId()."/", "", $sOutput);
00223 $sOutput = str_replace( "sid=".$mySession->getId()."&", "", $sOutput);
00224 $sOutput = str_replace( "sid=".$mySession->getId()."&", "", $sOutput);
00225 $sOutput = str_replace( "sid=".$mySession->getId(), "", $sOutput);
00226
00227 return $sOutput;
00228 }
00229
00239 public function shrink( $sInput, $iMaxSize, $blRemoveNewline = true)
00240 {
00241
00242 if ( $blRemoveNewline) {
00243 $sOutput = str_replace( "\r\n", " ", $sInput);
00244 $sOutput = str_replace( "\n", " ", $sOutput);
00245 } else {
00246 $sOutput = $sInput;
00247 }
00248
00249 $sOutput = str_replace( "\t", " ", $sOutput);
00250
00251 $sOutput = $this->_unHTMLEntities( strip_tags( $sOutput));
00252
00253 $oStr = getStr();
00254 if ( $oStr->strlen( $sOutput) > $iMaxSize - 3) {
00255 $sOutput = $oStr->substr( $sOutput, 0, $iMaxSize - 5) . "...";
00256 }
00257 return $sOutput;
00258 }
00259
00268 public function getCategoryString( & $oArticle, $sSeparator = "/")
00269 {
00270 $sLang = oxLang::getInstance()->getBaseLanguage();
00271 $oDB = oxDb::getDb();
00272
00273 $sCatView = getViewName('oxcategories');
00274 $sO2CView = getViewName('oxobject2category');
00275
00276
00277 $sQ = "select oxobject2category.oxcatnid, $sCatView.oxleft, $sCatView.oxright, $sCatView.oxrootid from $sO2CView as oxobject2category left join $sCatView on $sCatView.oxid = oxobject2category.oxcatnid ";
00278 $sQ .= "where oxobject2category.oxobjectid='".$oArticle->getId()."' and $sCatView.oxactive".(($sLang)?"_$sLang":"")." = 1 order by oxobject2category.oxtime ";
00279
00280 $aRet = array();
00281 $rs = $oDB->execute( $sQ);
00282 if ($rs != false && $rs->recordCount() > 0) {
00283 $sCatID = $rs->fields[0];
00284 $sLeft = $rs->fields[1];
00285 $sRight = $rs->fields[2];
00286 $sRootID = $rs->fields[3];
00287
00288
00289 $sQ = "select oxtitle".(($sLang)?"_$sLang":"")." from oxcategories where ";
00290 $sQ .= "oxrootid = '$sRootID' and oxright >= $sRight and oxleft <= $sLeft order by oxleft ";
00291
00292 $rs = $oDB->execute( $sQ);
00293 if ($rs != false && $rs->recordCount() > 0) {
00294 while (!$rs->EOF) {
00295 $aRet[] = $rs->fields[0];
00296 $rs->moveNext();
00297 }
00298 }
00299 }
00300 $sRet = implode($sSeparator, $aRet);
00301 return $sRet;
00302 }
00303
00311 public function getDefaultCategoryString($oArticle)
00312 {
00313 $sLang = oxLang::getInstance()->getBaseLanguage();
00314 $oDB = oxDb::getDb();
00315
00316 $sCatView = getViewName('oxcategories');
00317 $sO2CView = getViewName('oxobject2category');
00318
00319
00320 $sQ = "select $sCatView.oxtitle".(($sLang)?"_$sLang":"")." from $sO2CView as oxobject2category left join $sCatView on $sCatView.oxid = oxobject2category.oxcatnid ";
00321 $sQ .= "where oxobject2category.oxobjectid='".$oArticle->getId()."' and $sCatView.oxactive".(($sLang)?"_$sLang":"")." = 1 order by oxobject2category.oxtime ";
00322
00323 $rs = $oDB->getOne( $sQ);
00324
00325 return $rs;
00326 }
00327
00335 public function prepareCSV( $sInput)
00336 {
00337 $sInput = oxUtilsString::getInstance()->prepareCSVField( $sInput);
00338 $sOutput = str_replace( " ", " ", $sInput);
00339 $sOutput = str_replace( "€", "", $sOutput);
00340 $sOutput = str_replace( "|", "", $sOutput);
00341
00342 return $sOutput;
00343 }
00344
00352 public function prepareXML($sInput)
00353 {
00354
00355 $sOutput = str_replace("&", "&", $sInput);
00356 $sOutput = str_replace("\"", """, $sOutput);
00357 $sOutput = str_replace(">", ">", $sOutput);
00358 $sOutput = str_replace("<", "<", $sOutput);
00359 $sOutput = str_replace("'", "'", $sOutput);
00360 return $sOutput;
00361 }
00362
00370 public function getDeepestCategoryPath( & $oArticle)
00371 {
00372 $sRet = "";
00373 $this->_loadRootCats();
00374 $sRet = $this->_findDeepestCatPath($oArticle);
00375
00376 return $sRet;
00377 }
00378
00384 public function prepareExport()
00385 {
00386 $oDB = oxDb::getDb();
00387
00388 $sHeapTable = $this->_getHeapTableName();
00389
00390
00391
00392
00393 $rs = $oDB->execute("SHOW VARIABLES LIKE 'version'");
00394 $sMysqlVersion = $rs->fields[1];
00395
00396 $sTableCharset = $this->_generateTableCharSet($sMysqlVersion);
00397
00398
00399 $blRet = $this->_createHeapTable($sHeapTable, $sTableCharset);
00400 if ( $blRet == false) {
00401
00402 die( "Could not create HEAP Table $sHeapTable\n<br>");
00403 }
00404
00405 $aChosenCat = oxConfig::getParameter( "acat");
00406 $sCatAdd = $this->_getCatAdd($aChosenCat);
00407
00408 if ( !$this->_insertArticles($sHeapTable, $sCatAdd) ) {
00409 die( "Could not insert Articles in Table $sHeapTable\n<br>");
00410 }
00411
00412
00413
00414
00415
00416
00417 $this->_removeParentArticles($sHeapTable);
00418 $this->_setSessionParams();
00419
00420
00421 $iCnt = $oDB->getOne("select count(*) from $sHeapTable");
00422
00423 return $iCnt;
00424 }
00425
00434 public function getOneArticle( $iCnt, & $blContinue)
00435 {
00436 $myConfig = $this->getConfig();
00437
00438
00439 $myConfig->setConfigParam( 'blExport', true );
00440
00441 $oArticle = null;
00442 $blContinue = true;
00443
00444 $sHeapTable = $this->_getHeapTableName();
00445
00446 $oArticle = $this->_initArticle($sHeapTable, $iCnt);
00447 if (!isset($oArticle)) {
00448 $blContinue = false;
00449 return null;
00450 }
00451
00452 $oArticle = $this->_setCampaignDetailLink($oArticle);
00453
00454
00455
00456 $myConfig->setConfigParam( 'blExport', false );
00457
00458 return $oArticle;
00459 }
00460
00469 public function assureContent( $sInput, $sReplace = null)
00470 {
00471 if ( !strlen( $sInput)) {
00472 if ( !isset( $sReplace) || !strlen($sReplace)) {
00473 $sReplace = "-";
00474 }
00475 $sInput = $sReplace;
00476 }
00477 return $sInput;
00478 }
00479
00488 protected function _unHTMLEntities( $sInput)
00489 {
00490
00491 $trans_tbl = get_html_translation_table(HTML_ENTITIES);
00492 $trans_tbl = array_flip($trans_tbl);
00493 return strtr( $sInput, $trans_tbl);
00494 }
00495
00501 protected function _getHeapTableName()
00502 {
00503 $mySession = $this->getSession();
00504
00505
00506 $sHeapname = "tmp_".str_replace( "0", "", md5($mySession->getId()));
00507
00508 return $sHeapname;
00509 }
00510
00518 private function _generateTableCharSet($sMysqlVersion)
00519 {
00520
00521 $oDB = oxDb::getDb();
00522
00523
00524 if (version_compare($sMysqlVersion, '4.1.0', '>=')>0) {
00525 $rs = $oDB->execute("SHOW FULL COLUMNS FROM `oxarticles`");
00526 $sMysqlCollation = $rs->fields[2];
00527 $rs = $oDB->execute("SHOW VARIABLES LIKE 'character_set_connection'");
00528 $sMysqlCharacterSet = $rs->fields[1];
00529 if ($sMysqlCollation && $sMysqlCharacterSet) {
00530 $sTableCharset = "DEFAULT CHARACTER SET ".$sMysqlCharacterSet." COLLATE ".$sMysqlCollation;
00531 } else {
00532 $sTableCharset = "";
00533 }
00534 } else {
00535 $sTableCharset = "";
00536 }
00537 return $sTableCharset;
00538 }
00539
00548 private function _createHeapTable($sHeapTable, $sTableCharset)
00549 {
00550
00551 $oDB = oxDb::getDb();
00552
00553 $sSQL = "CREATE TABLE if not exists $sHeapTable ( oxid char(32) NOT NULL default '' ) TYPE=HEAP ".$sTableCharset;
00554
00555 $rs = $oDB->execute( $sSQL);
00556 if ( $rs == false) {
00557
00558 return false;
00559 }
00560 $oDB->execute( "truncate table $sHeapTable ");
00561 return true;
00562 }
00563
00571 private function _getCatAdd($aChosenCat)
00572 {
00573
00574 $sCatAdd = null;
00575 if ( isset( $aChosenCat)) {
00576 $sCatAdd = " and ( ";
00577 $blSep = false;
00578 foreach ( $aChosenCat as $sCat) {
00579 if ( $blSep) {
00580 $sCatAdd .= " or ";
00581 }
00582 $sCatAdd .= "oxobject2category.oxcatnid = '$sCat'";
00583 $blSep = true;
00584 }
00585 $sCatAdd .= ")";
00586 }
00587 return $sCatAdd;
00588 }
00589
00598 private function _insertArticles($sHeapTable, $sCatAdd)
00599 {
00600 $oDB = oxDb::getDb();
00601 $sSearchString = oxConfig::getParameter( "search");
00602 $blExportVars = oxConfig::getParameter( "blExportVars");
00603
00604 $language = oxLang::getInstance()->getLanguageTag( 0);
00605 $sShopID = $this->getConfig()->getShopID();
00606
00607 $sO2CView = getViewName('oxobject2category');
00608 $oArticle = oxNew( 'oxarticle' );
00609 $sArticleTable = $oArticle->getViewName();
00610
00611 $sSelect = "insert into $sHeapTable select $sArticleTable.oxid from $sArticleTable, $sO2CView as oxobject2category where ";
00612 $sSelect .= $oArticle->getSqlActiveSnippet();
00613
00614 if ( !$blExportVars) {
00615 $sSelect .= " and $sArticleTable.oxid = oxobject2category.oxobjectid and $sArticleTable.oxparentid='' ";
00616 } else {
00617 $sSelect .= " and ( $sArticleTable.oxid = oxobject2category.oxobjectid or $sArticleTable.oxparentid = oxobject2category.oxobjectid ) ";
00618 }
00619
00620
00621
00622
00623
00624 if ( isset( $sSearchString) && strlen( $sSearchString)) {
00625 $sSelect .= "and ( $sArticleTable.OXTITLE".$language." like '%$sSearchString%' ";
00626 $sSelect .= "or $sArticleTable.OXSHORTDESC".$language." like '%$sSearchString%' ";
00627 $sSelect .= "or $sArticleTable.oxsearchkeys like '%$sSearchString%') ";
00628 }
00629 if ( $sCatAdd) {
00630 $sSelect .= $sCatAdd;
00631 }
00632 if( !$sCatAdd)
00633 $sSelect .= " and oxarticles.oxshopid = '$sShopID' ";
00634
00635
00636 $dMinStock = oxConfig::getParameter( "sExportMinStock");
00637 if ( isset( $dMinStock) && $dMinStock && $this->getConfig()->getConfigParam( 'blUseStock' ) ) {
00638 $dMinStock = str_replace( array( ";", " ", "/", "'"), "", $dMinStock);
00639 $sSelect .= " and $sArticleTable.oxstock >= $dMinStock";
00640 }
00641 $sSelect .= " group by $sArticleTable.oxid";
00642
00643 if ( $oDB->execute( $sSelect) ) {
00644 return true;
00645 } else {
00646 return false;
00647 }
00648 }
00649
00657 private function _removeParentArticles($sHeapTable)
00658 {
00659 $oDB = oxDb::getDb();
00660 $blExportParentVars = oxConfig::getParameter( "blExportMainVars");
00661 $sArticleTable = getViewName('oxarticles');
00662
00663 if ( !isset( $blExportParentVars) || !$blExportParentVars) {
00664
00665 $rs = $oDB->execute( "select $sHeapTable.oxid from $sHeapTable, $sArticleTable where $sHeapTable.oxid = $sArticleTable.oxparentid group by $sHeapTable.oxid");
00666 $sDel = "delete from $sHeapTable where oxid in ( ";
00667 $blSep = false;
00668 if ($rs != false && $rs->recordCount() > 0) {
00669 while (!$rs->EOF) {
00670 if ( $blSep) {
00671 $sDel .= ",";
00672 }
00673 $sDel .= "'".$rs->fields[0]."'";
00674 $blSep = true;
00675 $rs->moveNext();
00676 }
00677 }
00678 $sDel .= " )";
00679 $oDB->execute( $sDel);
00680
00681 }
00682 }
00683
00690 private function _setSessionParams()
00691 {
00692
00693 oxSession::deleteVar("sExportDelCost");
00694 $dDelCost = oxConfig::getParameter( "sExportDelCost");
00695 if ( isset( $dDelCost)) {
00696 $dDelCost = str_replace( array( ";", " ", "/", "'"), "", $dDelCost);
00697 $dDelCost = str_replace( ",", ".", $dDelCost);
00698 oxSession::setVar( "sExportDelCost", $dDelCost);
00699 }
00700
00701 oxSession::deleteVar("sExportMinPrice");
00702 $dMinPrice = oxConfig::getParameter( "sExportMinPrice");
00703 if ( isset( $dMinPrice)) {
00704 $dMinPrice = str_replace( array( ";", " ", "/", "'"), "", $dMinPrice);
00705 $dMinPrice = str_replace( ",", ".", $dMinPrice);
00706 oxSession::setVar( "sExportMinPrice", $dMinPrice);
00707 }
00708
00709
00710 oxSession::deleteVar("sExportCampaign");
00711 $sCampaign = oxConfig::getParameter( "sExportCampaign");
00712 if ( isset( $sCampaign)) {
00713 $sCampaign = str_replace( array( ";", " ", "/", "'"), "", $sCampaign);
00714 oxSession::setVar( "sExportCampaign", $sCampaign);
00715 }
00716
00717 oxSession::deleteVar("blAppendCatToCampaign");
00718
00719 $blAppendCatToCampaign = oxConfig::getParameter( "blAppendCatToCampaign");
00720 if ( isset( $blAppendCatToCampaign) && $blAppendCatToCampaign) {
00721 oxSession::setVar( "blAppendCatToCampaign", $blAppendCatToCampaign);
00722 }
00723 }
00724
00730 private function _loadRootCats()
00731 {
00732 $myConfig = $this->getConfig();
00733 if ( !isset( $myConfig->aCatLvlCache) || !count( $myConfig->aCatLvlCache)) {
00734 $myConfig->aCatLvlCache = array();
00735
00736 $sLang = oxLang::getInstance()->getBaseLanguage();
00737 $sCatView = getViewName('oxcategories');
00738 $oDb = oxDb::getDb();
00739
00740
00741 $sSQL = "select oxid from $sCatView where oxparentid = 'oxrootid'";
00742 $rs = $oDb->Execute( $sSQL);
00743 if ($rs != false && $rs->recordCount() > 0) {
00744 while (!$rs->EOF) {
00745
00746 $sSQL = "SELECT s.oxid, s.oxtitle".(($sLang)?"_$sLang":"").", s.oxparentid, count( * ) AS LEVEL FROM oxcategories v, oxcategories s WHERE s.oxrootid = '".$rs->fields[0]."' and v.oxrootid='".$rs->fields[0]."' and s.oxleft BETWEEN v.oxleft AND v.oxright AND s.oxhidden = '0' GROUP BY s.oxleft order by level";
00747 $rs2 = $oDb->Execute( $sSQL);
00748 if ($rs2 != false && $rs2->recordCount() > 0) {
00749 while (!$rs2->EOF) {
00750
00751 $oCat = new stdClass();
00752 $oCat->_sOXID = $rs2->fields[0];
00753 $oCat->oxtitle = $rs2->fields[1];
00754 $oCat->oxparentid = $rs2->fields[2];
00755 $oCat->ilevel = $rs2->fields[3];
00756 $myConfig->aCatLvlCache[$oCat->_sOXID] = $oCat;
00757
00758 $rs2->moveNext();
00759 }
00760 }
00761 $rs->moveNext();
00762 }
00763 }
00764 }
00765 }
00766
00774 private function _findDeepestCatPath($oArticle)
00775 {
00776 $myConfig = $this->getConfig();
00777 $sRet = "";
00778
00779
00780 $aIDs = $oArticle->getCategoryIds();
00781 if ( isset( $aIDs) && count( $aIDs)) {
00782 $SIDMAX = null;
00783 $dMaxLvl = 0;
00784 foreach ( $aIDs as $key => $sCatID) {
00785 if ( $dMaxLvl < $myConfig->aCatLvlCache[$sCatID]->ilevel) {
00786 $dMaxLvl = $myConfig->aCatLvlCache[$sCatID]->ilevel;
00787 $SIDMAX = $sCatID;
00788 }
00789 }
00790
00791 $sRet = $myConfig->aCatLvlCache[$SIDMAX]->oxtitle;
00792
00793 for ( ;;) {
00794 $SIDMAX = @$myConfig->aCatLvlCache[$SIDMAX]->oxparentid;
00795 if ( !isset( $SIDMAX) || $SIDMAX == "oxrootid") {
00796 break;
00797 }
00798 $sRet = $myConfig->aCatLvlCache[$SIDMAX]->oxtitle."/".$sRet;
00799 }
00800 }
00801 return $sRet;
00802 }
00803
00812 private function _initArticle($sHeapTable, $iCnt)
00813 {
00814 $myConfig = $this->getConfig();
00815 $oDB = oxDb::getDb();
00816
00817 $oArticle = null;
00818
00819 $rs = $oDB->selectLimit( "select oxid from $sHeapTable", 1, $iCnt);
00820 if ($rs != false && $rs->recordCount() > 0) {
00821 $sOXID = $rs->fields[0];
00822
00823
00824
00825
00826 $oArticle = oxNew( 'oxarticle' );
00827 $oArticle->setLoadParentData(true);
00828 $oArticle->Load( $sOXID);
00829
00830
00831 $dMinPrice = oxConfig::getParameter( "sExportMinPrice");
00832 if ( isset( $dMinPrice) && ($oArticle->brutPrice < $dMinPrice)) {
00833 return null;
00834 }
00835 } else {
00836 return null;
00837 }
00838
00839
00840 $sTitle = $oArticle->oxarticles__oxvarselect->value?" ".$oArticle->oxarticles__oxvarselect->value:"";
00841 $oArticle->oxarticles__oxtitle->setValue($oArticle->oxarticles__oxtitle->value.$sTitle);
00842
00843
00844
00845 if ($oArticle->oxarticles__oxparentid->value) {
00846 $oArticle->oxdetaillink = str_replace($oArticle->oxarticles__oxparentid->value, $sOXID, $oArticle->oxdetaillink);
00847 }
00848 return $oArticle;
00849 }
00850
00858 private function _setCampaignDetailLink($oArticle)
00859 {
00860
00861 $sCampaign = oxConfig::getParameter( "sExportCampaign" );
00862 if ( $sCampaign ) {
00863
00864
00865 $oArticle->appendLink( "campaign=$sCampaign" );
00866
00867 if (oxConfig::getParameter( "blAppendCatToCampaign")) {
00868 if ( $sCat = $this->getCategoryString($oArticle) ) {
00869 $oArticle->appendLink( "/$sCat" );
00870 }
00871 }
00872 }
00873 return $oArticle;
00874 }
00875
00879 public function getViewId()
00880 {
00881 return 'dyn_interface';
00882 }
00883 }