3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
39 class CRM_Mailing_Form_Schedule
extends CRM_Core_Form
{
42 * Function to set variables up before form is built
47 public function preProcess() {
48 if (CRM_Mailing_Info
::workflowEnabled() &&
49 !CRM_Core_Permission
::check('schedule mailings')
51 $url = CRM_Utils_System
::url('civicrm/mailing/browse/unscheduled', 'reset=1&scheduled=false');
52 CRM_Utils_System
::redirect($url);
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){
61 $result = CRM_Core_BAO_PrevNextCache
::getSelectedContacts();
62 $this->assign("value", $result);
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;
72 * This function sets the default values for the form.
78 function setDefaultValues() {
80 if ($this->_scheduleFormOnly
) {
81 $count = CRM_Mailing_BAO_Recipients
::mailingSize($this->_mailingID
);
84 $count = $this->get('count');
86 $this->assign('count', $count);
92 * Build the form for the last step of the mailing wizard
99 public function buildQuickform() {
100 $this->addDateTime('start_date', ts('Schedule Mailing'), FALSE, array('formatType' => 'mailing'));
102 $this->addElement('checkbox', 'now', ts('Send Immediately'));
104 $this->addFormRule(array('CRM_Mailing_Form_Schedule', 'formRule'), $this);
106 if ($this->_scheduleFormOnly
) {
107 $title = ts('Schedule Mailing') . ' - ' . CRM_Core_DAO
::getFieldValue('CRM_Mailing_DAO_Mailing',
111 CRM_Utils_System
::setTitle($title);
113 array('type' => 'next',
114 'name' => ts('Submit Mailing'),
115 'spacing' => ' ',
120 'name' => ts('Cancel'),
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
) {
129 array('type' => 'back',
130 'name' => ts('<< Previous'),
134 'name' => ts('Submit Mailing'),
135 'spacing' => ' ',
142 array('type' => 'back',
143 'name' => ts('<< Previous'),
147 'name' => ts('Submit Mailing'),
148 'spacing' => ' ',
150 'js' => array('onclick' => "return submitOnce(this,'" . $this->_name
. "','" . ts('Processing') . "');"),
154 'name' => ts('Continue Later'),
159 $this->addButtons($buttons);
161 if (CRM_Mailing_Info
::workflowEnabled() &&
162 $this->_scheduleFormOnly
164 // add the preview elements
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',
171 $preview['viewURL'] = CRM_Utils_System
::url('civicrm/mailing/view', "reset=1&id={$this->_mailingID}");
173 $preview['attachment'] = CRM_Core_BAO_File
::attachmentInfo('civicrm_mailing', $this->_mailingID
);
175 $this->assign_by_ref('preview', $preview);
180 * Form rule to validate the date selector and/or if we should deliver
183 * Warning: if you make changes here, be sure to also make them in
186 * @param array $params The form values
188 * @return boolean True if either we deliver immediately, or the
189 * date is properly set.
192 public static function formRule($params, $files, $self) {
193 if (!empty($params['_qf_Schedule_submit'])) {
194 //when user perform mailing from search context
195 //redirect it to search result CRM-3711.
196 $ssID = $self->get('ssID');
197 if ($ssID && $self->_searchBasedMailing
) {
198 if ($self->_action
== CRM_Core_Action
::BASIC
) {
199 $fragment = 'search';
201 elseif ($self->_action
== CRM_Core_Action
::PROFILE
) {
202 $fragment = 'search/builder';
204 elseif ($self->_action
== CRM_Core_Action
::ADVANCED
) {
205 $fragment = 'search/advanced';
208 $fragment = 'search/custom';
211 $draftURL = CRM_Utils_System
::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1');
212 $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));
213 CRM_Core_Session
::setStatus($status, ts('Mailing Saved'), 'success');
215 //replace user context to search.
216 $context = $self->get('context');
217 if (!CRM_Contact_Form_Search
::isSearchContext($context)) {
221 $urlParams = "force=1&reset=1&ssID={$ssID}&context={$context}";
222 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', $this);
223 if (CRM_Utils_Rule
::qfKey($qfKey)) {
224 $urlParams .= "&qfKey=$qfKey";
226 $url = CRM_Utils_System
::url('civicrm/contact/' . $fragment, "force=1&reset=1&ssID={$ssID}");
229 $status = ts("Click the 'Continue' action to resume working on it.");
230 $url = CRM_Utils_System
::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1');
232 CRM_Core_Session
::setStatus($status, ts('Mailing Saved'), 'success');
233 CRM_Utils_System
::redirect($url);
235 if (isset($params['now']) || CRM_Utils_Array
::value('_qf_Schedule_back', $params) == '<< Previous') {
239 if (CRM_Utils_Date
::format(CRM_Utils_Date
::processDate($params['start_date'],
240 $params['start_date_time']
241 )) < CRM_Utils_Date
::format(date('YmdHi00'))) {
244 ts('Start date cannot be earlier than the current time.'),
251 * Process the posted form values. Create and schedule a mailing.
258 public function postProcess() {
261 $params['mailing_id'] = $ids['mailing_id'] = $this->_mailingID
;
263 if (empty($params['mailing_id'])) {
264 CRM_Core_Error
::fatal(ts('Could not find a mailing id'));
267 foreach (array('now', 'start_date', 'start_date_time') as $parameter) {
268 $params[$parameter] = $this->controller
->exportValue($this->_name
, $parameter);
272 // Previously, we checked if $mailing->is_template, and did *not*
273 // schedule if it was set. Discussed with Lobo, removed that check
274 // as it appeared to prevent mails being scheduled if they were
275 // saved as a template, and this wasn't the documented behaviour.
276 // $saveTemplate = $this->controller->exportValue('saveTemplate');
277 if ($params['now']) {
278 $params['scheduled_date'] = date('YmdHis');
281 $params['scheduled_date'] = CRM_Utils_Date
::processDate($params['start_date'] . ' ' . $params['start_date_time']);
284 $session = CRM_Core_Session
::singleton();
286 // set the scheduled_id
287 $params['scheduled_id'] = $session->get('userID');
289 // set approval details if workflow is not enabled
290 if (!CRM_Mailing_Info
::workflowEnabled()) {
291 $params['approver_id'] = $session->get('userID');
292 $params['approval_date'] = date('YmdHis');
293 $params['approval_status_id'] = 1;
296 // reset them in case this mailing was rejected
297 $params['approver_id'] = 'null';
298 $params['approval_date'] = 'null';
299 $params['approval_status_id'] = 'null';
302 /* Build the mailing object */
303 CRM_Mailing_BAO_Mailing
::create($params, $ids);
305 //when user perform mailing from search context
306 //redirect it to search result CRM-3711.
307 $ssID = $this->get('ssID');
308 if ($ssID && $this->_searchBasedMailing
&& !CRM_Mailing_Info
::workflowEnabled()) {
309 if ($this->_action
== CRM_Core_Action
::BASIC
) {
310 $fragment = 'search';
312 elseif ($this->_action
== CRM_Core_Action
::PROFILE
) {
313 $fragment = 'search/builder';
315 elseif ($this->_action
== CRM_Core_Action
::ADVANCED
) {
316 $fragment = 'search/advanced';
319 $fragment = 'search/custom';
321 $context = $this->get('context');
322 if (!CRM_Contact_Form_Search
::isSearchContext($context)) {
325 $urlParams = "force=1&reset=1&ssID={$ssID}&context={$context}";
326 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', $this);
327 if (CRM_Utils_Rule
::qfKey($qfKey)) {
328 $urlParams .= "&qfKey=$qfKey";
331 $url = CRM_Utils_System
::url('civicrm/contact/' . $fragment, $urlParams);
332 return $this->controller
->setDestination($url);
335 $session = CRM_Core_Session
::singleton();
336 $session->pushUserContext(CRM_Utils_System
::url('civicrm/mailing/browse/scheduled',
337 'reset=1&scheduled=true'
342 * Display Name of the form
348 public function getTitle() {
349 return ts('Schedule or Send');