newsletter_send.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class Newsletter_Send extends Newsletter_Selection
00008 {
00014     protected $_aMailErrors = array();
00015 
00023     public function render()
00024     {
00025         oxAdminDetails::render();
00026 
00027         // calculating
00028         $iUserCount = $this->getUserCount();
00029 
00030         $iStart = (int) oxConfig::getParameter( "iStart" );
00031 
00032         $oNewsletter = oxNew( "oxNewsLetter" );
00033         $oNewsletter->load( $this->getEditObjectId() );
00034         $oNewsletterGroups = $oNewsletter->getGroups();
00035 
00036         // send emails....
00037         $oDB = oxDb::getDb( oxDB::FETCH_MODE_ASSOC );
00038         $sQGroups = " ( oxobject2group.oxgroupsid in ( ";
00039         $blSep = false;
00040         foreach ( $oNewsletterGroups as $sInGroup ) {
00041             $sSearchKey = $sInGroup->oxgroups__oxid->value;
00042             if ( $blSep ) {
00043                 $sQGroups .= ",";
00044             }
00045             $sQGroups .= $oDB->quote( $sSearchKey );
00046             $blSep = true;
00047         }
00048         $sQGroups .= ") )";
00049 
00050         // no group selected
00051         if ( !$blSep ) {
00052             $sQGroups = " oxobject2group.oxobjectid is null ";
00053         }
00054 
00055         $myConfig  = $this->getConfig();
00056 
00057         $iSendCnt = 0;
00058         $iMaxCnt  = (int) $myConfig->getConfigParam( 'iCntofMails' );
00059         $sShopId  = $myConfig->getShopId();
00060 
00061         $sQ = "select oxnewssubscribed.oxuserid, oxnewssubscribed.oxemail, oxnewssubscribed.oxsal,
00062                oxnewssubscribed.oxfname, oxnewssubscribed.oxlname, oxnewssubscribed.oxemailfailed
00063                from oxnewssubscribed left join oxobject2group on
00064                oxobject2group.oxobjectid = oxnewssubscribed.oxuserid where
00065                ( oxobject2group.oxshopid = '{$sShopId}' or oxobject2group.oxshopid is null ) and
00066                $sQGroups and oxnewssubscribed.oxdboptin = 1 and oxnewssubscribed.oxshopid = '{$sShopId}'
00067                group by oxnewssubscribed.oxemail";
00068 
00069         $oRs = $oDB->selectLimit( $sQ, 100, $iStart );
00070         $blContinue = ( $oRs != false && $oRs->recordCount() > 0 );
00071 
00072         if ( $blContinue ) {
00073             $blLoadAction = $myConfig->getConfigParam( 'bl_perfLoadAktion' );
00074             while ( !$oRs->EOF && $iSendCnt < $iMaxCnt ) {
00075 
00076                 if ( $oRs->fields['oxemailfailed'] != "1" ) {
00077                     $sUserId = $oRs->fields['oxuserid'];
00078                     $iSendCnt++;
00079 
00080                     // must check if such user is in DB
00081                     if ( !$oDB->getOne( "select oxid from oxuser where oxid = ".$oDB->quote( $sUserId ), false, false ) ) {
00082                         $sUserId = null;
00083                     }
00084 
00085                     // #559
00086                     if ( !isset( $sUserId ) || !$sUserId ) {
00087                          // there is no user object so we fake one
00088                         $oUser = oxNew( "oxuser" );
00089                         $oUser->oxuser__oxusername = new oxField( $oRs->fields['oxemail'] );
00090                         $oUser->oxuser__oxsal      = new oxField( $oRs->fields['oxsal'] );
00091                         $oUser->oxuser__oxfname    = new oxField( $oRs->fields['oxfname'] );
00092                         $oUser->oxuser__oxlname    = new oxField( $oRs->fields['oxlname'] );
00093                         $oNewsletter->prepare( $oUser, $blLoadAction );
00094                     } else {
00095                         $oNewsletter->prepare( $sUserId, $blLoadAction );
00096                     }
00097 
00098                     if ( $oNewsletter->send( $iSendCnt ) ) {
00099                          // add user history
00100                         $oRemark = oxNew( "oxremark" );
00101                         $oRemark->oxremark__oxtext     = new oxField( $oNewsletter->getPlainText() );
00102                         $oRemark->oxremark__oxparentid = new oxField( $sUserId );
00103                         $oRemark->oxremark__oxshopid   = new oxField( $sShopId );
00104                         $oRemark->oxremark__oxtype     = new oxField( "n" );
00105                         $oRemark->save();
00106                     } else {
00107                         $this->_aMailErrors[] = "problem sending to : ".$oRs->fields['oxemail'];
00108                     }
00109                 }
00110 
00111                 $oRs->moveNext();
00112                 $iStart++;
00113             }
00114         }
00115 
00116         $iSend = $iSendCnt + ( ceil( $iStart / $iMaxCnt ) - 1 ) * $iMaxCnt;
00117         $iSend = $iSend > $iUserCount ? $iUserCount : $iSend;
00118 
00119         $this->_aViewData["iStart"] = $iStart;
00120         $this->_aViewData["iSend"]  = $iSend;
00121 
00122         // end ?
00123         if ( $blContinue ) {
00124             return "newsletter_send.tpl";
00125         } else {
00126             $this->resetUserCount();
00127             return "newsletter_done.tpl";
00128         }
00129     }
00130 
00136     public function getUserCount()
00137     {
00138         $iCnt = oxSession::getVar( "iUserCount" );
00139         if ( $iCnt === null ) {
00140             $iCnt = parent::getUserCount();
00141             oxSession::setVar( "iUserCount", $iCnt );
00142         }
00143         return $iCnt;
00144     }
00145 
00151     public function resetUserCount()
00152     {
00153         oxSession::deleteVar( "iUserCount" );
00154         $this->_iUserCount = null;
00155     }
00156 
00162     public function getMailErrors()
00163     {
00164         return $this->_aMailErrors;
00165     }
00166 
00174     protected function _setupNavigation( $sNode )
00175     {
00176         $sNode = 'newsletter_list';
00177 
00178         $myAdminNavig = $this->getNavigation();
00179 
00180         // active tab
00181         $iActTab = 3;
00182 
00183         // tabs
00184         $this->_aViewData['editnavi'] = $myAdminNavig->getTabs( $sNode, $iActTab );
00185 
00186         // active tab
00187         $this->_aViewData['actlocation'] = $myAdminNavig->getActiveTab( $sNode, $iActTab );
00188 
00189         // default tab
00190         $this->_aViewData['default_edit'] = $myAdminNavig->getActiveTab( $sNode, $this->_iDefEdit );
00191 
00192         // passign active tab number
00193         $this->_aViewData['actedit'] = $iActTab;
00194     }
00195 
00201     public function getListSorting()
00202     {
00203     }
00204 }