Special == 'module') { $object =& $event->getObject(); $object->addFilter('module_filter', '%1$s.Module = \'In-Commerce\''); } } /** * Sets status Front-End Only to selected email events * * @param kEvent $event */ function OnFrontOnly(&$event) { $this->StoreSelectedIDs($event); $ids = $this->getSelectedIDs($event); $ids = implode(',', $ids); $table = $this->Application->getUnitOption($event->Prefix,'TableName'); $sql = 'UPDATE '.$table.' SET Enabled = 2 WHERE EventId IN ('.$ids.')'; $this->Conn->Query($sql); } /** * Sets selected user to email events selected * * @param kEvent $event */ function OnSelectUser(&$event) { $user_name = $this->Application->GetVar( $event->getPrefixSpecial(true).'_PopupSelectedUser' ); if( strlen($user_name) > 0 ) { $this->StoreSelectedIDs($event); $ids = $this->getSelectedIDs($event); $ids = implode(',', $ids); $user_id = $this->Conn->GetOne('SELECT PortalUserId FROM '.TABLE_PREFIX.'PortalUser WHERE Login = '.$this->Conn->qstr($user_name) ); $table = $this->Application->getUnitOption($event->Prefix,'TableName'); $sql = 'UPDATE '.$table.' SET FromUserId = '.$user_id.' WHERE EventId IN ('.$ids.')'; $this->Conn->Query($sql); } } /** * Raised when email message shoul be sent * * @param kEvent $event */ function OnEmailEvent(&$event){ $email_event = $event->getEventParam('EmailEventName'); $to_user_id = $event->getEventParam('EmailEventToUserId'); $email_event_type = $event->getEventParam('EmailEventType'); $this->Application->setUnitOption('emailmessages', 'AutoLoad', false); $message_object = &$this->Application->recallObject('emailmessages'); $event_table = $this->Application->getUnitOption('emailevents', 'TableName'); $event_object = &$event->getObject(); $event_object->Load(array('Event'=>$email_event, 'Type'=>$email_event_type)); $event_id = $event_object->GetDBField('EventId'); $from_user_id = $event_object->GetDBField('FromUserId'); $type = $event_object->GetDBField('Type'); $enabled = $event_object->GetDBField('Enabled'); $direct_send_params = $event->getEventParam('DirectSendParams'); if ($enabled == 0) return; // disabled event if ($enabled == 2 && defined("ADMIN")) return; // event only for front-end if ($type == 1){ // For type "Admin" recipient is a user from field FromUserId which means From/To user in Email events list $to_user_id = $from_user_id; $from_user_id = -1; } /* if (!($to_user_id > 0) && !$direct_send_params){ // if we can not determine recepient we will not send email return; } */ //Parse Message Template $message_object->Load(array('EventId' => $event_id, 'LanguageId' => $this->Application->GetVar('m_lang'))); $message_type = $message_object->GetDBField('MessageType'); $message_template = $message_object->GetDBField('Template'); $email_object = &$this->Application->recallObject('kEmailMessage'); $email_object->Clear(); $old_autoload = $this->Application->getUnitOption('u', 'AutoLoad'); $this->Application->setUnitOption('u', 'AutoLoad', false); $from_user_object = &$this->Application->recallObject('u.-item'); $from_user_object->Load($from_user_id); // here if we don't have from_user loaded, it takes a default user from config values $from_user_email = $from_user_object->GetDBField('Email')?$from_user_object->GetDBField('Email'):$this->Application->ConfigValue('Smtp_AdminMailFrom'); $from_user_name = trim($from_user_object->GetDBField('FirstName').' '.$from_user_object->GetDBField('LastName')); $to_user_object = &$this->Application->recallObject('u.-item'); $to_user_object->Load($to_user_id); $to_user_email = $to_user_object->GetDBField('Email'); $to_user_name = trim($to_user_object->GetDBField('FirstName').' '.$to_user_object->GetDBField('LastName')); $this->Application->setUnitOption('u', 'AutoLoad', $old_autoload); if($direct_send_params){ $to_user_email = ($direct_send_params['to_email']?$direct_send_params['to_email']:$to_user_email); $to_user_name = ($direct_send_params['to_name']?$direct_send_params['to_name']:$to_user_name); $from_user_email = ($direct_send_params['from_email']?$direct_send_params['from_email']:$from_user_email); $from_user_name = ($direct_send_params['from_name']?$direct_send_params['from_name']:$from_user_name); $message_body_additional = $direct_send_params['message']; } $to_user_email = $to_user_email?$to_user_email:$this->Application->ConfigValue('Smtp_AdminMailFrom'); $this->Application->makeClass('Template'); $this->Application->InitParser(); $parser_params = $this->Application->Parser->Params; $direct_send_params['message_text'] = $message_body_additional; $this->Application->Parser->Params = array_merge_recursive2($this->Application->Parser->Params, $direct_send_params); $message_template = str_replace('Application->Parser->Parse($message_template, 'email_template', 0); $this->Application->Parser->Params = $parser_params; $message_template = str_replace("\r", "", $message_template); list($message_headers, $message_body) = explode("\n\n", $message_template, 2); $email_object->setFrom($from_user_email, $from_user_name); $email_object->setTo($to_user_email, $to_user_name); $email_object->setSubject('Mail message'); $email_object->setHeaders($message_headers); if ($message_type == 'html'){ $email_object->setHTMLBody($message_body); } else { $email_object->setTextBody($message_body); } $smtp_object = &$this->Application->recallObject('kSmtpClient'); $smtp_server = $this->Application->ConfigValue('Smtp_Server'); $smtp_port = $this->Application->ConfigValue('Smtp_Port'); $smtp_authenticate = $this->Application->ConfigValue('Smtp_Authenticate'); if ($smtp_authenticate){ $smtp_user = $this->Application->ConfigValue('Smtp_User'); $smtp_pass = $this->Application->ConfigValue('Smtp_Pass'); }else{ $smtp_user = ''; $smtp_pass = ''; } if ($smtp_server){ if ($email_object->sendSMTP($smtp_object, $smtp_server, $smtp_user, $smtp_pass, $smtp_authenticate)){ $event->status=erSUCCESS; } else { $event->status=erFAIL; } }else{ if($email_object->send()){ $event->status=erSUCCESS; } else { $event->status=erFAIL; } } if ($event->status == erSUCCESS){ if (!$from_user_name) { $from_user_name = $this->Application->ConfigValue('Site_Name'); } $sql = 'INSERT INTO '.TABLE_PREFIX.'EmailLog SET fromuser = '.$this->Conn->qstr($from_user_name.' ('.$from_user_email.')').', addressto = '.$this->Conn->qstr($to_user_name.' ('.$to_user_email.')').', subject = '.$this->Conn->qstr($email_object->Subject.')').', timestamp = UNIX_TIMESTAMP(), event = '.$this->Conn->qstr($email_event); $this->Conn->Query($sql); } return $event; } } ?>