示例#1
0
文件: Forward.php 项目: kidaa30/yes
 /**
  * Create a new forward event, create a new contact if necessary
  *
  * @param $job_id
  * @param $queue_id
  * @param $hash
  * @param $forward_email
  * @param null $fromEmail
  * @param null $comment
  *
  * @return bool
  */
 public static function &forward($job_id, $queue_id, $hash, $forward_email, $fromEmail = NULL, $comment = NULL)
 {
     $q = CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash);
     $successfulForward = FALSE;
     $contact_id = NULL;
     if (!$q) {
         return $successfulForward;
     }
     /* Find the email address/contact, if it exists */
     $contact = CRM_Contact_BAO_Contact::getTableName();
     $location = CRM_Core_BAO_Location::getTableName();
     $email = CRM_Core_BAO_Email::getTableName();
     $queueTable = CRM_Mailing_Event_BAO_Queue::getTableName();
     $job = CRM_Mailing_BAO_MailingJob::getTableName();
     $mailing = CRM_Mailing_BAO_Mailing::getTableName();
     $forward = self::getTableName();
     $domain = CRM_Core_BAO_Domain::getDomain();
     $dao = new CRM_Core_Dao();
     $dao->query("\n                SELECT      {$contact}.id as contact_id,\n                            {$email}.id as email_id,\n                            {$contact}.do_not_email as do_not_email,\n                            {$queueTable}.id as queue_id\n                FROM        ({$email}, {$job} as temp_job)\n                INNER JOIN  {$contact}\n                        ON  {$email}.contact_id = {$contact}.id\n                LEFT JOIN   {$queueTable}\n                        ON  {$email}.id = {$queueTable}.email_id\n                LEFT JOIN   {$job}\n                        ON  {$queueTable}.job_id = {$job}.id\n                        AND temp_job.mailing_id = {$job}.mailing_id\n                WHERE       {$queueTable}.job_id = {$job_id}\n                    AND     {$email}.email = '" . CRM_Utils_Type::escape($forward_email, 'String') . "'");
     $dao->fetch();
     $transaction = new CRM_Core_Transaction();
     if (isset($dao->queue_id) || isset($dao->do_not_email) && $dao->do_not_email == 1) {
         /* We already sent this mailing to $forward_email, or we should
          * never email this contact.  Give up. */
         return $successfulForward;
     }
     require_once 'api/api.php';
     $contactParams = array('email' => $forward_email, 'version' => 3);
     $contactValues = civicrm_api('contact', 'get', $contactParams);
     $count = $contactValues['count'];
     if ($count == 0) {
         /* If the contact does not exist, create one. */
         $formatted = array('contact_type' => 'Individual', 'version' => 3);
         $locationType = CRM_Core_BAO_LocationType::getDefault();
         $value = array('email' => $forward_email, 'location_type_id' => $locationType->id);
         require_once 'CRM/Utils/DeprecatedUtils.php';
         _civicrm_api3_deprecated_add_formatted_param($value, $formatted);
         $formatted['onDuplicate'] = CRM_Import_Parser::DUPLICATE_SKIP;
         $formatted['fixAddress'] = TRUE;
         $contact = civicrm_api('contact', 'create', $formatted);
         if (civicrm_error($contact)) {
             return $successfulForward;
         }
         $contact_id = $contact['id'];
     }
     $email = new CRM_Core_DAO_Email();
     $email->email = $forward_email;
     $email->find(TRUE);
     $email_id = $email->id;
     if (!$contact_id) {
         $contact_id = $email->contact_id;
     }
     /* Create a new queue event */
     $queue_params = array('email_id' => $email_id, 'contact_id' => $contact_id, 'job_id' => $job_id);
     $queue = CRM_Mailing_Event_BAO_Queue::create($queue_params);
     $forward = new CRM_Mailing_Event_BAO_Forward();
     $forward->time_stamp = date('YmdHis');
     $forward->event_queue_id = $queue_id;
     $forward->dest_queue_id = $queue->id;
     $forward->save();
     $dao->reset();
     $dao->query("   SELECT  {$job}.mailing_id as mailing_id\n                        FROM    {$job}\n                        WHERE   {$job}.id = " . CRM_Utils_Type::escape($job_id, 'Integer'));
     $dao->fetch();
     $mailing_obj = new CRM_Mailing_BAO_Mailing();
     $mailing_obj->id = $dao->mailing_id;
     $mailing_obj->find(TRUE);
     $config = CRM_Core_Config::singleton();
     $mailer = $config->getMailer();
     $recipient = NULL;
     $attachments = NULL;
     $message = $mailing_obj->compose($job_id, $queue->id, $queue->hash, $queue->contact_id, $forward_email, $recipient, FALSE, NULL, $attachments, TRUE, $fromEmail);
     //append comment if added while forwarding.
     if (count($comment)) {
         $message->_txtbody = CRM_Utils_Array::value('body_text', $comment) . $message->_txtbody;
         if (!empty($comment['body_html'])) {
             $message->_htmlbody = $comment['body_html'] . '<br />---------------Original message---------------------<br />' . $message->_htmlbody;
         }
     }
     $body = $message->get();
     $headers = $message->headers();
     $result = NULL;
     if (is_object($mailer)) {
         $errorScope = CRM_Core_TemporaryErrorScope::ignoreException();
         $result = $mailer->send($recipient, $headers, $body);
         unset($errorScope);
     }
     $params = array('event_queue_id' => $queue->id, 'job_id' => $job_id, 'hash' => $queue->hash);
     if (is_a($result, 'PEAR_Error')) {
         /* Register the bounce event */
         $params = array_merge($params, CRM_Mailing_BAO_BouncePattern::match($result->getMessage()));
         CRM_Mailing_Event_BAO_Bounce::create($params);
     } else {
         $successfulForward = TRUE;
         /* Register the delivery event */
         CRM_Mailing_Event_BAO_Delivered::create($params);
     }
     $transaction->commit();
     return $successfulForward;
 }
示例#2
0
 public function buildQuickForm()
 {
     $session = CRM_Core_Session::singleton();
     $this->add('text', 'test_email', ts('Send to This Address'));
     $defaults['test_email'] = $session->get('ufUniqID');
     $qfKey = $this->get('qfKey');
     $this->add('select', 'test_group', ts('Send to This Group'), array('' => ts('- none -')) + CRM_Core_PseudoConstant::group('Mailing'));
     $this->setDefaults($defaults);
     $this->add('submit', 'sendtest', ts('Send a Test Mailing'));
     $name = ts('Next');
     if (CRM_Mailing_Info::workflowEnabled()) {
         if (!CRM_Core_Permission::check('schedule mailings') && CRM_Core_Permission::check('create mailings')) {
             $name = ts('Inform Scheduler');
         }
     }
     $buttons = array(array('type' => 'back', 'name' => ts('Previous')), array('type' => 'next', 'name' => $name, 'spacing' => '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;', 'isDefault' => TRUE), array('type' => 'submit', 'name' => ts('Save & Continue Later')), array('type' => 'cancel', 'name' => ts('Cancel')));
     $this->addButtons($buttons);
     $mailingID = $this->get('mailing_id');
     $textFile = $this->get('textFile');
     $htmlFile = $this->get('htmlFile');
     $this->addFormRule(array('CRM_Mailing_Form_Test', 'testMail'), $this);
     $preview = array();
     if ($textFile) {
         $preview['text_link'] = CRM_Utils_System::url('civicrm/mailing/preview', "type=text&qfKey={$qfKey}");
     }
     if ($htmlFile) {
         $preview['html_link'] = CRM_Utils_System::url('civicrm/mailing/preview', "type=html&qfKey={$qfKey}");
     }
     $preview['attachment'] = CRM_Core_BAO_File::attachmentInfo('civicrm_mailing', $mailingID);
     $this->assign('preview', $preview);
     //Token Replacement of Subject in preview mailing
     $options = array();
     $prefix = "CRM_Mailing_Controller_Send_{$qfKey}";
     if ($this->_searchBasedMailing) {
         $prefix = "CRM_Contact_Controller_Search_{$qfKey}";
     }
     $session->getVars($options, $prefix);
     $mailing = new CRM_Mailing_BAO_Mailing();
     $mailing->id = $options['mailing_id'];
     $mailing->find(TRUE);
     $fromEmail = $mailing->from_email;
     $replyToEmail = $mailing->replyto_email;
     $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_mailing', $mailing->id);
     $returnProperties = $mailing->getReturnProperties();
     $userID = $session->get('userID');
     $params = array('contact_id' => $userID);
     $details = CRM_Utils_Token::getTokenDetails($params, $returnProperties, TRUE, TRUE, NULL, $mailing->getFlattenedTokens(), get_class($this));
     $allDetails =& $mailing->compose(NULL, NULL, NULL, $userID, $fromEmail, $fromEmail, TRUE, $details[0][$userID], $attachments);
     $this->assign('subject', $allDetails->_headers['Subject']);
 }
示例#3
0
 public function buildQuickForm()
 {
     $session = CRM_Core_Session::singleton();
     $this->add('text', 'test_email', ts('Send to This Address'));
     $defaults['test_email'] = $session->get('ufUniqID');
     $qfKey = $this->get('qfKey');
     $this->add('select', 'test_group', ts('Send to This Group'), array('' => ts('- none -')) + CRM_Core_PseudoConstant::group('Mailing'));
     $this->setDefaults($defaults);
     $this->add('submit', 'sendtest', ts('Send a Test Mailing'));
     $name = ts('Next >>');
     require_once 'CRM/Mailing/Info.php';
     if (CRM_Mailing_Info::workflowEnabled()) {
         if (!CRM_Core_Permission::check('schedule mailings') && CRM_Core_Permission::check('create mailings')) {
             $name = ts('Inform Scheduler');
         }
     }
     //FIXME : currently we are hiding save an continue later when
     //search base mailing, we should handle it when we fix CRM-3876
     $buttons = array(array('type' => 'back', 'name' => ts('<< Previous')), array('type' => 'next', 'name' => $name, 'spacing' => '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;', 'isDefault' => true), array('type' => 'submit', 'name' => ts('Save & Continue Later')), array('type' => 'cancel', 'name' => ts('Cancel')));
     if ($this->_searchBasedMailing && $this->get('ssID')) {
         $buttons = array(array('type' => 'back', 'name' => ts('<< Previous')), array('type' => 'next', 'name' => $name, 'spacing' => '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;', 'isDefault' => true), array('type' => 'cancel', 'name' => ts('Cancel')));
     }
     $this->addButtons($buttons);
     $mailingID = $this->get('mailing_id');
     $textFile = $this->get('textFile');
     $htmlFile = $this->get('htmlFile');
     $this->addFormRule(array('CRM_Mailing_Form_Test', 'testMail'), $this);
     $preview = array();
     if ($textFile) {
         $preview['text_link'] = CRM_Utils_System::url('civicrm/mailing/preview', "type=text&qfKey={$qfKey}");
     }
     if ($htmlFile) {
         $preview['html_link'] = CRM_Utils_System::url('civicrm/mailing/preview', "type=html&qfKey={$qfKey}");
     }
     require_once 'CRM/Core/BAO/File.php';
     $preview['attachment'] = CRM_Core_BAO_File::attachmentInfo('civicrm_mailing', $mailingID);
     $this->assign('preview', $preview);
     //Token Replacement of Subject in preview mailing
     $options = array();
     $prefix = "CRM_Mailing_Controller_Send_{$qfKey}";
     if ($this->_searchBasedMailing) {
         $prefix = "CRM_Contact_Controller_Search_{$qfKey}";
     }
     $session->getVars($options, $prefix);
     require_once 'CRM/Mailing/BAO/Mailing.php';
     $mailing = new CRM_Mailing_BAO_Mailing();
     $mailing->id = $options['mailing_id'];
     $mailing->find(true);
     $fromEmail = $mailing->from_email;
     require_once 'CRM/Core/BAO/File.php';
     $attachments =& CRM_Core_BAO_File::getEntityFile('civicrm_mailing', $mailing->id);
     $returnProperties = $mailing->getReturnProperties();
     $userID = $session->get('userID');
     $params = array('contact_id' => $userID);
     $details = $mailing->getDetails($params, $returnProperties);
     $allDetails =& $mailing->compose(null, null, null, $userID, $fromEmail, $fromEmail, true, $details[0][$userID], $attachments);
     $this->assign('subject', $allDetails->_headers['Subject']);
 }