newsletter_send.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class Newsletter_Send extends Newsletter_Selection
00008 {
00009 
00015     protected $_aMailErrors = array();
00016 
00024     public function render()
00025     {
00026         oxAdminDetails::render();
00027 
00028         // calculating
00029         $iUserCount = $this->getUserCount();
00030 
00031         $iStart = (int) oxRegistry::getConfig()->getRequestParameter("iStart");
00032 
00033         $oNewsletter = oxNew("oxNewsLetter");
00034         $oNewsletter->load($this->getEditObjectId());
00035         $oNewsletterGroups = $oNewsletter->getGroups();
00036 
00037         // send emails....
00038         $oDB = oxDb::getDb(oxDB::FETCH_MODE_ASSOC);
00039         $sQGroups = " ( oxobject2group.oxgroupsid in ( ";
00040         $blSep = false;
00041         foreach ($oNewsletterGroups as $sInGroup) {
00042             $sSearchKey = $sInGroup->oxgroups__oxid->value;
00043             if ($blSep) {
00044                 $sQGroups .= ",";
00045             }
00046             $sQGroups .= $oDB->quote($sSearchKey);
00047             $blSep = true;
00048         }
00049         $sQGroups .= ") )";
00050 
00051         // no group selected
00052         if (!$blSep) {
00053             $sQGroups = " oxobject2group.oxobjectid is null ";
00054         }
00055 
00056         $myConfig = $this->getConfig();
00057 
00058         $iSendCnt = 0;
00059         $iMaxCnt = (int) $myConfig->getConfigParam('iCntofMails');
00060         $sShopId = $myConfig->getShopId();
00061 
00062         $sQ = "select oxnewssubscribed.oxuserid, oxnewssubscribed.oxemail, oxnewssubscribed.oxsal,
00063            oxnewssubscribed.oxfname, oxnewssubscribed.oxlname, oxnewssubscribed.oxemailfailed
00064            from oxnewssubscribed left join oxobject2group on
00065            oxobject2group.oxobjectid = oxnewssubscribed.oxuserid where
00066            ( oxobject2group.oxshopid = '{$sShopId}' or oxobject2group.oxshopid is null ) and
00067            $sQGroups and oxnewssubscribed.oxdboptin = 1 and oxnewssubscribed.oxshopid = '{$sShopId}'
00068            group by oxnewssubscribed.oxemail";
00069 
00070         $oRs = $oDB->selectLimit($sQ, 100, $iStart);
00071         $blContinue = ($oRs != false && $oRs->recordCount() > 0);
00072 
00073         if ($blContinue) {
00074             $blLoadAction = $myConfig->getConfigParam('bl_perfLoadAktion');
00075             while (!$oRs->EOF && $iSendCnt < $iMaxCnt) {
00076 
00077                 if ($oRs->fields['oxemailfailed'] != "1") {
00078                     $sUserId = $oRs->fields['oxuserid'];
00079                     $iSendCnt++;
00080 
00081                     // must check if such user is in DB
00082                     if (!$oDB->getOne("select oxid from oxuser where oxid = " . $oDB->quote($sUserId), false, false)) {
00083                         $sUserId = null;
00084                     }
00085 
00086                     // #559
00087                     if (!isset($sUserId) || !$sUserId) {
00088                         // there is no user object so we fake one
00089                         $oUser = oxNew("oxuser");
00090                         $oUser->oxuser__oxusername = new oxField($oRs->fields['oxemail']);
00091                         $oUser->oxuser__oxsal = new oxField($oRs->fields['oxsal']);
00092                         $oUser->oxuser__oxfname = new oxField($oRs->fields['oxfname']);
00093                         $oUser->oxuser__oxlname = new oxField($oRs->fields['oxlname']);
00094                         $oNewsletter->prepare($oUser, $blLoadAction);
00095                     } else {
00096                         $oNewsletter->prepare($sUserId, $blLoadAction);
00097                     }
00098 
00099                     if ($oNewsletter->send($iSendCnt)) {
00100                         // add user history
00101                         $oRemark = oxNew("oxremark");
00102                         $oRemark->oxremark__oxtext = new oxField($oNewsletter->getPlainText());
00103                         $oRemark->oxremark__oxparentid = new oxField($sUserId);
00104                         $oRemark->oxremark__oxshopid = new oxField($sShopId);
00105                         $oRemark->oxremark__oxtype = new oxField("n");
00106                         $oRemark->save();
00107                     } else {
00108                         $this->_aMailErrors[] = "problem sending to : " . $oRs->fields['oxemail'];
00109                     }
00110                 }
00111 
00112                 $oRs->moveNext();
00113                 $iStart++;
00114             }
00115         }
00116 
00117         $iSend = $iSendCnt + (ceil($iStart / $iMaxCnt) - 1) * $iMaxCnt;
00118         $iSend = $iSend > $iUserCount ? $iUserCount : $iSend;
00119 
00120         $this->_aViewData["iStart"] = $iStart;
00121         $this->_aViewData["iSend"] = $iSend;
00122 
00123         // end ?
00124         if ($blContinue) {
00125             return "newsletter_send.tpl";
00126         } else {
00127             $this->resetUserCount();
00128 
00129             return "newsletter_done.tpl";
00130         }
00131     }
00132 
00138     public function getUserCount()
00139     {
00140         $iCnt = oxRegistry::getSession()->getVariable("iUserCount");
00141         if ($iCnt === null) {
00142             $iCnt = parent::getUserCount();
00143             oxRegistry::getSession()->setVariable("iUserCount", $iCnt);
00144         }
00145 
00146         return $iCnt;
00147     }
00148 
00152     public function resetUserCount()
00153     {
00154         oxRegistry::getSession()->deleteVariable("iUserCount");
00155         $this->_iUserCount = null;
00156     }
00157 
00163     public function getMailErrors()
00164     {
00165         return $this->_aMailErrors;
00166     }
00167 
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 
00198     public function getListSorting()
00199     {
00200     }
00201 }