3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
35 * A page for mailing preview.
37 class CRM_Mailing_Page_View
extends CRM_Core_Page
{
38 protected $_mailingID;
40 protected $_contactID;
43 * Lets do permission checking here.
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.
48 public function checkPermission() {
49 if (!$this->_mailing
) {
53 // check for visibility, if visibility is Public Pages and they have the permission
55 if ($this->_mailing
->visibility
== 'Public Pages' &&
56 CRM_Core_Permission
::check('view public CiviMail content')
61 // if user is an admin, return true
62 if (CRM_Core_Permission
::check('administer CiviCRM') ||
63 CRM_Core_Permission
::check('approve mailings') ||
64 CRM_Core_Permission
::check('access CiviMail')
73 * Run this page (figure out the action needed and perform it).
76 * @param int $contactID
78 * @param bool $allowID
81 * Not really sure if anything should be returned - parent doesn't
83 public function run($id = NULL, $contactID = NULL, $print = TRUE, $allowID = FALSE) {
84 if (is_numeric($id)) {
85 $this->_mailingID
= $id;
89 $this->_mailingID
= CRM_Utils_Request
::retrieve('id', 'String', CRM_Core_DAO
::$_nullObject, TRUE);
93 // override contactID from the function level if passed in
94 if (isset($contactID) &&
95 is_numeric($contactID)
97 $this->_contactID
= $contactID;
100 $this->_contactID
= CRM_Core_Session
::getLoggedInContactID();
104 if (Civi
::settings()->get('hash_mailing_url')) {
105 $this->_mailing
= new CRM_Mailing_BAO_Mailing();
107 if (!is_numeric($this->_mailingID
)) {
108 $this->_mailing
->hash
= $this->_mailingID
;
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() &&
115 CRM_Core_DAO
::getFieldValue('CRM_Mailing_BAO_Mailing', $this->_mailingID
, 'hash', 'id') &&
118 CRM_Utils_System
::permissionDenied();
124 $this->_mailing
= new CRM_Mailing_BAO_Mailing();
125 $this->_mailing
->id
= $this->_mailingID
;
128 if (!$this->_mailing
->find(TRUE) ||
129 !$this->checkPermission()
131 CRM_Utils_System
::permissionDenied();
135 CRM_Mailing_BAO_Mailing
::tokenReplace($this->_mailing
);
137 // get and format attachments
138 $attachments = CRM_Core_BAO_File
::getEntityFile('civicrm_mailing',
142 // get contact detail and compose if contact id exists
143 $returnProperties = $this->_mailing
->getReturnProperties();
144 if (isset($this->_contactID
)) {
145 // get details of contact with token value including Custom Field Token Values.CRM-3734
146 $params = array('contact_id' => $this->_contactID
);
147 $details = CRM_Utils_Token
::getTokenDetails($params,
150 $this->_mailing
->getFlattenedTokens(),
153 $details = $details[0][$this->_contactID
];
154 $contactId = $this->_contactID
;
157 // get tokens that are not contact specific resolved
158 $params = array('contact_id' => 0);
159 $details = CRM_Utils_Token
::getAnonymousTokenDetails($params,
162 $this->_mailing
->getFlattenedTokens(),
166 $details = CRM_Utils_Array
::value(0, $details[0]);
169 $mime = $this->_mailing
->compose(NULL, NULL, NULL, $contactId,
170 $this->_mailing
->from_email
,
171 $this->_mailing
->from_email
,
172 TRUE, $details, $attachments
176 if (isset($this->_mailing
->body_html
) && empty($_GET['text'])) {
177 $header = 'text/html; charset=utf-8';
178 $content = $mime->getHTMLBody();
179 if (strpos($content, '<head>') === FALSE && strpos($content, '<title>') === FALSE) {
180 $title = '<head><title>' . $this->_mailing
->subject
. '</title></head>';
184 $header = 'text/plain; charset=utf-8';
185 $content = $mime->getTXTBody();
187 CRM_Utils_System
::setTitle($this->_mailing
->subject
);
189 if (CRM_Utils_Array
::value('snippet', $_GET) === 'json') {
190 CRM_Core_Page_AJAX
::returnJsonResponse($content);
193 CRM_Utils_System
::setHttpHeader('Content-Type', $header);
196 CRM_Utils_System
::civiExit();