Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * | |
38 | */ | |
39 | class CRM_Mailing_Form_Schedule extends CRM_Core_Form { | |
40 | ||
41 | /** | |
42 | * Function to set variables up before form is built | |
43 | * | |
44 | * @return void | |
45 | * @access public | |
46 | */ | |
47 | public function preProcess() { | |
48 | if (CRM_Mailing_Info::workflowEnabled() && | |
49 | !CRM_Core_Permission::check('schedule mailings') | |
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 | * This function sets the default values for the form. | |
73 | * | |
74 | * @access public | |
75 | * | |
355ba699 | 76 | * @return void |
6a488035 TO |
77 | */ |
78 | 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 for the last step of the mailing wizard | |
93 | * | |
94 | * @param | |
95 | * | |
96 | * @return void | |
97 | * @access public | |
98 | */ | |
99 | public function buildQuickform() { | |
100 | $this->addDateTime('start_date', ts('Schedule Mailing'), FALSE, array('formatType' => 'mailing')); | |
101 | ||
102 | $this->addElement('checkbox', 'now', ts('Send Immediately')); | |
103 | ||
104 | $this->addFormRule(array('CRM_Mailing_Form_Schedule', 'formRule'), $this); | |
105 | ||
106 | if ($this->_scheduleFormOnly) { | |
107 | $title = ts('Schedule Mailing') . ' - ' . CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_Mailing', | |
108 | $this->_mailingID, | |
109 | 'name' | |
110 | ); | |
111 | CRM_Utils_System::setTitle($title); | |
112 | $buttons = array( | |
113 | array('type' => 'next', | |
114 | 'name' => ts('Submit Mailing'), | |
115 | 'spacing' => ' ', | |
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('type' => 'back', | |
130 | 'name' => ts('<< Previous'), | |
131 | ), | |
132 | array( | |
133 | 'type' => 'next', | |
134 | 'name' => ts('Submit Mailing'), | |
135 | 'spacing' => ' ', | |
136 | 'isDefault' => TRUE, | |
137 | ), | |
138 | ); | |
139 | } | |
140 | else { | |
141 | $buttons = array( | |
142 | array('type' => 'back', | |
143 | 'name' => ts('<< Previous'), | |
144 | ), | |
145 | array( | |
146 | 'type' => 'next', | |
147 | 'name' => ts('Submit Mailing'), | |
148 | 'spacing' => ' ', | |
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 | ); | |
c57f36a1 PJ |
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}"); | |
6a488035 TO |
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 | * | |
da6b46f4 EM |
192 | * @param array $params The form values |
193 | * | |
194 | * @param $files | |
195 | * @param $self | |
6a488035 TO |
196 | * |
197 | * @return boolean True if either we deliver immediately, or the | |
198 | * date is properly set. | |
199 | * @static | |
200 | */ | |
201 | public static function formRule($params, $files, $self) { | |
202 | if (!empty($params['_qf_Schedule_submit'])) { | |
203 | //when user perform mailing from search context | |
204 | //redirect it to search result CRM-3711. | |
205 | $ssID = $self->get('ssID'); | |
206 | if ($ssID && $self->_searchBasedMailing) { | |
207 | if ($self->_action == CRM_Core_Action::BASIC) { | |
208 | $fragment = 'search'; | |
209 | } | |
210 | elseif ($self->_action == CRM_Core_Action::PROFILE) { | |
211 | $fragment = 'search/builder'; | |
212 | } | |
213 | elseif ($self->_action == CRM_Core_Action::ADVANCED) { | |
214 | $fragment = 'search/advanced'; | |
215 | } | |
216 | else { | |
217 | $fragment = 'search/custom'; | |
218 | } | |
219 | ||
220 | $draftURL = CRM_Utils_System::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1'); | |
221 | $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)); | |
222 | CRM_Core_Session::setStatus($status, ts('Mailing Saved'), 'success'); | |
223 | ||
224 | //replace user context to search. | |
225 | $context = $self->get('context'); | |
226 | if (!CRM_Contact_Form_Search::isSearchContext($context)) { | |
227 | $context = 'search'; | |
228 | } | |
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 | $url = CRM_Utils_System::url('civicrm/contact/' . $fragment, "force=1&reset=1&ssID={$ssID}"); | |
236 | } | |
237 | else { | |
238 | $status = ts("Click the 'Continue' action to resume working on it."); | |
239 | $url = CRM_Utils_System::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1'); | |
240 | } | |
241 | CRM_Core_Session::setStatus($status, ts('Mailing Saved'), 'success'); | |
242 | CRM_Utils_System::redirect($url); | |
243 | } | |
244 | if (isset($params['now']) || CRM_Utils_Array::value('_qf_Schedule_back', $params) == '<< Previous') { | |
245 | return TRUE; | |
246 | } | |
247 | ||
248 | if (CRM_Utils_Date::format(CRM_Utils_Date::processDate($params['start_date'], | |
249 | $params['start_date_time'] | |
250 | )) < CRM_Utils_Date::format(date('YmdHi00'))) { | |
251 | return array( | |
252 | 'start_date' => | |
253 | 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 | * @access public | |
266 | */ | |
267 | public function postProcess() { | |
268 | $params = array(); | |
269 | ||
270 | $params['mailing_id'] = $ids['mailing_id'] = $this->_mailingID; | |
271 | ||
272 | if (empty($params['mailing_id'])) { | |
273 | CRM_Core_Error::fatal(ts('Could not find a mailing id')); | |
274 | } | |
275 | ||
276 | foreach (array('now', 'start_date', 'start_date_time') as $parameter) { | |
277 | $params[$parameter] = $this->controller->exportValue($this->_name, $parameter); | |
278 | } | |
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'); | |
6a488035 TO |
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(); | |
d63bd141 | 294 | |
6a488035 TO |
295 | // set the scheduled_id |
296 | $params['scheduled_id'] = $session->get('userID'); | |
6a488035 TO |
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 | |
d63bd141 DL |
306 | $params['approver_id'] = 'null'; |
307 | $params['approval_date'] = 'null'; | |
308 | $params['approval_status_id'] = 'null'; | |
6a488035 TO |
309 | } |
310 | ||
6a488035 TO |
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 | * @access public | |
354 | * | |
355 | * @return string | |
356 | */ | |
357 | public function getTitle() { | |
358 | return ts('Schedule or Send'); | |
359 | } | |
360 | } | |
361 |