Merge pull request #15962 from seamuslee001/dev_core_1422
[civicrm-core.git] / CRM / Mailing / Page / Preview.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33
34/**
35 * a page for mailing preview
36 */
37class CRM_Mailing_Page_Preview extends CRM_Core_Page {
38
39 /**
100fef9d 40 * Run this page (figure out the action needed and perform it).
6a488035 41 */
00be9182 42 public function run() {
6a488035
TO
43
44 $session = CRM_Core_Session::singleton();
45
46 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', CRM_Core_DAO::$_nullObject, FALSE, 'text');
47 $type = CRM_Utils_Request::retrieve('type', 'String', CRM_Core_DAO::$_nullObject, FALSE, 'text');
48
be2fb01f 49 $options = [];
6a488035
TO
50 $session->getVars($options, "CRM_Mailing_Controller_Send_$qfKey");
51
25606795 52 // get the options if control come from search context, CRM-3711
6a488035
TO
53 if (empty($options)) {
54 $session->getVars($options, "CRM_Contact_Controller_Search_$qfKey");
55 }
56
57 // FIXME: the below and CRM_Mailing_Form_Test::testMail()
58 // should be refactored
59 $fromEmail = NULL;
60 $mailing = new CRM_Mailing_BAO_Mailing();
61 if (!empty($options)) {
62 $mailing->id = $options['mailing_id'];
63 $fromEmail = CRM_Utils_Array::value('from_email', $options);
64 }
65
66 $mailing->find(TRUE);
67
68 CRM_Mailing_BAO_Mailing::tokenReplace($mailing);
69
70 // get and format attachments
71 $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_mailing',
72 $mailing->id
73 );
74
25606795 75 // get details of contact with token value including Custom Field Token Values.CRM-3734
6a488035 76 $returnProperties = $mailing->getReturnProperties();
be2fb01f 77 $params = ['contact_id' => $session->get('userID')];
6a488035
TO
78
79 $details = CRM_Utils_Token::getTokenDetails($params,
80 $returnProperties,
81 TRUE, TRUE, NULL,
82 $mailing->getFlattenedTokens(),
83 get_class($this)
84 );
85
86 $mime = &$mailing->compose(NULL, NULL, NULL, $session->get('userID'), $fromEmail, $fromEmail,
87 TRUE, $details[0][$session->get('userID')], $attachments
88 );
89
90 if ($type == 'html') {
d42a224c 91 CRM_Utils_System::setHttpHeader('Content-Type', 'text/html; charset=utf-8');
6a488035
TO
92 print $mime->getHTMLBody();
93 }
94 else {
d42a224c 95 CRM_Utils_System::setHttpHeader('Content-Type', 'text/plain; charset=utf-8');
6a488035
TO
96 print $mime->getTXTBody();
97 }
98 CRM_Utils_System::civiExit();
99 }
96025800 100
6a488035 101}