Merge pull request #4696 from colemanw/CRM-15669
[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
62 // when user come from search context.
63 $this->_searchBasedMailing = CRM_Contact_Form_Search::isSearchContext($this->get('context'));
64
65 //retrieve mid from different wizard and url contexts
66 $this->_mailingID = $this->get('mailing_id');
67 $this->_approveFormOnly = FALSE;
68 if (!$this->_mailingID) {
69 $this->_mailingID = CRM_Utils_Request::retrieve('mid', 'Integer', $this, TRUE);
70 $this->_approveFormOnly = TRUE;
71 }
72
73 $session = CRM_Core_Session::singleton();
74 $this->_contactID = $session->get('userID');
75
76 $this->_mailing = new CRM_Mailing_BAO_Mailing();
77 $this->_mailing->id = $this->_mailingID;
78 if (!$this->_mailing->find(TRUE)) {
79 $this->redirectToListing();
80 }
81 }
82
83 /**
84 * Set default values for the form.
85 *
86 *
87 * @return void
88 */
89 public function setDefaultValues() {
90 $defaults = array();
91 if ($this->_mailingID) {
92 $defaults['approval_status_id'] = $this->_mailing->approval_status_id;
93 $defaults['approval_note'] = $this->_mailing->approval_note;
94 }
95
96 return $defaults;
97 }
98
99 /**
100 * Build the form object for the approval/rejection mailing
101 *
102 * @param
103 *
104 * @return void
105 */
106 public function buildQuickform() {
107 $title = ts('Approve/Reject Mailing') . " - {$this->_mailing->name}";
108 CRM_Utils_System::setTitle($title);
109
110 $this->addElement('textarea', 'approval_note', ts('Approve/Reject Note'));
111
112 $mailApprovalStatus = CRM_Core_OptionGroup::values('mail_approval_status');
113
114 // eliminate the none option
115 $noneOptionID = CRM_Core_OptionGroup::getValue('mail_approval_status',
116 'None',
117 'name'
118 );
119 if ($noneOptionID) {
120 unset($mailApprovalStatus[$noneOptionID]);
121 }
122
123 $this->addRadio('approval_status_id', ts('Approval Status'), $mailApprovalStatus, TRUE, NULL, TRUE);
124
125 $buttons = array(
126 array('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
212 //when user perform mailing from search context
213 //redirect it to search result CRM-3711
214 $ssID = $this->get('ssID');
215 if ($ssID && $this->_searchBasedMailing) {
216 if ($this->_action == CRM_Core_Action::BASIC) {
217 $fragment = 'search';
218 }
219 elseif ($this->_action == CRM_Core_Action::PROFILE) {
220 $fragment = 'search/builder';
221 }
222 elseif ($this->_action == CRM_Core_Action::ADVANCED) {
223 $fragment = 'search/advanced';
224 }
225 else {
226 $fragment = 'search/custom';
227 }
228 $context = $this->get('context');
229 if (!CRM_Contact_Form_Search::isSearchContext($context)) {
230 $context = 'search';
231 }
232 $urlParams = "force=1&reset=1&ssID={$ssID}&context={$context}";
233 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this);
234 if (CRM_Utils_Rule::qfKey($qfKey)) {
235 $urlParams .= "&qfKey=$qfKey";
236 }
237
238 $url = CRM_Utils_System::url('civicrm/contact/' . $fragment, $urlParams);
239 return $this->controller->setDestination($url);
240 }
241
242 $session = CRM_Core_Session::singleton();
243 $session->pushUserContext(CRM_Utils_System::url('civicrm/mailing/browse/scheduled',
244 'reset=1&scheduled=true'
245 ));
246 }
247
248 /**
249 * Display Name of the form
250 *
251 *
252 * @return string
253 */
254 public function getTitle() {
255 return ts('Approve/Reject Mailing');
256 }
257 }