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' );
00312 $sO2CView = getViewName( 'oxobject2category' );
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".(($sLang)?"_$sLang":"")." = 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".( ( $sLang ) ? "_$sLang" : "" )." 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' );
00355 $sO2CView = getViewName( 'oxobject2category' );
00356
00357
00358 $sQ = "select $sCatView.oxtitle".(($sLang)?"_$sLang":"")." from $sO2CView as oxobject2category left join $sCatView on $sCatView.oxid = oxobject2category.oxcatnid ";
00359 $sQ .= "where oxobject2category.oxobjectid=".$oDB->quote( $oArticle->getId() )." and $sCatView.oxactive".(($sLang)?"_$sLang":"")." = 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 $iLanguage = oxLang::getInstance()->getLanguageTag( 0 );
00601
00602 $sO2CView = getViewName('oxobject2category');
00603 $oArticle = oxNew( 'oxarticle' );
00604 $sArticleTable = $oArticle->getViewName();
00605
00606 $sSelect = "insert into {$sHeapTable} select {$sArticleTable}.oxid from {$sArticleTable}, {$sO2CView} as oxobject2category where ";
00607 $sSelect .= $oArticle->getSqlActiveSnippet();
00608
00609 if ( ! oxConfig::getParameter( "blExportVars" ) ) {
00610 $sSelect .= " and {$sArticleTable}.oxid = oxobject2category.oxobjectid and {$sArticleTable}.oxparentid = '' ";
00611 } else {
00612 $sSelect .= " and ( {$sArticleTable}.oxid = oxobject2category.oxobjectid or {$sArticleTable}.oxparentid = oxobject2category.oxobjectid ) ";
00613 }
00614
00615 $sSearchString = oxConfig::getParameter( "search" );
00616 if ( isset( $sSearchString ) ) {
00617 $sSelect .= "and ( {$sArticleTable}.OXTITLE".$iLanguage." like ".$oDB->quote( "%{$sSearchString}%" );
00618 $sSelect .= " or {$sArticleTable}.OXSHORTDESC".$iLanguage." like ".$oDB->quote( "%$sSearchString%" );
00619 $sSelect .= " or {$sArticleTable}.oxsearchkeys like ".$oDB->quote( "%$sSearchString%" ) ." ) ";
00620 }
00621
00622 if ( $sCatAdd ) {
00623 $sSelect .= $sCatAdd;
00624 }
00625
00626 if ( !$sCatAdd ) {
00627 $sShopID = $this->getConfig()->getShopId();
00628 $sSelect .= " and {$sArticleTable}.oxshopid = '$sShopID' ";
00629 }
00630
00631
00632 if ( $this->getConfig()->getConfigParam( 'blUseStock' ) && ( $dMinStock = oxConfig::getParameter( "sExportMinStock" ) ) ) {
00633 $dMinStock = str_replace( array( ";", " ", "/", "'"), "", $dMinStock );
00634 $sSelect .= " and {$sArticleTable}.oxstock >= ".$oDB->quote( $dMinStock );
00635 }
00636
00637 $sSelect .= " group by {$sArticleTable}.oxid";
00638 return $oDB->execute( $sSelect ) ? true : false;
00639 }
00640
00648 protected function _removeParentArticles( $sHeapTable )
00649 {
00650 if ( !( oxConfig::getParameter( "blExportMainVars" ) ) ) {
00651
00652 $oDB = oxDb::getDb();
00653 $sArticleTable = getViewName('oxarticles');
00654
00655
00656 $sQ = "select $sHeapTable.oxid from $sHeapTable, $sArticleTable where
00657 $sHeapTable.oxid = $sArticleTable.oxparentid group by $sHeapTable.oxid";
00658
00659 $oRs = $oDB->execute( $sQ );
00660 $sDel = "delete from $sHeapTable where oxid in ( ";
00661 $blSep = false;
00662 if ($oRs != false && $oRs->recordCount() > 0) {
00663 while ( !$oRs->EOF ) {
00664 if ( $blSep ) {
00665 $sDel .= ",";
00666 }
00667 $sDel .= $oDB->quote( $oRs->fields[0] );
00668 $blSep = true;
00669 $oRs->moveNext();
00670 }
00671 }
00672 $sDel .= " )";
00673 $oDB->execute( $sDel );
00674 }
00675 }
00676
00683 protected function _setSessionParams()
00684 {
00685
00686 oxSession::deleteVar( "sExportDelCost" );
00687 $dDelCost = oxConfig::getParameter( "sExportDelCost");
00688 if ( isset( $dDelCost ) ) {
00689 $dDelCost = str_replace( array( ";", " ", "/", "'"), "", $dDelCost );
00690 $dDelCost = str_replace( ",", ".", $dDelCost );
00691 oxSession::setVar( "sExportDelCost", $dDelCost );
00692 }
00693
00694 oxSession::deleteVar( "sExportMinPrice" );
00695 $dMinPrice = oxConfig::getParameter( "sExportMinPrice" );
00696 if ( isset( $dMinPrice ) ) {
00697 $dMinPrice = str_replace( array( ";", " ", "/", "'"), "", $dMinPrice);
00698 $dMinPrice = str_replace( ",", ".", $dMinPrice);
00699 oxSession::setVar( "sExportMinPrice", $dMinPrice);
00700 }
00701
00702
00703 oxSession::deleteVar( "sExportCampaign" );
00704 $sCampaign = oxConfig::getParameter( "sExportCampaign" );
00705 if ( isset( $sCampaign ) ) {
00706 $sCampaign = str_replace( array( ";", " ", "/", "'"), "", $sCampaign );
00707 oxSession::setVar( "sExportCampaign", $sCampaign );
00708 }
00709
00710
00711 oxSession::deleteVar("blAppendCatToCampaign" );
00712
00713 $blAppendCatToCampaign = oxConfig::getParameter( "blAppendCatToCampaign" );
00714 if ( $blAppendCatToCampaign ) {
00715 oxSession::setVar( "blAppendCatToCampaign", $blAppendCatToCampaign );
00716 }
00717 }
00718
00724 protected function _loadRootCats()
00725 {
00726 if ( $this->_aCatLvlCache === null ) {
00727 $this->_aCatLvlCache = array();
00728
00729 $sLang = oxLang::getInstance()->getBaseLanguage();
00730 $sCatView = getViewName('oxcategories');
00731 $oDb = oxDb::getDb();
00732
00733
00734 $sSQL = "select oxid from $sCatView where oxparentid = 'oxrootid'";
00735 $oRs = $oDb->execute( $sSQL);
00736 if ( $oRs != false && $oRs->recordCount() > 0 ) {
00737 while ( !$oRs->EOF ) {
00738
00739 $sSQL = "SELECT s.oxid, s.oxtitle".(($sLang)?"_$sLang":"").",
00740 s.oxparentid, count( * ) AS LEVEL FROM oxcategories v,
00741 oxcategories s WHERE s.oxrootid = '".$oRs->fields[0]."' and
00742 v.oxrootid='".$oRs->fields[0]."' and s.oxleft BETWEEN
00743 v.oxleft AND v.oxright AND s.oxhidden = '0' GROUP BY s.oxleft order by level";
00744
00745 $oRs2 = $oDb->Execute( $sSQL );
00746 if ( $oRs2 != false && $oRs2->recordCount() > 0 ) {
00747 while ( !$oRs2->EOF ) {
00748
00749 $oCat = new OxStdClass();
00750 $oCat->_sOXID = $oRs2->fields[0];
00751 $oCat->oxtitle = $oRs2->fields[1];
00752 $oCat->oxparentid = $oRs2->fields[2];
00753 $oCat->ilevel = $oRs2->fields[3];
00754 $this->_aCatLvlCache[$oCat->_sOXID] = $oCat;
00755
00756 $oRs2->moveNext();
00757 }
00758 }
00759 $oRs->moveNext();
00760 }
00761 }
00762 }
00763
00764 return $this->_aCatLvlCache;
00765 }
00766
00774 protected function _findDeepestCatPath( $oArticle )
00775 {
00776 $sRet = "";
00777
00778
00779 $aIds = $oArticle->getCategoryIds();
00780 if ( is_array( $aIds ) && count( $aIds ) ) {
00781 if ( $aCatLvlCache = $this->_loadRootCats() ) {
00782 $sIdMax = null;
00783 $dMaxLvl = 0;
00784 foreach ( $aIds as $sCatId ) {
00785 if ( $dMaxLvl < $aCatLvlCache[$sCatId]->ilevel ) {
00786 $dMaxLvl = $aCatLvlCache[$sCatId]->ilevel;
00787 $sIdMax = $sCatId;
00788 $sRet = $aCatLvlCache[$sCatId]->oxtitle;
00789 }
00790 }
00791
00792
00793 for ( ;; ) {
00794 if ( !isset( $aCatLvlCache[$sIdMax]->oxparentid ) || $aCatLvlCache[$sIdMax]->oxparentid == "oxrootid" ) {
00795 break;
00796 }
00797 $sIdMax = $aCatLvlCache[$sIdMax]->oxparentid;
00798 $sRet = $aCatLvlCache[$sIdMax]->oxtitle."/".$sRet;
00799 }
00800 }
00801 }
00802 return $sRet;
00803 }
00804
00814 protected function _initArticle( $sHeapTable, $iCnt, & $blContinue )
00815 {
00816 $oRs = oxDb::getDb()->selectLimit( "select oxid from $sHeapTable", 1, $iCnt );
00817 if ( $oRs != false && $oRs->recordCount() > 0 ) {
00818 $oArticle = oxNew( 'oxarticle' );
00819 $oArticle->setLoadParentData( true );
00820
00821 if ( $oArticle->load( $oRs->fields[0] ) ) {
00822
00823 $blContinue = true;
00824
00825 $dMinPrice = oxConfig::getParameter( "sExportMinPrice" );
00826 if ( !isset( $dMinPrice ) || ( isset( $dMinPrice ) && ( $oArticle->brutPrice >= $dMinPrice ) ) ) {
00827
00828
00829 $sTitle = $oArticle->oxarticles__oxvarselect->value ? " " .$oArticle->oxarticles__oxvarselect->value : "";
00830 $oArticle->oxarticles__oxtitle->setValue( $oArticle->oxarticles__oxtitle->value . $sTitle );
00831
00832
00833 return $oArticle;
00834 }
00835 }
00836 }
00837 }
00838
00846 protected function _setCampaignDetailLink( $oArticle )
00847 {
00848
00849 if ( $sCampaign = oxConfig::getParameter( "sExportCampaign" ) ) {
00850
00851
00852 $oArticle->appendLink( "campaign={$sCampaign}" );
00853
00854 if ( oxConfig::getParameter( "blAppendCatToCampaign") &&
00855 ( $sCat = $this->getCategoryString( $oArticle ) ) ) {
00856 $oArticle->appendLink( "/$sCat" );
00857 }
00858 }
00859 return $oArticle;
00860 }
00861
00867 public function getViewId()
00868 {
00869 return 'dyn_interface';
00870 }
00871 }
00872