Merge pull request #19463 from colemanw/removeCampaignPseudoconstant
[civicrm-core.git] / CRM / Mailing / Page / Preview.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
19 * a page for mailing preview
20 */
21class CRM_Mailing_Page_Preview extends CRM_Core_Page {
22
23 /**
100fef9d 24 * Run this page (figure out the action needed and perform it).
6a488035 25 */
00be9182 26 public function run() {
6a488035
TO
27
28 $session = CRM_Core_Session::singleton();
29
30 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', CRM_Core_DAO::$_nullObject, FALSE, 'text');
31 $type = CRM_Utils_Request::retrieve('type', 'String', CRM_Core_DAO::$_nullObject, FALSE, 'text');
32
be2fb01f 33 $options = [];
6a488035
TO
34 $session->getVars($options, "CRM_Mailing_Controller_Send_$qfKey");
35
25606795 36 // get the options if control come from search context, CRM-3711
6a488035
TO
37 if (empty($options)) {
38 $session->getVars($options, "CRM_Contact_Controller_Search_$qfKey");
39 }
40
41 // FIXME: the below and CRM_Mailing_Form_Test::testMail()
42 // should be refactored
43 $fromEmail = NULL;
44 $mailing = new CRM_Mailing_BAO_Mailing();
45 if (!empty($options)) {
46 $mailing->id = $options['mailing_id'];
9c1bc317 47 $fromEmail = $options['from_email'] ?? NULL;
6a488035
TO
48 }
49
50 $mailing->find(TRUE);
51
52 CRM_Mailing_BAO_Mailing::tokenReplace($mailing);
53
54 // get and format attachments
55 $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_mailing',
56 $mailing->id
57 );
58
25606795 59 // get details of contact with token value including Custom Field Token Values.CRM-3734
6a488035 60 $returnProperties = $mailing->getReturnProperties();
be2fb01f 61 $params = ['contact_id' => $session->get('userID')];
6a488035 62
57ff8466 63 [$details] = CRM_Utils_Token::getTokenDetails($params,
6a488035
TO
64 $returnProperties,
65 TRUE, TRUE, NULL,
66 $mailing->getFlattenedTokens(),
67 get_class($this)
68 );
57ff8466 69 // $details is an array of [ contactID => contactDetails ]
6a488035 70 $mime = &$mailing->compose(NULL, NULL, NULL, $session->get('userID'), $fromEmail, $fromEmail,
57ff8466 71 TRUE, $details[$session->get('userID')], $attachments
6a488035
TO
72 );
73
74 if ($type == 'html') {
d42a224c 75 CRM_Utils_System::setHttpHeader('Content-Type', 'text/html; charset=utf-8');
6a488035
TO
76 print $mime->getHTMLBody();
77 }
78 else {
d42a224c 79 CRM_Utils_System::setHttpHeader('Content-Type', 'text/plain; charset=utf-8');
6a488035
TO
80 print $mime->getTXTBody();
81 }
82 CRM_Utils_System::civiExit();
83 }
96025800 84
6a488035 85}