OXID eShop CE  4.8.12
 All Classes Files Functions Variables Pages
oxnewsletter.php
Go to the documentation of this file.
1 <?php
2 
9 class oxNewsletter extends oxBase
10 {
16  protected $_sHtmlText = null;
17 
23  protected $_sPlainText = null;
24 
30  protected $_oGroups = null;
31 
37  protected $_oUser = null;
38 
44  protected $_sClassName = 'oxnewsletter';
45 
50  public function __construct()
51  {
53  $this->init( 'oxnewsletter' );
54  }
55 
63  public function delete( $sOxId = null )
64  {
65  if ( !$sOxId) {
66  $sOxId = $this->getId();
67  }
68  if ( !$sOxId) {
69  return false;
70  }
71 
72  $blDeleted = parent::delete( $sOxId );
73 
74  if ( $blDeleted ) {
75  $oDb = oxDb::getDb();
76  $sDelete = "delete from oxobject2group where oxobject2group.oxshopid = '".$this->getShopId()."' and oxobject2group.oxobjectid = ".$oDb->quote( $sOxId );
77  $oDb->execute( $sDelete );
78  }
79 
80  return $blDeleted;
81  }
82 
88  public function getGroups()
89  {
90  if ( isset( $this->_oGroups ) ) {
91  return $this->_oGroups;
92  }
93 
94  // usergroups
95  $this->_oGroups = oxNew( "oxList", "oxgroups" );
96  $sViewName = getViewName( "oxgroups" );
97 
98  // performance
99  $sSelect = "select {$sViewName}.* from {$sViewName}, oxobject2group
100  where oxobject2group.oxobjectid='".$this->getId()."'
101  and oxobject2group.oxgroupsid={$sViewName}.oxid ";
102  $this->_oGroups->selectString( $sSelect );
103 
104  return $this->_oGroups;
105  }
106 
112  public function getHtmlText()
113  {
114  return $this->_sHtmlText;
115  }
116 
122  public function getPlainText()
123  {
124  return $this->_sPlainText;
125  }
126 
136  public function prepare( $sUserid, $blPerfLoadAktion = false )
137  {
138  // switching off admin
139  $blAdmin = $this->isAdmin();
140  $this->setAdminMode( false );
141 
142  // add currency
143  $this->_setUser( $sUserid );
144  $this->_setParams( $blPerfLoadAktion );
145 
146  // restoring mode ..
147  $this->setAdminMode( $blAdmin );
148  }
149 
157  public function send()
158  {
159  $oxEMail = oxNew( 'oxemail' );
160  $blSend = $oxEMail->sendNewsletterMail( $this, $this->_oUser, $this->oxnewsletter__oxsubject->value );
161 
162  return $blSend;
163  }
164 
174  protected function _setParams( $blPerfLoadAktion = false )
175  {
176  $myConfig = $this->getConfig();
177 
178  $oShop = oxNew( 'oxshop' );
179  $oShop->load( $myConfig->getShopId() );
180 
181  $oView = oxNew( 'oxubase' );
182  $oShop = $oView->addGlobalParams( $oShop );
183 
184  $oView->addTplParam( 'myshop', $oShop );
185  $oView->addTplParam( 'shop', $oShop );
186  $oView->addTplParam( 'oViewConf', $oShop );
187  $oView->addTplParam( 'oView', $oView );
188  $oView->addTplParam( 'mycurrency', $myConfig->getActShopCurrencyObject() );
189  $oView->addTplParam( 'myuser', $this->_oUser );
190 
191  $this->_assignProducts( $oView, $blPerfLoadAktion );
192 
193  $aInput[] = array( $this->getId().'html', $this->oxnewsletter__oxtemplate->value );
194  $aInput[] = array( $this->getId().'plain', $this->oxnewsletter__oxplaintemplate->value );
195  $aRes = oxRegistry::get("oxUtilsView")->parseThroughSmarty( $aInput, null, $oView, true );
196 
197  $this->_sHtmlText = $aRes[0];
198  $this->_sPlainText = $aRes[1];
199  }
200 
208  protected function _setUser( $sUserid )
209  {
210  if ( is_string( $sUserid )) {
211  $oUser = oxNew( 'oxuser' );
212  if ( $oUser->load( $sUserid ) ) {
213  $this->_oUser = $oUser;
214  }
215  } else {
216  $this->_oUser = $sUserid; // we expect a full and valid user object
217  }
218  }
219 
229  protected function _assignProducts( $oView, $blPerfLoadAktion = false )
230  {
231  if ( $blPerfLoadAktion ) {
232  $oArtList = oxNew( 'oxarticlelist' );
233  $oArtList->loadActionArticles( 'OXNEWSLETTER' );
234  $oView->addTplParam( 'articlelist', $oArtList );
235  }
236 
237  if ( $this->_oUser->getId() ) {
238  $oArticle = oxNew( 'oxarticle' );
239  $sArticleTable = $oArticle->getViewName();
240 
241  // add products which fit to the last order of this user
242  $sSelect = "select $sArticleTable.* from oxorder left join oxorderarticles on oxorderarticles.oxorderid = oxorder.oxid";
243  $sSelect .= " left join $sArticleTable on oxorderarticles.oxartid = $sArticleTable.oxid";
244  $sSelect .= " where ".$oArticle->getSqlActiveSnippet();
245  $sSelect .= " and oxorder.oxuserid = '".$this->_oUser->getId()."' order by oxorder.oxorderdate desc limit 1";
246 
247  if ( $oArticle->assignRecord( $sSelect ) ) {
248  $oSimList = $oArticle->getSimilarProducts();
249  if ( $oSimList && $oSimList->count() ) {
250  $oView->addTplParam( 'simlist', $oSimList );
251  $iCnt = 0;
252  foreach ( $oSimList as $oArt ) {
253  $oView->addTplParam( "simarticle$iCnt", $oArt );
254  $iCnt++;
255  }
256  }
257  }
258  }
259  }
260 
270  protected function _setFieldData( $sFieldName, $sValue, $iDataType = oxField::T_TEXT )
271  {
272  if ( 'oxtemplate' === $sFieldName || 'oxplaintemplate' === $sFieldName ) {
273  $iDataType = oxField::T_RAW;
274  }
275  return parent::_setFieldData($sFieldName, $sValue, $iDataType);
276  }
277 }