3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This file is used to build the form configuring mass sms details.
21 class CRM_SMS_Form_Upload
extends CRM_Core_Form
{
24 public function preProcess() {
25 $this->_mailingID
= $this->get('mailing_id');
29 * Set default values for the form.
31 public function setDefaultValues() {
32 $mailingID = CRM_Utils_Request
::retrieve('mid', 'Integer', $this, FALSE, NULL);
34 // Need to differentiate new/reuse mailing, CRM-2873.
35 $reuseMailing = FALSE;
40 $mailingID = $this->_mailingID
;
43 $count = $this->get('count');
44 $this->assign('count', $count);
46 $this->set('skipTextFile', FALSE);
51 $dao = new CRM_Mailing_DAO_Mailing();
52 $dao->id
= $mailingID;
54 $dao->storeValues($dao, $defaults);
56 // We don't want to retrieve template details once it is
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);
68 $defaults['sms_text_message'] = $messageTemplate->msg_text
;
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);
80 $textFilePath = $this->get('textFilePath');
82 file_exists($textFilePath)
84 $defaults['sms_text_message'] = file_get_contents($textFilePath);
85 if (strlen($defaults['sms_text_message']) > 0) {
86 $this->set('skipTextFile', TRUE);
91 $defaults['upload_type'] = 1;
97 * Build the form object.
99 public function buildQuickForm() {
100 $session = CRM_Core_Session
::singleton();
101 $config = CRM_Core_Config
::singleton();
105 $this->assign('max_sms_length', CRM_SMS_Provider
::MAX_SMS_CHAR
);
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}"
112 $attributes = ['onclick' => "showHideUpload();"];
113 $options = [ts('Upload Content'), ts('Compose On-screen')];
115 $this->addRadio('upload_type', ts('I want to'), $options, $attributes, " ");
117 CRM_Mailing_BAO_Mailing
::commonCompose($this);
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');
124 $this->addFormRule(['CRM_SMS_Form_Upload', 'formRule'], $this);
129 'name' => ts('Previous'),
133 'name' => ts('Next'),
134 'spacing' => ' ',
139 'name' => ts('Cancel'),
143 $this->addButtons($buttons);
146 public function postProcess() {
148 $uploadParams = ['from_name'];
150 $formValues = $this->controller
->exportValues($this->_name
);
152 foreach ($uploadParams as $key) {
153 if (!empty($formValues[$key])) {
154 $params[$key] = $formValues[$key];
155 $this->set($key, $formValues[$key]);
159 if (!$formValues['upload_type']) {
161 if (isset($formValues['textFile']) &&
162 !empty($formValues['textFile'])
164 $contents = file_get_contents($formValues['textFile']['name']);
165 $this->set($key, $formValues['textFile']['name']);
168 $params['body_text'] = $contents;
171 $params['body_text'] = 'NULL';
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']);
181 $params['name'] = $this->get('name');
183 $session = CRM_Core_Session
::singleton();
184 $params['contact_id'] = $session->get('userID');
189 'SMSsaveTemplateName',
192 // Mail template is composed.
193 if ($formValues['upload_type']) {
195 foreach ($composeFields as $key) {
196 if (!empty($formValues[$key])) {
197 $composeParams[$key] = $formValues[$key];
198 $this->set($key, $formValues[$key]);
202 if (!empty($composeParams['SMSupdateTemplate'])) {
204 'msg_text' => $text_message,
209 $templateParams['id'] = $formValues['SMStemplate'];
211 $msgTemplate = CRM_Core_BAO_MessageTemplate
::add($templateParams);
214 if (!empty($composeParams['SMSsaveTemplate'])) {
216 'msg_text' => $text_message,
221 $templateParams['msg_title'] = $composeParams['SMSsaveTemplateName'];
223 $msgTemplate = CRM_Core_BAO_MessageTemplate
::add($templateParams);
226 if (isset($msgTemplate->id
)) {
227 $params['msg_template_id'] = $msgTemplate->id
;
230 $params['msg_template_id'] = $formValues['SMStemplate'] ??
NULL;
232 $this->set('template', $params['msg_template_id']);
235 $params['id'] = $this->_mailingID
;
237 // Build SMS in mailing table.
238 CRM_Mailing_BAO_Mailing
::create($params);
244 * @param array $params
245 * (ref.) an assoc array of name/value pairs.
251 * mixed true or array of errors
253 public static function formRule($params, $files, $self) {
254 if (!empty($_POST['_qf_Import_refresh'])) {
258 $template = CRM_Core_Smarty
::singleton();
260 $domain = CRM_Core_BAO_Domain
::getDomain();
262 $mailing = new CRM_Mailing_BAO_Mailing();
263 $mailing->id
= $self->_mailingID
;
264 $mailing->find(TRUE);
266 $session = CRM_Core_Session
::singleton();
268 'contact_id' => $session->get('userID'),
271 require_once 'api/api.php';
272 $contact = civicrm_api('contact', 'get', $values);
275 $contact = reset($contact['values']);
277 $verp = array_flip(['optOut', 'reply', 'unsubscribe', 'resubscribe', 'owner']);
278 foreach ($verp as $key => $value) {
282 $urls = array_flip(['forward', 'optOutUrl', 'unsubscribeUrl', 'resubscribeUrl']);
283 foreach ($urls as $key => $value) {
287 $skipTextFile = $self->get('skipTextFile');
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');
297 if (empty($params['sms_text_message'])) {
298 $errors['sms_text_message'] = ts('Please provide a Text');
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
]);
308 if (!empty($params['SMSsaveTemplate']) && empty($params['SMSsaveTemplateName'])) {
309 $errors['SMSsaveTemplateName'] = ts('Please provide a Template Name.');
313 if (($params['upload_type'] ||
file_exists(CRM_Utils_Array
::value('tmp_name', $files['textFile']))) ||
314 (!$params['upload_type'] && $params['text_message'])
317 if (!$params['upload_type']) {
318 $str = file_get_contents($files['textFile']['tmp_name']);
319 $name = $files['textFile']['name'];
322 $str = $params['sms_text_message'];
323 $name = 'text message';
328 // Do a full token replacement on a dummy verp, the current
329 // contact and domain, and the first organization.
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
338 $dummy_mail = new CRM_Mailing_BAO_Mailing();
340 $dummy_mail->$mess = $str;
341 $tokens = $dummy_mail->getTokens();
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']);
350 $unmatched = CRM_Utils_Token
::unmatchedTokens($str);
351 $contentCheck = CRM_Utils_String
::htmlToText($str);
353 if (!empty($unmatched) && 0) {
354 foreach ($unmatched as $token) {
355 $dataErrors[] = '<li>' . ts('Invalid token code') . ' {' . $token . '}</li>';
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>';
361 if (!empty($dataErrors)) {
362 $errors['textFile'] = ts('The following errors were detected in %1:', [
364 ]) . ' <ul>' . implode('', $dataErrors) . '</ul>';
368 $templateName = CRM_Core_BAO_MessageTemplate
::getMessageTemplates();
369 if (!empty($params['SMSsaveTemplate']) && in_array(CRM_Utils_Array
::value('SMSsaveTemplateName', $params), $templateName)
371 $errors['SMSsaveTemplate'] = ts('Duplicate Template Name.');
373 return empty($errors) ?
TRUE : $errors;
377 * Display Name of the form.
382 public function getTitle() {
383 return ts('SMS Content');
387 * List available tokens for this form.
391 public function listTokens() {
392 $tokens = CRM_Core_SelectValues
::contactTokens();