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