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