oxnewsletter.php

Go to the documentation of this file.
00001 <?php
00002 
00009 class oxNewsletter extends oxBase
00010 {
00016     protected $_sHtmlText = null;
00017 
00023     protected $_sPlainText = null;
00024 
00030     protected $_oGroups = null;
00031 
00037     protected $_oUser = null;
00038 
00044     protected $_sClassName = 'oxnewsletter';
00045 
00050     public function __construct()
00051     {
00052         parent::__construct();
00053         $this->init( 'oxnewsletter' );
00054     }
00055 
00063     public function delete( $sOxId = null )
00064     {
00065         if ( !$sOxId) {
00066             $sOxId = $this->getId();
00067         }
00068         if ( !$sOxId) {
00069             return false;
00070         }
00071 
00072         $blDeleted = parent::delete( $sOxId );
00073 
00074         if ( $blDeleted ) {
00075             $oDb = oxDb::getDb();
00076             $sDelete = "delete from oxobject2group where oxobject2group.oxshopid = '".$this->getShopId()."' and oxobject2group.oxobjectid = ".$oDb->quote( $sOxId );
00077             $oDb->execute( $sDelete );
00078         }
00079 
00080         return $blDeleted;
00081     }
00082 
00088     public function getGroups()
00089     {
00090         if ( isset( $this->_oGroups ) ) {
00091             return $this->_oGroups;
00092         }
00093 
00094         // usergroups
00095         $this->_oGroups = oxNew( "oxList", "oxgroups" );
00096         $sViewName = getViewName( "oxgroups" );
00097 
00098         // performance
00099         $sSelect = "select {$sViewName}.* from {$sViewName}, oxobject2group
00100                     where oxobject2group.oxobjectid='".$this->getId()."'
00101                     and oxobject2group.oxgroupsid={$sViewName}.oxid ";
00102         $this->_oGroups->selectString( $sSelect );
00103 
00104         return $this->_oGroups;
00105     }
00106 
00112     public function getHtmlText()
00113     {
00114         return $this->_sHtmlText;
00115     }
00116 
00122     public function getPlainText()
00123     {
00124         return $this->_sPlainText;
00125     }
00126 
00136     public function prepare( $sUserid, $blPerfLoadAktion = false )
00137     {
00138         // switching off admin
00139         $blAdmin = $this->isAdmin();
00140         $this->setAdminMode( false );
00141 
00142         // add currency
00143         $this->_setUser( $sUserid );
00144         $this->_setParams( $blPerfLoadAktion );
00145 
00146         // restoring mode ..
00147         $this->setAdminMode( $blAdmin );
00148     }
00149 
00157     public function send()
00158     {
00159         $oxEMail = oxNew( 'oxemail' );
00160         $blSend = $oxEMail->sendNewsletterMail( $this, $this->_oUser, $this->oxnewsletter__oxsubject->value );
00161 
00162         return $blSend;
00163     }
00164 
00174     protected function _setParams( $blPerfLoadAktion = false )
00175     {
00176         $myConfig = $this->getConfig();
00177 
00178         $oShop = oxNew( 'oxshop' );
00179         $oShop->load( $myConfig->getShopId() );
00180 
00181         $oView = oxNew( 'oxubase' );
00182         $oShop = $oView->addGlobalParams( $oShop );
00183 
00184         $oView->addTplParam( 'myshop', $oShop );
00185         $oView->addTplParam( 'shop', $oShop );
00186         $oView->addTplParam( 'oViewConf', $oShop );
00187         $oView->addTplParam( 'oView', $oView );
00188         $oView->addTplParam( 'mycurrency', $myConfig->getActShopCurrencyObject() );
00189         $oView->addTplParam( 'myuser', $this->_oUser );
00190 
00191         $this->_assignProducts( $oView, $blPerfLoadAktion );
00192 
00193         $aInput[] = array( $this->getId().'html', $this->oxnewsletter__oxtemplate->value );
00194         $aInput[] = array( $this->getId().'plain', $this->oxnewsletter__oxplaintemplate->value );
00195         $aRes = oxRegistry::get("oxUtilsView")->parseThroughSmarty( $aInput, null, $oView, true );
00196 
00197         $this->_sHtmlText  = $aRes[0];
00198         $this->_sPlainText = $aRes[1];
00199     }
00200 
00208     protected function _setUser( $sUserid )
00209     {
00210         if ( is_string( $sUserid )) {
00211             $oUser = oxNew( 'oxuser' );
00212             if ( $oUser->load( $sUserid ) ) {
00213                 $this->_oUser = $oUser;
00214             }
00215         } else {
00216             $this->_oUser = $sUserid;   // we expect a full and valid user object
00217         }
00218     }
00219 
00229     protected function _assignProducts( $oView, $blPerfLoadAktion = false )
00230     {
00231         if ( $blPerfLoadAktion ) {
00232             $oArtList = oxNew( 'oxarticlelist' );
00233             $oArtList->loadActionArticles( 'OXNEWSLETTER' );
00234             $oView->addTplParam( 'articlelist', $oArtList );
00235         }
00236 
00237         if ( $this->_oUser->getId() ) {
00238             $oArticle = oxNew( 'oxarticle' );
00239             $sArticleTable = $oArticle->getViewName();
00240 
00241             // add products which fit to the last order of this user
00242             $sSelect  = "select $sArticleTable.* from oxorder left join oxorderarticles on oxorderarticles.oxorderid = oxorder.oxid";
00243             $sSelect .= " left join $sArticleTable on oxorderarticles.oxartid = $sArticleTable.oxid";
00244             $sSelect .= " where ".$oArticle->getSqlActiveSnippet();
00245             $sSelect .= " and oxorder.oxuserid = '".$this->_oUser->getId()."' order by oxorder.oxorderdate desc limit 1";
00246 
00247             if ( $oArticle->assignRecord( $sSelect ) ) {
00248                 $oSimList = $oArticle->getSimilarProducts();
00249                 if ( $oSimList && $oSimList->count() ) {
00250                     $oView->addTplParam( 'simlist', $oSimList );
00251                     $iCnt = 0;
00252                     foreach ( $oSimList as $oArt ) {
00253                         $oView->addTplParam( "simarticle$iCnt", $oArt );
00254                         $iCnt++;
00255                     }
00256                 }
00257             }
00258         }
00259     }
00260 
00270     protected function _setFieldData( $sFieldName, $sValue, $iDataType = oxField::T_TEXT )
00271     {
00272         if ( 'oxtemplate' === $sFieldName || 'oxplaintemplate' === $sFieldName ) {
00273             $iDataType = oxField::T_RAW;
00274         }
00275         return parent::_setFieldData($sFieldName, $sValue, $iDataType);
00276     }
00277 }