3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
37 * This file is used to build the form configuring mailing details
39 class CRM_Mailing_Form_Upload
extends CRM_Core_Form
{
42 function preProcess() {
43 $this->_mailingID
= $this->get('mailing_id');
44 if (CRM_Core_Permission
::check('administer CiviCRM')) {
45 $this->assign('isAdmin', 1);
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);
59 * This function sets the default values for the form.
60 * the default values are retrieved from the database
66 function setDefaultValues() {
67 $mailingID = CRM_Utils_Request
::retrieve('mid', 'Integer', $this, FALSE, NULL);
69 //need to differentiate new/reuse mailing, CRM-2873
70 $reuseMailing = FALSE;
75 $mailingID = $this->_mailingID
;
78 $count = $this->get('count');
79 $this->assign('count', $count);
81 $this->set('skipTextFile', FALSE);
82 $this->set('skipHtmlFile', FALSE);
88 $dao = new CRM_Mailing_DAO_Mailing();
89 $dao->id
= $mailingID;
91 $dao->storeValues($dao, $defaults);
93 //we don't want to retrieve template details once it is
95 $templateId = $this->get('template');
96 $this->assign('templateSelected', $templateId ?
$templateId : 0);
97 if (isset($defaults['msg_template_id']) && !$templateId) {
98 $defaults['template'] = $defaults['msg_template_id'];
99 $messageTemplate = new CRM_Core_DAO_MessageTemplate();
100 $messageTemplate->id
= $defaults['msg_template_id'];
101 $messageTemplate->selectAdd();
102 $messageTemplate->selectAdd('msg_text, msg_html');
103 $messageTemplate->find(TRUE);
105 $defaults['text_message'] = $messageTemplate->msg_text
;
106 $htmlMessage = $messageTemplate->msg_html
;
109 if (isset($defaults['body_text'])) {
110 $defaults['text_message'] = $defaults['body_text'];
111 $this->set('textFile', $defaults['body_text']);
112 $this->set('skipTextFile', TRUE);
115 if (isset($defaults['body_html'])) {
116 $htmlMessage = $defaults['body_html'];
117 $this->set('htmlFile', $defaults['body_html']);
118 $this->set('skipHtmlFile', TRUE);
121 //set default from email address.
122 if (CRM_Utils_Array
::value('from_name', $defaults) && CRM_Utils_Array
::value('from_email', $defaults)) {
123 $defaults['from_email_address'] = array_search('"' . $defaults['from_name'] . '" <' . $defaults['from_email'] . '>',
124 CRM_Core_OptionGroup
::values('from_email_address')
128 //get the default from email address.
129 $defaultAddress = CRM_Core_OptionGroup
::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
130 foreach ($defaultAddress as $id => $value) {
131 $defaults['from_email_address'] = $id;
135 if (CRM_Utils_Array
::value('replyto_email', $defaults)) {
136 $replyToEmail = CRM_Core_OptionGroup
::values('from_email_address');
137 foreach ($replyToEmail as $value) {
138 if (strstr($value, $defaults['replyto_email'])) {
139 $replyToEmailAddress = $value;
143 $replyToEmailAddress = explode('<', $replyToEmailAddress);
144 if (count($replyToEmailAddress) > 1) {
145 $replyToEmailAddress = $replyToEmailAddress[0] . '<' . $replyToEmailAddress[1];
147 $defaults['reply_to_address'] = array_search($replyToEmailAddress, $replyToEmail);
152 if (!$reuseMailing) {
153 $textFilePath = $this->get('textFilePath');
155 file_exists($textFilePath)
157 $defaults['text_message'] = file_get_contents($textFilePath);
158 if (strlen($defaults['text_message']) > 0) {
159 $this->set('skipTextFile', TRUE);
163 $htmlFilePath = $this->get('htmlFilePath');
165 file_exists($htmlFilePath)
167 $defaults['html_message'] = file_get_contents($htmlFilePath);
168 if (strlen($defaults['html_message']) > 0) {
169 $htmlMessage = $defaults['html_message'];
170 $this->set('skipHtmlFile', TRUE);
175 if ($this->get('html_message')) {
176 $htmlMessage = $this->get('html_message');
179 $htmlMessage = str_replace(array("\n", "\r"), ' ', $htmlMessage);
180 $htmlMessage = str_replace("'", "\'", $htmlMessage);
181 $this->assign('message_html', $htmlMessage);
183 $defaults['upload_type'] = 1;
184 if (isset($defaults['body_html'])) {
185 $defaults['html_message'] = $defaults['body_html'];
188 //CRM-4678 setdefault to default component when composing new mailing.
189 if (!$reuseMailing) {
190 $componentFields = array(
191 'header_id' => 'Header',
192 'footer_id' => 'Footer',
194 foreach ($componentFields as $componentVar => $componentType) {
195 $defaults[$componentVar] = CRM_Mailing_PseudoConstant
::defaultComponent($componentType, '');
203 * Function to actually build the form
208 public function buildQuickForm() {
209 $session = CRM_Core_Session
::singleton();
210 $config = CRM_Core_Config
::singleton();
214 // this seems so hacky, not sure what we are doing here and why. Need to investigate and fix
215 $session->getVars($options,
216 "CRM_Mailing_Controller_Send_{$this->controller->_key}"
219 $fromEmailAddress = CRM_Core_OptionGroup
::values('from_email_address');
220 if (empty($fromEmailAddress)) {
221 //redirect user to enter from email address.
222 $url = CRM_Utils_System
::url('civicrm/admin/options/from_email_address', 'group=from_email_address&action=add&reset=1');
223 $status = ts("There is no valid from email address present. You can add here <a href='%1'>Add From Email Address.</a>", array(1 => $url));
224 $session->setStatus($status, ts('Notice'));
227 foreach ($fromEmailAddress as $key => $email) {
228 $fromEmailAddress[$key] = htmlspecialchars($fromEmailAddress[$key]);
232 $this->add('select', 'from_email_address',
233 ts('From Email Address'), array(
234 '' => '- select -') +
$fromEmailAddress, TRUE
237 //Added code to add custom field as Reply-To on form when it is enabled from Mailer settings
238 if (isset($config->replyTo
) && !empty($config->replyTo
) &&
239 ! CRM_Utils_Array
::value( 'override_verp', $options ) ) {
240 $this->add('select', 'reply_to_address', ts('Reply-To'),
241 array('' => '- select -') +
$fromEmailAddress
244 elseif (CRM_Utils_Array
::value('override_verp', $options)) {
245 $trackReplies = TRUE;
246 $this->assign('trackReplies', $trackReplies);
249 $this->add('text', 'subject', ts('Mailing Subject'),
250 CRM_Core_DAO
::getAttribute('CRM_Mailing_DAO_Mailing', 'subject'), TRUE
253 $attributes = array('onclick' => "showHideUpload();");
254 $options = array(ts('Upload Content'), ts('Compose On-screen'));
256 $this->addRadio('upload_type', ts('I want to'), $options, $attributes, " ");
258 CRM_Mailing_BAO_Mailing
::commonCompose($this);
260 $this->addElement('file', 'textFile', ts('Upload TEXT Message'), 'size=30 maxlength=60');
261 $this->addUploadElement('textFile');
262 $this->setMaxFileSize(1024 * 1024);
263 $this->addRule('textFile', ts('File size should be less than 1 MByte'), 'maxfilesize', 1024 * 1024);
264 $this->addRule('textFile', ts('File must be in UTF-8 encoding'), 'utf8File');
266 $this->addElement('file', 'htmlFile', ts('Upload HTML Message'), 'size=30 maxlength=60');
267 $this->addUploadElement('htmlFile');
268 $this->setMaxFileSize(1024 * 1024);
269 $this->addRule('htmlFile',
270 ts('File size should be less than %1 MByte(s)',
276 $this->addRule('htmlFile', ts('File must be in UTF-8 encoding'), 'utf8File');
278 //fix upload files when context is search. CRM-3711
279 $ssID = $this->get('ssID');
280 if ($this->_searchBasedMailing
&& $ssID) {
281 $this->set('uploadNames', array('textFile', 'htmlFile'));
284 CRM_Core_BAO_File
::buildAttachment($this,
289 $this->add('select', 'header_id', ts('Mailing Header'),
290 array('' => ts('- none -')) + CRM_Mailing_PseudoConstant
::component('Header')
293 $this->add('select', 'footer_id', ts('Mailing Footer'),
294 array('' => ts('- none -')) + CRM_Mailing_PseudoConstant
::component('Footer')
297 $this->addFormRule(array('CRM_Mailing_Form_Upload', 'formRule'), $this);
300 array('type' => 'back',
301 'name' => ts('<< Previous'),
305 'name' => ts('Next >>'),
306 'spacing' => ' ',
311 'name' => ts('Save & Continue Later'),
316 'name' => ts('Cancel'),
319 $this->addButtons($buttons);
322 public function postProcess() {
323 $params = $ids = array();
324 $uploadParams = array('header_id', 'footer_id', 'subject', 'from_name', 'from_email');
325 $fileType = array('textFile', 'htmlFile');
327 $formValues = $this->controller
->exportValues($this->_name
);
329 foreach ($uploadParams as $key) {
330 if (CRM_Utils_Array
::value($key, $formValues) ||
331 in_array($key, array('header_id', 'footer_id'))
333 $params[$key] = $formValues[$key];
334 $this->set($key, $formValues[$key]);
338 if (!$formValues['upload_type']) {
339 foreach ($fileType as $key) {
341 if (isset($formValues[$key]) &&
342 !empty($formValues[$key])
344 $contents = file_get_contents($formValues[$key]['name']);
345 $this->set($key, $formValues[$key]['name']);
348 $params['body_' . substr($key, 0, 4)] = $contents;
351 $params['body_' . substr($key, 0, 4)] = 'NULL';
356 $text_message = $formValues['text_message'];
357 $params['body_text'] = $text_message;
358 $this->set('textFile', $params['body_text']);
359 $this->set('text_message', $params['body_text']);
360 $html_message = $formValues['html_message'];
362 // dojo editor does some html conversion when tokens are
363 // inserted as links. Hence token replacement fails.
364 // this is hack to revert html conversion for { to %7B and
365 // } to %7D by dojo editor
366 $html_message = str_replace('%7B', '{', str_replace('%7D', '}', $html_message));
368 $params['body_html'] = $html_message;
369 $this->set('htmlFile', $params['body_html']);
370 $this->set('html_message', $params['body_html']);
373 $params['name'] = $this->get('name');
375 $session = CRM_Core_Session
::singleton();
376 $params['contact_id'] = $session->get('userID');
377 $composeFields = array(
378 'template', 'saveTemplate',
379 'updateTemplate', 'saveTemplateName',
382 //mail template is composed
383 if ($formValues['upload_type']) {
384 $composeParams = array();
386 foreach ($composeFields as $key) {
387 if (CRM_Utils_Array
::value($key, $formValues)) {
388 $composeParams[$key] = $formValues[$key];
389 $this->set($key, $formValues[$key]);
393 if (CRM_Utils_Array
::value('updateTemplate', $composeParams)) {
394 $templateParams = array(
395 'msg_text' => $text_message,
396 'msg_html' => $html_message,
397 'msg_subject' => $params['subject'],
401 $templateParams['id'] = $formValues['template'];
403 $msgTemplate = CRM_Core_BAO_MessageTemplate
::add($templateParams);
406 if (CRM_Utils_Array
::value('saveTemplate', $composeParams)) {
407 $templateParams = array(
408 'msg_text' => $text_message,
409 'msg_html' => $html_message,
410 'msg_subject' => $params['subject'],
414 $templateParams['msg_title'] = $composeParams['saveTemplateName'];
416 $msgTemplate = CRM_Core_BAO_MessageTemplate
::add($templateParams);
419 if (isset($msgTemplate->id
)) {
420 $params['msg_template_id'] = $msgTemplate->id
;
423 $params['msg_template_id'] = CRM_Utils_Array
::value('template', $formValues);
425 $this->set('template', $params['msg_template_id']);
428 CRM_Core_BAO_File
::formatAttachment($formValues,
433 $ids['mailing_id'] = $this->_mailingID
;
435 //handle mailing from name & address.
436 $fromEmailAddress = CRM_Utils_Array
::value($formValues['from_email_address'],
437 CRM_Core_OptionGroup
::values('from_email_address')
440 //get the from email address
441 $params['from_email'] = CRM_Utils_Mail
::pluckEmailFromHeader($fromEmailAddress);
444 $params['from_name'] = CRM_Utils_Array
::value(1, explode('"', $fromEmailAddress));
446 //Add Reply-To to headers
447 if (CRM_Utils_Array
::value('reply_to_address', $formValues)) {
448 $replyToEmail = CRM_Core_OptionGroup
::values('from_email_address');
449 $params['replyto_email'] = CRM_Utils_Array
::value($formValues['reply_to_address'], $replyToEmail);
452 /* Build the mailing object */
454 CRM_Mailing_BAO_Mailing
::create($params, $ids);
456 if (isset($this->_submitValues
['_qf_Upload_upload_save']) &&
457 $this->_submitValues
['_qf_Upload_upload_save'] == 'Save & Continue Later'
459 //when user perform mailing from search context
460 //redirect it to search result CRM-3711.
461 $ssID = $this->get('ssID');
462 if ($ssID && $this->_searchBasedMailing
) {
463 if ($this->_action
== CRM_Core_Action
::BASIC
) {
464 $fragment = 'search';
466 elseif ($this->_action
== CRM_Core_Action
::PROFILE
) {
467 $fragment = 'search/builder';
469 elseif ($this->_action
== CRM_Core_Action
::ADVANCED
) {
470 $fragment = 'search/advanced';
473 $fragment = 'search/custom';
476 $context = $this->get('context');
477 if (!CRM_Contact_Form_Search
::isSearchContext($context)) {
480 $urlParams = "force=1&reset=1&ssID={$ssID}&context={$context}";
481 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', $this);
482 if (CRM_Utils_Rule
::qfKey($qfKey)) {
483 $urlParams .= "&qfKey=$qfKey";
486 $session = CRM_Core_Session
::singleton();
487 $draftURL = CRM_Utils_System
::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1');
488 $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));
489 CRM_Core_Session
::setStatus($status, ts('Mailing Saved'), 'success');
491 // Redirect user to search.
492 $url = CRM_Utils_System
::url('civicrm/contact/' . $fragment, $urlParams);
495 $status = ts("Click the 'Continue' action to resume working on it.");
496 $url = CRM_Utils_System
::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1');
498 CRM_Core_Session
::setStatus($status, ts('Mailing Saved'), 'success');
499 return $this->controller
->setDestination($url);
504 * Function for validation
506 * @param array $params (ref.) an assoc array of name/value pairs
508 * @return mixed true or array of errors
512 static function formRule($params, $files, $self) {
513 if (CRM_Utils_Array
::value('_qf_Import_refresh', $_POST)) {
517 $template = CRM_Core_Smarty
::singleton();
520 if (isset($params['html_message'])) {
521 $htmlMessage = str_replace(array("\n", "\r"), ' ', $params['html_message']);
522 $htmlMessage = str_replace("'", "\'", $htmlMessage);
523 $template->assign('htmlContent', $htmlMessage);
526 $domain = CRM_Core_BAO_Domain
::getDomain();
528 $mailing = new CRM_Mailing_BAO_Mailing();
529 $mailing->id
= $self->_mailingID
;
530 $mailing->find(TRUE);
532 $session = CRM_Core_Session
::singleton();
533 $values = array('contact_id' => $session->get('userID'),
536 require_once 'api/api.php';
537 $contact = civicrm_api('contact', 'get', $values);
540 $contact = reset($contact['values']);
542 $verp = array_flip(array('optOut', 'reply', 'unsubscribe', 'resubscribe', 'owner'));
543 foreach ($verp as $key => $value) {
547 $urls = array_flip(array('forward', 'optOutUrl', 'unsubscribeUrl', 'resubscribeUrl'));
548 foreach ($urls as $key => $value) {
553 // set $header and $footer
555 'header', 'footer') as $part) {
557 if ($params["{$part}_id"]) {
559 $component = new CRM_Mailing_BAO_Component();
560 $component->id
= $params["{$part}_id"];
561 $component->find(TRUE);
562 $
{$part}['textFile'] = $component->body_text
;
563 $
{$part}['htmlFile'] = $component->body_html
;
567 $
{$part}['htmlFile'] = $
{$part}['textFile'] = '';
572 $skipTextFile = $self->get('skipTextFile');
573 $skipHtmlFile = $self->get('skipHtmlFile');
575 if (!$params['upload_type']) {
576 if ((!isset($files['textFile']) ||
!file_exists($files['textFile']['tmp_name'])) &&
577 (!isset($files['htmlFile']) ||
!file_exists($files['htmlFile']['tmp_name']))
579 if (!($skipTextFile ||
$skipHtmlFile)) {
580 $errors['textFile'] = ts('Please provide either a Text or HTML formatted message - or both.');
585 if (!CRM_Utils_Array
::value('text_message', $params) && !CRM_Utils_Array
::value('html_message', $params)) {
586 $errors['html_message'] = ts('Please provide either a Text or HTML formatted message - or both.');
588 if (CRM_Utils_Array
::value('saveTemplate', $params) && !CRM_Utils_Array
::value('saveTemplateName', $params)) {
589 $errors['saveTemplateName'] = ts('Please provide a Template Name.');
594 'text', 'html') as $file) {
595 if (!$params['upload_type'] && !file_exists(CRM_Utils_Array
::value('tmp_name', $files[$file . 'File']))) {
598 if ($params['upload_type'] && !$params[$file . '_message']) {
602 if (!$params['upload_type']) {
603 $str = file_get_contents($files[$file . 'File']['tmp_name']);
604 $name = $files[$file . 'File']['name'];
607 $str = $params[$file . '_message'];
608 $str = ($file == 'html') ?
str_replace('%7B', '{', str_replace('%7D', '}', $str)) : $str;
609 $name = $file . ' message';
612 /* append header/footer */
614 $str = $header[$file . 'File'] . $str . $footer[$file . 'File'];
616 $dataErrors = array();
618 /* First look for missing tokens */
620 if (!CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
, 'disable_mandatory_tokens_check')) {
621 $err = CRM_Utils_Token
::requiredTokens($str);
623 foreach ($err as $token => $desc) {
624 $dataErrors[] = '<li>' . ts('This message is missing a required token - {%1}: %2',
625 array(1 => $token, 2 => $desc)
631 /* Do a full token replacement on a dummy verp, the current
632 * contact and domain, and the first organization. */
635 // here we make a dummy mailing object so that we
636 // can retrieve the tokens that we need to replace
637 // so that we do get an invalid token error
638 // this is qute hacky and I hope that there might
639 // be a suggestion from someone on how to
640 // make it a bit more elegant
642 $dummy_mail = new CRM_Mailing_BAO_Mailing();
643 $mess = "body_{$file}";
644 $dummy_mail->$mess = $str;
645 $tokens = $dummy_mail->getTokens();
647 $str = CRM_Utils_Token
::replaceSubscribeInviteTokens($str);
648 $str = CRM_Utils_Token
::replaceDomainTokens($str, $domain, NULL, $tokens[$file]);
649 $str = CRM_Utils_Token
::replaceMailingTokens($str, $mailing, NULL, $tokens[$file]);
650 $str = CRM_Utils_Token
::replaceOrgTokens($str, $org);
651 $str = CRM_Utils_Token
::replaceActionTokens($str, $verp, $urls, NULL, $tokens[$file]);
652 $str = CRM_Utils_Token
::replaceContactTokens($str, $contact, NULL, $tokens[$file]);
654 $unmatched = CRM_Utils_Token
::unmatchedTokens($str);
656 if (!empty($unmatched) && 0) {
657 foreach ($unmatched as $token) {
658 $dataErrors[] = '<li>' . ts('Invalid token code') . ' {' . $token . '}</li>';
661 if (!empty($dataErrors)) {
662 $errors[$file . 'File'] = ts('The following errors were detected in %1:', array(
663 1 => $name)) . ' <ul>' . implode('', $dataErrors) . '</ul><br /><a href="' . CRM_Utils_System
::docURL2('Sample CiviMail Messages', TRUE, NULL, NULL, NULL, "wiki") . '" target="_blank">' . ts('More information on required tokens...') . '</a>';
667 $templateName = CRM_Core_BAO_MessageTemplate
::getMessageTemplates();
668 if (CRM_Utils_Array
::value('saveTemplate', $params)
669 && in_array(CRM_Utils_Array
::value('saveTemplateName', $params), $templateName)
671 $errors['saveTemplate'] = ts('Duplicate Template Name.');
673 return empty($errors) ?
TRUE : $errors;
677 * Display Name of the form
683 public function getTitle() {
684 return ts('Mailing Content');