OXID eShop CE  4.8.10
 All Classes Files Functions Variables Pages
newsletter_send.php
Go to the documentation of this file.
1 <?php
2 
8 {
14  protected $_aMailErrors = array();
15 
23  public function render()
24  {
26 
27  // calculating
28  $iUserCount = $this->getUserCount();
29 
30  $iStart = (int) oxConfig::getParameter( "iStart" );
31 
32  $oNewsletter = oxNew( "oxNewsLetter" );
33  $oNewsletter->load( $this->getEditObjectId() );
34  $oNewsletterGroups = $oNewsletter->getGroups();
35 
36  // send emails....
37  $oDB = oxDb::getDb( oxDB::FETCH_MODE_ASSOC );
38  $sQGroups = " ( oxobject2group.oxgroupsid in ( ";
39  $blSep = false;
40  foreach ( $oNewsletterGroups as $sInGroup ) {
41  $sSearchKey = $sInGroup->oxgroups__oxid->value;
42  if ( $blSep ) {
43  $sQGroups .= ",";
44  }
45  $sQGroups .= $oDB->quote( $sSearchKey );
46  $blSep = true;
47  }
48  $sQGroups .= ") )";
49 
50  // no group selected
51  if ( !$blSep ) {
52  $sQGroups = " oxobject2group.oxobjectid is null ";
53  }
54 
55  $myConfig = $this->getConfig();
56 
57  $iSendCnt = 0;
58  $iMaxCnt = (int) $myConfig->getConfigParam( 'iCntofMails' );
59  $sShopId = $myConfig->getShopId();
60 
61  $sQ = "select oxnewssubscribed.oxuserid, oxnewssubscribed.oxemail, oxnewssubscribed.oxsal,
62  oxnewssubscribed.oxfname, oxnewssubscribed.oxlname, oxnewssubscribed.oxemailfailed
63  from oxnewssubscribed left join oxobject2group on
64  oxobject2group.oxobjectid = oxnewssubscribed.oxuserid where
65  ( oxobject2group.oxshopid = '{$sShopId}' or oxobject2group.oxshopid is null ) and
66  $sQGroups and oxnewssubscribed.oxdboptin = 1 and oxnewssubscribed.oxshopid = '{$sShopId}'
67  group by oxnewssubscribed.oxemail";
68 
69  $oRs = $oDB->selectLimit( $sQ, 100, $iStart );
70  $blContinue = ( $oRs != false && $oRs->recordCount() > 0 );
71 
72  if ( $blContinue ) {
73  $blLoadAction = $myConfig->getConfigParam( 'bl_perfLoadAktion' );
74  while ( !$oRs->EOF && $iSendCnt < $iMaxCnt ) {
75 
76  if ( $oRs->fields['oxemailfailed'] != "1" ) {
77  $sUserId = $oRs->fields['oxuserid'];
78  $iSendCnt++;
79 
80  // must check if such user is in DB
81  if ( !$oDB->getOne( "select oxid from oxuser where oxid = ".$oDB->quote( $sUserId ), false, false ) ) {
82  $sUserId = null;
83  }
84 
85  // #559
86  if ( !isset( $sUserId ) || !$sUserId ) {
87  // there is no user object so we fake one
88  $oUser = oxNew( "oxuser" );
89  $oUser->oxuser__oxusername = new oxField( $oRs->fields['oxemail'] );
90  $oUser->oxuser__oxsal = new oxField( $oRs->fields['oxsal'] );
91  $oUser->oxuser__oxfname = new oxField( $oRs->fields['oxfname'] );
92  $oUser->oxuser__oxlname = new oxField( $oRs->fields['oxlname'] );
93  $oNewsletter->prepare( $oUser, $blLoadAction );
94  } else {
95  $oNewsletter->prepare( $sUserId, $blLoadAction );
96  }
97 
98  if ( $oNewsletter->send( $iSendCnt ) ) {
99  // add user history
100  $oRemark = oxNew( "oxremark" );
101  $oRemark->oxremark__oxtext = new oxField( $oNewsletter->getPlainText() );
102  $oRemark->oxremark__oxparentid = new oxField( $sUserId );
103  $oRemark->oxremark__oxshopid = new oxField( $sShopId );
104  $oRemark->oxremark__oxtype = new oxField( "n" );
105  $oRemark->save();
106  } else {
107  $this->_aMailErrors[] = "problem sending to : ".$oRs->fields['oxemail'];
108  }
109  }
110 
111  $oRs->moveNext();
112  $iStart++;
113  }
114  }
115 
116  $iSend = $iSendCnt + ( ceil( $iStart / $iMaxCnt ) - 1 ) * $iMaxCnt;
117  $iSend = $iSend > $iUserCount ? $iUserCount : $iSend;
118 
119  $this->_aViewData["iStart"] = $iStart;
120  $this->_aViewData["iSend"] = $iSend;
121 
122  // end ?
123  if ( $blContinue ) {
124  return "newsletter_send.tpl";
125  } else {
126  $this->resetUserCount();
127  return "newsletter_done.tpl";
128  }
129  }
130 
136  public function getUserCount()
137  {
138  $iCnt = oxSession::getVar( "iUserCount" );
139  if ( $iCnt === null ) {
140  $iCnt = parent::getUserCount();
141  oxSession::setVar( "iUserCount", $iCnt );
142  }
143  return $iCnt;
144  }
145 
151  public function resetUserCount()
152  {
153  oxSession::deleteVar( "iUserCount" );
154  $this->_iUserCount = null;
155  }
156 
162  public function getMailErrors()
163  {
164  return $this->_aMailErrors;
165  }
166 
174  protected function _setupNavigation( $sNode )
175  {
176  $sNode = 'newsletter_list';
177 
178  $myAdminNavig = $this->getNavigation();
179 
180  // active tab
181  $iActTab = 3;
182 
183  // tabs
184  $this->_aViewData['editnavi'] = $myAdminNavig->getTabs( $sNode, $iActTab );
185 
186  // active tab
187  $this->_aViewData['actlocation'] = $myAdminNavig->getActiveTab( $sNode, $iActTab );
188 
189  // default tab
190  $this->_aViewData['default_edit'] = $myAdminNavig->getActiveTab( $sNode, $this->_iDefEdit );
191 
192  // passign active tab number
193  $this->_aViewData['actedit'] = $iActTab;
194  }
195 
201  public function getListSorting()
202  {
203  }
204 }