3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * This file is used to build the form configuring mass sms details.
37 class CRM_SMS_Form_Upload
extends CRM_Core_Form
{
40 public function preProcess() {
41 $this->_mailingID
= $this->get('mailing_id');
45 * Set default values for the form.
47 public function setDefaultValues() {
48 $mailingID = CRM_Utils_Request
::retrieve('mid', 'Integer', $this, FALSE, NULL);
50 // Need to differentiate new/reuse mailing, CRM-2873.
51 $reuseMailing = FALSE;
56 $mailingID = $this->_mailingID
;
59 $count = $this->get('count');
60 $this->assign('count', $count);
62 $this->set('skipTextFile', FALSE);
67 $dao = new CRM_Mailing_DAO_Mailing();
68 $dao->id
= $mailingID;
70 $dao->storeValues($dao, $defaults);
72 // We don't want to retrieve template details once it is
74 $templateId = $this->get('template');
75 $this->assign('templateSelected', $templateId ?
$templateId : 0);
76 if (isset($defaults['msg_template_id']) && !$templateId) {
77 $defaults['SMStemplate'] = $defaults['msg_template_id'];
78 $messageTemplate = new CRM_Core_DAO_MessageTemplate();
79 $messageTemplate->id
= $defaults['msg_template_id'];
80 $messageTemplate->selectAdd();
81 $messageTemplate->selectAdd('msg_text');
82 $messageTemplate->find(TRUE);
84 $defaults['sms_text_message'] = $messageTemplate->msg_text
;
87 if (isset($defaults['body_text'])) {
88 $defaults['sms_text_message'] = $defaults['body_text'];
89 $this->set('textFile', $defaults['body_text']);
90 $this->set('skipTextFile', TRUE);
96 $textFilePath = $this->get('textFilePath');
98 file_exists($textFilePath)
100 $defaults['sms_text_message'] = file_get_contents($textFilePath);
101 if (strlen($defaults['sms_text_message']) > 0) {
102 $this->set('skipTextFile', TRUE);
107 $defaults['upload_type'] = 1;
113 * Build the form object.
115 public function buildQuickForm() {
116 $session = CRM_Core_Session
::singleton();
117 $config = CRM_Core_Config
::singleton();
121 $this->assign('max_sms_length', CRM_SMS_Provider
::MAX_SMS_CHAR
);
123 // this seems so hacky, not sure what we are doing here and why. Need to investigate and fix
124 $session->getVars($options,
125 "CRM_SMS_Controller_Send_{$this->controller->_key}"
128 $attributes = ['onclick' => "showHideUpload();"];
129 $options = [ts('Upload Content'), ts('Compose On-screen')];
131 $this->addRadio('upload_type', ts('I want to'), $options, $attributes, " ");
133 CRM_Mailing_BAO_Mailing
::commonCompose($this);
135 $this->addElement('file', 'textFile', ts('Upload TEXT Message'), 'size=30 maxlength=60');
136 $this->setMaxFileSize(1024 * 1024);
137 $this->addRule('textFile', ts('File size should be less than 1 MByte'), 'maxfilesize', 1024 * 1024);
138 $this->addRule('textFile', ts('File must be in UTF-8 encoding'), 'utf8File');
140 $this->addFormRule(['CRM_SMS_Form_Upload', 'formRule'], $this);
145 'name' => ts('Previous'),
149 'name' => ts('Next'),
150 'spacing' => ' ',
155 'name' => ts('Cancel'),
159 $this->addButtons($buttons);
162 public function postProcess() {
164 $uploadParams = ['from_name'];
166 $formValues = $this->controller
->exportValues($this->_name
);
168 foreach ($uploadParams as $key) {
169 if (!empty($formValues[$key])) {
170 $params[$key] = $formValues[$key];
171 $this->set($key, $formValues[$key]);
175 if (!$formValues['upload_type']) {
177 if (isset($formValues['textFile']) &&
178 !empty($formValues['textFile'])
180 $contents = file_get_contents($formValues['textFile']['name']);
181 $this->set($key, $formValues['textFile']['name']);
184 $params['body_text'] = $contents;
187 $params['body_text'] = 'NULL';
191 $text_message = $formValues['sms_text_message'];
192 $params['body_text'] = $text_message;
193 $this->set('textFile', $params['body_text']);
194 $this->set('text_message', $params['body_text']);
197 $params['name'] = $this->get('name');
199 $session = CRM_Core_Session
::singleton();
200 $params['contact_id'] = $session->get('userID');
205 'SMSsaveTemplateName',
208 // Mail template is composed.
209 if ($formValues['upload_type']) {
211 foreach ($composeFields as $key) {
212 if (!empty($formValues[$key])) {
213 $composeParams[$key] = $formValues[$key];
214 $this->set($key, $formValues[$key]);
218 if (!empty($composeParams['SMSupdateTemplate'])) {
220 'msg_text' => $text_message,
225 $templateParams['id'] = $formValues['SMStemplate'];
227 $msgTemplate = CRM_Core_BAO_MessageTemplate
::add($templateParams);
230 if (!empty($composeParams['SMSsaveTemplate'])) {
232 'msg_text' => $text_message,
237 $templateParams['msg_title'] = $composeParams['SMSsaveTemplateName'];
239 $msgTemplate = CRM_Core_BAO_MessageTemplate
::add($templateParams);
242 if (isset($msgTemplate->id
)) {
243 $params['msg_template_id'] = $msgTemplate->id
;
246 $params['msg_template_id'] = CRM_Utils_Array
::value('SMStemplate', $formValues);
248 $this->set('template', $params['msg_template_id']);
251 $ids['mailing_id'] = $this->_mailingID
;
253 // Build SMS in mailing table.
254 CRM_Mailing_BAO_Mailing
::create($params, $ids);
260 * @param array $params
261 * (ref.) an assoc array of name/value pairs.
267 * mixed true or array of errors
269 public static function formRule($params, $files, $self) {
270 if (!empty($_POST['_qf_Import_refresh'])) {
274 $template = CRM_Core_Smarty
::singleton();
276 $domain = CRM_Core_BAO_Domain
::getDomain();
278 $mailing = new CRM_Mailing_BAO_Mailing();
279 $mailing->id
= $self->_mailingID
;
280 $mailing->find(TRUE);
282 $session = CRM_Core_Session
::singleton();
284 'contact_id' => $session->get('userID'),
287 require_once 'api/api.php';
288 $contact = civicrm_api('contact', 'get', $values);
291 $contact = reset($contact['values']);
293 $verp = array_flip(['optOut', 'reply', 'unsubscribe', 'resubscribe', 'owner']);
294 foreach ($verp as $key => $value) {
298 $urls = array_flip(['forward', 'optOutUrl', 'unsubscribeUrl', 'resubscribeUrl']);
299 foreach ($urls as $key => $value) {
303 $skipTextFile = $self->get('skipTextFile');
305 if (!$params['upload_type']) {
306 if ((!isset($files['textFile']) ||
!file_exists($files['textFile']['tmp_name']))) {
307 if (!($skipTextFile)) {
308 $errors['textFile'] = ts('Please provide a Text');
313 if (empty($params['sms_text_message'])) {
314 $errors['sms_text_message'] = ts('Please provide a Text');
317 if (!empty($params['text_message'])) {
318 $messageCheck = CRM_Utils_Array
::value('text_message', $params);
319 if ($messageCheck && (strlen($messageCheck) > CRM_SMS_Provider
::MAX_SMS_CHAR
)) {
320 $errors['text_message'] = ts("You can configure the SMS message body up to %1 characters", [1 => CRM_SMS_Provider
::MAX_SMS_CHAR
]);
324 if (!empty($params['SMSsaveTemplate']) && empty($params['SMSsaveTemplateName'])) {
325 $errors['SMSsaveTemplateName'] = ts('Please provide a Template Name.');
329 if (($params['upload_type'] ||
file_exists(CRM_Utils_Array
::value('tmp_name', $files['textFile']))) ||
330 (!$params['upload_type'] && $params['text_message'])
333 if (!$params['upload_type']) {
334 $str = file_get_contents($files['textFile']['tmp_name']);
335 $name = $files['textFile']['name'];
338 $str = $params['sms_text_message'];
339 $name = 'text message';
344 // Do a full token replacement on a dummy verp, the current
345 // contact and domain, and the first organization.
347 // here we make a dummy mailing object so that we
348 // can retrieve the tokens that we need to replace
349 // so that we do get an invalid token error
350 // this is qute hacky and I hope that there might
351 // be a suggestion from someone on how to
352 // make it a bit more elegant
354 $dummy_mail = new CRM_Mailing_BAO_Mailing();
356 $dummy_mail->$mess = $str;
357 $tokens = $dummy_mail->getTokens();
359 $str = CRM_Utils_Token
::replaceSubscribeInviteTokens($str);
360 $str = CRM_Utils_Token
::replaceDomainTokens($str, $domain, NULL, $tokens['text']);
361 $str = CRM_Utils_Token
::replaceMailingTokens($str, $mailing, NULL, $tokens['text']);
362 $str = CRM_Utils_Token
::replaceOrgTokens($str, $org);
363 $str = CRM_Utils_Token
::replaceActionTokens($str, $verp, $urls, NULL, $tokens['text']);
364 $str = CRM_Utils_Token
::replaceContactTokens($str, $contact, NULL, $tokens['text']);
366 $unmatched = CRM_Utils_Token
::unmatchedTokens($str);
367 $contentCheck = CRM_Utils_String
::htmlToText($str);
369 if (!empty($unmatched) && 0) {
370 foreach ($unmatched as $token) {
371 $dataErrors[] = '<li>' . ts('Invalid token code') . ' {' . $token . '}</li>';
374 if (strlen($contentCheck) > CRM_SMS_Provider
::MAX_SMS_CHAR
) {
375 $dataErrors[] = '<li>' . ts('The body of the SMS cannot exceed %1 characters.', [1 => CRM_SMS_Provider
::MAX_SMS_CHAR
]) . '</li>';
377 if (!empty($dataErrors)) {
378 $errors['textFile'] = ts('The following errors were detected in %1:', [
380 ]) . ' <ul>' . implode('', $dataErrors) . '</ul>';
384 $templateName = CRM_Core_BAO_MessageTemplate
::getMessageTemplates();
385 if (!empty($params['SMSsaveTemplate']) && in_array(CRM_Utils_Array
::value('SMSsaveTemplateName', $params), $templateName)
387 $errors['SMSsaveTemplate'] = ts('Duplicate Template Name.');
389 return empty($errors) ?
TRUE : $errors;
393 * Display Name of the form.
398 public function getTitle() {
399 return ts('SMS Content');
403 * List available tokens for this form.
407 public function listTokens() {
408 $tokens = CRM_Core_SelectValues
::contactTokens();