00001 <?php
00002
00006 DEFINE("ERR_SUCCESS", -2);
00007 DEFINE("ERR_GENERAL", -1);
00008 DEFINE("ERR_FILEIO", 1);
00009
00010
00017 class DynExportBase extends oxAdminDetails
00018 {
00019 public $sClass_do = "";
00020 public $sClass_main = "";
00021
00022
00023 public $sExportPath = "export/";
00024 public $sExportFileType = "txt";
00025 public $sExportFileName = "dynexport";
00026 public $fpFile = null;
00027 public $iExportPerTick = 30;
00028
00029
00030 protected $_sFilePath = null;
00031 protected $_aExportResultset = array();
00032
00033 protected $_sThisTemplate = "dynexportbase.tpl";
00034
00035
00040 public function __construct()
00041 {
00042 parent::__construct();
00043
00044
00045 $this->_sFilePath = $this->getConfig()->getConfigParam( 'sShopDir' ) . "/". $this->sExportPath . $this->sExportFileName . "." . $this->sExportFileType;
00046
00047 }
00048
00055 public function render()
00056 {
00057 parent::render();
00058
00059
00060 $aClass_vars = get_object_vars( $this);
00061 while (list($name, $value) = each($aClass_vars)) {
00062 $this->_aViewData[$name] = $value;
00063 }
00064
00065 $this->_aViewData['sOutputFile'] = $this->_sFilePath;
00066 $this->_aViewData['sDownloadFile'] = $this->getConfig()->getConfigParam( 'sShopURL' ) . $this->sExportPath . $this->sExportFileName . "." . $this->sExportFileType;
00067
00068 return $this->_sThisTemplate;
00069 }
00070
00076 public function createMainExportView()
00077 {
00078 $myConfig = $this->getConfig();
00079
00080 $oCatTree = oxNew( "oxCategoryList" );
00081 $oCatTree->buildList($myConfig->getConfigParam( 'bl_perfLoadCatTree' ));
00082 $this->_aViewData["cattree"] = $oCatTree;
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 }
00102
00108 public function start()
00109 {
00110
00111 $this->fpFile = @fopen( $this->_sFilePath, "w");
00112 if ( !isset( $this->fpFile) || !$this->fpFile) {
00113
00114 $this->stop( ERR_FILEIO);
00115 } else {
00116 $this->_aViewData['refresh'] = 0;
00117 $this->_aViewData['iStart'] = 0;
00118 fclose( $this->fpFile);
00119
00120
00121 $iEnd = $this->prepareExport();
00122 oxSession::setVar( "iEnd", $iEnd);
00123 $this->_aViewData['iEnd'] = $iEnd;
00124 }
00125 }
00126
00134 public function stop( $iError = 0)
00135 {
00136 if ( $iError) {
00137 $this->_aViewData['iError'] = $iError;
00138 }
00139
00140
00141 $sHeapTable = $this->_getHeapTableName();
00142 oxDb::getDb()->Execute( "drop TABLE if exists $sHeapTable ");
00143 }
00144
00152 public function nextTick( $iCnt)
00153 {
00154 return false;
00155 }
00156
00164 public function write( $sLine)
00165 {
00166
00167 $sLine = $this->removeSID( $sLine);
00168 $sLine = str_replace( array("\r\n","\n"), "", $sLine);
00169 fwrite( $this->fpFile, $sLine."\r\n");
00170 }
00171
00177 public function run()
00178 {
00179 $blContinue = true;
00180
00181 $this->fpFile = @fopen( $this->_sFilePath, "a");
00182 if ( !isset( $this->fpFile) || !$this->fpFile) {
00183
00184 $this->stop( ERR_FILEIO);
00185 } else {
00186
00187 $iStart = oxConfig::getParameter("iStart");
00188
00189 $this->_aExportResultset = oxConfig::getParameter( "aExportResultset");
00190
00191 for ( $i = $iStart; $i < $iStart + $this->iExportPerTick; $i++) {
00192 if ( !$this->nextTick( $i)) {
00193
00194 $this->stop( ERR_SUCCESS);
00195 $blContinue = false;
00196 break;
00197 }
00198 }
00199 if ( $blContinue) {
00200
00201 $this->_aViewData['refresh'] = 0;
00202 $this->_aViewData['iStart'] = $i;
00203 $this->_aViewData['iEnd'] = oxConfig::getParameter("iEnd");
00204 }
00205 fclose( $this->fpFile);
00206 }
00207 }
00208
00216 public function removeSID( $sInput)
00217 {
00218
00219 $mySession = $this->getSession();
00220
00221
00222 $sOutput = str_replace( "sid=".$mySession->getId()."/", "", $sInput);
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 $sOutput = str_replace( "sid=".$mySession->getId(), "", $sOutput);
00227
00228 return $sOutput;
00229 }
00230
00240 public function shrink( $sInput, $iMaxSize, $blRemoveNewline = true)
00241 {
00242
00243 if ( $blRemoveNewline) {
00244 $sOutput = str_replace( "\r\n", " ", $sInput);
00245 $sOutput = str_replace( "\n", " ", $sOutput);
00246 } else {
00247 $sOutput = $sInput;
00248 }
00249
00250 $sOutput = str_replace( "\t", " ", $sOutput);
00251
00252 $sOutput = $this->_unHTMLEntities( strip_tags( $sOutput));
00253
00254 if ( strlen( $sOutput) > $iMaxSize - 3) {
00255 $sOutput = 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
00453
00454
00455
00456
00457 $oArticle = $this->_setCampaignDetailLink($oArticle);
00458
00459
00460
00461 $myConfig->setConfigParam( 'blExport', false );
00462
00463 return $oArticle;
00464 }
00465
00474 public function assureContent( $sInput, $sReplace = null)
00475 {
00476 if ( !strlen( $sInput)) {
00477 if ( !isset( $sReplace) || !strlen($sReplace)) {
00478 $sReplace = "-";
00479 }
00480 $sInput = $sReplace;
00481 }
00482 return $sInput;
00483 }
00484
00493 protected function _unHTMLEntities( $sInput)
00494 {
00495
00496 $trans_tbl = get_html_translation_table(HTML_ENTITIES);
00497 $trans_tbl = array_flip($trans_tbl);
00498 return strtr( $sInput, $trans_tbl);
00499 }
00500
00506 protected function _getHeapTableName()
00507 {
00508 $mySession = $this->getSession();
00509
00510
00511 $sHeapname = "tmp_".str_replace( "0", "", md5($mySession->getId()));
00512
00513 return $sHeapname;
00514 }
00515
00523 private function _generateTableCharSet($sMysqlVersion)
00524 {
00525
00526 $oDB = oxDb::getDb();
00527
00528
00529 if (version_compare($sMysqlVersion, '4.1.0', '>=')>0) {
00530 $rs = $oDB->execute("SHOW FULL COLUMNS FROM `oxarticles`");
00531 $sMysqlCollation = $rs->fields[2];
00532 $rs = $oDB->execute("SHOW VARIABLES LIKE 'character_set_connection'");
00533 $sMysqlCharacterSet = $rs->fields[1];
00534 if ($sMysqlCollation && $sMysqlCharacterSet) {
00535 $sTableCharset = "DEFAULT CHARACTER SET ".$sMysqlCharacterSet." COLLATE ".$sMysqlCollation;
00536 } else {
00537 $sTableCharset = "";
00538 }
00539 } else {
00540 $sTableCharset = "";
00541 }
00542 return $sTableCharset;
00543 }
00544
00553 private function _createHeapTable($sHeapTable, $sTableCharset)
00554 {
00555
00556 $oDB = oxDb::getDb();
00557
00558 $sSQL = "CREATE TABLE if not exists $sHeapTable ( oxid char(32) NOT NULL default '' ) TYPE=HEAP ".$sTableCharset;
00559
00560 $rs = $oDB->execute( $sSQL);
00561 if ( $rs == false) {
00562
00563 return false;
00564 }
00565 $oDB->execute( "truncate table $sHeapTable ");
00566 return true;
00567 }
00568
00576 private function _getCatAdd($aChosenCat)
00577 {
00578
00579 $sCatAdd = null;
00580 if ( isset( $aChosenCat)) {
00581 $sCatAdd = " and ( ";
00582 $blSep = false;
00583 foreach ( $aChosenCat as $sCat) {
00584 if ( $blSep) {
00585 $sCatAdd .= " or ";
00586 }
00587 $sCatAdd .= "oxobject2category.oxcatnid = '$sCat'";
00588 $blSep = true;
00589 }
00590 $sCatAdd .= ")";
00591 }
00592 return $sCatAdd;
00593 }
00594
00603 private function _insertArticles($sHeapTable, $sCatAdd)
00604 {
00605 $oDB = oxDb::getDb();
00606 $sSearchString = oxConfig::getParameter( "search");
00607 $blExportVars = oxConfig::getParameter( "blExportVars");
00608
00609 $language = oxLang::getInstance()->getLanguageTag( 0);
00610 $sShopID = $this->getConfig()->getShopID();
00611
00612 $sO2CView = getViewName('oxobject2category');
00613 $oArticle = oxNew( 'oxarticle' );
00614 $sArticleTable = $oArticle->getViewName();
00615
00616 $sSelect = "insert into $sHeapTable select $sArticleTable.oxid from $sArticleTable, $sO2CView as oxobject2category where ";
00617 $sSelect .= $oArticle->getSqlActiveSnippet();
00618
00619 if ( !$blExportVars) {
00620 $sSelect .= " and $sArticleTable.oxid = oxobject2category.oxobjectid and $sArticleTable.oxparentid='' ";
00621 } else {
00622 $sSelect .= " and ( $sArticleTable.oxid = oxobject2category.oxobjectid or $sArticleTable.oxparentid = oxobject2category.oxobjectid ) ";
00623 }
00624
00625
00626
00627
00628
00629 if ( isset( $sSearchString) && strlen( $sSearchString)) {
00630 $sSelect .= "and ( $sArticleTable.OXTITLE".$language." like '%$sSearchString%' ";
00631 $sSelect .= "or $sArticleTable.OXSHORTDESC".$language." like '%$sSearchString%' ";
00632 $sSelect .= "or $sArticleTable.oxsearchkeys like '%$sSearchString%') ";
00633 }
00634 if ( $sCatAdd) {
00635 $sSelect .= $sCatAdd;
00636 }
00637 if( !$sCatAdd)
00638 $sSelect .= " and oxarticles.oxshopid = '$sShopID' ";
00639
00640
00641 $dMinStock = oxConfig::getParameter( "sExportMinStock");
00642 if ( isset( $dMinStock) && $dMinStock && $this->getConfig()->getConfigParam( 'blUseStock' ) ) {
00643 $dMinStock = str_replace( array( ";", " ", "/", "'"), "", $dMinStock);
00644 $sSelect .= " and $sArticleTable.oxstock >= $dMinStock";
00645 }
00646 $sSelect .= " group by $sArticleTable.oxid";
00647
00648 if ( $oDB->execute( $sSelect) ) {
00649 return true;
00650 } else {
00651 return false;
00652 }
00653 }
00654
00662 private function _removeParentArticles($sHeapTable)
00663 {
00664 $oDB = oxDb::getDb();
00665 $blExportParentVars = oxConfig::getParameter( "blExportMainVars");
00666 $sArticleTable = getViewName('oxarticles');
00667
00668 if ( !isset( $blExportParentVars) || !$blExportParentVars) {
00669
00670 $rs = $oDB->execute( "select $sHeapTable.oxid from $sHeapTable, $sArticleTable where $sHeapTable.oxid = $sArticleTable.oxparentid group by $sHeapTable.oxid");
00671 $sDel = "delete from $sHeapTable where oxid in ( ";
00672 $blSep = false;
00673 if ($rs != false && $rs->recordCount() > 0) {
00674 while (!$rs->EOF) {
00675 if ( $blSep) {
00676 $sDel .= ",";
00677 }
00678 $sDel .= "'".$rs->fields[0]."'";
00679 $blSep = true;
00680 $rs->moveNext();
00681 }
00682 }
00683 $sDel .= " )";
00684 $oDB->execute( $sDel);
00685
00686 }
00687 }
00688
00695 private function _setSessionParams()
00696 {
00697
00698 oxSession::deleteVar("sExportDelCost");
00699 $dDelCost = oxConfig::getParameter( "sExportDelCost");
00700 if ( isset( $dDelCost)) {
00701 $dDelCost = str_replace( array( ";", " ", "/", "'"), "", $dDelCost);
00702 $dDelCost = str_replace( ",", ".", $dDelCost);
00703 oxSession::setVar( "sExportDelCost", $dDelCost);
00704 }
00705
00706 oxSession::deleteVar("sExportMinPrice");
00707 $dMinPrice = oxConfig::getParameter( "sExportMinPrice");
00708 if ( isset( $dMinPrice)) {
00709 $dMinPrice = str_replace( array( ";", " ", "/", "'"), "", $dMinPrice);
00710 $dMinPrice = str_replace( ",", ".", $dMinPrice);
00711 oxSession::setVar( "sExportMinPrice", $dMinPrice);
00712 }
00713
00714
00715 oxSession::deleteVar("sExportCampaign");
00716 $sCampaign = oxConfig::getParameter( "sExportCampaign");
00717 if ( isset( $sCampaign)) {
00718 $sCampaign = str_replace( array( ";", " ", "/", "'"), "", $sCampaign);
00719 oxSession::setVar( "sExportCampaign", $sCampaign);
00720 }
00721
00722 oxSession::deleteVar("blAppendCatToCampaign");
00723
00724 $blAppendCatToCampaign = oxConfig::getParameter( "blAppendCatToCampaign");
00725 if ( isset( $blAppendCatToCampaign) && $blAppendCatToCampaign) {
00726 oxSession::setVar( "blAppendCatToCampaign", $blAppendCatToCampaign);
00727 }
00728 }
00729
00735 private function _loadRootCats()
00736 {
00737 $myConfig = $this->getConfig();
00738 if ( !isset( $myConfig->aCatLvlCache) || !count( $myConfig->aCatLvlCache)) {
00739 $myConfig->aCatLvlCache = array();
00740
00741 $sLang = oxLang::getInstance()->getBaseLanguage();
00742 $sCatView = getViewName('oxcategories');
00743
00744
00745 $sSQL = "select oxid from $sCatView where oxparentid = 'oxrootid'";
00746 $rs = oxDb::getDb()->Execute( $sSQL);
00747 if ($rs != false && $rs->recordCount() > 0) {
00748 while (!$rs->EOF) {
00749
00750 $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";
00751 $rs2 = oxDb::getDb()->Execute( $sSQL);
00752 if ($rs2 != false && $rs2->recordCount() > 0) {
00753 while (!$rs2->EOF) {
00754
00755 $oCat = new stdClass();
00756 $oCat->_sOXID = $rs2->fields[0];
00757 $oCat->oxtitle = $rs2->fields[1];
00758 $oCat->oxparentid = $rs2->fields[2];
00759 $oCat->ilevel = $rs2->fields[3];
00760 $myConfig->aCatLvlCache[$oCat->_sOXID] = $oCat;
00761
00762 $rs2->moveNext();
00763 }
00764 }
00765 $rs->moveNext();
00766 }
00767 }
00768 }
00769 }
00770
00778 private function _findDeepestCatPath($oArticle)
00779 {
00780 $myConfig = $this->getConfig();
00781 $sRet = "";
00782
00783
00784 $aIDs = $oArticle->getCategoryIds();
00785 if ( isset( $aIDs) && count( $aIDs)) {
00786 $SIDMAX = null;
00787 $dMaxLvl = 0;
00788 foreach ( $aIDs as $key => $sCatID) {
00789 if ( $dMaxLvl < $myConfig->aCatLvlCache[$sCatID]->ilevel) {
00790 $dMaxLvl = $myConfig->aCatLvlCache[$sCatID]->ilevel;
00791 $SIDMAX = $sCatID;
00792 }
00793 }
00794
00795 $sRet = $myConfig->aCatLvlCache[$SIDMAX]->oxtitle;
00796
00797 for ( ;;) {
00798 $SIDMAX = @$myConfig->aCatLvlCache[$SIDMAX]->oxparentid;
00799 if ( !isset( $SIDMAX) || $SIDMAX == "oxrootid") {
00800 break;
00801 }
00802 $sRet = $myConfig->aCatLvlCache[$SIDMAX]->oxtitle."/".$sRet;
00803 }
00804 }
00805 return $sRet;
00806 }
00807
00816 private function _initArticle($sHeapTable, $iCnt)
00817 {
00818 $myConfig = $this->getConfig();
00819 $oDB = oxDb::getDb();
00820
00821 $oArticle = null;
00822
00823 $rs = $oDB->selectLimit( "select oxid from $sHeapTable", 1, $iCnt);
00824 if ($rs != false && $rs->recordCount() > 0) {
00825 $sOXID = $rs->fields[0];
00826
00827
00828
00829
00830 $oArticle = oxNew( 'oxarticle' );
00831 $oArticle->setLoadParentData(true);
00832 $oArticle->Load( $sOXID);
00833
00834
00835 $dMinPrice = oxConfig::getParameter( "sExportMinPrice");
00836 if ( isset( $dMinPrice) && ($oArticle->brutPrice < $dMinPrice)) {
00837 return null;
00838 }
00839 } else {
00840 return null;
00841 }
00842
00843
00844 $sTitle = $oArticle->oxarticles__oxvarselect->value?" ".$oArticle->oxarticles__oxvarselect->value:"";
00845 $oArticle->oxarticles__oxtitle->setValue($oArticle->oxarticles__oxtitle->value.$sTitle);
00846
00847 if ($oArticle->oxarticles__oxparentid->value) {
00848 $oArticle->oxdetaillink = str_replace($oArticle->oxarticles__oxparentid->value, $sOXID, $oArticle->oxdetaillink);
00849 }
00850 return $oArticle;
00851 }
00852
00860 private function _setCampaignDetailLink($oArticle)
00861 {
00862
00863 $sCampaign = oxConfig::getParameter( "sExportCampaign" );
00864 if ( $sCampaign ) {
00865
00866
00867 $oArticle->appendLink( "campaign=$sCampaign" );
00868
00869 if (oxConfig::getParameter( "blAppendCatToCampaign")) {
00870 if ( $sCat = $this->getCategoryString($oArticle) ) {
00871 $oArticle->appendLink( "/$sCat" );
00872 }
00873 }
00874 }
00875 return $oArticle;
00876 }
00877
00881 public function getViewId()
00882 {
00883 return 'dyn_interface';
00884 }
00885 }