Merge pull request #18712 from eileenmcnaughton/locev3
[civicrm-core.git] / CRM / SMS / Form / Upload.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
18 /**
19 * This file is used to build the form configuring mass sms details.
20 */
21 class CRM_SMS_Form_Upload extends CRM_Core_Form {
22 public $_mailingID;
23
24 public function preProcess() {
25 $this->_mailingID = $this->get('mailing_id');
26 }
27
28 /**
29 * Set default values for the form.
30 */
31 public function setDefaultValues() {
32 $mailingID = CRM_Utils_Request::retrieve('mid', 'Integer', $this, FALSE, NULL);
33
34 // Need to differentiate new/reuse mailing, CRM-2873.
35 $reuseMailing = FALSE;
36 if ($mailingID) {
37 $reuseMailing = TRUE;
38 }
39 else {
40 $mailingID = $this->_mailingID;
41 }
42
43 $count = $this->get('count');
44 $this->assign('count', $count);
45
46 $this->set('skipTextFile', FALSE);
47
48 $defaults = [];
49
50 if ($mailingID) {
51 $dao = new CRM_Mailing_DAO_Mailing();
52 $dao->id = $mailingID;
53 $dao->find(TRUE);
54 $dao->storeValues($dao, $defaults);
55
56 // We don't want to retrieve template details once it is
57 // set in session.
58 $templateId = $this->get('template');
59 $this->assign('templateSelected', $templateId ? $templateId : 0);
60 if (isset($defaults['msg_template_id']) && !$templateId) {
61 $defaults['SMStemplate'] = $defaults['msg_template_id'];
62 $messageTemplate = new CRM_Core_DAO_MessageTemplate();
63 $messageTemplate->id = $defaults['msg_template_id'];
64 $messageTemplate->selectAdd();
65 $messageTemplate->selectAdd('msg_text');
66 $messageTemplate->find(TRUE);
67
68 $defaults['sms_text_message'] = $messageTemplate->msg_text;
69 }
70
71 if (isset($defaults['body_text'])) {
72 $defaults['sms_text_message'] = $defaults['body_text'];
73 $this->set('textFile', $defaults['body_text']);
74 $this->set('skipTextFile', TRUE);
75 }
76 }
77
78 // Fix for CRM-2873.
79 if (!$reuseMailing) {
80 $textFilePath = $this->get('textFilePath');
81 if ($textFilePath &&
82 file_exists($textFilePath)
83 ) {
84 $defaults['sms_text_message'] = file_get_contents($textFilePath);
85 if (strlen($defaults['sms_text_message']) > 0) {
86 $this->set('skipTextFile', TRUE);
87 }
88 }
89 }
90
91 $defaults['upload_type'] = 1;
92
93 return $defaults;
94 }
95
96 /**
97 * Build the form object.
98 */
99 public function buildQuickForm() {
100 $session = CRM_Core_Session::singleton();
101 $config = CRM_Core_Config::singleton();
102 $options = [];
103 $tempVar = FALSE;
104
105 $this->assign('max_sms_length', CRM_SMS_Provider::MAX_SMS_CHAR);
106
107 // this seems so hacky, not sure what we are doing here and why. Need to investigate and fix
108 $session->getVars($options,
109 "CRM_SMS_Controller_Send_{$this->controller->_key}"
110 );
111
112 $attributes = ['onclick' => "showHideUpload();"];
113 $options = [ts('Upload Content'), ts('Compose On-screen')];
114
115 $this->addRadio('upload_type', ts('I want to'), $options, $attributes, "&nbsp;&nbsp;");
116
117 CRM_Mailing_BAO_Mailing::commonCompose($this);
118
119 $this->addElement('file', 'textFile', ts('Upload TEXT Message'), 'size=30 maxlength=60');
120 $this->setMaxFileSize(1024 * 1024);
121 $this->addRule('textFile', ts('File size should be less than 1 MByte'), 'maxfilesize', 1024 * 1024);
122 $this->addRule('textFile', ts('File must be in UTF-8 encoding'), 'utf8File');
123
124 $this->addFormRule(['CRM_SMS_Form_Upload', 'formRule'], $this);
125
126 $buttons = [
127 [
128 'type' => 'back',
129 'name' => ts('Previous'),
130 ],
131 [
132 'type' => 'upload',
133 'name' => ts('Next'),
134 'spacing' => '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;',
135 'isDefault' => TRUE,
136 ],
137 [
138 'type' => 'cancel',
139 'name' => ts('Cancel'),
140 ],
141 ];
142
143 $this->addButtons($buttons);
144 }
145
146 public function postProcess() {
147 $params = [];
148 $uploadParams = ['from_name'];
149
150 $formValues = $this->controller->exportValues($this->_name);
151
152 foreach ($uploadParams as $key) {
153 if (!empty($formValues[$key])) {
154 $params[$key] = $formValues[$key];
155 $this->set($key, $formValues[$key]);
156 }
157 }
158
159 if (!$formValues['upload_type']) {
160 $contents = NULL;
161 if (isset($formValues['textFile']) &&
162 !empty($formValues['textFile'])
163 ) {
164 $contents = file_get_contents($formValues['textFile']['name']);
165 $this->set($key, $formValues['textFile']['name']);
166 }
167 if ($contents) {
168 $params['body_text'] = $contents;
169 }
170 else {
171 $params['body_text'] = 'NULL';
172 }
173 }
174 else {
175 $text_message = $formValues['sms_text_message'];
176 $params['body_text'] = $text_message;
177 $this->set('textFile', $params['body_text']);
178 $this->set('text_message', $params['body_text']);
179 }
180
181 $params['name'] = $this->get('name');
182
183 $session = CRM_Core_Session::singleton();
184 $params['contact_id'] = $session->get('userID');
185 $composeFields = [
186 'SMStemplate',
187 'SMSsaveTemplate',
188 'SMSupdateTemplate',
189 'SMSsaveTemplateName',
190 ];
191 $msgTemplate = NULL;
192 // Mail template is composed.
193 if ($formValues['upload_type']) {
194 $composeParams = [];
195 foreach ($composeFields as $key) {
196 if (!empty($formValues[$key])) {
197 $composeParams[$key] = $formValues[$key];
198 $this->set($key, $formValues[$key]);
199 }
200 }
201
202 if (!empty($composeParams['SMSupdateTemplate'])) {
203 $templateParams = [
204 'msg_text' => $text_message,
205 'is_active' => TRUE,
206 'is_sms' => TRUE,
207 ];
208
209 $templateParams['id'] = $formValues['SMStemplate'];
210
211 $msgTemplate = CRM_Core_BAO_MessageTemplate::add($templateParams);
212 }
213
214 if (!empty($composeParams['SMSsaveTemplate'])) {
215 $templateParams = [
216 'msg_text' => $text_message,
217 'is_active' => TRUE,
218 'is_sms' => TRUE,
219 ];
220
221 $templateParams['msg_title'] = $composeParams['SMSsaveTemplateName'];
222
223 $msgTemplate = CRM_Core_BAO_MessageTemplate::add($templateParams);
224 }
225
226 if (isset($msgTemplate->id)) {
227 $params['msg_template_id'] = $msgTemplate->id;
228 }
229 else {
230 $params['msg_template_id'] = $formValues['SMStemplate'] ?? NULL;
231 }
232 $this->set('template', $params['msg_template_id']);
233 }
234
235 $params['id'] = $this->_mailingID;
236
237 // Build SMS in mailing table.
238 CRM_Mailing_BAO_Mailing::create($params);
239 }
240
241 /**
242 * Validation.
243 *
244 * @param array $params
245 * (ref.) an assoc array of name/value pairs.
246 *
247 * @param $files
248 * @param $self
249 *
250 * @return bool|array
251 * mixed true or array of errors
252 */
253 public static function formRule($params, $files, $self) {
254 if (!empty($_POST['_qf_Import_refresh'])) {
255 return TRUE;
256 }
257 $errors = [];
258 $template = CRM_Core_Smarty::singleton();
259
260 $domain = CRM_Core_BAO_Domain::getDomain();
261
262 $mailing = new CRM_Mailing_BAO_Mailing();
263 $mailing->id = $self->_mailingID;
264 $mailing->find(TRUE);
265
266 $session = CRM_Core_Session::singleton();
267 $values = [
268 'contact_id' => $session->get('userID'),
269 'version' => 3,
270 ];
271 require_once 'api/api.php';
272 $contact = civicrm_api('contact', 'get', $values);
273
274 // CRM-4524.
275 $contact = reset($contact['values']);
276
277 $verp = array_flip(['optOut', 'reply', 'unsubscribe', 'resubscribe', 'owner']);
278 foreach ($verp as $key => $value) {
279 $verp[$key]++;
280 }
281
282 $urls = array_flip(['forward', 'optOutUrl', 'unsubscribeUrl', 'resubscribeUrl']);
283 foreach ($urls as $key => $value) {
284 $urls[$key]++;
285 }
286
287 $skipTextFile = $self->get('skipTextFile');
288
289 if (!$params['upload_type']) {
290 if ((!isset($files['textFile']) || !file_exists($files['textFile']['tmp_name']))) {
291 if (!($skipTextFile)) {
292 $errors['textFile'] = ts('Please provide a Text');
293 }
294 }
295 }
296 else {
297 if (empty($params['sms_text_message'])) {
298 $errors['sms_text_message'] = ts('Please provide a Text');
299 }
300 else {
301 if (!empty($params['text_message'])) {
302 $messageCheck = $params['text_message'] ?? NULL;
303 if ($messageCheck && (strlen($messageCheck) > CRM_SMS_Provider::MAX_SMS_CHAR)) {
304 $errors['text_message'] = ts("You can configure the SMS message body up to %1 characters", [1 => CRM_SMS_Provider::MAX_SMS_CHAR]);
305 }
306 }
307 }
308 if (!empty($params['SMSsaveTemplate']) && empty($params['SMSsaveTemplateName'])) {
309 $errors['SMSsaveTemplateName'] = ts('Please provide a Template Name.');
310 }
311 }
312
313 if (($params['upload_type'] || file_exists(CRM_Utils_Array::value('tmp_name', $files['textFile']))) ||
314 (!$params['upload_type'] && $params['text_message'])
315 ) {
316
317 if (!$params['upload_type']) {
318 $str = file_get_contents($files['textFile']['tmp_name']);
319 $name = $files['textFile']['name'];
320 }
321 else {
322 $str = $params['sms_text_message'];
323 $name = 'text message';
324 }
325
326 $dataErrors = [];
327
328 // Do a full token replacement on a dummy verp, the current
329 // contact and domain, and the first organization.
330
331 // here we make a dummy mailing object so that we
332 // can retrieve the tokens that we need to replace
333 // so that we do get an invalid token error
334 // this is qute hacky and I hope that there might
335 // be a suggestion from someone on how to
336 // make it a bit more elegant
337
338 $dummy_mail = new CRM_Mailing_BAO_Mailing();
339 $mess = "body_text";
340 $dummy_mail->$mess = $str;
341 $tokens = $dummy_mail->getTokens();
342
343 $str = CRM_Utils_Token::replaceSubscribeInviteTokens($str);
344 $str = CRM_Utils_Token::replaceDomainTokens($str, $domain, NULL, $tokens['text']);
345 $str = CRM_Utils_Token::replaceMailingTokens($str, $mailing, NULL, $tokens['text']);
346 $str = CRM_Utils_Token::replaceOrgTokens($str, $org);
347 $str = CRM_Utils_Token::replaceActionTokens($str, $verp, $urls, NULL, $tokens['text']);
348 $str = CRM_Utils_Token::replaceContactTokens($str, $contact, NULL, $tokens['text']);
349
350 $unmatched = CRM_Utils_Token::unmatchedTokens($str);
351 $contentCheck = CRM_Utils_String::htmlToText($str);
352
353 if (!empty($unmatched) && 0) {
354 foreach ($unmatched as $token) {
355 $dataErrors[] = '<li>' . ts('Invalid token code') . ' {' . $token . '}</li>';
356 }
357 }
358 if (strlen($contentCheck) > CRM_SMS_Provider::MAX_SMS_CHAR) {
359 $dataErrors[] = '<li>' . ts('The body of the SMS cannot exceed %1 characters.', [1 => CRM_SMS_Provider::MAX_SMS_CHAR]) . '</li>';
360 }
361 if (!empty($dataErrors)) {
362 $errors['textFile'] = ts('The following errors were detected in %1:', [
363 1 => $name,
364 ]) . ' <ul>' . implode('', $dataErrors) . '</ul>';
365 }
366 }
367
368 $templateName = CRM_Core_BAO_MessageTemplate::getMessageTemplates();
369 if (!empty($params['SMSsaveTemplate']) && in_array(CRM_Utils_Array::value('SMSsaveTemplateName', $params), $templateName)
370 ) {
371 $errors['SMSsaveTemplate'] = ts('Duplicate Template Name.');
372 }
373 return empty($errors) ? TRUE : $errors;
374 }
375
376 /**
377 * Display Name of the form.
378 *
379 *
380 * @return string
381 */
382 public function getTitle() {
383 return ts('SMS Content');
384 }
385
386 /**
387 * List available tokens for this form.
388 *
389 * @return array
390 */
391 public function listTokens() {
392 $tokens = CRM_Core_SelectValues::contactTokens();
393 return $tokens;
394 }
395
396 }