Merge pull request #4955 from atif-shaikh/master-cleanup
[civicrm-core.git] / CRM / Mailing / Form / Approve.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 *
38 */
39 class CRM_Mailing_Form_Approve extends CRM_Core_Form {
40
41 public function redirectToListing() {
42 $url = CRM_Utils_System::url('civicrm/mailing/browse/scheduled', 'reset=1&scheduled=true');
43 CRM_Utils_System::redirect($url);
44 }
45
46 /**
47 * Set variables up before form is built
48 *
49 * @return void
50 */
51 public function preProcess() {
52 if (CRM_Mailing_Info::workflowEnabled()) {
53 if (!CRM_Core_Permission::check('approve mailings')) {
54 $this->redirectToListing();
55 }
56 }
57 else {
58 $this->redirectToListing();
59 }
60
61 // when user come from search context.
62 $this->_searchBasedMailing = CRM_Contact_Form_Search::isSearchContext($this->get('context'));
63
64 //retrieve mid from different wizard and url contexts
65 $this->_mailingID = $this->get('mailing_id');
66 $this->_approveFormOnly = FALSE;
67 if (!$this->_mailingID) {
68 $this->_mailingID = CRM_Utils_Request::retrieve('mid', 'Integer', $this, TRUE);
69 $this->_approveFormOnly = TRUE;
70 }
71
72 $session = CRM_Core_Session::singleton();
73 $this->_contactID = $session->get('userID');
74
75 $this->_mailing = new CRM_Mailing_BAO_Mailing();
76 $this->_mailing->id = $this->_mailingID;
77 if (!$this->_mailing->find(TRUE)) {
78 $this->redirectToListing();
79 }
80 }
81
82 /**
83 * Set default values for the form.
84 *
85 *
86 * @return void
87 */
88 public function setDefaultValues() {
89 $defaults = array();
90 if ($this->_mailingID) {
91 $defaults['approval_status_id'] = $this->_mailing->approval_status_id;
92 $defaults['approval_note'] = $this->_mailing->approval_note;
93 }
94
95 return $defaults;
96 }
97
98 /**
99 * Build the form object for the approval/rejection mailing
100 *
101 * @param
102 *
103 * @return void
104 */
105 public function buildQuickform() {
106 $title = ts('Approve/Reject Mailing') . " - {$this->_mailing->name}";
107 CRM_Utils_System::setTitle($title);
108
109 $this->addElement('textarea', 'approval_note', ts('Approve/Reject Note'));
110
111 $mailApprovalStatus = CRM_Core_OptionGroup::values('mail_approval_status');
112
113 // eliminate the none option
114 $noneOptionID = CRM_Core_OptionGroup::getValue('mail_approval_status',
115 'None',
116 'name'
117 );
118 if ($noneOptionID) {
119 unset($mailApprovalStatus[$noneOptionID]);
120 }
121
122 $this->addRadio('approval_status_id', ts('Approval Status'), $mailApprovalStatus, TRUE, NULL, TRUE);
123
124 $buttons = array(
125 array(
126 'type' => 'next',
127 'name' => ts('Save'),
128 'spacing' => '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;',
129 'isDefault' => TRUE,
130 ),
131 array(
132 'type' => 'cancel',
133 'name' => ts('Cancel'),
134 ),
135 );
136
137 $this->addButtons($buttons);
138
139 // add the preview elements
140 $preview = array();
141
142 $preview['subject'] = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_Mailing',
143 $this->_mailingID,
144 'subject'
145 );
146
147 $mailingKey = $this->_mailingID;
148 if ($hash = CRM_Mailing_BAO_Mailing::getMailingHash($mailingKey)) {
149 $mailingKey = $hash;
150 }
151
152 $preview['viewURL'] = CRM_Utils_System::url('civicrm/mailing/view', "reset=1&id={$mailingKey}");
153 $preview['type'] = $this->_mailing->body_html ? 'html' : 'text';
154 $preview['attachment'] = CRM_Core_BAO_File::attachmentInfo('civicrm_mailing', $this->_mailingID);
155
156 $this->assign_by_ref('preview', $preview);
157 }
158
159 /**
160 * Process the posted form values. Approve /reject a mailing.
161 *
162 * @param
163 *
164 * @return void
165 */
166 public function postProcess() {
167 // get the submitted form values.
168 $params = $this->controller->exportValues($this->_name);
169
170 $ids = array();
171 if (isset($this->_mailingID)) {
172 $ids['mailing_id'] = $this->_mailingID;
173 }
174 else {
175 $ids['mailing_id'] = $this->get('mailing_id');
176 }
177
178 if (!$ids['mailing_id']) {
179 CRM_Core_Error::fatal();
180 }
181
182 $params['approver_id'] = $this->_contactID;
183 $params['approval_date'] = date('YmdHis');
184
185 // if rejected, then we need to reset the scheduled date and scheduled id
186 $rejectOptionID = CRM_Core_OptionGroup::getValue('mail_approval_status',
187 'Rejected',
188 'name'
189 );
190 if ($rejectOptionID &&
191 $params['approval_status_id'] == $rejectOptionID
192 ) {
193 $params['scheduled_id'] = 'null';
194 $params['scheduled_date'] = 'null';
195
196 // also delete any jobs associated with this mailing
197 $job = new CRM_Mailing_BAO_MailingJob();
198 $job->mailing_id = $ids['mailing_id'];
199 $job->delete();
200 }
201 else {
202 $mailing = new CRM_Mailing_BAO_Mailing();
203 $mailing->id = $ids['mailing_id'];
204 $mailing->find(TRUE);
205
206 $params['scheduled_date'] = CRM_Utils_Date::processDate($mailing->scheduled_date);
207 }
208
209 CRM_Mailing_BAO_Mailing::create($params, $ids);
210
211 //when user perform mailing from search context
212 //redirect it to search result CRM-3711
213 $ssID = $this->get('ssID');
214 if ($ssID && $this->_searchBasedMailing) {
215 if ($this->_action == CRM_Core_Action::BASIC) {
216 $fragment = 'search';
217 }
218 elseif ($this->_action == CRM_Core_Action::PROFILE) {
219 $fragment = 'search/builder';
220 }
221 elseif ($this->_action == CRM_Core_Action::ADVANCED) {
222 $fragment = 'search/advanced';
223 }
224 else {
225 $fragment = 'search/custom';
226 }
227 $context = $this->get('context');
228 if (!CRM_Contact_Form_Search::isSearchContext($context)) {
229 $context = 'search';
230 }
231 $urlParams = "force=1&reset=1&ssID={$ssID}&context={$context}";
232 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this);
233 if (CRM_Utils_Rule::qfKey($qfKey)) {
234 $urlParams .= "&qfKey=$qfKey";
235 }
236
237 $url = CRM_Utils_System::url('civicrm/contact/' . $fragment, $urlParams);
238 return $this->controller->setDestination($url);
239 }
240
241 $session = CRM_Core_Session::singleton();
242 $session->pushUserContext(CRM_Utils_System::url('civicrm/mailing/browse/scheduled',
243 'reset=1&scheduled=true'
244 ));
245 }
246
247 /**
248 * Display Name of the form
249 *
250 *
251 * @return string
252 */
253 public function getTitle() {
254 return ts('Approve/Reject Mailing');
255 }
256 }