commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / CRM / Mailing / Form / Test.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
31 * @copyright CiviCRM LLC (c) 2004-2015
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 * Set variables up before form is built.
43 *
44 * @return void
45 */
46 public function preProcess() {
47 //when user come from search context.
48 $ssID = $this->get('ssID');
49 $this->assign('ssid', $ssID);
50 $this->_searchBasedMailing = CRM_Contact_Form_Search::isSearchContext($this->get('context'));
51 if (CRM_Contact_Form_Search::isSearchContext($this->get('context')) && !$ssID) {
52 $params = array();
53 $result = CRM_Core_BAO_PrevNextCache::getSelectedContacts();
54 $this->assign("value", $result);
55 }
56 }
57
58 /**
59 * Set default values for the form.
60 *
61 *
62 * @return void
63 */
64 public function setDefaultValues() {
65 $count = $this->get('count');
66 $this->assign('count', $count);
67 }
68
69 public function buildQuickForm() {
70 $session = CRM_Core_Session::singleton();
71 $this->add('text', 'test_email', ts('Send to This Address'));
72 $defaults['test_email'] = $session->get('ufUniqID');
73 $qfKey = $this->get('qfKey');
74
75 $this->add('select',
76 'test_group',
77 ts('Send to This Group'),
78 array('' => ts('- none -')) + CRM_Core_PseudoConstant::group('Mailing')
79 );
80 $this->setDefaults($defaults);
81
82 $this->add('submit', 'sendtest', ts('Send a Test Mailing'));
83 $name = ts('Next');
84 if (CRM_Mailing_Info::workflowEnabled()) {
85 if (!CRM_Core_Permission::check('schedule mailings') &&
86 CRM_Core_Permission::check('create mailings')
87 ) {
88 $name = ts('Inform Scheduler');
89 }
90 }
91
92 $buttons = array(
93 array(
94 'type' => 'back',
95 'name' => ts('Previous'),
96 ),
97 array(
98 'type' => 'next',
99 'name' => $name,
100 'spacing' => '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;',
101 'isDefault' => TRUE,
102 ),
103 array(
104 'type' => 'submit',
105 'name' => ts('Save & Continue Later'),
106 ),
107 array(
108 'type' => 'cancel',
109 'name' => ts('Cancel'),
110 ),
111 );
112
113 $this->addButtons($buttons);
114
115 $mailingID = $this->get('mailing_id');
116 $textFile = $this->get('textFile');
117 $htmlFile = $this->get('htmlFile');
118
119 $this->addFormRule(array('CRM_Mailing_Form_Test', 'testMail'), $this);
120 $preview = array();
121 if ($textFile) {
122 $preview['text_link'] = CRM_Utils_System::url('civicrm/mailing/preview', "type=text&qfKey=$qfKey");
123 }
124 if ($htmlFile) {
125 $preview['html_link'] = CRM_Utils_System::url('civicrm/mailing/preview', "type=html&qfKey=$qfKey");
126 }
127
128 $preview['attachment'] = CRM_Core_BAO_File::attachmentInfo('civicrm_mailing', $mailingID);
129 $this->assign('preview', $preview);
130 //Token Replacement of Subject in preview mailing
131 $options = array();
132 $prefix = "CRM_Mailing_Controller_Send_$qfKey";
133 if ($this->_searchBasedMailing) {
134 $prefix = "CRM_Contact_Controller_Search_$qfKey";
135 }
136 $session->getVars($options, $prefix);
137
138 $mailing = new CRM_Mailing_BAO_Mailing();
139 $mailing->id = $options['mailing_id'];
140 $mailing->find(TRUE);
141 $fromEmail = $mailing->from_email;
142 $replyToEmail = $mailing->replyto_email;
143
144 $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_mailing',
145 $mailing->id
146 );
147
148 $returnProperties = $mailing->getReturnProperties();
149 $userID = $session->get('userID');
150 $params = array('contact_id' => $userID);
151
152 $details = CRM_Utils_Token::getTokenDetails($params,
153 $returnProperties,
154 TRUE, TRUE, NULL,
155 $mailing->getFlattenedTokens(),
156 get_class($this)
157 );
158
159 $allDetails = &$mailing->compose(NULL, NULL, NULL,
160 $userID,
161 $fromEmail,
162 $fromEmail,
163 TRUE,
164 $details[0][$userID],
165 $attachments
166 );
167
168 $this->assign('subject', $allDetails->_headers['Subject']);
169 }
170
171 /**
172 * Form rule to send out a test mailing.
173 *
174 * @param aray $testParams
175 * @param array $files
176 * Any files posted to the form.
177 * @param array $self
178 * An current this object.
179 *
180 * @return bool
181 * true on successful SMTP handoff
182 */
183 public static function testMail($testParams, $files, $self) {
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;
206 if (!empty($testParams['sendtest'])) {
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 ? ',' : '') . "'" . CRM_Core_DAO::escapeString($email) . "'";
218 if (!CRM_Utils_Rule::email($email)) {
219 CRM_Core_Session::setStatus(ts('Please enter a valid email address.'), ts('Test not sent.'), 'error');
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
232 if (!empty($testParams['_qf_Test_submit'])) {
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
264 if (!empty($testParams['_qf_Test_next']) &&
265 $self->get('count') <= 0
266 ) {
267 return array(
268 '_qf_default' =>
269 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'."),
270 );
271 }
272
273 if (!empty($_POST['_qf_Import_refresh']) || !empty($testParams['_qf_Test_next']) || empty($testParams['sendtest'])) {
274 $error = TRUE;
275 return $error;
276 }
277
278 $job = new CRM_Mailing_BAO_MailingJob();
279 $job->mailing_id = $self->get('mailing_id');
280 $job->is_test = TRUE;
281 $job->save();
282 $newEmails = NULL;
283 $session = CRM_Core_Session::singleton();
284 if (!empty($testParams['emails'])) {
285 $query = "
286 SELECT e.id, e.contact_id, e.email
287 FROM civicrm_email e
288 INNER JOIN civicrm_contact c ON e.contact_id = c.id
289 WHERE e.email IN ($emails)
290 AND e.on_hold = 0
291 AND c.is_opt_out = 0
292 AND c.do_not_email = 0
293 AND c.is_deleted = 0
294 AND c.is_deceased = 0
295 GROUP BY e.id
296 ORDER BY e.is_bulkmail DESC, e.is_primary DESC
297 ";
298
299 $dao = CRM_Core_DAO::executeQuery($query);
300 $emailDetail = array();
301 // fetch contact_id and email id for all existing emails
302 while ($dao->fetch()) {
303 $emailDetail[$dao->email] = array(
304 'contact_id' => $dao->contact_id,
305 'email_id' => $dao->id,
306 );
307 }
308
309 $dao->free();
310 foreach ($testParams['emails'] as $key => $email) {
311 // Email addresses are forced to lower case when saved, so ensure
312 // we have the same case when comparing.
313 $email = trim(strtolower($email));
314 $contactId = $emailId = NULL;
315 if (array_key_exists($email, $emailDetail)) {
316 $emailId = $emailDetail[$email]['email_id'];
317 $contactId = $emailDetail[$email]['contact_id'];
318 }
319
320 if (!$contactId) {
321 //create new contact.
322 $params = array(
323 'contact_type' => 'Individual',
324 'email' => array(
325 1 => array(
326 'email' => $email,
327 'is_primary' => 1,
328 'location_type_id' => 1,
329 ),
330 ),
331 );
332 $contact = CRM_Contact_BAO_Contact::create($params);
333 $emailId = $contact->email[0]->id;
334 $contactId = $contact->id;
335 $contact->free();
336 }
337 $params = array(
338 'job_id' => $job->id,
339 'email_id' => $emailId,
340 'contact_id' => $contactId,
341 );
342 CRM_Mailing_Event_BAO_Queue::create($params);
343 }
344 }
345
346 $testParams['job_id'] = $job->id;
347 $isComplete = FALSE;
348 while (!$isComplete) {
349 $isComplete = CRM_Mailing_BAO_MailingJob::runJobs($testParams);
350 }
351
352 if (!empty($testParams['sendtest'])) {
353 $status = NULL;
354 if (CRM_Mailing_Info::workflowEnabled()) {
355 if ((
356 CRM_Core_Permission::check('schedule mailings') &&
357 CRM_Core_Permission::check('create mailings')
358 ) ||
359 CRM_Core_Permission::check('access CiviMail')
360 ) {
361 $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 }
363 }
364 else {
365 $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).");
366 }
367
368 if ($status) {
369 CRM_Core_Session::setStatus($status, ts('Test message sent'), 'success');
370 }
371 $url = CRM_Utils_System::url($urlString, $urlParams);
372 CRM_Utils_System::redirect($url);
373 }
374 $error = TRUE;
375 return $error;
376 }
377
378 /**
379 * Display Name of the form.
380 *
381 *
382 * @return string
383 */
384 public function getTitle() {
385 return ts('Test');
386 }
387
388 public function postProcess() {
389 }
390
391 }