Merge pull request #17640 from samuelsov/bugreportcivigrant
[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 $sendOptions = [
56 $this->createElement('radio', NULL, NULL, ts('Send immediately'), 'send_immediate', ['id' => 'send_immediate', 'style' => 'margin-bottom: 10px;']),
57 $this->createElement('radio', NULL, NULL, ts('Send at:'), 'send_later', ['id' => 'send_later']),
58 ];
59 $this->addGroup($sendOptions, 'send_option', '', '<br>');
60
61 $this->add('datepicker', 'start_date', '', NULL, FALSE, ['minDate' => time()]);
62
63 $this->addFormRule(['CRM_SMS_Form_Schedule', 'formRule'], $this);
64
65 $buttons = [
66 [
67 'type' => 'back',
68 'name' => ts('Previous'),
69 ],
70 [
71 'type' => 'next',
72 'name' => ts('Submit Mass SMS'),
73 'spacing' => '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;',
74 'isDefault' => TRUE,
75 ],
76 [
77 'type' => 'cancel',
78 'name' => ts('Continue Later'),
79 ],
80 ];
81
82 $this->addButtons($buttons);
83
84 $preview = [];
85 $preview['type'] = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_Mailing', $this->_mailingID, 'body_html') ? 'html' : 'text';
86 $preview['viewURL'] = CRM_Utils_System::url('civicrm/mailing/view', "reset=1&id={$this->_mailingID}");
87 $this->assign_by_ref('preview', $preview);
88 }
89
90 /**
91 * Form rule to validate the date selector and/or if we should deliver
92 * immediately.
93 *
94 * Warning: if you make changes here, be sure to also make them in
95 * Retry.php
96 *
97 * @param array $params
98 * The form values.
99 *
100 * @param $files
101 * @param $self
102 *
103 * @return bool
104 * True if either we deliver immediately, or the date is properly
105 * set.
106 */
107 public static function formRule($params, $files, $self) {
108
109 if (!empty($params['_qf_Schedule_submit'])) {
110 CRM_Core_Session::setStatus(ts("Your Mass SMS has been saved. Click the 'Continue' action to resume working on it."), ts('Saved'), 'success');
111 $url = CRM_Utils_System::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1&sms=1');
112 CRM_Utils_System::redirect($url);
113 }
114
115 if ((isset($params['send_option']) && $params['send_option'] == 'send_immediate') || CRM_Utils_Array::value('_qf_Schedule_back', $params) == ts('Previous')) {
116 return TRUE;
117 }
118
119 if (strtotime($params['start_date']) < time()) {
120 return [
121 'start_date' => ts('Start date cannot be earlier than the current time.'),
122 ];
123 }
124
125 return TRUE;
126 }
127
128 /**
129 * Process the posted form values. Create and schedule a Mass SMS.
130 */
131 public function postProcess() {
132 $params = [];
133
134 $params['id'] = $this->_mailingID;
135
136 if (empty($params['id'])) {
137 CRM_Core_Error::statusBounce(ts('Could not find a mailing id'));
138 }
139
140 $params['send_option'] = $this->controller->exportValue($this->_name, 'send_option');
141 if (isset($params['send_option']) && $params['send_option'] == 'send_immediate') {
142 $params['scheduled_date'] = date('YmdHis');
143 }
144 else {
145 $params['scheduled_date'] = $this->controller->exportValue($this->_name, 'start_date');
146 }
147
148 $session = CRM_Core_Session::singleton();
149 // set the scheduled_id
150 $params['scheduled_id'] = $session->get('userID');
151
152 // set approval details if workflow is not enabled
153 if (!CRM_Mailing_Info::workflowEnabled()) {
154 $params['approver_id'] = $session->get('userID');
155 $params['approval_date'] = date('YmdHis');
156 $params['approval_status_id'] = 1;
157 }
158
159 // Build the mailing object.
160 CRM_Mailing_BAO_Mailing::create($params);
161
162 $session = CRM_Core_Session::singleton();
163 $session->pushUserContext(CRM_Utils_System::url('civicrm/mailing/browse/scheduled',
164 'reset=1&scheduled=true&sms=1'
165 ));
166 }
167
168 /**
169 * Display Name of the form.
170 *
171 *
172 * @return string
173 */
174 public function getTitle() {
175 return ts('Schedule or Send');
176 }
177
178 }