newsletter_send.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class Newsletter_Send extends oxAdminList
00008 {
00014     protected $_aMailErrors = array();
00015 
00023     public function render()
00024     {
00025         $myConfig  = $this->getConfig();
00026         parent::render();
00027 
00028         $iStart = (int) oxConfig::getParameter( "iStart" );
00029         $iUser  = oxConfig::getParameter( "user" );
00030         $sID    = oxConfig::getParameter( "id" );
00031 
00032         $oNewsletter = oxNew( "oxNewsLetter" );
00033         $oNewsletter->load( $sID );
00034         $oNewsletterGroups = $oNewsletter->getGroups();
00035 
00036         // #493A - fetching cached newsletter info
00037         $oCachedNewsletter = oxSession::getVar( "_oNewsletter" );
00038         if ( isset( $oCachedNewsletter ) ) {
00039             //checking if cached session id is the same as current user session id
00040             if ( $oCachedNewsletter->sID != $sID ) {
00041                 oxSession::deleteVar( "_oNewsletter" );
00042             } else {
00043                 // setting cached values
00044                 $iStart = $oCachedNewsletter->iStart;
00045                 $iUser  = $oCachedNewsletter->iUser;
00046                 $sID    = $oCachedNewsletter->sID;
00047             }
00048         } else {
00049             // setting initial values
00050             $oCachedNewsletter = new oxStdClass();
00051             $oCachedNewsletter->iStart = $iStart;
00052             $oCachedNewsletter->iUser  = $iUser;
00053             $oCachedNewsletter->sID    = $sID;
00054         }
00055 
00056         // send emails....
00057         $oDB = oxDb::getDb();
00058         $sSelectGroups =  " ( oxobject2group.oxgroupsid in ( ";
00059         $blSep = false;
00060         foreach ( $oNewsletterGroups as $sInGroup ) {
00061             $sSearchKey = $sInGroup->oxgroups__oxid->value;
00062             if ( $blSep ) {
00063                 $sSelectGroups .= ",";
00064             }
00065             $sSelectGroups .= $oDB->quote( $sSearchKey );
00066             $blSep = true;
00067         }
00068         $sSelectGroups .= ") )";
00069 
00070         // no group selected
00071         if ( !$blSep ) {
00072             $sSelectGroups = " oxobject2group.oxobjectid is null ";
00073         }
00074 
00075         $sSelect = "select oxnewssubscribed.oxuserid, oxnewssubscribed.oxemail, oxnewssubscribed.oxsal, oxnewssubscribed.oxfname, oxnewssubscribed.oxlname from oxnewssubscribed left join oxobject2group on oxobject2group.oxobjectid = oxnewssubscribed.oxuserid where ( oxobject2group.oxshopid = '".$myConfig->getShopId()."' or oxobject2group.oxshopid is null ) and $sSelectGroups and oxnewssubscribed.oxdboptin = 1 and (not (oxnewssubscribed.oxemailfailed = '1')) group by oxnewssubscribed.oxemail";
00076         $rs = $oDB->selectLimit( $sSelect, $myConfig->getConfigParam( 'iCntofMails' ), $iStart );
00077         ini_set( "session.gc_maxlifetime", 36000 );
00078 
00079         if ( $rs != false && $rs->recordCount() > 0 ) {
00080 
00081             $sShopID = oxSession::getVar( "actshop");
00082             $blLoadAction = $myConfig->getConfigParam( 'bl_perfLoadAktion' );
00083 
00084             while ( !$rs->EOF ) {
00085                 $sUserID = $rs->fields[0];
00086 
00087                 // must check if such user is in DB
00088                 if ( !$oDB->getOne( "select oxid from oxuser where oxid = '$sUserID'" ) ) {
00089                     $sUserID = null;
00090                 }
00091 
00092                 // #559
00093                 if ( !isset( $sUserID ) || !$sUserID ) {
00094                      // there is no user object so we fake one
00095                     $oUser = oxNew( "oxuser" );
00096                     $oUser->oxuser__oxusername = new oxField( $rs->fields[1] );
00097                     $oUser->oxuser__oxsal      = new oxField( $rs->fields[2] );
00098                     $oUser->oxuser__oxfname    = new oxField( $rs->fields[3] );
00099                     $oUser->oxuser__oxlname    = new oxField( $rs->fields[4] );
00100                     $oNewsletter->prepare( $oUser, $blLoadAction );
00101                 } else {
00102                     $oNewsletter->prepare( $sUserID, $blLoadAction );
00103                 }
00104 
00105                 if ( $oNewsletter->send() ) {
00106                      // add user history
00107                     $oRemark = oxNew( "oxremark" );
00108                     $oRemark->oxremark__oxtext     = new oxField( $oNewsletter->sPlainText );
00109                     $oRemark->oxremark__oxparentid = new oxField( $sUserID );
00110                     $oRemark->oxremark__oxshopid   = new oxField( $sShopID );
00111                     $oRemark->save();
00112                     oxSession::setVar( "keepalive", "yes");
00113                 } else {
00114                     $this->_aMailErrors[] = "problem sending to : ".$rs->fields[1]."<br>";
00115                 }
00116 
00117                $rs->moveNext();
00118             }
00119         }
00120 
00121         // adavance mail pointer and set parameter
00122         $iStart += $myConfig->getConfigParam( 'iCntofMails' );
00123 
00124         // #493A - setting new values
00125         $oCachedNewsletter->iStart = $iStart;
00126         $oCachedNewsletter->iUser  = $iUser;
00127         $oCachedNewsletter->sID    = $sID;
00128 
00129         $this->_aViewData["iStart"] =  $iStart;
00130         $this->_aViewData["user"]   =  $iUser;
00131         $this->_aViewData["id"]     =  $sID;
00132 
00133         // end ?
00134         if ( $iStart < $iUser) {
00135             $sPage = "newsletter_send.tpl";
00136             // #493A - saving changes
00137             oxSession::setVar( "_oNewsletter", $oCachedNewsletter );
00138         } else {
00139             $sPage = "newsletter_done.tpl";
00140             // #493A - deleting cache variable
00141             oxSession::deleteVar( "_oNewsletter" );
00142         }
00143 
00144         return $sPage;
00145     }
00146 
00152     public function getMailErrors()
00153     {
00154         return $this->_aMailErrors;
00155     }
00156 
00164     protected function _setupNavigation( $sId )
00165     {
00166         parent::_setupNavigation( 'newsletter_list' );
00167     }
00168 }

Generated by  doxygen 1.6.2