invite.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class Invite extends oxUBase
00009 {
00014     protected $_sThisTemplate = 'invite.tpl';
00015 
00020     protected $_aReqFields = array( 'rec_email', 'send_name', 'send_email', 'send_message', 'send_subject' );
00021 
00026     protected $_oCrossSelling = null;
00027 
00032     protected $_oSimilarProducts = null;
00033 
00038     protected $_oRecommList = null;
00039 
00044     protected $_aInviteData = null;
00045 
00050     protected $_oCaptcha = null;
00051 
00056     protected $_iMailStatus = null;
00057 
00058 
00071     public function render()
00072     {
00073         parent::render();
00074 
00075         //getting captcha
00076         $this->_aViewData['oCaptcha'] = $this->getCaptcha();
00077 
00078         $this->_aViewData['editval'] = $this->getInviteData();
00079 
00080         //checking if email was sent
00081         if ( $this->_iMailStatus == 1 ) {
00082             $this->_aViewData['success'] = true;
00083         }
00084 
00085         return $this->_sThisTemplate;
00086     }
00087 
00097     public function send()
00098     {
00099         $aParams = oxConfig::getParameter( 'editval', true );
00100         if ( !is_array( $aParams ) ) {
00101             return;
00102         }
00103 
00104         // storing used written values
00105         $oParams = (object) $aParams;
00106         $this->setInviteData( (object) oxConfig::getParameter( 'editval' ) );
00107 
00108         // spam spider prevension
00109         $sMac     = oxConfig::getParameter( 'c_mac' );
00110         $sMacHash = oxConfig::getParameter( 'c_mach' );
00111         $oCaptcha = oxNew('oxCaptcha');
00112 
00113         if ( !$oCaptcha->pass($sMac, $sMacHash ) ) {
00114             // even if there is no exception, use this as a default display method
00115             oxUtilsView::getInstance()->addErrorToDisplay( 'EXCEPTION_INPUT_NOTALLFIELDS' );
00116             return;
00117         }
00118 
00119         $oUtilsView = oxUtilsView::getInstance();
00120 
00121         // filled not all fields ?
00122         foreach ( $this->_aReqFields as $sFieldName ) {
00123             //checking if any email was entered
00124             if ( $sFieldName == "rec_email" ) {
00125                 foreach ( $aParams[$sFieldName] as $sKey => $sEmail ) {
00126                     //removing empty emails fields from eMails array
00127                     if ( empty( $sEmail ) ) {
00128                         unset( $aParams[$sFieldName][$sKey] );
00129                     }
00130                 }
00131 
00132                 //counting entered eMails
00133                 if ( count( $aParams[$sFieldName] ) < 1 ) {
00134                     $oUtilsView->addErrorToDisplay('INVITE_COMLETECORRECTLYFIELDS');
00135                     return;
00136                 }
00137 
00138                 //updating values object
00139                 $oParams->rec_email = $aParams[$sFieldName];
00140             }
00141 
00142             if ( !isset( $aParams[$sFieldName] ) || !$aParams[$sFieldName] ) {
00143                 $oUtilsView->addErrorToDisplay('INVITE_COMLETECORRECTLYFIELDS');
00144                 return;
00145             }
00146         }
00147 
00148         $oUtils = oxUtils::getInstance();
00149 
00150         //validating entered emails
00151         foreach ( $aParams["rec_email"] as $sRecipientEmail ) {
00152             if ( !$oUtils->isValidEmail( $sRecipientEmail ) ) {
00153                 $oUtilsView->addErrorToDisplay('INVITE_INCORRECTEMAILADDRESS');
00154                 return;
00155             }
00156         }
00157 
00158         if ( !$oUtils->isValidEmail( $aParams["send_email"] ) ) {
00159             $oUtilsView->addErrorToDisplay('INVITE_INCORRECTEMAILADDRESS');
00160             return;
00161         }
00162 
00163         // sending invite email
00164         $oEmail = oxNew( 'oxemail' );
00165 
00166         if ( $oEmail->sendInviteMail( $oParams ) ) {
00167             $this->_iMailStatus = 1;
00168 
00169             //getting active user
00170             $oUser = $this->getUser();
00171 
00172             //saving statitics for sended emails
00173             if ( $oUser ) {
00174                 $this->_updateStatistics( $oUser->getId(), $aParams["rec_email"] );
00175             }
00176 
00177         } else {
00178             oxUtilsView::getInstance()->addErrorToDisplay('INVITE_ERRORWHILESENDINGMAIL');
00179         }
00180     }
00181 
00189     public function setInviteData( $oData )
00190     {
00191         $this->_aInviteData = $oData;
00192     }
00193 
00199     public function getInviteData()
00200     {
00201         return $this->_aInviteData;
00202     }
00203 
00209     public function getCaptcha()
00210     {
00211         if ( $this->_oCaptcha === null ) {
00212             $this->_oCaptcha = oxNew('oxCaptcha');
00213         }
00214         return $this->_oCaptcha;
00215     }
00216 
00225     protected function _updateStatistics( $sUserId, $aRecEmail )
00226     {
00227         $oDb = oxDb::getDb( true );
00228 
00229         if ( $sUserId && is_array( $aRecEmail ) && count( $aRecEmail ) > 0 ) {
00230             //iserting statistics about invitation
00231             $sDate = oxUtilsDate::getInstance()->formatDBDate( date("Y-m-d"), true );
00232             $aRecEmail = oxDb::getInstance()->quoteArray( $aRecEmail );
00233             foreach ( $aRecEmail as $sRecEmail ) {
00234                 $sSql = " INSERT INTO oxinvitations SET oxuserid = '$sUserId', oxemail = $sRecEmail,  oxdate='$sDate', oxpending = '1', oxaccepted = '0', oxtype = '1' ";
00235                 $oDb->execute( $sSql );
00236             }
00237         }
00238     }
00239 
00240 
00241 }