Merge pull request #4657 from monishdeb/CRM-14512
[civicrm-core.git] / CRM / Mailing / Form / Upload.php
CommitLineData
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 * This file is used to build the form configuring mailing details
38 */
39class CRM_Mailing_Form_Upload extends CRM_Core_Form {
320e1703 40
6a488035 41 public $_mailingID;
03e04002 42
6a488035
TO
43 function preProcess() {
44 $this->_mailingID = $this->get('mailing_id');
45 if (CRM_Core_Permission::check('administer CiviCRM')) {
46 $this->assign('isAdmin', 1);
47 }
48
49 //when user come from search context.
50 $ssID = $this->get('ssID');
51 $this->assign('ssid',$ssID);
52 $this->_searchBasedMailing = CRM_Contact_Form_Search::isSearchContext($this->get('context'));
53 if(CRM_Contact_Form_Search::isSearchContext($this->get('context')) && !$ssID){
54 $params = array();
55 $result = CRM_Core_BAO_PrevNextCache::getSelectedContacts();
56 $this->assign("value", $result);
57 }
58 }
59 /**
c490a46a 60 * Set default values for the form.
6a488035
TO
61 * the default values are retrieved from the database
62 *
63 * @access public
64 *
355ba699 65 * @return void
6a488035
TO
66 */
67 function setDefaultValues() {
68 $mailingID = CRM_Utils_Request::retrieve('mid', 'Integer', $this, FALSE, NULL);
69
70 //need to differentiate new/reuse mailing, CRM-2873
71 $reuseMailing = FALSE;
72 if ($mailingID) {
73 $reuseMailing = TRUE;
74 }
75 else {
76 $mailingID = $this->_mailingID;
77 }
78
79 $count = $this->get('count');
80 $this->assign('count', $count);
81
82 $this->set('skipTextFile', FALSE);
83 $this->set('skipHtmlFile', FALSE);
84
85 $defaults = array();
86
87 $htmlMessage = NULL;
88 if ($mailingID) {
89 $dao = new CRM_Mailing_DAO_Mailing();
90 $dao->id = $mailingID;
91 $dao->find(TRUE);
92 $dao->storeValues($dao, $defaults);
93
94 //we don't want to retrieve template details once it is
95 //set in session
96 $templateId = $this->get('template');
97 $this->assign('templateSelected', $templateId ? $templateId : 0);
98 if (isset($defaults['msg_template_id']) && !$templateId) {
99 $defaults['template'] = $defaults['msg_template_id'];
c6327d7d 100 $messageTemplate = new CRM_Core_DAO_MessageTemplate();
6a488035
TO
101 $messageTemplate->id = $defaults['msg_template_id'];
102 $messageTemplate->selectAdd();
103 $messageTemplate->selectAdd('msg_text, msg_html');
104 $messageTemplate->find(TRUE);
105
106 $defaults['text_message'] = $messageTemplate->msg_text;
107 $htmlMessage = $messageTemplate->msg_html;
108 }
109
110 if (isset($defaults['body_text'])) {
111 $defaults['text_message'] = $defaults['body_text'];
112 $this->set('textFile', $defaults['body_text']);
113 $this->set('skipTextFile', TRUE);
114 }
115
116 if (isset($defaults['body_html'])) {
117 $htmlMessage = $defaults['body_html'];
118 $this->set('htmlFile', $defaults['body_html']);
119 $this->set('skipHtmlFile', TRUE);
120 }
121
122 //set default from email address.
8cc574cf 123 if (!empty($defaults['from_name']) && !empty($defaults['from_email'])) {
6a488035
TO
124 $defaults['from_email_address'] = array_search('"' . $defaults['from_name'] . '" <' . $defaults['from_email'] . '>',
125 CRM_Core_OptionGroup::values('from_email_address')
126 );
127 }
128 else {
129 //get the default from email address.
130 $defaultAddress = CRM_Core_OptionGroup::values('from_email_address', NULL, NULL, NULL, ' AND is_default = 1');
131 foreach ($defaultAddress as $id => $value) {
132 $defaults['from_email_address'] = $id;
133 }
134 }
135
a7488080 136 if (!empty($defaults['replyto_email'])) {
6a488035
TO
137 $replyToEmail = CRM_Core_OptionGroup::values('from_email_address');
138 foreach ($replyToEmail as $value) {
139 if (strstr($value, $defaults['replyto_email'])) {
140 $replyToEmailAddress = $value;
141 break;
142 }
143 }
144 $replyToEmailAddress = explode('<', $replyToEmailAddress);
cc229ff1 145 if (count($replyToEmailAddress) > 1) {
146 $replyToEmailAddress = $replyToEmailAddress[0] . '<' . $replyToEmailAddress[1];
147 }
148 $defaults['reply_to_address'] = array_search($replyToEmailAddress, $replyToEmail);
6a488035
TO
149 }
150 }
151
152 //fix for CRM-2873
153 if (!$reuseMailing) {
154 $textFilePath = $this->get('textFilePath');
155 if ($textFilePath &&
156 file_exists($textFilePath)
157 ) {
158 $defaults['text_message'] = file_get_contents($textFilePath);
159 if (strlen($defaults['text_message']) > 0) {
160 $this->set('skipTextFile', TRUE);
161 }
162 }
163
164 $htmlFilePath = $this->get('htmlFilePath');
165 if ($htmlFilePath &&
166 file_exists($htmlFilePath)
167 ) {
168 $defaults['html_message'] = file_get_contents($htmlFilePath);
169 if (strlen($defaults['html_message']) > 0) {
170 $htmlMessage = $defaults['html_message'];
171 $this->set('skipHtmlFile', TRUE);
172 }
173 }
174 }
175
176 if ($this->get('html_message')) {
177 $htmlMessage = $this->get('html_message');
178 }
179
180 $htmlMessage = str_replace(array("\n", "\r"), ' ', $htmlMessage);
181 $htmlMessage = str_replace("'", "\'", $htmlMessage);
182 $this->assign('message_html', $htmlMessage);
183
184 $defaults['upload_type'] = 1;
185 if (isset($defaults['body_html'])) {
186 $defaults['html_message'] = $defaults['body_html'];
187 }
188
189 //CRM-4678 setdefault to default component when composing new mailing.
190 if (!$reuseMailing) {
191 $componentFields = array(
192 'header_id' => 'Header',
193 'footer_id' => 'Footer',
194 );
195 foreach ($componentFields as $componentVar => $componentType) {
196 $defaults[$componentVar] = CRM_Mailing_PseudoConstant::defaultComponent($componentType, '');
197 }
198 }
199
200 return $defaults;
201 }
202
203 /**
c490a46a 204 * Build the form object
6a488035 205 *
355ba699 206 * @return void
6a488035
TO
207 * @access public
208 */
209 public function buildQuickForm() {
210 $session = CRM_Core_Session::singleton();
211 $config = CRM_Core_Config::singleton();
212 $options = array();
213 $tempVar = FALSE;
214
215 // this seems so hacky, not sure what we are doing here and why. Need to investigate and fix
216 $session->getVars($options,
217 "CRM_Mailing_Controller_Send_{$this->controller->_key}"
218 );
219
cbf48754 220 $fromEmailAddress = CRM_Core_OptionGroup::values('from_email_address');
6a488035
TO
221 if (empty($fromEmailAddress)) {
222 //redirect user to enter from email address.
5c3522e8 223 $url = CRM_Utils_System::url('civicrm/admin/options/from_email_address', 'action=add&reset=1');
6a488035
TO
224 $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));
225 $session->setStatus($status, ts('Notice'));
226 }
227 else {
228 foreach ($fromEmailAddress as $key => $email) {
229 $fromEmailAddress[$key] = htmlspecialchars($fromEmailAddress[$key]);
230 }
231 }
232
233 $this->add('select', 'from_email_address',
234 ts('From Email Address'), array(
235 '' => '- select -') + $fromEmailAddress, TRUE
236 );
237
238 //Added code to add custom field as Reply-To on form when it is enabled from Mailer settings
8cc574cf 239 if (isset($config->replyTo) && !empty($config->replyTo) && empty($options['override_verp'])) {
6a488035
TO
240 $this->add('select', 'reply_to_address', ts('Reply-To'),
241 array('' => '- select -') + $fromEmailAddress
242 );
243 }
a7488080 244 elseif (!empty($options['override_verp'])) {
6a488035
TO
245 $trackReplies = TRUE;
246 $this->assign('trackReplies', $trackReplies);
247 }
248
249 $this->add('text', 'subject', ts('Mailing Subject'),
250 CRM_Core_DAO::getAttribute('CRM_Mailing_DAO_Mailing', 'subject'), TRUE
251 );
252
253 $attributes = array('onclick' => "showHideUpload();");
254 $options = array(ts('Upload Content'), ts('Compose On-screen'));
255
256 $this->addRadio('upload_type', ts('I want to'), $options, $attributes, "&nbsp;&nbsp;");
257
258 CRM_Mailing_BAO_Mailing::commonCompose($this);
259
260 $this->addElement('file', 'textFile', ts('Upload TEXT Message'), 'size=30 maxlength=60');
3a6b1049 261 $this->addUploadElement('textFile');
6a488035
TO
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');
265
266 $this->addElement('file', 'htmlFile', ts('Upload HTML Message'), 'size=30 maxlength=60');
3a6b1049 267 $this->addUploadElement('htmlFile');
6a488035
TO
268 $this->setMaxFileSize(1024 * 1024);
269 $this->addRule('htmlFile',
270 ts('File size should be less than %1 MByte(s)',
271 array(1 => 1)
272 ),
273 'maxfilesize',
274 1024 * 1024
275 );
276 $this->addRule('htmlFile', ts('File must be in UTF-8 encoding'), 'utf8File');
277
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'));
282 }
283
284 CRM_Core_BAO_File::buildAttachment($this,
285 'civicrm_mailing',
286 $this->_mailingID
287 );
288
289 $this->add('select', 'header_id', ts('Mailing Header'),
290 array('' => ts('- none -')) + CRM_Mailing_PseudoConstant::component('Header')
291 );
292
293 $this->add('select', 'footer_id', ts('Mailing Footer'),
294 array('' => ts('- none -')) + CRM_Mailing_PseudoConstant::component('Footer')
295 );
296
297 $this->addFormRule(array('CRM_Mailing_Form_Upload', 'formRule'), $this);
298
299 $buttons = array(
300 array('type' => 'back',
301 'name' => ts('<< Previous'),
302 ),
303 array(
304 'type' => 'upload',
305 'name' => ts('Next >>'),
306 'spacing' => '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;',
307 'isDefault' => TRUE,
308 ),
309 array(
310 'type' => 'upload',
311 'name' => ts('Save & Continue Later'),
312 'subName' => 'save',
313 ),
314 array(
315 'type' => 'cancel',
316 'name' => ts('Cancel'),
317 ),
318 );
319 $this->addButtons($buttons);
320 }
321
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');
326
327 $formValues = $this->controller->exportValues($this->_name);
328
329 foreach ($uploadParams as $key) {
a7488080 330 if (!empty($formValues[$key]) ||
6a488035
TO
331 in_array($key, array('header_id', 'footer_id'))
332 ) {
333 $params[$key] = $formValues[$key];
334 $this->set($key, $formValues[$key]);
335 }
336 }
337
338 if (!$formValues['upload_type']) {
339 foreach ($fileType as $key) {
340 $contents = NULL;
341 if (isset($formValues[$key]) &&
342 !empty($formValues[$key])
343 ) {
344 $contents = file_get_contents($formValues[$key]['name']);
345 $this->set($key, $formValues[$key]['name']);
346 }
347 if ($contents) {
348 $params['body_' . substr($key, 0, 4)] = $contents;
349 }
350 else {
351 $params['body_' . substr($key, 0, 4)] = 'NULL';
352 }
353 }
354 }
355 else {
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'];
361
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));
367
368 $params['body_html'] = $html_message;
369 $this->set('htmlFile', $params['body_html']);
370 $this->set('html_message', $params['body_html']);
371 }
372
373 $params['name'] = $this->get('name');
374
375 $session = CRM_Core_Session::singleton();
376 $params['contact_id'] = $session->get('userID');
377 $composeFields = array(
378 'template', 'saveTemplate',
379 'updateTemplate', 'saveTemplateName',
380 );
381 $msgTemplate = NULL;
382 //mail template is composed
383 if ($formValues['upload_type']) {
384 $composeParams = array();
385
386 foreach ($composeFields as $key) {
a7488080 387 if (!empty($formValues[$key])) {
6a488035
TO
388 $composeParams[$key] = $formValues[$key];
389 $this->set($key, $formValues[$key]);
390 }
391 }
392
a7488080 393 if (!empty($composeParams['updateTemplate'])) {
6a488035
TO
394 $templateParams = array(
395 'msg_text' => $text_message,
396 'msg_html' => $html_message,
397 'msg_subject' => $params['subject'],
398 'is_active' => TRUE,
399 );
400
401 $templateParams['id'] = $formValues['template'];
402
c6327d7d 403 $msgTemplate = CRM_Core_BAO_MessageTemplate::add($templateParams);
6a488035
TO
404 }
405
a7488080 406 if (!empty($composeParams['saveTemplate'])) {
6a488035
TO
407 $templateParams = array(
408 'msg_text' => $text_message,
409 'msg_html' => $html_message,
410 'msg_subject' => $params['subject'],
411 'is_active' => TRUE,
412 );
413
414 $templateParams['msg_title'] = $composeParams['saveTemplateName'];
415
c6327d7d 416 $msgTemplate = CRM_Core_BAO_MessageTemplate::add($templateParams);
6a488035
TO
417 }
418
419 if (isset($msgTemplate->id)) {
420 $params['msg_template_id'] = $msgTemplate->id;
421 }
422 else {
423 $params['msg_template_id'] = CRM_Utils_Array::value('template', $formValues);
424 }
425 $this->set('template', $params['msg_template_id']);
426 }
427
428 CRM_Core_BAO_File::formatAttachment($formValues,
429 $params,
430 'civicrm_mailing',
431 $this->_mailingID
432 );
433 $ids['mailing_id'] = $this->_mailingID;
434
435 //handle mailing from name & address.
436 $fromEmailAddress = CRM_Utils_Array::value($formValues['from_email_address'],
cbf48754 437 CRM_Core_OptionGroup::values('from_email_address')
6a488035
TO
438 );
439
440 //get the from email address
441 $params['from_email'] = CRM_Utils_Mail::pluckEmailFromHeader($fromEmailAddress);
442
443 //get the from Name
444 $params['from_name'] = CRM_Utils_Array::value(1, explode('"', $fromEmailAddress));
445
446 //Add Reply-To to headers
a7488080 447 if (!empty($formValues['reply_to_address'])) {
cbf48754 448 $replyToEmail = CRM_Core_OptionGroup::values('from_email_address');
6a488035
TO
449 $params['replyto_email'] = CRM_Utils_Array::value($formValues['reply_to_address'], $replyToEmail);
450 }
451
452 /* Build the mailing object */
453
454 CRM_Mailing_BAO_Mailing::create($params, $ids);
455
456 if (isset($this->_submitValues['_qf_Upload_upload_save']) &&
457 $this->_submitValues['_qf_Upload_upload_save'] == 'Save & Continue Later'
458 ) {
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';
465 }
466 elseif ($this->_action == CRM_Core_Action::PROFILE) {
467 $fragment = 'search/builder';
468 }
469 elseif ($this->_action == CRM_Core_Action::ADVANCED) {
470 $fragment = 'search/advanced';
471 }
472 else {
473 $fragment = 'search/custom';
474 }
475
476 $context = $this->get('context');
477 if (!CRM_Contact_Form_Search::isSearchContext($context)) {
478 $context = 'search';
479 }
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";
484 }
485
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');
490
491 // Redirect user to search.
492 $url = CRM_Utils_System::url('civicrm/contact/' . $fragment, $urlParams);
493 }
494 else {
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');
497 }
498 CRM_Core_Session::setStatus($status, ts('Mailing Saved'), 'success');
499 return $this->controller->setDestination($url);
500 }
501 }
502
503 /**
100fef9d 504 * Validation
6a488035
TO
505 *
506 * @param array $params (ref.) an assoc array of name/value pairs
507 *
2a6da8d7
EM
508 * @param $files
509 * @param $self
510 *
6a488035
TO
511 * @return mixed true or array of errors
512 * @access public
513 * @static
514 */
515 static function formRule($params, $files, $self) {
a7488080 516 if (!empty($_POST['_qf_Import_refresh'])) {
6a488035
TO
517 return TRUE;
518 }
519 $errors = array();
520 $template = CRM_Core_Smarty::singleton();
521
522
523 if (isset($params['html_message'])) {
524 $htmlMessage = str_replace(array("\n", "\r"), ' ', $params['html_message']);
525 $htmlMessage = str_replace("'", "\'", $htmlMessage);
526 $template->assign('htmlContent', $htmlMessage);
527 }
528
529 $domain = CRM_Core_BAO_Domain::getDomain();
530
531 $mailing = new CRM_Mailing_BAO_Mailing();
532 $mailing->id = $self->_mailingID;
533 $mailing->find(TRUE);
534
535 $session = CRM_Core_Session::singleton();
536 $values = array('contact_id' => $session->get('userID'),
537 'version' => 3,
538 );
539 require_once 'api/api.php';
540 $contact = civicrm_api('contact', 'get', $values);
541
542 //CRM-4524
543 $contact = reset($contact['values']);
544
545 $verp = array_flip(array('optOut', 'reply', 'unsubscribe', 'resubscribe', 'owner'));
546 foreach ($verp as $key => $value) {
547 $verp[$key]++;
548 }
549
550 $urls = array_flip(array('forward', 'optOutUrl', 'unsubscribeUrl', 'resubscribeUrl'));
551 foreach ($urls as $key => $value) {
552 $urls[$key]++;
553 }
554
555
556 // set $header and $footer
557 foreach (array(
558 'header', 'footer') as $part) {
559 $$part = array();
560 if ($params["{$part}_id"]) {
561 //echo "found<p>";
562 $component = new CRM_Mailing_BAO_Component();
563 $component->id = $params["{$part}_id"];
564 $component->find(TRUE);
565 ${$part}['textFile'] = $component->body_text;
566 ${$part}['htmlFile'] = $component->body_html;
567 $component->free();
568 }
569 else {
570 ${$part}['htmlFile'] = ${$part}['textFile'] = '';
571 }
572 }
573
574
575 $skipTextFile = $self->get('skipTextFile');
576 $skipHtmlFile = $self->get('skipHtmlFile');
577
578 if (!$params['upload_type']) {
579 if ((!isset($files['textFile']) || !file_exists($files['textFile']['tmp_name'])) &&
580 (!isset($files['htmlFile']) || !file_exists($files['htmlFile']['tmp_name']))
581 ) {
582 if (!($skipTextFile || $skipHtmlFile)) {
583 $errors['textFile'] = ts('Please provide either a Text or HTML formatted message - or both.');
584 }
585 }
586 }
587 else {
8cc574cf 588 if (empty($params['text_message']) && empty($params['html_message'])) {
6a488035
TO
589 $errors['html_message'] = ts('Please provide either a Text or HTML formatted message - or both.');
590 }
8cc574cf 591 if (!empty($params['saveTemplate']) && empty($params['saveTemplateName'])) {
6a488035
TO
592 $errors['saveTemplateName'] = ts('Please provide a Template Name.');
593 }
594 }
595
596 foreach (array(
597 'text', 'html') as $file) {
598 if (!$params['upload_type'] && !file_exists(CRM_Utils_Array::value('tmp_name', $files[$file . 'File']))) {
599 continue;
600 }
601 if ($params['upload_type'] && !$params[$file . '_message']) {
602 continue;
603 }
604
605 if (!$params['upload_type']) {
606 $str = file_get_contents($files[$file . 'File']['tmp_name']);
607 $name = $files[$file . 'File']['name'];
608 }
609 else {
610 $str = $params[$file . '_message'];
611 $str = ($file == 'html') ? str_replace('%7B', '{', str_replace('%7D', '}', $str)) : $str;
612 $name = $file . ' message';
613 }
614
615 /* append header/footer */
616
617 $str = $header[$file . 'File'] . $str . $footer[$file . 'File'];
618
619 $dataErrors = array();
620
621 /* First look for missing tokens */
622
9babb598
OB
623 if (!CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, 'disable_mandatory_tokens_check')) {
624 $err = CRM_Utils_Token::requiredTokens($str);
625 if ($err !== TRUE) {
626 foreach ($err as $token => $desc) {
627 $dataErrors[] = '<li>' . ts('This message is missing a required token - {%1}: %2',
628 array(1 => $token, 2 => $desc)
629 ) . '</li>';
630 }
6a488035
TO
631 }
632 }
633
634 /* Do a full token replacement on a dummy verp, the current
635 * contact and domain, and the first organization. */
636
637
638 // here we make a dummy mailing object so that we
639 // can retrieve the tokens that we need to replace
640 // so that we do get an invalid token error
641 // this is qute hacky and I hope that there might
642 // be a suggestion from someone on how to
643 // make it a bit more elegant
644
645 $dummy_mail = new CRM_Mailing_BAO_Mailing();
646 $mess = "body_{$file}";
647 $dummy_mail->$mess = $str;
648 $tokens = $dummy_mail->getTokens();
649
650 $str = CRM_Utils_Token::replaceSubscribeInviteTokens($str);
651 $str = CRM_Utils_Token::replaceDomainTokens($str, $domain, NULL, $tokens[$file]);
652 $str = CRM_Utils_Token::replaceMailingTokens($str, $mailing, NULL, $tokens[$file]);
653 $str = CRM_Utils_Token::replaceOrgTokens($str, $org);
654 $str = CRM_Utils_Token::replaceActionTokens($str, $verp, $urls, NULL, $tokens[$file]);
655 $str = CRM_Utils_Token::replaceContactTokens($str, $contact, NULL, $tokens[$file]);
656
657 $unmatched = CRM_Utils_Token::unmatchedTokens($str);
658
659 if (!empty($unmatched) && 0) {
660 foreach ($unmatched as $token) {
661 $dataErrors[] = '<li>' . ts('Invalid token code') . ' {' . $token . '}</li>';
662 }
663 }
664 if (!empty($dataErrors)) {
665 $errors[$file . 'File'] = ts('The following errors were detected in %1:', array(
666 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 }
668 }
669
c6327d7d 670 $templateName = CRM_Core_BAO_MessageTemplate::getMessageTemplates();
a7488080 671 if (!empty($params['saveTemplate']) && in_array(CRM_Utils_Array::value('saveTemplateName', $params), $templateName)
6a488035
TO
672 ) {
673 $errors['saveTemplate'] = ts('Duplicate Template Name.');
674 }
675 return empty($errors) ? TRUE : $errors;
676 }
677
678 /**
679 * Display Name of the form
680 *
681 * @access public
682 *
683 * @return string
684 */
685 public function getTitle() {
686 return ts('Mailing Content');
687 }
688}
689