Merge pull request #11757 from eileenmcnaughton/manual
[civicrm-core.git] / CRM / Mailing / Page / View.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
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
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
6a488035
TO
32 */
33
34/**
25606795 35 * A page for mailing preview.
6a488035
TO
36 */
37class CRM_Mailing_Page_View extends CRM_Core_Page {
38 protected $_mailingID;
39 protected $_mailing;
40 protected $_contactID;
41
42 /**
fe482240 43 * Lets do permission checking here.
25606795
SB
44 * First check for valid mailing, if false return fatal.
45 * Second check for visibility.
46 * Call a hook to see if hook wants to override visibility setting.
6a488035 47 */
00be9182 48 public function checkPermission() {
6a488035
TO
49 if (!$this->_mailing) {
50 return FALSE;
51 }
52
53 // check for visibility, if visibility is Public Pages and they have the permission
54 // return true
55 if ($this->_mailing->visibility == 'Public Pages' &&
56 CRM_Core_Permission::check('view public CiviMail content')
57 ) {
58 return TRUE;
59 }
60
61 // if user is an admin, return true
62 if (CRM_Core_Permission::check('administer CiviCRM') ||
cb7b73f0 63 CRM_Core_Permission::check('approve mailings') ||
6a488035
TO
64 CRM_Core_Permission::check('access CiviMail')
65 ) {
66 return TRUE;
67 }
68
69 return FALSE;
70 }
71
72 /**
100fef9d 73 * Run this page (figure out the action needed and perform it).
6a488035 74 *
100fef9d
CW
75 * @param int $id
76 * @param int $contactID
77b97be7
EM
77 * @param bool $print
78 * @param bool $allowID
7535623a 79 *
80 * @return null|string
3b2bf3b5 81 * Not really sure if anything should be returned - parent doesn't
6a488035 82 */
00be9182 83 public function run($id = NULL, $contactID = NULL, $print = TRUE, $allowID = FALSE) {
6a488035
TO
84 if (is_numeric($id)) {
85 $this->_mailingID = $id;
86 }
87 else {
88 $print = TRUE;
c57f36a1 89 $this->_mailingID = CRM_Utils_Request::retrieve('id', 'String', CRM_Core_DAO::$_nullObject, TRUE);
6a488035
TO
90 }
91
92 // # CRM-7651
93 // override contactID from the function level if passed in
94 if (isset($contactID) &&
95 is_numeric($contactID)
96 ) {
97 $this->_contactID = $contactID;
98 }
99 else {
3bdcd4ec 100 $this->_contactID = CRM_Core_Session::getLoggedInContactID();
6a488035
TO
101 }
102
c57f36a1 103 // mailing key check
aaffa79f 104 if (Civi::settings()->get('hash_mailing_url')) {
c57f36a1
PJ
105 $this->_mailing = new CRM_Mailing_BAO_Mailing();
106
107 if (!is_numeric($this->_mailingID)) {
108 $this->_mailing->hash = $this->_mailingID;
109 }
110 elseif (is_numeric($this->_mailingID)) {
111 $this->_mailing->id = $this->_mailingID;
112 // if mailing is present and associated hash is present
113 // while 'hash' is not been used for mailing view : throw 'permissionDenied'
114 if ($this->_mailing->find() &&
303aa3b3 115 CRM_Core_DAO::getFieldValue('CRM_Mailing_BAO_Mailing', $this->_mailingID, 'hash', 'id') &&
c57f36a1
PJ
116 !$allowID
117 ) {
118 CRM_Utils_System::permissionDenied();
3b2bf3b5 119 return NULL;
c57f36a1
PJ
120 }
121 }
122 }
123 else {
124 $this->_mailing = new CRM_Mailing_BAO_Mailing();
125 $this->_mailing->id = $this->_mailingID;
126 }
6a488035
TO
127
128 if (!$this->_mailing->find(TRUE) ||
129 !$this->checkPermission()
130 ) {
131 CRM_Utils_System::permissionDenied();
3b2bf3b5 132 return NULL;
6a488035
TO
133 }
134
135 CRM_Mailing_BAO_Mailing::tokenReplace($this->_mailing);
136
137 // get and format attachments
138 $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_mailing',
139 $this->_mailing->id
140 );
141
142 // get contact detail and compose if contact id exists
54d1bc76 143 $returnProperties = $this->_mailing->getReturnProperties();
6a488035 144 if (isset($this->_contactID)) {
25606795 145 // get details of contact with token value including Custom Field Token Values.CRM-3734
72decf50
BS
146 $params = array('contact_id' => $this->_contactID);
147 $details = CRM_Utils_Token::getTokenDetails($params,
6a488035 148 $returnProperties,
72decf50 149 FALSE, TRUE, NULL,
6a488035
TO
150 $this->_mailing->getFlattenedTokens(),
151 get_class($this)
152 );
153 $details = $details[0][$this->_contactID];
72decf50 154 $contactId = $this->_contactID;
6a488035
TO
155 }
156 else {
25606795 157 // get tokens that are not contact specific resolved
353ffa53 158 $params = array('contact_id' => 0);
d20c4dad
EM
159 $details = CRM_Utils_Token::getAnonymousTokenDetails($params,
160 $returnProperties,
161 TRUE, TRUE, NULL,
162 $this->_mailing->getFlattenedTokens(),
163 get_class($this)
164 );
165
bdae0b8d 166 $details = CRM_Utils_Array::value(0, $details[0]);
72decf50 167 $contactId = 0;
6a488035 168 }
aaf1f8f4 169 $mime = $this->_mailing->compose(NULL, NULL, NULL, $contactId,
6a488035
TO
170 $this->_mailing->from_email,
171 $this->_mailing->from_email,
172 TRUE, $details, $attachments
173 );
174
3f32bb8b 175 $title = NULL;
fc164be7 176 if (isset($this->_mailing->body_html) && empty($_GET['text'])) {
956d2f84 177 $header = 'text/html; charset=utf-8';
6a488035 178 $content = $mime->getHTMLBody();
3f32bb8b
DG
179 if (strpos($content, '<head>') === FALSE && strpos($content, '<title>') === FALSE) {
180 $title = '<head><title>' . $this->_mailing->subject . '</title></head>';
181 }
6a488035
TO
182 }
183 else {
956d2f84 184 $header = 'text/plain; charset=utf-8';
6a488035
TO
185 $content = $mime->getTXTBody();
186 }
fc164be7 187 CRM_Utils_System::setTitle($this->_mailing->subject);
6a488035 188
fc164be7
CW
189 if (CRM_Utils_Array::value('snippet', $_GET) === 'json') {
190 CRM_Core_Page_AJAX::returnJsonResponse($content);
191 }
6a488035 192 if ($print) {
956d2f84 193 CRM_Utils_System::setHttpHeader('Content-Type', $header);
3f32bb8b 194 print $title;
6a488035
TO
195 print $content;
196 CRM_Utils_System::civiExit();
197 }
198 else {
199 return $content;
200 }
201 }
96025800 202
6a488035 203}