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