Update copyright date for 2020
[civicrm-core.git] / CRM / SMS / Form / Schedule.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
f299f7db 6 | Copyright CiviCRM LLC (c) 2004-2020 |
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
f299f7db 31 * @copyright CiviCRM LLC (c) 2004-2020
6a488035
TO
32 */
33class CRM_SMS_Form_Schedule extends CRM_Core_Form {
34
423616fa
CW
35 public $submitOnce = TRUE;
36
6a488035 37 /**
fe482240 38 * Set variables up before form is built.
6a488035
TO
39 */
40 public function preProcess() {
41
42 $this->_mailingID = $this->get('mailing_id');
43
44 if (!$this->_mailingID) {
45 $this->_mailingID = CRM_Utils_Request::retrieve('mid', 'Integer', $this, TRUE);
46 }
47 }
48
49 /**
c490a46a 50 * Set default values for the form.
6a488035 51 */
00be9182 52 public function setDefaultValues() {
be2fb01f 53 $defaults = [];
6a488035
TO
54
55 $count = $this->get('count');
56
57 $this->assign('count', $count);
e737155e 58 $defaults['send_option'] = 'send_immediate';
6a488035
TO
59 return $defaults;
60 }
61
62 /**
fe482240 63 * Build the form object for the last step of the sms wizard.
6a488035
TO
64 */
65 public function buildQuickform() {
6a488035 66
e737155e
J
67 // Fix Firefox issue where the non-default field is displayed as checked
68 // on page refresh.
69 $this->setAttribute('autocomplete', 'off');
70
71 $sendOptions = [
3f126e3a
ML
72 $this->createElement('radio', NULL, NULL, ts('Send immediately'), 'send_immediate', ['id' => 'send_immediate', 'style' => 'margin-bottom: 10px;']),
73 $this->createElement('radio', NULL, NULL, ts('Send at:'), 'send_later', ['id' => 'send_later']),
e737155e
J
74 ];
75 $this->addGroup($sendOptions, 'send_option', '', '<br>');
76
77 $this->add('datepicker', 'start_date', '', NULL, FALSE, ['minDate' => time()]);
6a488035 78
be2fb01f 79 $this->addFormRule(['CRM_SMS_Form_Schedule', 'formRule'], $this);
6a488035 80
be2fb01f
CW
81 $buttons = [
82 [
6ea503d4 83 'type' => 'back',
f212d37d 84 'name' => ts('Previous'),
be2fb01f
CW
85 ],
86 [
6a488035
TO
87 'type' => 'next',
88 'name' => ts('Submit Mass SMS'),
89 'spacing' => '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;',
90 'isDefault' => TRUE,
be2fb01f
CW
91 ],
92 [
6a488035
TO
93 'type' => 'cancel',
94 'name' => ts('Continue Later'),
be2fb01f
CW
95 ],
96 ];
6a488035
TO
97
98 $this->addButtons($buttons);
99
be2fb01f 100 $preview = [];
353ffa53 101 $preview['type'] = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_Mailing', $this->_mailingID, 'body_html') ? 'html' : 'text';
6a488035
TO
102 $preview['viewURL'] = CRM_Utils_System::url('civicrm/mailing/view', "reset=1&id={$this->_mailingID}");
103 $this->assign_by_ref('preview', $preview);
104 }
105
106 /**
107 * Form rule to validate the date selector and/or if we should deliver
108 * immediately.
109 *
110 * Warning: if you make changes here, be sure to also make them in
111 * Retry.php
112 *
901b501a
TO
113 * @param array $params
114 * The form values.
fd31fa4c
EM
115 *
116 * @param $files
117 * @param $self
6a488035 118 *
8d7a9d07
CB
119 * @return bool
120 * True if either we deliver immediately, or the date is properly
121 * set.
6a488035
TO
122 */
123 public static function formRule($params, $files, $self) {
6a488035 124
e737155e 125 if (!empty($params['_qf_Schedule_submit'])) {
6a488035
TO
126 CRM_Core_Session::setStatus(ts("Your Mass SMS has been saved. Click the 'Continue' action to resume working on it."), ts('Saved'), 'success');
127 $url = CRM_Utils_System::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1&sms=1');
128 CRM_Utils_System::redirect($url);
129 }
e737155e
J
130
131 if ((isset($params['send_option']) && $params['send_option'] == 'send_immediate') || CRM_Utils_Array::value('_qf_Schedule_back', $params) == ts('Previous')) {
6a488035
TO
132 return TRUE;
133 }
134
e737155e 135 if (strtotime($params['start_date']) < time()) {
be2fb01f 136 return [
9d72cede 137 'start_date' => ts('Start date cannot be earlier than the current time.'),
be2fb01f 138 ];
6a488035 139 }
e737155e 140
6a488035
TO
141 return TRUE;
142 }
143
144 /**
e737155e 145 * Process the posted form values. Create and schedule a Mass SMS.
6a488035
TO
146 */
147 public function postProcess() {
be2fb01f 148 $params = [];
6a488035
TO
149
150 $params['mailing_id'] = $ids['mailing_id'] = $this->_mailingID;
151
152 if (empty($params['mailing_id'])) {
153 CRM_Core_Error::fatal(ts('Could not find a mailing id'));
154 }
155
80b33705 156 $params['send_option'] = $this->controller->exportValue($this->_name, 'send_option');
e737155e 157 if (isset($params['send_option']) && $params['send_option'] == 'send_immediate') {
1e2f5f25
DS
158 $params['scheduled_date'] = date('YmdHis');
159 }
160 else {
e737155e 161 $params['scheduled_date'] = $this->controller->exportValue($this->_name, 'start_date');
1e2f5f25
DS
162 }
163
164 $session = CRM_Core_Session::singleton();
165 // set the scheduled_id
166 $params['scheduled_id'] = $session->get('userID');
1e2f5f25
DS
167
168 // set approval details if workflow is not enabled
169 if (!CRM_Mailing_Info::workflowEnabled()) {
170 $params['approver_id'] = $session->get('userID');
171 $params['approval_date'] = date('YmdHis');
172 $params['approval_status_id'] = 1;
6a488035
TO
173 }
174
39404680 175 // Build the mailing object.
1e2f5f25
DS
176 CRM_Mailing_BAO_Mailing::create($params, $ids);
177
6a488035
TO
178 $session = CRM_Core_Session::singleton();
179 $session->pushUserContext(CRM_Utils_System::url('civicrm/mailing/browse/scheduled',
353ffa53
TO
180 'reset=1&scheduled=true&sms=1'
181 ));
6a488035
TO
182 }
183
184 /**
fe482240 185 * Display Name of the form.
6a488035 186 *
6a488035
TO
187 *
188 * @return string
189 */
190 public function getTitle() {
191 return ts('Schedule or Send');
192 }
96025800 193
6a488035 194}