Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * a page for mailing preview | |
38 | */ | |
39 | class CRM_Mailing_Page_View extends CRM_Core_Page { | |
40 | protected $_mailingID; | |
41 | protected $_mailing; | |
42 | protected $_contactID; | |
43 | ||
44 | /** | |
45 | * Lets do permission checking here | |
46 | * First check for valid mailing, if false return fatal | |
47 | * Second check for visibility | |
48 | * Call a hook to see if hook wants to override visibility setting | |
49 | */ | |
50 | function checkPermission() { | |
51 | if (!$this->_mailing) { | |
52 | return FALSE; | |
53 | } | |
54 | ||
55 | // check for visibility, if visibility is Public Pages and they have the permission | |
56 | // return true | |
57 | if ($this->_mailing->visibility == 'Public Pages' && | |
58 | CRM_Core_Permission::check('view public CiviMail content') | |
59 | ) { | |
60 | return TRUE; | |
61 | } | |
62 | ||
63 | // if user is an admin, return true | |
64 | if (CRM_Core_Permission::check('administer CiviCRM') || | |
65 | CRM_Core_Permission::check('access CiviMail') | |
66 | ) { | |
67 | return TRUE; | |
68 | } | |
69 | ||
70 | return FALSE; | |
71 | } | |
72 | ||
73 | /** | |
74 | * run this page (figure out the action needed and perform it). | |
75 | * | |
76 | * @return void | |
77 | */ | |
78 | function run($id = NULL, $contact_id = NULL, $print = TRUE) { | |
79 | if (is_numeric($id)) { | |
80 | $this->_mailingID = $id; | |
81 | } | |
82 | else { | |
83 | $print = TRUE; | |
84 | $this->_mailingID = CRM_Utils_Request::retrieve('id', 'Integer', CRM_Core_DAO::$_nullObject, TRUE); | |
85 | } | |
86 | ||
87 | // # CRM-7651 | |
88 | // override contactID from the function level if passed in | |
89 | if (isset($contactID) && | |
90 | is_numeric($contactID) | |
91 | ) { | |
92 | $this->_contactID = $contactID; | |
93 | } | |
94 | else { | |
95 | $session = CRM_Core_Session::singleton(); | |
96 | $this->_contactID = $session->get('userID'); | |
97 | } | |
98 | ||
99 | $this->_mailing = new CRM_Mailing_BAO_Mailing(); | |
100 | $this->_mailing->id = $this->_mailingID; | |
101 | ||
102 | if (!$this->_mailing->find(TRUE) || | |
103 | !$this->checkPermission() | |
104 | ) { | |
105 | CRM_Utils_System::permissionDenied(); | |
106 | return; | |
107 | } | |
108 | ||
109 | CRM_Mailing_BAO_Mailing::tokenReplace($this->_mailing); | |
110 | ||
111 | // get and format attachments | |
112 | $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_mailing', | |
113 | $this->_mailing->id | |
114 | ); | |
115 | ||
116 | // get contact detail and compose if contact id exists | |
117 | if (isset($this->_contactID)) { | |
118 | //get details of contact with token value including Custom Field Token Values.CRM-3734 | |
119 | $returnProperties = $this->_mailing->getReturnProperties(); | |
120 | $params = array('contact_id' => $this->_contactID); | |
121 | $details = CRM_Utils_Token::getTokenDetails($params, | |
122 | $returnProperties, | |
123 | TRUE, TRUE, NULL, | |
124 | $this->_mailing->getFlattenedTokens(), | |
125 | get_class($this) | |
126 | ); | |
127 | $details = $details[0][$this->_contactID]; | |
128 | } | |
129 | else { | |
130 | $details = array('test'); | |
131 | } | |
132 | $mime = &$this->_mailing->compose(NULL, NULL, NULL, 0, | |
133 | $this->_mailing->from_email, | |
134 | $this->_mailing->from_email, | |
135 | TRUE, $details, $attachments | |
136 | ); | |
137 | ||
3f32bb8b | 138 | $title = NULL; |
6a488035 | 139 | if (isset($this->_mailing->body_html)) { |
3f32bb8b | 140 | |
6a488035 TO |
141 | $header = 'Content-Type: text/html; charset=utf-8'; |
142 | $content = $mime->getHTMLBody(); | |
3f32bb8b DG |
143 | if (strpos($content, '<head>') === FALSE && strpos($content, '<title>') === FALSE) { |
144 | $title = '<head><title>' . $this->_mailing->subject . '</title></head>'; | |
145 | } | |
6a488035 TO |
146 | } |
147 | else { | |
148 | $header = 'Content-Type: text/plain; charset=utf-8'; | |
149 | $content = $mime->getTXTBody(); | |
150 | } | |
151 | ||
152 | if ($print) { | |
153 | header($header); | |
3f32bb8b | 154 | print $title; |
6a488035 TO |
155 | print $content; |
156 | CRM_Utils_System::civiExit(); | |
157 | } | |
158 | else { | |
159 | return $content; | |
160 | } | |
161 | } | |
162 | } | |
163 |