Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
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. | | |
13 | | | | |
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. | | |
18 | | | | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * Form to send test mail | |
38 | */ | |
39 | class CRM_Mailing_Form_Test extends CRM_Core_Form { | |
40 | ||
41 | /** | |
42 | * Function to set variables up before form is built | |
43 | * | |
44 | * @return void | |
45 | * @access public | |
46 | */ | |
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){ | |
53 | $params = array(); | |
54 | $result = CRM_Core_BAO_PrevNextCache::getSelectedContacts(); | |
55 | $this->assign("value", $result); | |
56 | } | |
57 | } | |
58 | ||
59 | /** | |
60 | * This function sets the default values for the form. | |
61 | * | |
62 | * @access public | |
63 | * | |
355ba699 | 64 | * @return void |
6a488035 TO |
65 | */ |
66 | function setDefaultValues() { | |
67 | $count = $this->get('count'); | |
68 | $this->assign('count', $count); | |
69 | } | |
70 | ||
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'); | |
76 | ||
77 | $this->add('select', | |
78 | 'test_group', | |
79 | ts('Send to This Group'), | |
80 | array('' => ts('- none -')) + CRM_Core_PseudoConstant::group('Mailing') | |
81 | ); | |
82 | $this->setDefaults($defaults); | |
83 | ||
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') | |
89 | ) { | |
90 | $name = ts('Inform Scheduler'); | |
91 | } | |
92 | } | |
93 | ||
94 | $buttons = array( | |
95 | array('type' => 'back', | |
96 | 'name' => ts('<< Previous'), | |
97 | ), | |
98 | array( | |
99 | 'type' => 'next', | |
100 | 'name' => $name, | |
101 | 'spacing' => ' ', | |
102 | 'isDefault' => TRUE, | |
103 | ), | |
104 | array( | |
105 | 'type' => 'submit', | |
106 | 'name' => ts('Save & Continue Later'), | |
107 | ), | |
108 | array( | |
109 | 'type' => 'cancel', | |
110 | 'name' => ts('Cancel'), | |
111 | ), | |
112 | ); | |
113 | ||
114 | $this->addButtons($buttons); | |
115 | ||
116 | $mailingID = $this->get('mailing_id'); | |
117 | $textFile = $this->get('textFile'); | |
118 | $htmlFile = $this->get('htmlFile'); | |
119 | ||
120 | $this->addFormRule(array('CRM_Mailing_Form_Test', 'testMail'), $this); | |
121 | $preview = array(); | |
122 | if ($textFile) { | |
123 | $preview['text_link'] = CRM_Utils_System::url('civicrm/mailing/preview', "type=text&qfKey=$qfKey"); | |
124 | } | |
125 | if ($htmlFile) { | |
126 | $preview['html_link'] = CRM_Utils_System::url('civicrm/mailing/preview', "type=html&qfKey=$qfKey"); | |
127 | } | |
128 | ||
129 | $preview['attachment'] = CRM_Core_BAO_File::attachmentInfo('civicrm_mailing', $mailingID); | |
130 | $this->assign('preview', $preview); | |
131 | //Token Replacement of Subject in preview mailing | |
132 | $options = array(); | |
133 | $prefix = "CRM_Mailing_Controller_Send_$qfKey"; | |
134 | if ($this->_searchBasedMailing) { | |
135 | $prefix = "CRM_Contact_Controller_Search_$qfKey"; | |
136 | } | |
137 | $session->getVars($options, $prefix); | |
138 | ||
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; | |
144 | ||
145 | $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_mailing', | |
146 | $mailing->id | |
147 | ); | |
148 | ||
149 | $returnProperties = $mailing->getReturnProperties(); | |
150 | $userID = $session->get('userID'); | |
151 | $params = array('contact_id' => $userID); | |
152 | ||
153 | $details = CRM_Utils_Token::getTokenDetails($params, | |
154 | $returnProperties, | |
155 | TRUE, TRUE, NULL, | |
156 | $mailing->getFlattenedTokens(), | |
157 | get_class($this) | |
158 | ); | |
159 | ||
160 | $allDetails = &$mailing->compose(NULL, NULL, NULL, | |
161 | $userID, | |
162 | $fromEmail, | |
163 | $fromEmail, | |
164 | TRUE, | |
165 | $details[0][$userID], | |
166 | $attachments | |
167 | ); | |
168 | ||
169 | $this->assign('subject', $allDetails->_headers['Subject']); | |
170 | } | |
171 | ||
172 | /** | |
173 | * Form rule to send out a test mailing. | |
174 | * | |
fd31fa4c EM |
175 | * @param $testParams |
176 | * @param array $files Any files posted to the form | |
177 | * @param array $self an current this object | |
6a488035 | 178 | * |
fd31fa4c | 179 | * @internal param array $params Array of the form values |
ceb10dc7 | 180 | * @return boolean true on successful SMTP handoff |
6a488035 TO |
181 | * @access public |
182 | */ | |
fb6afb9e | 183 | static function testMail($testParams, $files, $self) { |
6a488035 TO |
184 | $error = NULL; |
185 | ||
186 | $urlString = 'civicrm/mailing/send'; | |
187 | $urlParams = "_qf_Test_display=true&qfKey={$testParams['qfKey']}"; | |
188 | ||
189 | $ssID = $self->get('ssID'); | |
190 | if ($ssID && $self->_searchBasedMailing) { | |
191 | if ($self->_action == CRM_Core_Action::BASIC) { | |
192 | $fragment = 'search'; | |
193 | } | |
194 | elseif ($self->_action == CRM_Core_Action::PROFILE) { | |
195 | $fragment = 'search/builder'; | |
196 | } | |
197 | elseif ($self->_action == CRM_Core_Action::ADVANCED) { | |
198 | $fragment = 'search/advanced'; | |
199 | } | |
200 | else { | |
201 | $fragment = 'search/custom'; | |
202 | } | |
203 | $urlString = 'civicrm/contact/' . $fragment; | |
204 | } | |
205 | $emails = NULL; | |
a7488080 | 206 | if (!empty($testParams['sendtest'])) { |
6a488035 TO |
207 | if (!($testParams['test_group'] || $testParams['test_email'])) { |
208 | CRM_Core_Session::setStatus(ts('You did not provide an email address or select a group.'), ts('Test not sent.'), 'error'); | |
209 | $error = TRUE; | |
210 | } | |
211 | ||
212 | if ($testParams['test_email']) { | |
213 | $emailAdd = explode(',', $testParams['test_email']); | |
214 | foreach ($emailAdd as $key => $value) { | |
215 | $email = trim($value); | |
216 | $testParams['emails'][] = $email; | |
217 | $emails .= $emails ? ",'$email'" : "'$email'"; | |
218 | if (!CRM_Utils_Rule::email($email)) { | |
f289559e | 219 | CRM_Core_Session::setStatus(ts('Please enter a valid email address.'), ts('Test not sent.'), 'error'); |
6a488035 TO |
220 | $error = TRUE; |
221 | } | |
222 | } | |
223 | } | |
224 | ||
225 | if ($error) { | |
226 | $url = CRM_Utils_System::url($urlString, $urlParams); | |
227 | CRM_Utils_System::redirect($url); | |
228 | return $error; | |
229 | } | |
230 | } | |
231 | ||
a7488080 | 232 | if (!empty($testParams['_qf_Test_submit'])) { |
6a488035 TO |
233 | //when user perform mailing from search context |
234 | //redirect it to search result CRM-3711. | |
235 | if ($ssID && $self->_searchBasedMailing) { | |
236 | $draftURL = CRM_Utils_System::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1'); | |
237 | $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 | ||
239 | //replace user context to search. | |
240 | $context = $self->get('context'); | |
241 | if (!CRM_Contact_Form_Search::isSearchContext($context)) { | |
242 | $context = 'search'; | |
243 | } | |
244 | $urlParams = "force=1&reset=1&ssID={$ssID}&context={$context}&qfKey={$testParams['qfKey']}"; | |
245 | $url = CRM_Utils_System::url($urlString, $urlParams); | |
246 | } | |
247 | else { | |
248 | $status = ts("Click the 'Continue' action to resume working on it."); | |
249 | $url = CRM_Utils_System::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1'); | |
250 | } | |
251 | CRM_Core_Session::setStatus($status, ts('Mailing Saved'), 'success'); | |
252 | CRM_Utils_System::redirect($url); | |
253 | } | |
254 | ||
255 | if (CRM_Mailing_Info::workflowEnabled()) { | |
256 | if (!CRM_Core_Permission::check('schedule mailings') && | |
257 | CRM_Core_Permission::check('create mailings') | |
258 | ) { | |
259 | $url = CRM_Utils_System::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1'); | |
260 | CRM_Utils_System::redirect($url); | |
261 | } | |
262 | } | |
263 | ||
a7488080 | 264 | if (!empty($testParams['_qf_Test_next']) && |
fb6afb9e DS |
265 | $self->get('count') <= 0) { |
266 | return array( | |
267 | '_qf_default' => | |
268 | 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'."), | |
269 | ); | |
270 | } | |
271 | ||
8cc574cf | 272 | if (!empty($_POST['_qf_Import_refresh']) || !empty($testParams['_qf_Test_next']) || empty($testParams['sendtest'])) { |
6a488035 TO |
273 | $error = TRUE; |
274 | return $error; | |
275 | } | |
276 | ||
9da8dc8c | 277 | $job = new CRM_Mailing_BAO_MailingJob(); |
6a488035 TO |
278 | $job->mailing_id = $self->get('mailing_id'); |
279 | $job->is_test = TRUE; | |
280 | $job->save(); | |
281 | $newEmails = NULL; | |
282 | $session = CRM_Core_Session::singleton(); | |
283 | if (!empty($testParams['emails'])) { | |
284 | $query = " | |
285 | SELECT e.id, e.contact_id, e.email | |
286 | FROM civicrm_email e | |
287 | INNER JOIN civicrm_contact c ON e.contact_id = c.id | |
288 | WHERE e.email IN ($emails) | |
289 | AND e.on_hold = 0 | |
290 | AND c.is_opt_out = 0 | |
291 | AND c.do_not_email = 0 | |
fa6d0f5a | 292 | AND c.is_deleted = 0 |
6a488035 TO |
293 | AND c.is_deceased = 0 |
294 | GROUP BY e.id | |
295 | ORDER BY e.is_bulkmail DESC, e.is_primary DESC | |
296 | "; | |
297 | ||
298 | $dao = CRM_Core_DAO::executeQuery($query); | |
299 | $emailDetail = array(); | |
300 | // fetch contact_id and email id for all existing emails | |
301 | while ($dao->fetch()) { | |
302 | $emailDetail[$dao->email] = array( | |
303 | 'contact_id' => $dao->contact_id, | |
304 | 'email_id' => $dao->id, | |
305 | ); | |
306 | } | |
307 | ||
308 | $dao->free(); | |
309 | foreach ($testParams['emails'] as $key => $email) { | |
fa6d0f5a JM |
310 | // Email addresses are forced to lower case when saved, so ensure |
311 | // we have the same case when comparing. | |
312 | $email = trim(strtolower($email)); | |
6a488035 TO |
313 | $contactId = $emailId = NULL; |
314 | if (array_key_exists($email, $emailDetail)) { | |
315 | $emailId = $emailDetail[$email]['email_id']; | |
316 | $contactId = $emailDetail[$email]['contact_id']; | |
317 | } | |
318 | ||
319 | if (!$contactId) { | |
320 | //create new contact. | |
321 | $params = array( | |
322 | 'contact_type' => 'Individual', | |
323 | 'email' => array( | |
324 | 1 => array('email' => $email, | |
325 | 'is_primary' => 1, | |
326 | 'location_type_id' => 1, | |
327 | )), | |
328 | ); | |
329 | $contact = CRM_Contact_BAO_Contact::create($params); | |
330 | $emailId = $contact->email[0]->id; | |
331 | $contactId = $contact->id; | |
332 | $contact->free(); | |
333 | } | |
334 | $params = array( | |
335 | 'job_id' => $job->id, | |
336 | 'email_id' => $emailId, | |
337 | 'contact_id' => $contactId, | |
338 | ); | |
339 | CRM_Mailing_Event_BAO_Queue::create($params); | |
340 | } | |
341 | } | |
342 | ||
343 | $testParams['job_id'] = $job->id; | |
344 | $isComplete = FALSE; | |
345 | while (!$isComplete) { | |
9da8dc8c | 346 | $isComplete = CRM_Mailing_BAO_MailingJob::runJobs($testParams); |
6a488035 TO |
347 | } |
348 | ||
a7488080 | 349 | if (!empty($testParams['sendtest'])) { |
6a488035 TO |
350 | $status = NULL; |
351 | if (CRM_Mailing_Info::workflowEnabled()) { | |
352 | if (( | |
353 | CRM_Core_Permission::check('schedule mailings') && | |
354 | CRM_Core_Permission::check('create mailings') | |
355 | ) || | |
356 | CRM_Core_Permission::check('access CiviMail') | |
357 | ) { | |
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)."); | |
359 | } | |
360 | } | |
361 | else { | |
362 | $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)."); | |
363 | } | |
364 | ||
365 | if ($status) { | |
366 | CRM_Core_Session::setStatus($status, ts('Test message sent'), 'success'); | |
367 | } | |
368 | $url = CRM_Utils_System::url($urlString, $urlParams); | |
369 | CRM_Utils_System::redirect($url); | |
370 | } | |
371 | $error = TRUE; | |
372 | return $error; | |
373 | } | |
374 | ||
375 | /** | |
376 | * Display Name of the form | |
377 | * | |
378 | * @access public | |
379 | * | |
380 | * @return string | |
381 | */ | |
382 | public function getTitle() { | |
383 | return ts('Test'); | |
384 | } | |
385 | ||
386 | public function postProcess() { | |
387 | } | |
388 | ||
389 | } | |
390 |