3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
37 * This file is used to build the form configuring mass sms details
39 class CRM_SMS_Form_Upload
extends CRM_Core_Form
{
42 public function preProcess() {
43 $this->_mailingID
= $this->get('mailing_id');
44 if (CRM_Core_Permission
::check('administer CiviCRM')) {
45 $this->assign('isAdmin', 1);
50 * Set default values for the form.
51 * the default values are retrieved from the database
56 public function setDefaultValues() {
57 $mailingID = CRM_Utils_Request
::retrieve('mid', 'Integer', $this, FALSE, NULL);
59 //need to differentiate new/reuse mailing, CRM-2873
60 $reuseMailing = FALSE;
65 $mailingID = $this->_mailingID
;
68 $count = $this->get('count');
69 $this->assign('count', $count);
71 $this->set('skipTextFile', FALSE);
76 $dao = new CRM_Mailing_DAO_Mailing();
77 $dao->id
= $mailingID;
79 $dao->storeValues($dao, $defaults);
81 //we don't want to retrieve template details once it is
83 $templateId = $this->get('template');
84 $this->assign('templateSelected', $templateId ?
$templateId : 0);
85 if (isset($defaults['msg_template_id']) && !$templateId) {
86 $defaults['SMStemplate'] = $defaults['msg_template_id'];
87 $messageTemplate = new CRM_Core_DAO_MessageTemplate();
88 $messageTemplate->id
= $defaults['msg_template_id'];
89 $messageTemplate->selectAdd();
90 $messageTemplate->selectAdd('msg_text');
91 $messageTemplate->find(TRUE);
93 $defaults['sms_text_message'] = $messageTemplate->msg_text
;
96 if (isset($defaults['body_text'])) {
97 $defaults['sms_text_message'] = $defaults['body_text'];
98 $this->set('textFile', $defaults['body_text']);
99 $this->set('skipTextFile', TRUE);
104 if (!$reuseMailing) {
105 $textFilePath = $this->get('textFilePath');
107 file_exists($textFilePath)
109 $defaults['sms_text_message'] = file_get_contents($textFilePath);
110 if (strlen($defaults['sms_text_message']) > 0) {
111 $this->set('skipTextFile', TRUE);
116 $defaults['upload_type'] = 1;
122 * Build the form object
126 public function buildQuickForm() {
127 $session = CRM_Core_Session
::singleton();
128 $config = CRM_Core_Config
::singleton();
132 $this->assign('max_sms_length', CRM_SMS_Provider
::MAX_SMS_CHAR
);
134 // this seems so hacky, not sure what we are doing here and why. Need to investigate and fix
135 $session->getVars($options,
136 "CRM_SMS_Controller_Send_{$this->controller->_key}"
139 $providers = CRM_SMS_BAO_Provider
::getProviders(array('id', 'title'));
141 if (empty($providers)) {
142 //redirect user to configure sms provider.
143 $url = CRM_Utils_System
::url('civicrm/admin/sms/provider', 'action=add&reset=1');
144 $status = ts("There is no SMS Provider Configured. You can add here <a href='%1'>Add SMS Provider</a>", array(1 => $url));
145 $session->setStatus($status);
148 $providerSelect[''] = '- select -';
149 foreach ($providers as $provider) {
150 $providerSelect[$provider['id']] = $provider['title'];
154 $this->add('select', 'sms_provider_id',
155 ts('SMS Provider'), $providerSelect, TRUE
158 $attributes = array('onclick' => "showHideUpload();");
159 $options = array(ts('Upload Content'), ts('Compose On-screen'));
161 $this->addRadio('upload_type', ts('I want to'), $options, $attributes, " ");
163 CRM_Mailing_BAO_Mailing
::commonCompose($this);
165 $this->addElement('file', 'textFile', ts('Upload TEXT Message'), 'size=30 maxlength=60');
166 $this->setMaxFileSize(1024 * 1024);
167 $this->addRule('textFile', ts('File size should be less than 1 MByte'), 'maxfilesize', 1024 * 1024);
168 $this->addRule('textFile', ts('File must be in UTF-8 encoding'), 'utf8File');
170 $this->addFormRule(array('CRM_SMS_Form_Upload', 'formRule'), $this);
173 array('type' => 'back',
174 'name' => ts('<< Previous'),
178 'name' => ts('Next >>'),
179 'spacing' => ' ',
184 'name' => ts('Cancel'),
188 $this->addButtons($buttons);
191 public function postProcess() {
192 $params = $ids = array();
193 $uploadParams = array('from_name');
195 $formValues = $this->controller
->exportValues($this->_name
);
197 foreach ($uploadParams as $key) {
198 if (!empty($formValues[$key])) {
199 $params[$key] = $formValues[$key];
200 $this->set($key, $formValues[$key]);
204 if (!$formValues['upload_type']) {
206 if (isset($formValues['textFile']) &&
207 !empty($formValues['textFile'])
209 $contents = file_get_contents($formValues['textFile']['name']);
210 $this->set($key, $formValues['textFile']['name']);
213 $params['body_text'] = $contents;
216 $params['body_text'] = 'NULL';
220 $text_message = $formValues['sms_text_message'];
221 $params['body_text'] = $text_message;
222 $this->set('textFile', $params['body_text']);
223 $this->set('text_message', $params['body_text']);
226 $params['name'] = $this->get('name');
228 $session = CRM_Core_Session
::singleton();
229 $params['contact_id'] = $session->get('userID');
230 $composeFields = array(
231 'SMStemplate', 'SMSsaveTemplate',
232 'SMSupdateTemplate', 'SMSsaveTemplateName',
235 //mail template is composed
236 if ($formValues['upload_type']) {
237 $composeParams = array();
238 foreach ($composeFields as $key) {
239 if (!empty($formValues[$key])) {
240 $composeParams[$key] = $formValues[$key];
241 $this->set($key, $formValues[$key]);
245 if (!empty($composeParams['SMSupdateTemplate'])) {
246 $templateParams = array(
247 'msg_text' => $text_message,
252 $templateParams['id'] = $formValues['SMStemplate'];
254 $msgTemplate = CRM_Core_BAO_MessageTemplate
::add($templateParams);
257 if (!empty($composeParams['SMSsaveTemplate'])) {
258 $templateParams = array(
259 'msg_text' => $text_message,
264 $templateParams['msg_title'] = $composeParams['SMSsaveTemplateName'];
266 $msgTemplate = CRM_Core_BAO_MessageTemplate
::add($templateParams);
269 if (isset($msgTemplate->id
)) {
270 $params['msg_template_id'] = $msgTemplate->id
;
273 $params['msg_template_id'] = CRM_Utils_Array
::value('SMStemplate', $formValues);
275 $this->set('template', $params['msg_template_id']);
278 $ids['mailing_id'] = $this->_mailingID
;
280 //get the from email address
281 $params['sms_provider_id'] = $formValues['sms_provider_id'];
284 $params['from_name'] = CRM_Core_DAO
::getFieldValue('CRM_SMS_DAO_Provider', $params['sms_provider_id'], 'username');
286 //Build SMS in mailing table
287 CRM_Mailing_BAO_Mailing
::create($params, $ids);
293 * @param array $params (ref.) an assoc array of name/value pairs
298 * @return mixed true or array of errors
301 public static function formRule($params, $files, $self) {
302 if (!empty($_POST['_qf_Import_refresh'])) {
306 $template = CRM_Core_Smarty
::singleton();
309 $domain = CRM_Core_BAO_Domain
::getDomain();
311 $mailing = new CRM_Mailing_BAO_Mailing();
312 $mailing->id
= $self->_mailingID
;
313 $mailing->find(TRUE);
315 $session = CRM_Core_Session
::singleton();
316 $values = array('contact_id' => $session->get('userID'),
319 require_once 'api/api.php';
320 $contact = civicrm_api('contact', 'get', $values);
323 $contact = reset($contact['values']);
325 $verp = array_flip(array('optOut', 'reply', 'unsubscribe', 'resubscribe', 'owner'));
326 foreach ($verp as $key => $value) {
330 $urls = array_flip(array('forward', 'optOutUrl', 'unsubscribeUrl', 'resubscribeUrl'));
331 foreach ($urls as $key => $value) {
336 $skipTextFile = $self->get('skipTextFile');
338 if (!$params['upload_type']) {
339 if ((!isset($files['textFile']) ||
!file_exists($files['textFile']['tmp_name']))) {
340 if (!($skipTextFile)) {
341 $errors['textFile'] = ts('Please provide a Text');
346 if (empty($params['sms_text_message'])) {
347 $errors['sms_text_message'] = ts('Please provide a Text');
350 if (!empty($params['text_message'])) {
351 $messageCheck = CRM_Utils_Array
::value('text_message', $params);
352 if ($messageCheck && (strlen($messageCheck) > CRM_SMS_Provider
::MAX_SMS_CHAR
)) {
353 $errors['text_message'] = ts("You can configure the SMS message body up to %1 characters", array(1 => CRM_SMS_Provider
::MAX_SMS_CHAR
));
357 if (!empty($params['SMSsaveTemplate']) && empty($params['SMSsaveTemplateName'])) {
358 $errors['SMSsaveTemplateName'] = ts('Please provide a Template Name.');
362 if (($params['upload_type'] ||
file_exists(CRM_Utils_Array
::value('tmp_name', $files['textFile']))) ||
363 (!$params['upload_type'] && $params['text_message'])
366 if (!$params['upload_type']) {
367 $str = file_get_contents($files['textFile']['tmp_name']);
368 $name = $files['textFile']['name'];
371 $str = $params['sms_text_message'];
372 $name = 'text message';
375 $dataErrors = array();
377 /* Do a full token replacement on a dummy verp, the current
378 * contact and domain, and the first organization. */
381 // here we make a dummy mailing object so that we
382 // can retrieve the tokens that we need to replace
383 // so that we do get an invalid token error
384 // this is qute hacky and I hope that there might
385 // be a suggestion from someone on how to
386 // make it a bit more elegant
388 $dummy_mail = new CRM_Mailing_BAO_Mailing();
390 $dummy_mail->$mess = $str;
391 $tokens = $dummy_mail->getTokens();
393 $str = CRM_Utils_Token
::replaceSubscribeInviteTokens($str);
394 $str = CRM_Utils_Token
::replaceDomainTokens($str, $domain, NULL, $tokens['text']);
395 $str = CRM_Utils_Token
::replaceMailingTokens($str, $mailing, NULL, $tokens['text']);
396 $str = CRM_Utils_Token
::replaceOrgTokens($str, $org);
397 $str = CRM_Utils_Token
::replaceActionTokens($str, $verp, $urls, NULL, $tokens['text']);
398 $str = CRM_Utils_Token
::replaceContactTokens($str, $contact, NULL, $tokens['text']);
400 $unmatched = CRM_Utils_Token
::unmatchedTokens($str);
401 $contentCheck = CRM_Utils_String
::htmlToText($str);
403 if (!empty($unmatched) && 0) {
404 foreach ($unmatched as $token) {
405 $dataErrors[] = '<li>' . ts('Invalid token code') . ' {' . $token . '}</li>';
408 if (strlen($contentCheck) > CRM_SMS_Provider
::MAX_SMS_CHAR
) {
409 $dataErrors[] = '<li>' . ts('The body of the SMS cannot exceed %1 characters.', array(1 => CRM_SMS_Provider
::MAX_SMS_CHAR
)) . '</li>';
411 if (!empty($dataErrors)) {
412 $errors['textFile'] = ts('The following errors were detected in %1:', array(
413 1 => $name)) . ' <ul>' . implode('', $dataErrors) . '</ul>';
417 $templateName = CRM_Core_BAO_MessageTemplate
::getMessageTemplates();
418 if (!empty($params['SMSsaveTemplate']) && in_array(CRM_Utils_Array
::value('SMSsaveTemplateName', $params), $templateName)
420 $errors['SMSsaveTemplate'] = ts('Duplicate Template Name.');
422 return empty($errors) ?
TRUE : $errors;
426 * Display Name of the form
431 public function getTitle() {
432 return ts('SMS Content');