Merge pull request #4861 from colemanw/CRM-15789
[civicrm-core.git] / CRM / Mailing / Form / Schedule.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_Schedule extends CRM_Core_Form {
40
41 /**
42 * Set variables up before form is built
43 *
44 * @return void
45 */
46 public function preProcess() {
47 if (CRM_Mailing_Info::workflowEnabled() &&
48 !CRM_Core_Permission::check('schedule mailings')
49 ) {
50 $url = CRM_Utils_System::url('civicrm/mailing/browse/unscheduled', 'reset=1&scheduled=false');
51 CRM_Utils_System::redirect($url);
52 }
53
54 //when user come from search context.
55 $ssID = $this->get('ssID');
56 $this->assign('ssid', $ssID);
57 $this->_searchBasedMailing = CRM_Contact_Form_Search::isSearchContext($this->get('context'));
58 if (CRM_Contact_Form_Search::isSearchContext($this->get('context')) && !$ssID) {
59 $params = array();
60 $result = CRM_Core_BAO_PrevNextCache::getSelectedContacts();
61 $this->assign("value", $result);
62 }
63 $this->_mailingID = $this->get('mailing_id');
64 $this->_scheduleFormOnly = FALSE;
65 if (!$this->_mailingID) {
66 $this->_mailingID = CRM_Utils_Request::retrieve('mid', 'Integer', $this, TRUE);
67 $this->_scheduleFormOnly = TRUE;
68 }
69 }
70 /**
71 * Set default values for the form.
72 *
73 *
74 * @return void
75 */
76 public function setDefaultValues() {
77 $defaults = array();
78 if ($this->_scheduleFormOnly) {
79 $count = CRM_Mailing_BAO_Recipients::mailingSize($this->_mailingID);
80 }
81 else {
82 $count = $this->get('count');
83 }
84 $this->assign('count', $count);
85 $defaults['now'] = 1;
86 return $defaults;
87 }
88
89 /**
90 * Build the form object for the last step of the mailing wizard
91 *
92 * @param
93 *
94 * @return void
95 */
96 public function buildQuickform() {
97 $this->addDateTime('start_date', ts('Schedule Mailing'), FALSE, array('formatType' => 'mailing'));
98
99 $this->addElement('checkbox', 'now', ts('Send Immediately'));
100
101 $this->addFormRule(array('CRM_Mailing_Form_Schedule', 'formRule'), $this);
102
103 if ($this->_scheduleFormOnly) {
104 $title = ts('Schedule Mailing') . ' - ' . CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_Mailing',
105 $this->_mailingID,
106 'name'
107 );
108 CRM_Utils_System::setTitle($title);
109 $buttons = array(
110 array(
111 'type' => 'next',
112 'name' => ts('Submit Mailing'),
113 'spacing' => '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;',
114 'isDefault' => TRUE,
115 ),
116 array(
117 'type' => 'cancel',
118 'name' => ts('Cancel'),
119 ),
120 );
121 }
122 else {
123 //FIXME : currently we are hiding save an continue later when
124 //search base mailing, we should handle it when we fix CRM-3876
125 if ($this->_searchBasedMailing) {
126 $buttons = array(
127 array(
128 'type' => 'back',
129 'name' => ts('Previous'),
130 ),
131 array(
132 'type' => 'next',
133 'name' => ts('Submit Mailing'),
134 'spacing' => '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;',
135 'isDefault' => TRUE,
136 ),
137 );
138 }
139 else {
140 $buttons = array(
141 array(
142 'type' => 'back',
143 'name' => ts('Previous'),
144 ),
145 array(
146 'type' => 'next',
147 'name' => ts('Submit Mailing'),
148 'spacing' => '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;',
149 'isDefault' => TRUE,
150 'js' => array('onclick' => "return submitOnce(this,'" . $this->_name . "','" . ts('Processing') . "');"),
151 ),
152 array(
153 'type' => 'cancel',
154 'name' => ts('Continue Later'),
155 ),
156 );
157 }
158 }
159 $this->addButtons($buttons);
160
161 if (CRM_Mailing_Info::workflowEnabled() &&
162 $this->_scheduleFormOnly
163 ) {
164 // add the preview elements
165 $preview = array();
166 $preview['type'] = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_Mailing', $this->_mailingID, 'body_html') ? 'html' : 'text';
167 $preview['subject'] = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_Mailing',
168 $this->_mailingID,
169 'subject'
170 );
171
172 $mailingKey = $this->_mailingID;
173 if ($hash = CRM_Mailing_BAO_Mailing::getMailingHash($mailingKey)) {
174 $mailingKey = $hash;
175 }
176
177 $preview['viewURL'] = CRM_Utils_System::url('civicrm/mailing/view', "reset=1&id={$mailingKey}");
178
179 $preview['attachment'] = CRM_Core_BAO_File::attachmentInfo('civicrm_mailing', $this->_mailingID);
180
181 $this->assign_by_ref('preview', $preview);
182 }
183 }
184
185 /**
186 * Form rule to validate the date selector and/or if we should deliver
187 * immediately.
188 *
189 * Warning: if you make changes here, be sure to also make them in
190 * Retry.php
191 *
192 * @param array $params
193 * The form values.
194 *
195 * @param $files
196 * @param $self
197 *
198 * @return boolean True if either we deliver immediately, or the
199 * date is properly set.
200 * @static
201 */
202 public static function formRule($params, $files, $self) {
203 if (!empty($params['_qf_Schedule_submit'])) {
204 //when user perform mailing from search context
205 //redirect it to search result CRM-3711.
206 $ssID = $self->get('ssID');
207 if ($ssID && $self->_searchBasedMailing) {
208 if ($self->_action == CRM_Core_Action::BASIC) {
209 $fragment = 'search';
210 }
211 elseif ($self->_action == CRM_Core_Action::PROFILE) {
212 $fragment = 'search/builder';
213 }
214 elseif ($self->_action == CRM_Core_Action::ADVANCED) {
215 $fragment = 'search/advanced';
216 }
217 else {
218 $fragment = 'search/custom';
219 }
220
221 $draftURL = CRM_Utils_System::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1');
222 $status = ts("Your mailing has been saved. You can continue later by clicking the 'Continue' action to resume working on it.<br />From <a href='%1'>Draft and Unscheduled Mailings</a>.", array(1 => $draftURL));
223 CRM_Core_Session::setStatus($status, ts('Mailing Saved'), 'success');
224
225 //replace user context to search.
226 $context = $self->get('context');
227 if (!CRM_Contact_Form_Search::isSearchContext($context)) {
228 $context = 'search';
229 }
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 $url = CRM_Utils_System::url('civicrm/contact/' . $fragment, "force=1&reset=1&ssID={$ssID}");
237 }
238 else {
239 $status = ts("Click the 'Continue' action to resume working on it.");
240 $url = CRM_Utils_System::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1');
241 }
242 CRM_Core_Session::setStatus($status, ts('Mailing Saved'), 'success');
243 CRM_Utils_System::redirect($url);
244 }
245 if (isset($params['now']) || CRM_Utils_Array::value('_qf_Schedule_back', $params) == '<< Previous') {
246 return TRUE;
247 }
248
249 if (CRM_Utils_Date::format(CRM_Utils_Date::processDate($params['start_date'],
250 $params['start_date_time']
251 )) < CRM_Utils_Date::format(date('YmdHi00'))) {
252 return array(
253 'start_date' => ts('Start date cannot be earlier than the current time.'),
254 );
255 }
256 return TRUE;
257 }
258
259 /**
260 * Process the posted form values. Create and schedule a mailing.
261 *
262 * @param
263 *
264 * @return void
265 */
266 public function postProcess() {
267 $params = array();
268
269 $params['mailing_id'] = $ids['mailing_id'] = $this->_mailingID;
270
271 if (empty($params['mailing_id'])) {
272 CRM_Core_Error::fatal(ts('Could not find a mailing id'));
273 }
274
275 foreach (array('now', 'start_date', 'start_date_time') as $parameter) {
276 $params[$parameter] = $this->controller->exportValue($this->_name, $parameter);
277 }
278
279 // Previously, we checked if $mailing->is_template, and did *not*
280 // schedule if it was set. Discussed with Lobo, removed that check
281 // as it appeared to prevent mails being scheduled if they were
282 // saved as a template, and this wasn't the documented behaviour.
283 // $saveTemplate = $this->controller->exportValue('saveTemplate');
284 if ($params['now']) {
285 $params['scheduled_date'] = date('YmdHis');
286 }
287 else {
288 $params['scheduled_date'] = CRM_Utils_Date::processDate($params['start_date'] . ' ' . $params['start_date_time']);
289 }
290
291 $session = CRM_Core_Session::singleton();
292
293 // set the scheduled_id
294 $params['scheduled_id'] = $session->get('userID');
295
296 // set approval details if workflow is not enabled
297 if (!CRM_Mailing_Info::workflowEnabled()) {
298 $params['approver_id'] = $session->get('userID');
299 $params['approval_date'] = date('YmdHis');
300 $params['approval_status_id'] = 1;
301 }
302 else {
303 // reset them in case this mailing was rejected
304 $params['approver_id'] = 'null';
305 $params['approval_date'] = 'null';
306 $params['approval_status_id'] = 'null';
307 }
308
309 /* Build the mailing object */
310 CRM_Mailing_BAO_Mailing::create($params, $ids);
311
312 //when user perform mailing from search context
313 //redirect it to search result CRM-3711.
314 $ssID = $this->get('ssID');
315 if ($ssID && $this->_searchBasedMailing && !CRM_Mailing_Info::workflowEnabled()) {
316 if ($this->_action == CRM_Core_Action::BASIC) {
317 $fragment = 'search';
318 }
319 elseif ($this->_action == CRM_Core_Action::PROFILE) {
320 $fragment = 'search/builder';
321 }
322 elseif ($this->_action == CRM_Core_Action::ADVANCED) {
323 $fragment = 'search/advanced';
324 }
325 else {
326 $fragment = 'search/custom';
327 }
328 $context = $this->get('context');
329 if (!CRM_Contact_Form_Search::isSearchContext($context)) {
330 $context = 'search';
331 }
332 $urlParams = "force=1&reset=1&ssID={$ssID}&context={$context}";
333 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this);
334 if (CRM_Utils_Rule::qfKey($qfKey)) {
335 $urlParams .= "&qfKey=$qfKey";
336 }
337
338 $url = CRM_Utils_System::url('civicrm/contact/' . $fragment, $urlParams);
339 return $this->controller->setDestination($url);
340 }
341
342 $session = CRM_Core_Session::singleton();
343 $session->pushUserContext(CRM_Utils_System::url('civicrm/mailing/browse/scheduled',
344 'reset=1&scheduled=true'
345 ));
346 }
347
348 /**
349 * Display Name of the form
350 *
351 *
352 * @return string
353 */
354 public function getTitle() {
355 return ts('Schedule or Send');
356 }
357 }