Index: trunk/kernel/units/email_events/email_events_event_handler.php =================================================================== diff -u -N --- trunk/kernel/units/email_events/email_events_event_handler.php (revision 6625) +++ trunk/kernel/units/email_events/email_events_event_handler.php (revision 0) @@ -1,271 +0,0 @@ - Array('self' => 'edit'), - 'OnSaveSelected' => Array('self' => 'view'), - ); - $this->permMapping = array_merge($this->permMapping, $permissions); - } - - /** - * Changes permission section to one from REQUEST, not from config - * - * @param kEvent $event - */ - function CheckPermission(&$event) - { - $module = $this->Application->GetVar('module'); - $module = explode(':', $module, 2); - - if (count($module) == 1) { - $main_prefix = $this->Application->findModule('Name', $module[0], 'Var'); - } - else { - $exceptions = Array('Category' => 'c', 'Users' => 'u'); - $main_prefix = $exceptions[ $module[1] ]; - } - $section = $this->Application->getUnitOption($main_prefix.'.email', 'PermSection'); - - $event->setEventParam('PermSection', $section); - return parent::CheckPermission($event); - } - - /** - * Apply any custom changes to list's sql query - * - * @param kEvent $event - * @access protected - * @see OnListBuild - */ - function SetCustomQuery(&$event) - { - if ($event->Special == 'module') { - $object =& $event->getObject(); - $module = $this->Application->GetVar('module'); - $object->addFilter('module_filter', '%1$s.Module = '.$this->Conn->qstr($module)); - } - } - - /** - * Sets status Front-End Only to selected email events - * - * @param kEvent $event - */ - function OnFrontOnly(&$event) - { - $ids = implode(',', $this->StoreSelectedIDs($event)); - - $table_name = $this->Application->getUnitOption($event->Prefix, 'TableName'); - $sql = 'UPDATE '.$table_name.' - SET Enabled = 2 - WHERE EventId IN ('.$ids.')'; - $this->Conn->Query($sql); - } - - /** - * Sets selected user to email events selected - * - * @param kEvent $event - */ - function OnSelectUser(&$event) - { - $items_info = $this->Application->GetVar('u'); - if ($items_info) { - $user_id = array_shift( array_keys($items_info) ); - - $ids = $this->Application->RecallVar($event->getPrefixSpecial().'_selected_ids'); - $id_field = $this->Application->getUnitOption($event->Prefix, 'IDField'); - $table_name = $this->Application->getUnitOption($event->Prefix, 'TableName'); - $sql = 'UPDATE '.$table_name.' - SET '.$this->Application->RecallVar('dst_field').' = '.$user_id.' - WHERE '.$id_field.' IN ('.$ids.')'; - $this->Conn->Query($sql); - } - - $this->finalizePopup($event); - } - - /** - * Saves selected ids to session - * - * @param kEvent $event - */ - function OnSaveSelected(&$event) - { - $this->StoreSelectedIDs($event); - } - - /** - * Raised when email message shoul be sent - * - * @param kEvent $event - */ - function OnEmailEvent(&$event){ - - $email_event = $event->getEventParam('EmailEventName'); - if( strpos($email_event, '_') !== false ) - { - trigger_error('Invalid email event name '.$email_event.'. Use only UPPERCASE characters and dots as email event names', E_USER_ERROR); - } - - $to_user_id = $event->getEventParam('EmailEventToUserId'); - $email_event_type = $event->getEventParam('EmailEventType'); - - $message_object = &$this->Application->recallObject('emailmessages', null, Array('skip_autoload' => true)); - $event_table = $this->Application->getUnitOption('emailevents', 'TableName'); - - $event_object =& $event->getObject( Array('skip_autoload' => true) ); - $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 && $this->Application->IsAdmin() ) 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'); - - $email_object = &$this->Application->recallObject('kEmailMessage'); - $email_object->Clear(); - - // add footer: begin - $sql = 'SELECT em.Template - FROM '.$message_object->TableName.' em - LEFT JOIN '.TABLE_PREFIX.'Events e ON e.EventId = em.EventId - WHERE em.LanguageId = '.$message_object->GetDBField('LanguageId').' AND e.Event = "COMMON.FOOTER"'; - $footer = explode("\n\n", $this->Conn->GetOne($sql)); - $footer = $message_object->GetDBField('MessageType') == 'text' ? $email_object->convertHTMLtoPlain($footer[1]) : $footer[1]; - $message_template = $message_object->GetDBField('Template')."\r\n".$footer; - // add footer: end - - $from_user_object = &$this->Application->recallObject('u.email-from', null, Array('skip_autoload' => true)); - $from_user_object->Load($from_user_id); - // here if we don't have from_user loaded, it takes a default user from config values - if ( $from_user_object->IsLoaded() ) { - $from_user_email = $from_user_object->GetDBField('Email'); - $from_user_name = trim($from_user_object->GetDBField('FirstName').' '.$from_user_object->GetDBField('LastName')); - } - else { - $from_user_email = $this->Application->ConfigValue('Smtp_AdminMailFrom'); - } - - $to_user_object = &$this->Application->recallObject('u.email-to', null, Array('skip_autoload' => true)); - $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')); - - 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_object->debug = $this->Application->isDebugMode() && constOn('DBG_SMTP'); - - $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 = strip_tags( $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); - } - - $this->Application->removeObject('u.email-from'); - $this->Application->removeObject('u.email-to'); - return $event; - } - } -?> \ No newline at end of file