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(true);
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 group by oxnewssubscribed.oxemail";
00067 
00068         $oRs = $oDB->selectLimit( $sQ, 100, $iStart );
00069         $blContinue = ( $oRs != false && $oRs->recordCount() > 0 );
00070 
00071         if ( $blContinue ) {
00072             $blLoadAction = $myConfig->getConfigParam( 'bl_perfLoadAktion' );
00073             while ( !$oRs->EOF && $iSendCnt < $iMaxCnt ) {
00074 
00075                 if ( $oRs->fields['oxemailfailed'] != "1" ) {
00076                     $sUserId = $oRs->fields['oxuserid'];
00077                     $iSendCnt++;
00078 
00079                     // must check if such user is in DB
00080                     if ( !$oDB->getOne( "select oxid from oxuser where oxid = ".$oDB->quote( $sUserId ) ) ) {
00081                         $sUserId = null;
00082                     }
00083 
00084                     // #559
00085                     if ( !isset( $sUserId ) || !$sUserId ) {
00086                          // there is no user object so we fake one
00087                         $oUser = oxNew( "oxuser" );
00088                         $oUser->oxuser__oxusername = new oxField( $oRs->fields['oxemail'] );
00089                         $oUser->oxuser__oxsal      = new oxField( $oRs->fields['oxsal'] );
00090                         $oUser->oxuser__oxfname    = new oxField( $oRs->fields['oxfname'] );
00091                         $oUser->oxuser__oxlname    = new oxField( $oRs->fields['oxlname'] );
00092                         $oNewsletter->prepare( $oUser, $blLoadAction );
00093                     } else {
00094                         $oNewsletter->prepare( $sUserId, $blLoadAction );
00095                     }
00096 
00097                     if ( $oNewsletter->send( $iSendCnt ) ) {
00098                          // add user history
00099                         $oRemark = oxNew( "oxremark" );
00100                         $oRemark->oxremark__oxtext     = new oxField( $oNewsletter->getPlainText() );
00101                         $oRemark->oxremark__oxparentid = new oxField( $sUserId );
00102                         $oRemark->oxremark__oxshopid   = new oxField( $sShopId );
00103                         $oRemark->oxremark__oxtype     = new oxField( "n" );
00104                         $oRemark->save();
00105                     } else {
00106                         $this->_aMailErrors[] = "problem sending to : ".$oRs->fields['oxemail'];
00107                     }
00108                 }
00109 
00110                 $oRs->moveNext();
00111                 $iStart++;
00112             }
00113         }
00114 
00115         $iSend = $iSendCnt + ( ceil( $iStart / $iMaxCnt ) - 1 ) * $iMaxCnt;
00116         $iSend = $iSend > $iUserCount ? $iUserCount : $iSend;
00117 
00118         $this->_aViewData["iStart"] = $iStart;
00119         $this->_aViewData["iSend"]  = $iSend;
00120 
00121         // end ?
00122         if ( $blContinue ) {
00123             return "newsletter_send.tpl";
00124         } else {
00125             $this->resetUserCount();
00126             return "newsletter_done.tpl";
00127         }
00128     }
00129 
00135     public function getUserCount()
00136     {
00137         $iCnt = oxSession::getVar( "iUserCount" );
00138         if ( $iCnt === null ) {
00139             $iCnt = parent::getUserCount();
00140             oxSession::setVar( "iUserCount", $iCnt );
00141         }
00142         return $iCnt;
00143     }
00144 
00150     public function resetUserCount()
00151     {
00152         oxSession::deleteVar( "iUserCount" );
00153         $this->_iUserCount = null;
00154     }
00155 
00161     public function getMailErrors()
00162     {
00163         return $this->_aMailErrors;
00164     }
00165 
00173     protected function _setupNavigation( $sNode )
00174     {
00175         $sNode = 'newsletter_list';
00176 
00177         $myAdminNavig = $this->getNavigation();
00178 
00179         // active tab
00180         $iActTab = 3;
00181 
00182         // tabs
00183         $this->_aViewData['editnavi'] = $myAdminNavig->getTabs( $sNode, $iActTab );
00184 
00185         // active tab
00186         $this->_aViewData['actlocation'] = $myAdminNavig->getActiveTab( $sNode, $iActTab );
00187 
00188         // default tab
00189         $this->_aViewData['default_edit'] = $myAdminNavig->getActiveTab( $sNode, $this->_iDefEdit );
00190 
00191         // passign active tab number
00192         $this->_aViewData['actedit'] = $iActTab;
00193     }
00194 
00200     public function getListSorting()
00201     {
00202     }
00203 }