Merge pull request #14084 from colemanw/UFFieldBAO
[civicrm-core.git] / CRM / SMS / Form / Schedule.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
32 */
33 class CRM_SMS_Form_Schedule extends CRM_Core_Form {
34
35 /**
36 * Set variables up before form is built.
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 /**
48 * Set default values for the form.
49 */
50 public function setDefaultValues() {
51 $defaults = [];
52
53 $count = $this->get('count');
54
55 $this->assign('count', $count);
56 $defaults['send_option'] = 'send_immediate';
57 return $defaults;
58 }
59
60 /**
61 * Build the form object for the last step of the sms wizard.
62 */
63 public function buildQuickform() {
64
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 = [
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']),
72 ];
73 $this->addGroup($sendOptions, 'send_option', '', '<br>');
74
75 $this->add('datepicker', 'start_date', '', NULL, FALSE, ['minDate' => time()]);
76
77 $this->addFormRule(['CRM_SMS_Form_Schedule', 'formRule'], $this);
78
79 $buttons = [
80 [
81 'type' => 'back',
82 'name' => ts('Previous'),
83 ],
84 [
85 'type' => 'next',
86 'name' => ts('Submit Mass SMS'),
87 'spacing' => '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;',
88 'isDefault' => TRUE,
89 'js' => ['onclick' => "return submitOnce(this,'" . $this->_name . "','" . ts('Processing') . "');"],
90 ],
91 [
92 'type' => 'cancel',
93 'name' => ts('Continue Later'),
94 ],
95 ];
96
97 $this->addButtons($buttons);
98
99 $preview = [];
100 $preview['type'] = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_Mailing', $this->_mailingID, 'body_html') ? 'html' : 'text';
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 *
112 * @param array $params
113 * The form values.
114 *
115 * @param $files
116 * @param $self
117 *
118 * @return bool
119 * True if either we deliver immediately, or the date is properly
120 * set.
121 */
122 public static function formRule($params, $files, $self) {
123
124 if (!empty($params['_qf_Schedule_submit'])) {
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 }
129
130 if ((isset($params['send_option']) && $params['send_option'] == 'send_immediate') || CRM_Utils_Array::value('_qf_Schedule_back', $params) == ts('Previous')) {
131 return TRUE;
132 }
133
134 if (strtotime($params['start_date']) < time()) {
135 return [
136 'start_date' => ts('Start date cannot be earlier than the current time.'),
137 ];
138 }
139
140 return TRUE;
141 }
142
143 /**
144 * Process the posted form values. Create and schedule a Mass SMS.
145 */
146 public function postProcess() {
147 $params = [];
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
155 $params['send_option'] = $this->controller->exportValue($this->_name, 'send_option');
156 if (isset($params['send_option']) && $params['send_option'] == 'send_immediate') {
157 $params['scheduled_date'] = date('YmdHis');
158 }
159 else {
160 $params['scheduled_date'] = $this->controller->exportValue($this->_name, 'start_date');
161 }
162
163 $session = CRM_Core_Session::singleton();
164 // set the scheduled_id
165 $params['scheduled_id'] = $session->get('userID');
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;
172 }
173
174 // Build the mailing object.
175 CRM_Mailing_BAO_Mailing::create($params, $ids);
176
177 $session = CRM_Core_Session::singleton();
178 $session->pushUserContext(CRM_Utils_System::url('civicrm/mailing/browse/scheduled',
179 'reset=1&scheduled=true&sms=1'
180 ));
181 }
182
183 /**
184 * Display Name of the form.
185 *
186 *
187 * @return string
188 */
189 public function getTitle() {
190 return ts('Schedule or Send');
191 }
192
193 }