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