Index: branches/5.2.x/core/units/email_events/email_events_event_handler.php =================================================================== diff -u -N -r15215 -r15222 --- branches/5.2.x/core/units/email_events/email_events_event_handler.php (.../email_events_event_handler.php) (revision 15215) +++ branches/5.2.x/core/units/email_events/email_events_event_handler.php (.../email_events_event_handler.php) (revision 15222) @@ -1,6 +1,6 @@ SetDBField('Headers', $this->Application->ConfigValue('Smtp_DefaultHeaders')); + $this->setRequired($event); } /** @@ -152,17 +153,17 @@ */ function OnFrontOnly($event) { - if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) { + if ( $this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1) ) { $event->status = kEvent::erFAIL; - return ; + return; } $ids = implode(',', $this->StoreSelectedIDs($event)); $table_name = $this->Application->getUnitOption($event->Prefix, 'TableName'); - $sql = 'UPDATE '.$table_name.' + $sql = 'UPDATE ' . $table_name . ' SET FrontEndOnly = 1 - WHERE EventId IN ('.$ids.')'; + WHERE EventId IN (' . $ids . ')'; $this->Conn->Query($sql); $this->clearSelectedIDs($event); @@ -225,58 +226,58 @@ function OnProcessEmailQueue($event) { $deliver_count = $event->getEventParam('deliver_count'); - if ($deliver_count === false) { + if ( $deliver_count === false ) { $deliver_count = $this->Application->ConfigValue('MailingListSendPerStep'); - if ($deliver_count === false) { + if ( $deliver_count === false ) { $deliver_count = 10; // 10 emails per script run (if not specified directly) } } $processing_type = $this->Application->GetVar('type'); - if ($processing_type = 'return_progress') { + if ( $processing_type = 'return_progress' ) { $email_queue_progress = $this->Application->RecallVar('email_queue_progress'); - if ($email_queue_progress === false) { + if ( $email_queue_progress === false ) { $emails_sent = 0; $sql = 'SELECT COUNT(*) FROM ' . TABLE_PREFIX . 'EmailQueue WHERE (SendRetries < 5) AND (LastSendRetry < ' . strtotime('-2 hours') . ')'; $total_emails = $this->Conn->GetOne($sql); - $this->Application->StoreVar('email_queue_progress', $emails_sent.':'.$total_emails); + $this->Application->StoreVar('email_queue_progress', $emails_sent . ':' . $total_emails); } else { list ($emails_sent, $total_emails) = explode(':', $email_queue_progress); } } $sql = 'SELECT * - FROM '.TABLE_PREFIX.'EmailQueue + FROM ' . TABLE_PREFIX . 'EmailQueue WHERE (SendRetries < 5) AND (LastSendRetry < ' . strtotime('-2 hours') . ') LIMIT 0,' . $deliver_count; $messages = $this->Conn->Query($sql); $message_count = count($messages); - if (!$message_count) { + if ( !$message_count ) { // no messages left to send in queue - if ($processing_type = 'return_progress') { + if ( $processing_type = 'return_progress' ) { $this->Application->RemoveVar('email_queue_progress'); $this->Application->Redirect($this->Application->GetVar('finish_template')); } - return ; + return; } $mailing_list_helper = $this->Application->recallObject('MailingListHelper'); /* @var $mailing_list_helper MailingListHelper */ $mailing_list_helper->processQueue($messages); - if ($processing_type = 'return_progress') { + if ( $processing_type = 'return_progress' ) { $emails_sent += $message_count; - if ($emails_sent >= $total_emails) { + if ( $emails_sent >= $total_emails ) { $this->Application->RemoveVar('email_queue_progress'); $this->Application->Redirect($this->Application->GetVar('finish_template')); } - $this->Application->StoreVar('email_queue_progress', $emails_sent.':'.$total_emails); + $this->Application->StoreVar('email_queue_progress', $emails_sent . ':' . $total_emails); $event->status = kEvent::erSTOP; echo ($emails_sent / $total_emails) * 100; } @@ -296,7 +297,7 @@ $options = Array (); foreach ($this->Application->ModuleInfo as $module_name => $module_info) { - if ($module_name == 'In-Portal') { + if ( $module_name == 'In-Portal' ) { continue; } @@ -307,7 +308,7 @@ $fields['Module']['options'] = $options; $this->Application->setUnitOption($event->Prefix, 'Fields', $fields); - if ($this->Application->GetVar('regional')) { + if ( $this->Application->GetVar('regional') ) { $this->Application->setUnitOption($event->Prefix, 'PopulateMlFields', true); } } @@ -342,8 +343,8 @@ $object = $event->getObject(); /* @var $object kDBItem */ - if (!$this->Application->isDebugMode(false)) { - if ($object->GetDBField('AllowChangingRecipient')) { + if ( !$this->Application->isDebugMode(false) ) { + if ( $object->GetDBField('AllowChangingRecipient') ) { $object->SetDBField('RecipientType', EmailEvent::RECIPIENT_TYPE_TO); } else { @@ -365,6 +366,8 @@ $xml = $minput_helper->prepareMInputXML($records, Array ('Tag', 'Replacement')); $object->SetDBField('ReplacementTagsXML', $xml); + + $this->setRequired($event); } /** @@ -381,28 +384,28 @@ $this->_validateEmailTemplate($object); // validate sender and recipient addresses - if ($object->GetDBField('CustomSender')) { + if ( $object->GetDBField('CustomSender') ) { $this->_validateAddress($event, 'Sender'); } $this->_validateAddress($event, 'Recipient'); - if (!$this->Application->isDebugMode(false)) { + if ( !$this->Application->isDebugMode(false) ) { // only allow to enable/disable event while in debug mode $to_restore = Array ('Enabled', 'AllowChangingSender', 'AllowChangingRecipient'); - if (!$object->GetOriginalField('AllowChangingSender')) { + if ( !$object->GetOriginalField('AllowChangingSender') ) { $to_restore = array_merge($to_restore, Array ('CustomSender', 'SenderName', 'SenderAddressType', 'SenderAddress')); } - if (!$object->GetOriginalField('AllowChangingRecipient')) { - $to_restore = array_merge($to_restore, Array ('CustomRecipient'/*, 'Recipients'*/)); + if ( !$object->GetOriginalField('AllowChangingRecipient') ) { + $to_restore = array_merge($to_restore, Array ('CustomRecipient' /*, 'Recipients'*/)); } // prevent specific fields from editing foreach ($to_restore as $restore_field) { $original_value = $object->GetOriginalField($restore_field); - if ($object->GetDBField($restore_field) != $original_value) { + if ( $object->GetDBField($restore_field) != $original_value ) { $object->SetDBField($restore_field, $original_value); } } @@ -414,10 +417,10 @@ /* @var $minput_helper MInputHelper */ $replacement_tags = Array (); - $records = $minput_helper->parseMInputXML( $object->GetDBField('ReplacementTagsXML') ); + $records = $minput_helper->parseMInputXML($object->GetDBField('ReplacementTagsXML')); foreach ($records as $record) { - $replacement_tags[ trim($record['Tag']) ] = trim($record['Replacement']); + $replacement_tags[trim($record['Tag'])] = trim($record['Replacement']); } $object->SetDBField('ReplacementTags', $replacement_tags ? serialize($replacement_tags) : NULL); @@ -427,18 +430,38 @@ $object->SetDBField('LastChanged_date', TIMENOW); $object->SetDBField('LastChanged_time', TIMENOW); } + + $this->setRequired($event); } /** + * Dynamically changes required fields + * + * @param kEvent $event + * @return void + * @access protected + */ + protected function setRequired(kEvent $event) + { + $object = $event->getObject(); + /* @var $object kDBItem */ + + $language_prefix = 'l' . $this->Application->GetVar('m_lang') . '_'; + + $object->setRequired($language_prefix . 'HtmlBody', !$object->GetField('PlainTextBody')); + $object->setRequired($language_prefix . 'PlainTextBody', !$object->GetField('HtmlBody')); + } + + /** * Checks, that at least one of phrase's translations was changed * * @param kDBItem $object * @return bool */ - function translationChanged(&$object) + function translationChanged($object) { - $changed_fields = array_keys( $object->GetChangedFields() ); - $translation_fields = Array ('Subject', 'Body'); + $changed_fields = array_keys($object->GetChangedFields()); + $translation_fields = Array ('Subject', 'HtmlBody', 'PlainTextBody'); foreach ($changed_fields as $changed_field) { $changed_field = preg_replace('/^l[\d]+_/', '', $changed_field); @@ -466,14 +489,14 @@ $object->setRequired($field_prefix . 'Address', $address_type > 0); $address = $object->GetDBField($field_prefix . 'Address'); - if (!$address) { + if ( !$address ) { // don't validate against empty address - return ; + return; } switch ($address_type) { case EmailEvent::ADDRESS_TYPE_EMAIL: - if (!preg_match('/^(' . REGEX_EMAIL_USER . '@' . REGEX_EMAIL_DOMAIN . ')$/i', $address)) { + if ( !preg_match('/^(' . REGEX_EMAIL_USER . '@' . REGEX_EMAIL_DOMAIN . ')$/i', $address) ) { $object->SetError($field_prefix . 'Address', 'invalid_email'); } break; @@ -482,7 +505,7 @@ $sql = 'SELECT PortalUserId FROM ' . TABLE_PREFIX . 'Users WHERE Username = ' . $this->Conn->qstr($address); - if (!$this->Conn->GetOne($sql)) { + if ( !$this->Conn->GetOne($sql) ) { $object->SetError($field_prefix . 'Address', 'invalid_user'); } break; @@ -491,7 +514,7 @@ $sql = 'SELECT GroupId FROM ' . TABLE_PREFIX . 'UserGroups WHERE Name = ' . $this->Conn->qstr($address); - if (!$this->Conn->GetOne($sql)) { + if ( !$this->Conn->GetOne($sql) ) { $object->SetError($field_prefix . 'Address', 'invalid_group'); } break; @@ -589,21 +612,26 @@ /** * Validates subject and body fields of Email template + * * @param kDBItem $object + * @return void + * @access protected */ - function _validateEmailTemplate(&$object) + protected function _validateEmailTemplate($object) { $this->parseField($object, 'Subject'); - $this->parseField($object, 'Body'); + $this->parseField($object, 'HtmlBody'); + $this->parseField($object, 'PlainTextBody'); } /** * Parses contents of given object field and sets error, when invalid in-portal tags found * @param kDBItem $object * @param string $field * @return void + * @access protected */ - function parseField(&$object, $field) + protected function parseField($object, $field) { $this->Application->InitParser();