Merge pull request #18712 from eileenmcnaughton/locev3
[civicrm-core.git] / CRM / SMS / Form / Schedule.php
CommitLineData
6a488035
TO
1<?php
2/*
bc77d7c0
TO
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 +--------------------------------------------------------------------+
6a488035
TO
10 */
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17class CRM_SMS_Form_Schedule extends CRM_Core_Form {
18
423616fa
CW
19 public $submitOnce = TRUE;
20
6a488035 21 /**
fe482240 22 * Set variables up before form is built.
6a488035
TO
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 /**
c490a46a 34 * Set default values for the form.
6a488035 35 */
00be9182 36 public function setDefaultValues() {
be2fb01f 37 $defaults = [];
6a488035
TO
38
39 $count = $this->get('count');
40
41 $this->assign('count', $count);
e737155e 42 $defaults['send_option'] = 'send_immediate';
6a488035
TO
43 return $defaults;
44 }
45
46 /**
fe482240 47 * Build the form object for the last step of the sms wizard.
6a488035
TO
48 */
49 public function buildQuickform() {
6a488035 50
e737155e
J
51 // Fix Firefox issue where the non-default field is displayed as checked
52 // on page refresh.
53 $this->setAttribute('autocomplete', 'off');
54
39405208
SL
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 ]);
e737155e 62
99f914d4 63 $this->add('datepicker', 'start_date', '', NULL, FALSE, ['minDate' => date('Y-m-d')]);
6a488035 64
be2fb01f 65 $this->addFormRule(['CRM_SMS_Form_Schedule', 'formRule'], $this);
6a488035 66
be2fb01f
CW
67 $buttons = [
68 [
6ea503d4 69 'type' => 'back',
f212d37d 70 'name' => ts('Previous'),
be2fb01f
CW
71 ],
72 [
6a488035
TO
73 'type' => 'next',
74 'name' => ts('Submit Mass SMS'),
75 'spacing' => '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;',
76 'isDefault' => TRUE,
be2fb01f
CW
77 ],
78 [
6a488035
TO
79 'type' => 'cancel',
80 'name' => ts('Continue Later'),
be2fb01f
CW
81 ],
82 ];
6a488035
TO
83
84 $this->addButtons($buttons);
85
be2fb01f 86 $preview = [];
353ffa53 87 $preview['type'] = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_Mailing', $this->_mailingID, 'body_html') ? 'html' : 'text';
6a488035
TO
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 *
901b501a
TO
99 * @param array $params
100 * The form values.
fd31fa4c
EM
101 *
102 * @param $files
103 * @param $self
6a488035 104 *
8d7a9d07
CB
105 * @return bool
106 * True if either we deliver immediately, or the date is properly
107 * set.
6a488035
TO
108 */
109 public static function formRule($params, $files, $self) {
6a488035 110
e737155e 111 if (!empty($params['_qf_Schedule_submit'])) {
6a488035
TO
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 }
e737155e
J
116
117 if ((isset($params['send_option']) && $params['send_option'] == 'send_immediate') || CRM_Utils_Array::value('_qf_Schedule_back', $params) == ts('Previous')) {
6a488035
TO
118 return TRUE;
119 }
120
e737155e 121 if (strtotime($params['start_date']) < time()) {
be2fb01f 122 return [
9d72cede 123 'start_date' => ts('Start date cannot be earlier than the current time.'),
be2fb01f 124 ];
6a488035 125 }
e737155e 126
6a488035
TO
127 return TRUE;
128 }
129
130 /**
e737155e 131 * Process the posted form values. Create and schedule a Mass SMS.
6a488035
TO
132 */
133 public function postProcess() {
be2fb01f 134 $params = [];
6a488035 135
77552d44 136 $params['id'] = $this->_mailingID;
6a488035 137
77552d44 138 if (empty($params['id'])) {
4a0e3fe7 139 CRM_Core_Error::statusBounce(ts('Could not find a mailing id'));
6a488035
TO
140 }
141
80b33705 142 $params['send_option'] = $this->controller->exportValue($this->_name, 'send_option');
e737155e 143 if (isset($params['send_option']) && $params['send_option'] == 'send_immediate') {
1e2f5f25
DS
144 $params['scheduled_date'] = date('YmdHis');
145 }
146 else {
e737155e 147 $params['scheduled_date'] = $this->controller->exportValue($this->_name, 'start_date');
1e2f5f25
DS
148 }
149
150 $session = CRM_Core_Session::singleton();
151 // set the scheduled_id
152 $params['scheduled_id'] = $session->get('userID');
1e2f5f25
DS
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;
6a488035
TO
159 }
160
39404680 161 // Build the mailing object.
77552d44 162 CRM_Mailing_BAO_Mailing::create($params);
1e2f5f25 163
6a488035
TO
164 $session = CRM_Core_Session::singleton();
165 $session->pushUserContext(CRM_Utils_System::url('civicrm/mailing/browse/scheduled',
353ffa53
TO
166 'reset=1&scheduled=true&sms=1'
167 ));
6a488035
TO
168 }
169
170 /**
fe482240 171 * Display Name of the form.
6a488035 172 *
6a488035
TO
173 *
174 * @return string
175 */
176 public function getTitle() {
177 return ts('Schedule or Send');
178 }
96025800 179
6a488035 180}