CRM-13955 rework mailing default values and workflow
[civicrm-core.git] / CRM / Mailing / Form / Approve.php
CommitLineData
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 *
38 */
39class 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 * Function to 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 * This function sets the default values for the form.
86 *
87 * @access public
88 *
89 * @return None
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 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
6ce01b02 115 $mailApprovalStatus = CRM_Core_OptionGroup::values('mail_approval_status');
6a488035
TO
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 $preview['viewURL'] = CRM_Utils_System::url('civicrm/mailing/view', "reset=1&id={$this->_mailingID}");
150 $preview['type'] = $this->_mailing->body_html ? 'html' : 'text';
151 $preview['attachment'] = CRM_Core_BAO_File::attachmentInfo('civicrm_mailing', $this->_mailingID);
152
153 $this->assign_by_ref('preview', $preview);
154 }
155
156 /**
157 * Process the posted form values. Approve /reject a mailing.
158 *
159 * @param
160 *
161 * @return void
162 * @access public
163 */
164 public function postProcess() {
165 // get the submitted form values.
166 $params = $this->controller->exportValues($this->_name);
167
168 $ids = array();
169 if (isset($this->_mailingID)) {
170 $ids['mailing_id'] = $this->_mailingID;
171 }
172 else {
173 $ids['mailing_id'] = $this->get('mailing_id');
174 }
175
176 if (!$ids['mailing_id']) {
177 CRM_Core_Error::fatal();
178 }
179
180 $params['approver_id'] = $this->_contactID;
181 $params['approval_date'] = date('YmdHis');
182
183 // if rejected, then we need to reset the scheduled date and scheduled id
184 $rejectOptionID = CRM_Core_OptionGroup::getValue('mail_approval_status',
185 'Rejected',
186 'name'
187 );
188 if ($rejectOptionID &&
189 $params['approval_status_id'] == $rejectOptionID
190 ) {
1365ea2f
BS
191 $params['scheduled_id'] = NULL;
192 $params['scheduled_date'] = NULL;
6a488035
TO
193
194 // also delete any jobs associated with this mailing
9da8dc8c 195 $job = new CRM_Mailing_BAO_MailingJob();
6a488035
TO
196 $job->mailing_id = $ids['mailing_id'];
197 $job->delete();
198 }
8817ccc5
BS
199 else {
200 $mailing = new CRM_Mailing_BAO_Mailing();
201 $mailing->id = $ids['mailing_id'];
202 $mailing->find(TRUE);
203
204 $params['scheduled_date'] = CRM_Utils_Date::processDate($mailing->scheduled_date);
205 }
6a488035
TO
206
207 CRM_Mailing_BAO_Mailing::create($params, $ids);
208
209
210 //when user perform mailing from search context
211 //redirect it to search result CRM-3711
212 $ssID = $this->get('ssID');
213 if ($ssID && $this->_searchBasedMailing) {
214 if ($this->_action == CRM_Core_Action::BASIC) {
215 $fragment = 'search';
216 }
217 elseif ($this->_action == CRM_Core_Action::PROFILE) {
218 $fragment = 'search/builder';
219 }
220 elseif ($this->_action == CRM_Core_Action::ADVANCED) {
221 $fragment = 'search/advanced';
222 }
223 else {
224 $fragment = 'search/custom';
225 }
226 $context = $this->get('context');
227 if (!CRM_Contact_Form_Search::isSearchContext($context)) {
228 $context = 'search';
229 }
230 $urlParams = "force=1&reset=1&ssID={$ssID}&context={$context}";
231 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this);
232 if (CRM_Utils_Rule::qfKey($qfKey)) {
233 $urlParams .= "&qfKey=$qfKey";
234 }
235
236 $url = CRM_Utils_System::url('civicrm/contact/' . $fragment, $urlParams);
237 return $this->controller->setDestination($url);
238 }
239
240 $session = CRM_Core_Session::singleton();
241 $session->pushUserContext(CRM_Utils_System::url('civicrm/mailing/browse/scheduled',
242 'reset=1&scheduled=true'
243 ));
244 }
245
246 /**
247 * Display Name of the form
248 *
249 * @access public
250 *
251 * @return string
252 */
253 public function getTitle() {
254 return ts('Approve/Reject Mailing');
255 }
256}
257