Index: branches/5.1.x/core/units/forms/form_submissions/form_submissions_eh.php =================================================================== diff -u -N -r13823 -r13824 --- branches/5.1.x/core/units/forms/form_submissions/form_submissions_eh.php (.../form_submissions_eh.php) (revision 13823) +++ branches/5.1.x/core/units/forms/form_submissions/form_submissions_eh.php (.../form_submissions_eh.php) (revision 13824) @@ -1,6 +1,6 @@ SetDBField('IPAddress', $_SERVER['REMOTE_ADDR']); - $object->SetDBField('ReferrerURL', $_SERVER['HTTP_REFERER']); + if ( !$object->GetDBField('ReferrerURL') ) { + $base_url = preg_quote($this->Application->BaseURL(), '/'); + $referrer = preg_replace('/^' . $base_url . '/', '/', $_SERVER['HTTP_REFERER'], 1); + + $object->SetDBField('ReferrerURL', $referrer); + } + $form_submission_helper =& $this->Application->recallObject('FormSubmissionHelper'); /* @var $form_submission_helper FormSubmissionHelper */ @@ -238,6 +244,21 @@ } /** + * Checks, that target submission was selected for merging + * + * @param kEvent $event + */ + function OnBeforeItemUpdate(&$event) + { + parent::OnBeforeItemUpdate($event); + + $object =& $event->getObject(); + /* @var $object kDBItem */ + + $object->setRequired('MergeToSubmission', $object->GetDBField('IsMergeToSubmission')); + } + + /** * Passes form_id, when using "Prev"/"Next" toolbar buttons * * @param kEvent $event @@ -289,4 +310,165 @@ $event->SetRedirectParam('pass', 'm,form,' . $event->getPrefixSpecial()); } + + /** + * Fills merge-to dropdown + * + * @param kEvent $event + */ + function OnAfterItemLoad(&$event) + { + parent::OnAfterItemLoad($event); + + if ($event->Special == 'merge-to') { + return ; + } + + $object =& $event->getObject(); + /* @var $object kDBItem */ + + $form_id = $object->GetDBField('FormId'); + $email_field = $this->getFieldByRole($form_id, EMAIL_COMMUNICATION_ROLE_EMAIL); + + if (!$email_field) { + return ; + } + + $merge_to =& $this->Application->recallObject($event->Prefix . '.merge-to', null, Array ('skip_autoload' => true)); + /* @var $merge_to kDBItem */ + + $sql = $merge_to->GetSelectSQL() . ' WHERE (FormId = ' . $form_id . ') AND (' . $email_field . ' = ' . $this->Conn->qstr( $object->GetDBField($email_field) ) . ')'; + $submissions = $this->Conn->Query($sql, $object->IDField); + + // remove this submission + unset($submissions[ $object->GetID() ]); + + if (!$submissions) { + return ; + } + + $options = Array (); + + $name_field = $this->getFieldByRole($form_id, EMAIL_COMMUNICATION_ROLE_NAME); + $subject_field = $this->getFieldByRole($form_id, EMAIL_COMMUNICATION_ROLE_SUBJECT); + + $language =& $this->Application->recallObject('lang.current'); + /* @var $language kDBItem */ + + $date_format = $language->GetDBField('DateFormat'); + + foreach ($submissions as $submission_id => $submission_data) { + $option_title = ''; // SenderName (email@address.com) - Subject (06/29/2010) + $merge_to->LoadFromHash($submission_data); + + if ($name_field) { + $option_title = $merge_to->GetDBField($name_field) . ' (' . $merge_to->GetDBField($email_field) . ') - '; + } + else { + $option_title = $merge_to->GetDBField($email_field) . ' - '; + } + + if ($subject_field) { + $option_title .= $merge_to->GetField($subject_field) . ' (' . $merge_to->GetField('SubmissionTime', $date_format) . ')'; + } + else { + $option_title .= $merge_to->GetField('SubmissionTime', $date_format); + } + + $options[$submission_id] = $option_title; + } + + $field_options = $object->GetFieldOptions('MergeToSubmission'); + $field_options['options'] = $options; + $object->SetFieldOptions('MergeToSubmission', $field_options); + } + + /** + * Returns submission field name based on given role + * + * @param int $form_id + * @param string $role + * @return string + */ + function getFieldByRole($form_id, $role) + { + static $cache = Array (); + + if (!array_key_exists($form_id, $cache)) { + $id_field = $this->Application->getUnitOption('formflds', 'IDField'); + $table_name = $this->Application->getUnitOption('formflds', 'TableName'); + + $sql = 'SELECT ' . $id_field . ', EmailCommunicationRole + FROM ' . $table_name . ' + WHERE FormId = ' . $form_id . ' AND EmailCommunicationRole <> 0'; + $cache[$form_id] = $this->Conn->GetCol($sql, 'EmailCommunicationRole'); + } + + // get field name by role + return array_key_exists($role, $cache[$form_id]) ? 'fld_' . $cache[$form_id][$role] : false; + } + + /** + * Performs submission merge + * + * @param kEvent $event + */ + function OnUpdate(&$event) + { + parent::OnUpdate($event); + + if ($event->status == erSUCCESS) { + $object =& $event->getObject(); + /* @var $object kDBItem */ + + $merge_to = $object->GetDBField('MergeToSubmission'); + + if (!$merge_to) { + return ; + } + + $form_id = $object->GetDBField('FormId'); + + $sql = 'SELECT * + FROM ' . TABLE_PREFIX . 'Forms + WHERE FormId = ' . $form_id; + $form_info = $this->Conn->GetRow($sql); + + $reply =& $this->Application->recallObject('submission-log.merge', null, Array ('skip_autoload' => true)); + /* @var $reply kDBItem */ + + $email_field = $this->getFieldByRole($form_id, EMAIL_COMMUNICATION_ROLE_EMAIL); + $subject_field = $this->getFieldByRole($form_id, EMAIL_COMMUNICATION_ROLE_SUBJECT); + $body_field = $this->getFieldByRole($form_id, EMAIL_COMMUNICATION_ROLE_BODY); + + $reply->SetDBField('FormSubmissionId', $merge_to); + + if ($email_field) { + $reply->SetDBField('FromEmail', $object->GetDBField($email_field)); + } + + $reply->SetDBField('ToEmail', $form_info['ReplyFromEmail']); + + if ($subject_field) { + $reply->SetDBField('Subject', $object->GetDBField($subject_field)); + } + + if ($body_field) { + $reply->SetDBField('Message', $object->GetDBField($body_field)); + } + + $reply->SetDBField('SentOn_date', $object->GetDBField('SubmissionTime')); + $reply->SetDBField('SentOn_time', $object->GetDBField('SubmissionTime')); + $reply->SetDBField('MessageId', $object->GetDBField('MessageId')); + $reply->SetDBField('SentStatus', SUBMISSION_LOG_SENT); + + // as if emails was really received via mailbox + $this->Application->SetVar('client_mode', 1); + + if ($reply->Create()) { + // delete submission, since it was merged + $object->Delete(); + } + } + } } \ No newline at end of file