3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * Form to send test mail
39 class CRM_Mailing_Form_Test
extends CRM_Core_Form
{
42 * Function to set variables up before form is built
47 public function preProcess() {
48 //when user come from search context.
49 $ssID = $this->get('ssID');
50 $this->assign('ssid',$ssID);
51 $this->_searchBasedMailing
= CRM_Contact_Form_Search
::isSearchContext($this->get('context'));
52 if(CRM_Contact_Form_Search
::isSearchContext($this->get('context')) && !$ssID){
54 $result = CRM_Core_BAO_PrevNextCache
::getSelectedContacts();
55 $this->assign("value", $result);
60 * This function sets the default values for the form.
66 function setDefaultValues() {
67 $count = $this->get('count');
68 $this->assign('count', $count);
71 public function buildQuickForm() {
72 $session = CRM_Core_Session
::singleton();
73 $this->add('text', 'test_email', ts('Send to This Address'));
74 $defaults['test_email'] = $session->get('ufUniqID');
75 $qfKey = $this->get('qfKey');
79 ts('Send to This Group'),
80 array('' => ts('- none -')) + CRM_Core_PseudoConstant
::group('Mailing')
82 $this->setDefaults($defaults);
84 $this->add('submit', 'sendtest', ts('Send a Test Mailing'));
85 $name = ts('Next >>');
86 if (CRM_Mailing_Info
::workflowEnabled()) {
87 if (!CRM_Core_Permission
::check('schedule mailings') &&
88 CRM_Core_Permission
::check('create mailings')
90 $name = ts('Inform Scheduler');
95 array('type' => 'back',
96 'name' => ts('<< Previous'),
101 'spacing' => ' ',
106 'name' => ts('Save & Continue Later'),
110 'name' => ts('Cancel'),
114 $this->addButtons($buttons);
116 $mailingID = $this->get('mailing_id');
117 $textFile = $this->get('textFile');
118 $htmlFile = $this->get('htmlFile');
120 $this->addFormRule(array('CRM_Mailing_Form_Test', 'testMail'), $this);
123 $preview['text_link'] = CRM_Utils_System
::url('civicrm/mailing/preview', "type=text&qfKey=$qfKey");
126 $preview['html_link'] = CRM_Utils_System
::url('civicrm/mailing/preview', "type=html&qfKey=$qfKey");
129 $preview['attachment'] = CRM_Core_BAO_File
::attachmentInfo('civicrm_mailing', $mailingID);
130 $this->assign('preview', $preview);
131 //Token Replacement of Subject in preview mailing
133 $prefix = "CRM_Mailing_Controller_Send_$qfKey";
134 if ($this->_searchBasedMailing
) {
135 $prefix = "CRM_Contact_Controller_Search_$qfKey";
137 $session->getVars($options, $prefix);
139 $mailing = new CRM_Mailing_BAO_Mailing();
140 $mailing->id
= $options['mailing_id'];
141 $mailing->find(TRUE);
142 $fromEmail = $mailing->from_email
;
143 $replyToEmail = $mailing->replyto_email
;
145 $attachments = CRM_Core_BAO_File
::getEntityFile('civicrm_mailing',
149 $returnProperties = $mailing->getReturnProperties();
150 $userID = $session->get('userID');
151 $params = array('contact_id' => $userID);
153 $details = CRM_Utils_Token
::getTokenDetails($params,
156 $mailing->getFlattenedTokens(),
160 $allDetails = &$mailing->compose(NULL, NULL, NULL,
165 $details[0][$userID],
169 $this->assign('subject', $allDetails->_headers
['Subject']);
173 * Form rule to send out a test mailing.
175 * @param array $params Array of the form values
176 * @param array $files Any files posted to the form
177 * @param array $self an current this object
179 * @return boolean true on successful SMTP handoff
182 static function testMail($testParams, $files, $self) {
185 $urlString = 'civicrm/mailing/send';
186 $urlParams = "_qf_Test_display=true&qfKey={$testParams['qfKey']}";
188 $ssID = $self->get('ssID');
189 if ($ssID && $self->_searchBasedMailing
) {
190 if ($self->_action
== CRM_Core_Action
::BASIC
) {
191 $fragment = 'search';
193 elseif ($self->_action
== CRM_Core_Action
::PROFILE
) {
194 $fragment = 'search/builder';
196 elseif ($self->_action
== CRM_Core_Action
::ADVANCED
) {
197 $fragment = 'search/advanced';
200 $fragment = 'search/custom';
202 $urlString = 'civicrm/contact/' . $fragment;
205 if (!empty($testParams['sendtest'])) {
206 if (!($testParams['test_group'] ||
$testParams['test_email'])) {
207 CRM_Core_Session
::setStatus(ts('You did not provide an email address or select a group.'), ts('Test not sent.'), 'error');
211 if ($testParams['test_email']) {
212 $emailAdd = explode(',', $testParams['test_email']);
213 foreach ($emailAdd as $key => $value) {
214 $email = trim($value);
215 $testParams['emails'][] = $email;
216 $emails .= $emails ?
",'$email'" : "'$email'";
217 if (!CRM_Utils_Rule
::email($email)) {
218 CRM_Core_Session
::setStatus(ts('Please enter a valid email addresses.'), ts('Test not sent.'), 'error');
225 $url = CRM_Utils_System
::url($urlString, $urlParams);
226 CRM_Utils_System
::redirect($url);
231 if (!empty($testParams['_qf_Test_submit'])) {
232 //when user perform mailing from search context
233 //redirect it to search result CRM-3711.
234 if ($ssID && $self->_searchBasedMailing
) {
235 $draftURL = CRM_Utils_System
::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1');
236 $status = ts("You can continue later by clicking the 'Continue' action to resume working on it.<br />From <a href='%1'>Draft and Unscheduled Mailings</a>.", array(1 => $draftURL));
238 //replace user context to search.
239 $context = $self->get('context');
240 if (!CRM_Contact_Form_Search
::isSearchContext($context)) {
243 $urlParams = "force=1&reset=1&ssID={$ssID}&context={$context}&qfKey={$testParams['qfKey']}";
244 $url = CRM_Utils_System
::url($urlString, $urlParams);
247 $status = ts("Click the 'Continue' action to resume working on it.");
248 $url = CRM_Utils_System
::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1');
250 CRM_Core_Session
::setStatus($status, ts('Mailing Saved'), 'success');
251 CRM_Utils_System
::redirect($url);
254 if (CRM_Mailing_Info
::workflowEnabled()) {
255 if (!CRM_Core_Permission
::check('schedule mailings') &&
256 CRM_Core_Permission
::check('create mailings')
258 $url = CRM_Utils_System
::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1');
259 CRM_Utils_System
::redirect($url);
263 if (!empty($testParams['_qf_Test_next']) &&
264 $self->get('count') <= 0) {
267 ts("You can not schedule or send this mailing because there are currently no recipients selected. Click 'Previous' to return to the Select Recipients step, OR click 'Save & Continue Later'."),
271 if (!empty($_POST['_qf_Import_refresh']) ||
!empty($testParams['_qf_Test_next']) ||
empty($testParams['sendtest'])) {
276 $job = new CRM_Mailing_BAO_MailingJob();
277 $job->mailing_id
= $self->get('mailing_id');
278 $job->is_test
= TRUE;
281 $session = CRM_Core_Session
::singleton();
282 if (!empty($testParams['emails'])) {
284 SELECT e.id, e.contact_id, e.email
286 INNER JOIN civicrm_contact c ON e.contact_id = c.id
287 WHERE e.email IN ($emails)
290 AND c.do_not_email = 0
291 AND c.is_deceased = 0
293 ORDER BY e.is_bulkmail DESC, e.is_primary DESC
296 $dao = CRM_Core_DAO
::executeQuery($query);
297 $emailDetail = array();
298 // fetch contact_id and email id for all existing emails
299 while ($dao->fetch()) {
300 $emailDetail[$dao->email
] = array(
301 'contact_id' => $dao->contact_id
,
302 'email_id' => $dao->id
,
307 foreach ($testParams['emails'] as $key => $email) {
308 $email = trim($email);
309 $contactId = $emailId = NULL;
310 if (array_key_exists($email, $emailDetail)) {
311 $emailId = $emailDetail[$email]['email_id'];
312 $contactId = $emailDetail[$email]['contact_id'];
316 //create new contact.
318 'contact_type' => 'Individual',
320 1 => array('email' => $email,
322 'location_type_id' => 1,
325 $contact = CRM_Contact_BAO_Contact
::create($params);
326 $emailId = $contact->email
[0]->id
;
327 $contactId = $contact->id
;
331 'job_id' => $job->id
,
332 'email_id' => $emailId,
333 'contact_id' => $contactId,
335 CRM_Mailing_Event_BAO_Queue
::create($params);
339 $testParams['job_id'] = $job->id
;
341 while (!$isComplete) {
342 $isComplete = CRM_Mailing_BAO_MailingJob
::runJobs($testParams);
345 if (!empty($testParams['sendtest'])) {
347 if (CRM_Mailing_Info
::workflowEnabled()) {
349 CRM_Core_Permission
::check('schedule mailings') &&
350 CRM_Core_Permission
::check('create mailings')
352 CRM_Core_Permission
::check('access CiviMail')
354 $status = ts("Click 'Next' when you are ready to Schedule or Send your live mailing (you will still have a chance to confirm or cancel sending this mailing on the next page).");
358 $status = ts("Click 'Next' when you are ready to Schedule or Send your live mailing (you will still have a chance to confirm or cancel sending this mailing on the next page).");
362 CRM_Core_Session
::setStatus($status, ts('Test message sent'), 'success');
364 $url = CRM_Utils_System
::url($urlString, $urlParams);
365 CRM_Utils_System
::redirect($url);
372 * Display Name of the form
378 public function getTitle() {
382 public function postProcess() {