00001 <?php
00002
00006 DEFINE("ERR_SUCCESS", -2);
00007 DEFINE("ERR_GENERAL", -1);
00008 DEFINE("ERR_FILEIO", 1);
00009
00015 class DynExportBase extends oxAdminDetails
00016 {
00022 public $sClassDo = "";
00023
00029 public $sClassMain = "";
00030
00036 public $sExportPath = "export/";
00037
00043 public $sExportFileType = "txt";
00044
00050 public $sExportFileName = "dynexport";
00051
00057 public $fpFile = null;
00058
00064 public $iExportPerTick = 30;
00065
00071 protected $_sFilePath = null;
00072
00078 protected $_aExportResultset = array();
00079
00085 protected $_sThisTemplate = "dynexportbase.tpl";
00086
00092 protected $_aCatLvlCache = null;
00093
00099 public function __construct()
00100 {
00101 parent::__construct();
00102
00103
00104 $this->_sFilePath = $this->getConfig()->getConfigParam( 'sShopDir' ) . "/". $this->sExportPath . $this->sExportFileName . "." . $this->sExportFileType;
00105 }
00106
00113 public function render()
00114 {
00115 parent::render();
00116
00117
00118 $aClassVars = get_object_vars( $this );
00119 while ( list( $name, $value ) = each( $aClassVars ) ) {
00120 $this->_aViewData[$name] = $value;
00121 }
00122
00123 $this->_aViewData['sOutputFile'] = $this->_sFilePath;
00124 $this->_aViewData['sDownloadFile'] = $this->getConfig()->getConfigParam( 'sShopURL' ) . $this->sExportPath . $this->sExportFileName . "." . $this->sExportFileType;
00125
00126 return $this->_sThisTemplate;
00127 }
00128
00134 public function createMainExportView()
00135 {
00136
00137 $this->_aViewData["cattree"] = oxNew( "oxCategoryList" );
00138 $this->_aViewData["cattree"]->buildList( $this->getConfig()->getConfigParam( 'bl_perfLoadCatTree' ) );
00139 }
00140
00146 public function start()
00147 {
00148
00149 $this->fpFile = @fopen( $this->_sFilePath, "w" );
00150 if ( !isset( $this->fpFile ) || !$this->fpFile ) {
00151
00152 $this->stop( ERR_FILEIO );
00153 } else {
00154 $this->_aViewData['refresh'] = 0;
00155 $this->_aViewData['iStart'] = 0;
00156 fclose( $this->fpFile );
00157
00158
00159 $iEnd = $this->prepareExport();
00160 oxSession::setVar( "iEnd", $iEnd );
00161 $this->_aViewData['iEnd'] = $iEnd;
00162 }
00163 }
00164
00172 public function stop( $iError = 0 )
00173 {
00174 if ( $iError ) {
00175 $this->_aViewData['iError'] = $iError;
00176 }
00177
00178
00179 oxDb::getDb()->execute( "drop TABLE if exists ". $this->_getHeapTableName() );
00180 }
00181
00189 public function nextTick( $iCnt)
00190 {
00191 return false;
00192 }
00193
00201 public function write( $sLine )
00202 {
00203 $sLine = $this->removeSID( $sLine );
00204 $sLine = str_replace( array("\r\n","\n"), "", $sLine);
00205 fwrite( $this->fpFile, $sLine."\r\n");
00206 }
00207
00213 public function run()
00214 {
00215 $blContinue = true;
00216 $iExportedItems = 0;
00217
00218 $this->fpFile = @fopen( $this->_sFilePath, "a");
00219 if ( !isset( $this->fpFile) || !$this->fpFile) {
00220
00221 $this->stop( ERR_FILEIO);
00222 } else {
00223
00224 $iStart = oxConfig::getParameter("iStart");
00225
00226 $this->_aExportResultset = oxConfig::getParameter( "aExportResultset");
00227
00228 for ( $i = $iStart; $i < $iStart + $this->iExportPerTick; $i++) {
00229 if ( ( $iExportedItems = $this->nextTick( $i ) ) === false ) {
00230
00231 $this->stop( ERR_SUCCESS);
00232 $blContinue = false;
00233 break;
00234 }
00235 }
00236 if ( $blContinue) {
00237
00238 $this->_aViewData['refresh'] = 0;
00239 $this->_aViewData['iStart'] = $i;
00240 $this->_aViewData['iExpItems'] = $iExportedItems;
00241 }
00242 fclose( $this->fpFile);
00243 }
00244 }
00245
00253 public function removeSid( $sInput )
00254 {
00255 $sSid = $this->getSession()->getId();
00256
00257
00258 $sOutput = str_replace( "sid={$sSid}/", "", $sInput);
00259 $sOutput = str_replace( "sid/{$sSid}/", "", $sOutput);
00260 $sOutput = str_replace( "sid={$sSid}&", "", $sOutput);
00261 $sOutput = str_replace( "sid={$sSid}&", "", $sOutput);
00262 $sOutput = str_replace( "sid={$sSid}", "", $sOutput);
00263
00264 return $sOutput;
00265 }
00266
00276 public function shrink( $sInput, $iMaxSize, $blRemoveNewline = true )
00277 {
00278 if ( $blRemoveNewline ) {
00279 $sInput = str_replace( "\r\n", " ", $sInput );
00280 $sInput = str_replace( "\n", " ", $sInput );
00281 }
00282
00283 $sInput = str_replace( "\t", " ", $sInput );
00284
00285
00286 $sInput = $this->_unHTMLEntities( strip_tags( $sInput ) );
00287
00288 $oStr = getStr();
00289 if ( $oStr->strlen( $sInput ) > $iMaxSize - 3 ) {
00290 $sInput = $oStr->substr( $sInput, 0, $iMaxSize - 5 ) . "...";
00291 }
00292
00293 return $sInput;
00294 }
00295
00304 public function getCategoryString( $oArticle, $sSeparator = "/" )
00305 {
00306 $sCatStr = '';
00307
00308 $sLang = oxLang::getInstance()->getBaseLanguage();
00309 $oDB = oxDb::getDb();
00310
00311 $sCatView = getViewName( 'oxcategories', $sLang );
00312 $sO2CView = getViewName( 'oxobject2category', $sLang );
00313
00314
00315 $sQ = "select $sCatView.oxleft, $sCatView.oxright, $sCatView.oxrootid from $sO2CView as oxobject2category left join $sCatView on $sCatView.oxid = oxobject2category.oxcatnid ";
00316 $sQ .= "where oxobject2category.oxobjectid=".$oDB->quote( $oArticle->getId() )." and $sCatView.oxactive = 1 order by oxobject2category.oxtime ";
00317
00318 $oRs = $oDB->execute( $sQ );
00319 if ( $oRs != false && $oRs->recordCount() > 0 ) {
00320 $sLeft = $oRs->fields[0];
00321 $sRight = $oRs->fields[1];
00322 $sRootId = $oRs->fields[2];
00323
00324
00325 $sQ = "select oxtitle from $sCatView where oxright >= {$sRight} and oxleft <= {$sLeft} and oxrootid = '{$sRootId}' order by oxleft ";
00326
00327 $oRs = $oDB->execute( $sQ );
00328 if ( $oRs != false && $oRs->recordCount() > 0 ) {
00329 while ( !$oRs->EOF ) {
00330 if ( $sCatStr ) {
00331 $sCatStr .= $sSeparator;
00332 }
00333 $sCatStr .= $oRs->fields[0];
00334 $oRs->moveNext();
00335 }
00336 }
00337 }
00338
00339 return $sCatStr;
00340 }
00341
00349 public function getDefaultCategoryString( $oArticle )
00350 {
00351 $sLang = oxLang::getInstance()->getBaseLanguage();
00352 $oDB = oxDb::getDb();
00353
00354 $sCatView = getViewName( 'oxcategories', $sLang );
00355 $sO2CView = getViewName( 'oxobject2category', $sLang );
00356
00357
00358 $sQ = "select $sCatView.oxtitle from $sO2CView as oxobject2category left join $sCatView on $sCatView.oxid = oxobject2category.oxcatnid ";
00359 $sQ .= "where oxobject2category.oxobjectid=".$oDB->quote( $oArticle->getId() )." and $sCatView.oxactive = 1 order by oxobject2category.oxtime ";
00360
00361 return $oDB->getOne( $sQ);
00362 }
00363
00371 public function prepareCSV( $sInput )
00372 {
00373 $sInput = oxUtilsString::getInstance()->prepareCSVField( $sInput );
00374 return str_replace( array( " ", "€", "|" ), array( " ", "", "" ), $sInput );
00375 }
00376
00384 public function prepareXML( $sInput )
00385 {
00386 $sOutput = str_replace( "&", "&", $sInput );
00387 $sOutput = str_replace( "\"", """, $sOutput );
00388 $sOutput = str_replace( ">", ">", $sOutput );
00389 $sOutput = str_replace( "<", "<", $sOutput );
00390 $sOutput = str_replace( "'", "'", $sOutput );
00391
00392 return $sOutput;
00393 }
00394
00402 public function getDeepestCategoryPath( $oArticle )
00403 {
00404 return $this->_findDeepestCatPath( $oArticle );
00405 }
00406
00412 public function prepareExport()
00413 {
00414 $oDB = oxDb::getDb();
00415 $sHeapTable = $this->_getHeapTableName();
00416
00417
00418
00419 $oRs = $oDB->execute( "SHOW VARIABLES LIKE 'version'" );
00420 $sTableCharset = $this->_generateTableCharSet( $oRs->fields[1] );
00421
00422
00423 if ( !( $this->_createHeapTable( $sHeapTable, $sTableCharset ) ) ) {
00424
00425 oxUtils::getInstance()->showMessageAndExit( "Could not create HEAP Table {$sHeapTable}\n<br>" );
00426 }
00427
00428 $sCatAdd = $this->_getCatAdd( oxConfig::getParameter( "acat" ) );
00429 if ( !$this->_insertArticles( $sHeapTable, $sCatAdd ) ) {
00430 oxUtils::getInstance()->showMessageAndExit( "Could not insert Articles in Table {$sHeapTable}\n<br>" );
00431 }
00432
00433 $this->_removeParentArticles( $sHeapTable );
00434 $this->_setSessionParams();
00435
00436
00437 return $oDB->getOne( "select count(*) from {$sHeapTable}" );
00438 }
00439
00448 public function getOneArticle( $iCnt, & $blContinue )
00449 {
00450 $myConfig = $this->getConfig();
00451
00452
00453
00454 $myConfig->setConfigParam( 'blExport', true );
00455 $blContinue = false;
00456
00457 if ( ( $oArticle = $this->_initArticle( $this->_getHeapTableName(), $iCnt, $blContinue ) ) ) {
00458 $blContinue = true;
00459 $oArticle = $this->_setCampaignDetailLink( $oArticle );
00460 }
00461
00462
00463
00464 $myConfig->setConfigParam( 'blExport', false );
00465
00466 return $oArticle;
00467 }
00468
00477 public function assureContent( $sInput, $sReplace = null)
00478 {
00479 $oStr = getStr();
00480 if ( !$oStr->strlen( $sInput ) ) {
00481 if ( !isset( $sReplace ) || !$oStr->strlen( $sReplace ) ) {
00482 $sReplace = "-";
00483 }
00484 $sInput = $sReplace;
00485 }
00486 return $sInput;
00487 }
00488
00497 protected function _unHtmlEntities( $sInput )
00498 {
00499 $aTransTbl = array_flip( get_html_translation_table( HTML_ENTITIES ) );
00500 return strtr( $sInput, $aTransTbl );
00501 }
00502
00508 protected function _getHeapTableName()
00509 {
00510
00511 return "tmp_".str_replace( "0", "", md5( $this->getSession()->getId() ) );
00512 }
00513
00521 protected function _generateTableCharSet( $sMysqlVersion )
00522 {
00523 $sTableCharset = "";
00524
00525
00526 if ( version_compare( $sMysqlVersion, '4.1.0', '>=' ) > 0 ) {
00527 $oDB = oxDb::getDb( true );
00528 $oRs = $oDB->execute( "SHOW FULL COLUMNS FROM `oxarticles` WHERE field like 'OXID'" );
00529 if ( isset( $oRs->fields['Collation'] ) && ( $sMysqlCollation = $oRs->fields['Collation'] ) ) {
00530 $oRs = $oDB->execute( "SHOW COLLATION LIKE '{$sMysqlCollation}'" );
00531 if ( isset( $oRs->fields['Charset'] ) && ( $sMysqlCharacterSet = $oRs->fields['Charset'] ) ) {
00532 $sTableCharset = "DEFAULT CHARACTER SET {$sMysqlCharacterSet} COLLATE {$sMysqlCollation}";
00533 }
00534 }
00535
00536 }
00537 return $sTableCharset;
00538 }
00539
00548 protected function _createHeapTable( $sHeapTable, $sTableCharset )
00549 {
00550 $blDone = false;
00551
00552 $oDB = oxDb::getDb();
00553 $sQ = "CREATE TABLE if not exists {$sHeapTable} ( oxid char(32) NOT NULL default '' ) TYPE=HEAP {$sTableCharset}";
00554 if ( ( $oDB->execute( $sQ ) ) !== false ) {
00555 $blDone = true;
00556 $oDB->execute( "truncate table {$sHeapTable}" );
00557 }
00558
00559 return $blDone;
00560 }
00561
00569 protected function _getCatAdd( $aChosenCat )
00570 {
00571 $sCatAdd = null;
00572 if ( is_array( $aChosenCat ) && count( $aChosenCat ) ) {
00573 $oDB = oxDb::getDb();
00574 $sCatAdd = " and ( ";
00575 $blSep = false;
00576 foreach ( $aChosenCat as $sCat ) {
00577 if ( $blSep ) {
00578 $sCatAdd .= " or ";
00579 }
00580 $sCatAdd .= "oxobject2category.oxcatnid = ".$oDB->quote( $sCat );
00581 $blSep = true;
00582 }
00583 $sCatAdd .= ")";
00584 }
00585 return $sCatAdd;
00586 }
00587
00596 protected function _insertArticles( $sHeapTable, $sCatAdd )
00597 {
00598 $oDB = oxDb::getDb();
00599
00600 $sO2CView = getViewName( 'oxobject2category' );
00601 $sArticleTable = getViewName( "oxarticles" );
00602
00603 $sSelect = "insert into {$sHeapTable} select {$sArticleTable}.oxid from {$sArticleTable}, {$sO2CView} as oxobject2category where ";
00604 $sSelect .= oxNew( 'oxarticle' )->getSqlActiveSnippet();
00605
00606 if ( ! oxConfig::getParameter( "blExportVars" ) ) {
00607 $sSelect .= " and {$sArticleTable}.oxid = oxobject2category.oxobjectid and {$sArticleTable}.oxparentid = '' ";
00608 } else {
00609 $sSelect .= " and ( {$sArticleTable}.oxid = oxobject2category.oxobjectid or {$sArticleTable}.oxparentid = oxobject2category.oxobjectid ) ";
00610 }
00611
00612 $sSearchString = oxConfig::getParameter( "search" );
00613 if ( isset( $sSearchString ) ) {
00614 $sSelect .= "and ( {$sArticleTable}.OXTITLE like ".$oDB->quote( "%{$sSearchString}%" );
00615 $sSelect .= " or {$sArticleTable}.OXSHORTDESC like ".$oDB->quote( "%$sSearchString%" );
00616 $sSelect .= " or {$sArticleTable}.oxsearchkeys like ".$oDB->quote( "%$sSearchString%" ) ." ) ";
00617 }
00618
00619 if ( $sCatAdd ) {
00620 $sSelect .= $sCatAdd;
00621 }
00622
00623 if ( !$sCatAdd ) {
00624 $sShopID = $this->getConfig()->getShopId();
00625 $sSelect .= " and {$sArticleTable}.oxshopid = '$sShopID' ";
00626 }
00627
00628
00629 if ( $this->getConfig()->getConfigParam( 'blUseStock' ) && ( $dMinStock = oxConfig::getParameter( "sExportMinStock" ) ) ) {
00630 $dMinStock = str_replace( array( ";", " ", "/", "'"), "", $dMinStock );
00631 $sSelect .= " and {$sArticleTable}.oxstock >= ".$oDB->quote( $dMinStock );
00632 }
00633
00634 $sSelect .= " group by {$sArticleTable}.oxid";
00635 return $oDB->execute( $sSelect ) ? true : false;
00636 }
00637
00645 protected function _removeParentArticles( $sHeapTable )
00646 {
00647 if ( !( oxConfig::getParameter( "blExportMainVars" ) ) ) {
00648
00649 $oDB = oxDb::getDb();
00650 $sArticleTable = getViewName('oxarticles');
00651
00652
00653 $sQ = "select $sHeapTable.oxid from $sHeapTable, $sArticleTable where
00654 $sHeapTable.oxid = $sArticleTable.oxparentid group by $sHeapTable.oxid";
00655
00656 $oRs = $oDB->execute( $sQ );
00657 $sDel = "delete from $sHeapTable where oxid in ( ";
00658 $blSep = false;
00659 if ($oRs != false && $oRs->recordCount() > 0) {
00660 while ( !$oRs->EOF ) {
00661 if ( $blSep ) {
00662 $sDel .= ",";
00663 }
00664 $sDel .= $oDB->quote( $oRs->fields[0] );
00665 $blSep = true;
00666 $oRs->moveNext();
00667 }
00668 }
00669 $sDel .= " )";
00670 $oDB->execute( $sDel );
00671 }
00672 }
00673
00680 protected function _setSessionParams()
00681 {
00682
00683 oxSession::deleteVar( "sExportDelCost" );
00684 $dDelCost = oxConfig::getParameter( "sExportDelCost");
00685 if ( isset( $dDelCost ) ) {
00686 $dDelCost = str_replace( array( ";", " ", "/", "'"), "", $dDelCost );
00687 $dDelCost = str_replace( ",", ".", $dDelCost );
00688 oxSession::setVar( "sExportDelCost", $dDelCost );
00689 }
00690
00691 oxSession::deleteVar( "sExportMinPrice" );
00692 $dMinPrice = oxConfig::getParameter( "sExportMinPrice" );
00693 if ( isset( $dMinPrice ) ) {
00694 $dMinPrice = str_replace( array( ";", " ", "/", "'"), "", $dMinPrice);
00695 $dMinPrice = str_replace( ",", ".", $dMinPrice);
00696 oxSession::setVar( "sExportMinPrice", $dMinPrice);
00697 }
00698
00699
00700 oxSession::deleteVar( "sExportCampaign" );
00701 $sCampaign = oxConfig::getParameter( "sExportCampaign" );
00702 if ( isset( $sCampaign ) ) {
00703 $sCampaign = str_replace( array( ";", " ", "/", "'"), "", $sCampaign );
00704 oxSession::setVar( "sExportCampaign", $sCampaign );
00705 }
00706
00707
00708 oxSession::deleteVar("blAppendCatToCampaign" );
00709
00710 $blAppendCatToCampaign = oxConfig::getParameter( "blAppendCatToCampaign" );
00711 if ( $blAppendCatToCampaign ) {
00712 oxSession::setVar( "blAppendCatToCampaign", $blAppendCatToCampaign );
00713 }
00714 }
00715
00721 protected function _loadRootCats()
00722 {
00723 if ( $this->_aCatLvlCache === null ) {
00724 $this->_aCatLvlCache = array();
00725
00726 $sCatView = getViewName('oxcategories');
00727 $oDb = oxDb::getDb();
00728
00729
00730 $sSQL = "select oxid from $sCatView where oxparentid = 'oxrootid'";
00731 $oRs = $oDb->execute( $sSQL);
00732 if ( $oRs != false && $oRs->recordCount() > 0 ) {
00733 while ( !$oRs->EOF ) {
00734
00735 $sSQL = "SELECT s.oxid, s.oxtitle,
00736 s.oxparentid, count( * ) AS LEVEL FROM $sCatView v,
00737 $sCatView s WHERE s.oxrootid = '".$oRs->fields[0]."' and
00738 v.oxrootid='".$oRs->fields[0]."' and s.oxleft BETWEEN
00739 v.oxleft AND v.oxright AND s.oxhidden = '0' GROUP BY s.oxleft order by level";
00740
00741 $oRs2 = $oDb->Execute( $sSQL );
00742 if ( $oRs2 != false && $oRs2->recordCount() > 0 ) {
00743 while ( !$oRs2->EOF ) {
00744
00745 $oCat = new OxStdClass();
00746 $oCat->_sOXID = $oRs2->fields[0];
00747 $oCat->oxtitle = $oRs2->fields[1];
00748 $oCat->oxparentid = $oRs2->fields[2];
00749 $oCat->ilevel = $oRs2->fields[3];
00750 $this->_aCatLvlCache[$oCat->_sOXID] = $oCat;
00751
00752 $oRs2->moveNext();
00753 }
00754 }
00755 $oRs->moveNext();
00756 }
00757 }
00758 }
00759
00760 return $this->_aCatLvlCache;
00761 }
00762
00770 protected function _findDeepestCatPath( $oArticle )
00771 {
00772 $sRet = "";
00773
00774
00775 $aIds = $oArticle->getCategoryIds();
00776 if ( is_array( $aIds ) && count( $aIds ) ) {
00777 if ( $aCatLvlCache = $this->_loadRootCats() ) {
00778 $sIdMax = null;
00779 $dMaxLvl = 0;
00780 foreach ( $aIds as $sCatId ) {
00781 if ( $dMaxLvl < $aCatLvlCache[$sCatId]->ilevel ) {
00782 $dMaxLvl = $aCatLvlCache[$sCatId]->ilevel;
00783 $sIdMax = $sCatId;
00784 $sRet = $aCatLvlCache[$sCatId]->oxtitle;
00785 }
00786 }
00787
00788
00789 for ( ;; ) {
00790 if ( !isset( $aCatLvlCache[$sIdMax]->oxparentid ) || $aCatLvlCache[$sIdMax]->oxparentid == "oxrootid" ) {
00791 break;
00792 }
00793 $sIdMax = $aCatLvlCache[$sIdMax]->oxparentid;
00794 $sRet = $aCatLvlCache[$sIdMax]->oxtitle."/".$sRet;
00795 }
00796 }
00797 }
00798 return $sRet;
00799 }
00800
00810 protected function _initArticle( $sHeapTable, $iCnt, & $blContinue )
00811 {
00812 $oRs = oxDb::getDb()->selectLimit( "select oxid from $sHeapTable", 1, $iCnt );
00813 if ( $oRs != false && $oRs->recordCount() > 0 ) {
00814 $oArticle = oxNew( 'oxarticle' );
00815 $oArticle->setLoadParentData( true );
00816
00817 if ( $oArticle->load( $oRs->fields[0] ) ) {
00818
00819 $blContinue = true;
00820
00821 $dMinPrice = oxConfig::getParameter( "sExportMinPrice" );
00822 if ( !isset( $dMinPrice ) || ( isset( $dMinPrice ) && ( $oArticle->getPrice()->getBruttoPrice() >= $dMinPrice ) ) ) {
00823
00824
00825 $sTitle = $oArticle->oxarticles__oxvarselect->value ? " " .$oArticle->oxarticles__oxvarselect->value : "";
00826 $oArticle->oxarticles__oxtitle->setValue( $oArticle->oxarticles__oxtitle->value . $sTitle );
00827
00828
00829 return $oArticle;
00830 }
00831 }
00832 }
00833 }
00834
00842 protected function _setCampaignDetailLink( $oArticle )
00843 {
00844
00845 if ( $sCampaign = oxConfig::getParameter( "sExportCampaign" ) ) {
00846
00847
00848 $oArticle->appendLink( "campaign={$sCampaign}" );
00849
00850 if ( oxConfig::getParameter( "blAppendCatToCampaign") &&
00851 ( $sCat = $this->getCategoryString( $oArticle ) ) ) {
00852 $oArticle->appendLink( "/$sCat" );
00853 }
00854 }
00855 return $oArticle;
00856 }
00857
00863 public function getViewId()
00864 {
00865 return 'dyn_interface';
00866 }
00867 }
00868