Merge pull request #7258 from yashodha/CRM-15306
[civicrm-core.git] / CRM / Mailing / Page / View.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
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
6a488035 79 */
00be9182 80 public function run($id = NULL, $contactID = NULL, $print = TRUE, $allowID = FALSE) {
6a488035
TO
81 if (is_numeric($id)) {
82 $this->_mailingID = $id;
83 }
84 else {
85 $print = TRUE;
c57f36a1 86 $this->_mailingID = CRM_Utils_Request::retrieve('id', 'String', CRM_Core_DAO::$_nullObject, TRUE);
6a488035
TO
87 }
88
89 // # CRM-7651
90 // override contactID from the function level if passed in
91 if (isset($contactID) &&
92 is_numeric($contactID)
93 ) {
94 $this->_contactID = $contactID;
95 }
96 else {
97 $session = CRM_Core_Session::singleton();
98 $this->_contactID = $session->get('userID');
99 }
100
c57f36a1
PJ
101 // mailing key check
102 if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, 'hash_mailing_url')) {
103 $this->_mailing = new CRM_Mailing_BAO_Mailing();
104
105 if (!is_numeric($this->_mailingID)) {
106 $this->_mailing->hash = $this->_mailingID;
107 }
108 elseif (is_numeric($this->_mailingID)) {
109 $this->_mailing->id = $this->_mailingID;
110 // if mailing is present and associated hash is present
111 // while 'hash' is not been used for mailing view : throw 'permissionDenied'
112 if ($this->_mailing->find() &&
303aa3b3 113 CRM_Core_DAO::getFieldValue('CRM_Mailing_BAO_Mailing', $this->_mailingID, 'hash', 'id') &&
c57f36a1
PJ
114 !$allowID
115 ) {
116 CRM_Utils_System::permissionDenied();
117 return;
118 }
119 }
120 }
121 else {
122 $this->_mailing = new CRM_Mailing_BAO_Mailing();
123 $this->_mailing->id = $this->_mailingID;
124 }
6a488035
TO
125
126 if (!$this->_mailing->find(TRUE) ||
127 !$this->checkPermission()
128 ) {
129 CRM_Utils_System::permissionDenied();
130 return;
131 }
132
133 CRM_Mailing_BAO_Mailing::tokenReplace($this->_mailing);
134
135 // get and format attachments
136 $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_mailing',
137 $this->_mailing->id
138 );
139
140 // get contact detail and compose if contact id exists
54d1bc76 141 $returnProperties = $this->_mailing->getReturnProperties();
6a488035 142 if (isset($this->_contactID)) {
25606795 143 // get details of contact with token value including Custom Field Token Values.CRM-3734
72decf50
BS
144 $params = array('contact_id' => $this->_contactID);
145 $details = CRM_Utils_Token::getTokenDetails($params,
6a488035 146 $returnProperties,
72decf50 147 FALSE, TRUE, NULL,
6a488035
TO
148 $this->_mailing->getFlattenedTokens(),
149 get_class($this)
150 );
151 $details = $details[0][$this->_contactID];
72decf50 152 $contactId = $this->_contactID;
6a488035
TO
153 }
154 else {
25606795 155 // get tokens that are not contact specific resolved
353ffa53 156 $params = array('contact_id' => 0);
d20c4dad
EM
157 $details = CRM_Utils_Token::getAnonymousTokenDetails($params,
158 $returnProperties,
159 TRUE, TRUE, NULL,
160 $this->_mailing->getFlattenedTokens(),
161 get_class($this)
162 );
163
bdae0b8d 164 $details = CRM_Utils_Array::value(0, $details[0]);
72decf50 165 $contactId = 0;
6a488035 166 }
72decf50 167 $mime = &$this->_mailing->compose(NULL, NULL, NULL, $contactId,
6a488035
TO
168 $this->_mailing->from_email,
169 $this->_mailing->from_email,
170 TRUE, $details, $attachments
171 );
172
3f32bb8b 173 $title = NULL;
fc164be7 174 if (isset($this->_mailing->body_html) && empty($_GET['text'])) {
956d2f84 175 $header = 'text/html; charset=utf-8';
6a488035 176 $content = $mime->getHTMLBody();
3f32bb8b
DG
177 if (strpos($content, '<head>') === FALSE && strpos($content, '<title>') === FALSE) {
178 $title = '<head><title>' . $this->_mailing->subject . '</title></head>';
179 }
6a488035
TO
180 }
181 else {
956d2f84 182 $header = 'text/plain; charset=utf-8';
6a488035
TO
183 $content = $mime->getTXTBody();
184 }
fc164be7 185 CRM_Utils_System::setTitle($this->_mailing->subject);
6a488035 186
fc164be7
CW
187 if (CRM_Utils_Array::value('snippet', $_GET) === 'json') {
188 CRM_Core_Page_AJAX::returnJsonResponse($content);
189 }
6a488035 190 if ($print) {
956d2f84 191 CRM_Utils_System::setHttpHeader('Content-Type', $header);
3f32bb8b 192 print $title;
6a488035
TO
193 print $content;
194 CRM_Utils_System::civiExit();
195 }
196 else {
197 return $content;
198 }
199 }
96025800 200
6a488035 201}