Merge pull request #18449 from colemanw/multiValueAutocomplete
[civicrm-core.git] / CRM / SMS / Form / Schedule.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_SMS_Form_Schedule extends CRM_Core_Form {
18
19 public $submitOnce = TRUE;
20
21 /**
22 * Set variables up before form is built.
23 */
24 public function preProcess() {
25
26 $this->_mailingID = $this->get('mailing_id');
27
28 if (!$this->_mailingID) {
29 $this->_mailingID = CRM_Utils_Request::retrieve('mid', 'Integer', $this, TRUE);
30 }
31 }
32
33 /**
34 * Set default values for the form.
35 */
36 public function setDefaultValues() {
37 $defaults = [];
38
39 $count = $this->get('count');
40
41 $this->assign('count', $count);
42 $defaults['send_option'] = 'send_immediate';
43 return $defaults;
44 }
45
46 /**
47 * Build the form object for the last step of the sms wizard.
48 */
49 public function buildQuickform() {
50
51 // Fix Firefox issue where the non-default field is displayed as checked
52 // on page refresh.
53 $this->setAttribute('autocomplete', 'off');
54
55 $this->addRadio('send_option', '', [
56 'send_immediate' => ts('Send immediately'),
57 'send_later' => ts('Send at:'),
58 ], [], '<br>', FALSE, [
59 'send_immediate' => ['id' => 'send_immediate', 'style' => 'margin-bottom: 10px;'],
60 'send_later' => ['id' => 'send_later'],
61 ]);
62
63 $this->add('datepicker', 'start_date', '', NULL, FALSE, ['minDate' => date('Y-m-d')]);
64
65 $this->addFormRule(['CRM_SMS_Form_Schedule', 'formRule'], $this);
66
67 $buttons = [
68 [
69 'type' => 'back',
70 'name' => ts('Previous'),
71 ],
72 [
73 'type' => 'next',
74 'name' => ts('Submit Mass SMS'),
75 'spacing' => '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;',
76 'isDefault' => TRUE,
77 ],
78 [
79 'type' => 'cancel',
80 'name' => ts('Continue Later'),
81 ],
82 ];
83
84 $this->addButtons($buttons);
85
86 $preview = [];
87 $preview['type'] = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_Mailing', $this->_mailingID, 'body_html') ? 'html' : 'text';
88 $preview['viewURL'] = CRM_Utils_System::url('civicrm/mailing/view', "reset=1&id={$this->_mailingID}");
89 $this->assign_by_ref('preview', $preview);
90 }
91
92 /**
93 * Form rule to validate the date selector and/or if we should deliver
94 * immediately.
95 *
96 * Warning: if you make changes here, be sure to also make them in
97 * Retry.php
98 *
99 * @param array $params
100 * The form values.
101 *
102 * @param $files
103 * @param $self
104 *
105 * @return bool
106 * True if either we deliver immediately, or the date is properly
107 * set.
108 */
109 public static function formRule($params, $files, $self) {
110
111 if (!empty($params['_qf_Schedule_submit'])) {
112 CRM_Core_Session::setStatus(ts("Your Mass SMS has been saved. Click the 'Continue' action to resume working on it."), ts('Saved'), 'success');
113 $url = CRM_Utils_System::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1&sms=1');
114 CRM_Utils_System::redirect($url);
115 }
116
117 if ((isset($params['send_option']) && $params['send_option'] == 'send_immediate') || CRM_Utils_Array::value('_qf_Schedule_back', $params) == ts('Previous')) {
118 return TRUE;
119 }
120
121 if (strtotime($params['start_date']) < time()) {
122 return [
123 'start_date' => ts('Start date cannot be earlier than the current time.'),
124 ];
125 }
126
127 return TRUE;
128 }
129
130 /**
131 * Process the posted form values. Create and schedule a Mass SMS.
132 */
133 public function postProcess() {
134 $params = [];
135
136 $params['id'] = $this->_mailingID;
137
138 if (empty($params['id'])) {
139 CRM_Core_Error::statusBounce(ts('Could not find a mailing id'));
140 }
141
142 $params['send_option'] = $this->controller->exportValue($this->_name, 'send_option');
143 if (isset($params['send_option']) && $params['send_option'] == 'send_immediate') {
144 $params['scheduled_date'] = date('YmdHis');
145 }
146 else {
147 $params['scheduled_date'] = $this->controller->exportValue($this->_name, 'start_date');
148 }
149
150 $session = CRM_Core_Session::singleton();
151 // set the scheduled_id
152 $params['scheduled_id'] = $session->get('userID');
153
154 // set approval details if workflow is not enabled
155 if (!CRM_Mailing_Info::workflowEnabled()) {
156 $params['approver_id'] = $session->get('userID');
157 $params['approval_date'] = date('YmdHis');
158 $params['approval_status_id'] = 1;
159 }
160
161 // Build the mailing object.
162 CRM_Mailing_BAO_Mailing::create($params);
163
164 $session = CRM_Core_Session::singleton();
165 $session->pushUserContext(CRM_Utils_System::url('civicrm/mailing/browse/scheduled',
166 'reset=1&scheduled=true&sms=1'
167 ));
168 }
169
170 /**
171 * Display Name of the form.
172 *
173 *
174 * @return string
175 */
176 public function getTitle() {
177 return ts('Schedule or Send');
178 }
179
180 }