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