Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
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. | | |
13 | | | | |
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. | | |
18 | | | | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This file is used to build the form configuring mass sms details | |
38 | */ | |
39 | class CRM_SMS_Form_Upload extends CRM_Core_Form { | |
40 | public $_mailingID; | |
41 | ||
00be9182 | 42 | public function preProcess() { |
6a488035 TO |
43 | $this->_mailingID = $this->get('mailing_id'); |
44 | if (CRM_Core_Permission::check('administer CiviCRM')) { | |
45 | $this->assign('isAdmin', 1); | |
46 | } | |
47 | } | |
48 | ||
49 | /** | |
c490a46a | 50 | * Set default values for the form. |
6a488035 TO |
51 | * the default values are retrieved from the database |
52 | * | |
6a488035 | 53 | * |
355ba699 | 54 | * @return void |
6a488035 | 55 | */ |
00be9182 | 56 | public function setDefaultValues() { |
6a488035 TO |
57 | $mailingID = CRM_Utils_Request::retrieve('mid', 'Integer', $this, FALSE, NULL); |
58 | ||
59 | //need to differentiate new/reuse mailing, CRM-2873 | |
60 | $reuseMailing = FALSE; | |
61 | if ($mailingID) { | |
62 | $reuseMailing = TRUE; | |
63 | } | |
64 | else { | |
65 | $mailingID = $this->_mailingID; | |
66 | } | |
67 | ||
68 | $count = $this->get('count'); | |
69 | $this->assign('count', $count); | |
70 | ||
71 | $this->set('skipTextFile', FALSE); | |
72 | ||
73 | $defaults = array(); | |
74 | ||
75 | if ($mailingID) { | |
76 | $dao = new CRM_Mailing_DAO_Mailing(); | |
77 | $dao->id = $mailingID; | |
78 | $dao->find(TRUE); | |
79 | $dao->storeValues($dao, $defaults); | |
80 | ||
81 | //we don't want to retrieve template details once it is | |
82 | //set in session | |
83 | $templateId = $this->get('template'); | |
84 | $this->assign('templateSelected', $templateId ? $templateId : 0); | |
85 | if (isset($defaults['msg_template_id']) && !$templateId) { | |
1e035d58 | 86 | $defaults['SMStemplate'] = $defaults['msg_template_id']; |
c6327d7d | 87 | $messageTemplate = new CRM_Core_DAO_MessageTemplate(); |
6a488035 TO |
88 | $messageTemplate->id = $defaults['msg_template_id']; |
89 | $messageTemplate->selectAdd(); | |
90 | $messageTemplate->selectAdd('msg_text'); | |
91 | $messageTemplate->find(TRUE); | |
92 | ||
1e035d58 | 93 | $defaults['sms_text_message'] = $messageTemplate->msg_text; |
6a488035 TO |
94 | } |
95 | ||
96 | if (isset($defaults['body_text'])) { | |
1e035d58 | 97 | $defaults['sms_text_message'] = $defaults['body_text']; |
6a488035 TO |
98 | $this->set('textFile', $defaults['body_text']); |
99 | $this->set('skipTextFile', TRUE); | |
100 | } | |
101 | } | |
102 | ||
103 | //fix for CRM-2873 | |
104 | if (!$reuseMailing) { | |
105 | $textFilePath = $this->get('textFilePath'); | |
106 | if ($textFilePath && | |
107 | file_exists($textFilePath) | |
108 | ) { | |
1e035d58 | 109 | $defaults['sms_text_message'] = file_get_contents($textFilePath); |
110 | if (strlen($defaults['sms_text_message']) > 0) { | |
6a488035 TO |
111 | $this->set('skipTextFile', TRUE); |
112 | } | |
113 | } | |
114 | } | |
115 | ||
116 | $defaults['upload_type'] = 1; | |
117 | ||
118 | return $defaults; | |
119 | } | |
120 | ||
121 | /** | |
fe482240 | 122 | * Build the form object. |
6a488035 | 123 | * |
355ba699 | 124 | * @return void |
6a488035 TO |
125 | */ |
126 | public function buildQuickForm() { | |
127 | $session = CRM_Core_Session::singleton(); | |
353ffa53 | 128 | $config = CRM_Core_Config::singleton(); |
6a488035 TO |
129 | $options = array(); |
130 | $tempVar = FALSE; | |
131 | ||
132 | $this->assign('max_sms_length', CRM_SMS_Provider::MAX_SMS_CHAR); | |
133 | ||
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}" | |
137 | ); | |
138 | ||
139 | $providers = CRM_SMS_BAO_Provider::getProviders(array('id', 'title')); | |
140 | ||
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); | |
146 | } | |
147 | else { | |
148 | $providerSelect[''] = '- select -'; | |
149 | foreach ($providers as $provider) { | |
150 | $providerSelect[$provider['id']] = $provider['title']; | |
151 | } | |
152 | } | |
153 | ||
154 | $this->add('select', 'sms_provider_id', | |
155 | ts('SMS Provider'), $providerSelect, TRUE | |
156 | ); | |
157 | ||
158 | $attributes = array('onclick' => "showHideUpload();"); | |
159 | $options = array(ts('Upload Content'), ts('Compose On-screen')); | |
160 | ||
161 | $this->addRadio('upload_type', ts('I want to'), $options, $attributes, " "); | |
162 | ||
163 | CRM_Mailing_BAO_Mailing::commonCompose($this); | |
164 | ||
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'); | |
169 | ||
170 | $this->addFormRule(array('CRM_SMS_Form_Upload', 'formRule'), $this); | |
171 | ||
172 | $buttons = array( | |
6ea503d4 TO |
173 | array( |
174 | 'type' => 'back', | |
f212d37d | 175 | 'name' => ts('Previous'), |
6a488035 TO |
176 | ), |
177 | array( | |
178 | 'type' => 'upload', | |
f212d37d | 179 | 'name' => ts('Next'), |
6a488035 TO |
180 | 'spacing' => ' ', |
181 | 'isDefault' => TRUE, | |
182 | ), | |
183 | array( | |
184 | 'type' => 'cancel', | |
185 | 'name' => ts('Cancel'), | |
186 | ), | |
187 | ); | |
188 | ||
189 | $this->addButtons($buttons); | |
190 | } | |
191 | ||
192 | public function postProcess() { | |
193 | $params = $ids = array(); | |
194 | $uploadParams = array('from_name'); | |
195 | ||
196 | $formValues = $this->controller->exportValues($this->_name); | |
197 | ||
198 | foreach ($uploadParams as $key) { | |
a7488080 | 199 | if (!empty($formValues[$key])) { |
6a488035 TO |
200 | $params[$key] = $formValues[$key]; |
201 | $this->set($key, $formValues[$key]); | |
202 | } | |
203 | } | |
204 | ||
205 | if (!$formValues['upload_type']) { | |
206 | $contents = NULL; | |
207 | if (isset($formValues['textFile']) && | |
208 | !empty($formValues['textFile']) | |
209 | ) { | |
210 | $contents = file_get_contents($formValues['textFile']['name']); | |
211 | $this->set($key, $formValues['textFile']['name']); | |
212 | } | |
213 | if ($contents) { | |
214 | $params['body_text'] = $contents; | |
215 | } | |
216 | else { | |
217 | $params['body_text'] = 'NULL'; | |
218 | } | |
219 | } | |
220 | else { | |
1e035d58 | 221 | $text_message = $formValues['sms_text_message']; |
6a488035 TO |
222 | $params['body_text'] = $text_message; |
223 | $this->set('textFile', $params['body_text']); | |
224 | $this->set('text_message', $params['body_text']); | |
225 | } | |
226 | ||
227 | $params['name'] = $this->get('name'); | |
228 | ||
229 | $session = CRM_Core_Session::singleton(); | |
230 | $params['contact_id'] = $session->get('userID'); | |
231 | $composeFields = array( | |
353ffa53 TO |
232 | 'SMStemplate', |
233 | 'SMSsaveTemplate', | |
234 | 'SMSupdateTemplate', | |
235 | 'SMSsaveTemplateName', | |
6a488035 TO |
236 | ); |
237 | $msgTemplate = NULL; | |
238 | //mail template is composed | |
239 | if ($formValues['upload_type']) { | |
240 | $composeParams = array(); | |
241 | foreach ($composeFields as $key) { | |
a7488080 | 242 | if (!empty($formValues[$key])) { |
6a488035 TO |
243 | $composeParams[$key] = $formValues[$key]; |
244 | $this->set($key, $formValues[$key]); | |
245 | } | |
246 | } | |
247 | ||
1e035d58 | 248 | if (!empty($composeParams['SMSupdateTemplate'])) { |
6a488035 TO |
249 | $templateParams = array( |
250 | 'msg_text' => $text_message, | |
251 | 'is_active' => TRUE, | |
1e035d58 | 252 | 'is_sms' => TRUE, |
6a488035 TO |
253 | ); |
254 | ||
1e035d58 | 255 | $templateParams['id'] = $formValues['SMStemplate']; |
6a488035 | 256 | |
c6327d7d | 257 | $msgTemplate = CRM_Core_BAO_MessageTemplate::add($templateParams); |
6a488035 TO |
258 | } |
259 | ||
1e035d58 | 260 | if (!empty($composeParams['SMSsaveTemplate'])) { |
6a488035 TO |
261 | $templateParams = array( |
262 | 'msg_text' => $text_message, | |
263 | 'is_active' => TRUE, | |
1e035d58 | 264 | 'is_sms' => TRUE, |
6a488035 TO |
265 | ); |
266 | ||
1e035d58 | 267 | $templateParams['msg_title'] = $composeParams['SMSsaveTemplateName']; |
6a488035 | 268 | |
c6327d7d | 269 | $msgTemplate = CRM_Core_BAO_MessageTemplate::add($templateParams); |
6a488035 TO |
270 | } |
271 | ||
272 | if (isset($msgTemplate->id)) { | |
273 | $params['msg_template_id'] = $msgTemplate->id; | |
274 | } | |
275 | else { | |
1e035d58 | 276 | $params['msg_template_id'] = CRM_Utils_Array::value('SMStemplate', $formValues); |
6a488035 TO |
277 | } |
278 | $this->set('template', $params['msg_template_id']); | |
279 | } | |
280 | ||
281 | $ids['mailing_id'] = $this->_mailingID; | |
282 | ||
283 | //get the from email address | |
284 | $params['sms_provider_id'] = $formValues['sms_provider_id']; | |
285 | ||
286 | //get the from Name | |
287 | $params['from_name'] = CRM_Core_DAO::getFieldValue('CRM_SMS_DAO_Provider', $params['sms_provider_id'], 'username'); | |
288 | ||
289 | //Build SMS in mailing table | |
290 | CRM_Mailing_BAO_Mailing::create($params, $ids); | |
291 | } | |
292 | ||
293 | /** | |
fe482240 | 294 | * Validation. |
6a488035 | 295 | * |
901b501a TO |
296 | * @param array $params |
297 | * (ref.) an assoc array of name/value pairs. | |
6a488035 | 298 | * |
77b97be7 EM |
299 | * @param $files |
300 | * @param $self | |
301 | * | |
72b3a70c CW |
302 | * @return bool|array |
303 | * mixed true or array of errors | |
6a488035 | 304 | */ |
00be9182 | 305 | public static function formRule($params, $files, $self) { |
a7488080 | 306 | if (!empty($_POST['_qf_Import_refresh'])) { |
6a488035 TO |
307 | return TRUE; |
308 | } | |
309 | $errors = array(); | |
310 | $template = CRM_Core_Smarty::singleton(); | |
311 | ||
6a488035 TO |
312 | $domain = CRM_Core_BAO_Domain::getDomain(); |
313 | ||
314 | $mailing = new CRM_Mailing_BAO_Mailing(); | |
315 | $mailing->id = $self->_mailingID; | |
316 | $mailing->find(TRUE); | |
317 | ||
318 | $session = CRM_Core_Session::singleton(); | |
6ea503d4 TO |
319 | $values = array( |
320 | 'contact_id' => $session->get('userID'), | |
6a488035 TO |
321 | 'version' => 3, |
322 | ); | |
323 | require_once 'api/api.php'; | |
324 | $contact = civicrm_api('contact', 'get', $values); | |
325 | ||
326 | //CRM-4524 | |
327 | $contact = reset($contact['values']); | |
328 | ||
329 | $verp = array_flip(array('optOut', 'reply', 'unsubscribe', 'resubscribe', 'owner')); | |
330 | foreach ($verp as $key => $value) { | |
331 | $verp[$key]++; | |
332 | } | |
333 | ||
334 | $urls = array_flip(array('forward', 'optOutUrl', 'unsubscribeUrl', 'resubscribeUrl')); | |
335 | foreach ($urls as $key => $value) { | |
336 | $urls[$key]++; | |
337 | } | |
338 | ||
6a488035 TO |
339 | $skipTextFile = $self->get('skipTextFile'); |
340 | ||
341 | if (!$params['upload_type']) { | |
342 | if ((!isset($files['textFile']) || !file_exists($files['textFile']['tmp_name']))) { | |
343 | if (!($skipTextFile)) { | |
344 | $errors['textFile'] = ts('Please provide a Text'); | |
345 | } | |
346 | } | |
347 | } | |
348 | else { | |
1e035d58 | 349 | if (empty($params['sms_text_message'])) { |
350 | $errors['sms_text_message'] = ts('Please provide a Text'); | |
6a488035 TO |
351 | } |
352 | else { | |
a7488080 | 353 | if (!empty($params['text_message'])) { |
6a488035 TO |
354 | $messageCheck = CRM_Utils_Array::value('text_message', $params); |
355 | if ($messageCheck && (strlen($messageCheck) > CRM_SMS_Provider::MAX_SMS_CHAR)) { | |
356 | $errors['text_message'] = ts("You can configure the SMS message body up to %1 characters", array(1 => CRM_SMS_Provider::MAX_SMS_CHAR)); | |
357 | } | |
358 | } | |
359 | } | |
1e035d58 | 360 | if (!empty($params['SMSsaveTemplate']) && empty($params['SMSsaveTemplateName'])) { |
361 | $errors['SMSsaveTemplateName'] = ts('Please provide a Template Name.'); | |
6a488035 TO |
362 | } |
363 | } | |
364 | ||
365 | if (($params['upload_type'] || file_exists(CRM_Utils_Array::value('tmp_name', $files['textFile']))) || | |
366 | (!$params['upload_type'] && $params['text_message']) | |
367 | ) { | |
368 | ||
369 | if (!$params['upload_type']) { | |
370 | $str = file_get_contents($files['textFile']['tmp_name']); | |
371 | $name = $files['textFile']['name']; | |
372 | } | |
373 | else { | |
1e035d58 | 374 | $str = $params['sms_text_message']; |
6a488035 TO |
375 | $name = 'text message'; |
376 | } | |
377 | ||
378 | $dataErrors = array(); | |
379 | ||
380 | /* Do a full token replacement on a dummy verp, the current | |
e70a7fc0 | 381 | * contact and domain, and the first organization. */ |
6a488035 | 382 | |
6a488035 TO |
383 | // here we make a dummy mailing object so that we |
384 | // can retrieve the tokens that we need to replace | |
385 | // so that we do get an invalid token error | |
386 | // this is qute hacky and I hope that there might | |
387 | // be a suggestion from someone on how to | |
388 | // make it a bit more elegant | |
389 | ||
353ffa53 TO |
390 | $dummy_mail = new CRM_Mailing_BAO_Mailing(); |
391 | $mess = "body_text"; | |
6a488035 | 392 | $dummy_mail->$mess = $str; |
353ffa53 | 393 | $tokens = $dummy_mail->getTokens(); |
6a488035 TO |
394 | |
395 | $str = CRM_Utils_Token::replaceSubscribeInviteTokens($str); | |
396 | $str = CRM_Utils_Token::replaceDomainTokens($str, $domain, NULL, $tokens['text']); | |
397 | $str = CRM_Utils_Token::replaceMailingTokens($str, $mailing, NULL, $tokens['text']); | |
398 | $str = CRM_Utils_Token::replaceOrgTokens($str, $org); | |
399 | $str = CRM_Utils_Token::replaceActionTokens($str, $verp, $urls, NULL, $tokens['text']); | |
400 | $str = CRM_Utils_Token::replaceContactTokens($str, $contact, NULL, $tokens['text']); | |
401 | ||
402 | $unmatched = CRM_Utils_Token::unmatchedTokens($str); | |
403 | $contentCheck = CRM_Utils_String::htmlToText($str); | |
404 | ||
405 | if (!empty($unmatched) && 0) { | |
406 | foreach ($unmatched as $token) { | |
407 | $dataErrors[] = '<li>' . ts('Invalid token code') . ' {' . $token . '}</li>'; | |
408 | } | |
409 | } | |
410 | if (strlen($contentCheck) > CRM_SMS_Provider::MAX_SMS_CHAR) { | |
411 | $dataErrors[] = '<li>' . ts('The body of the SMS cannot exceed %1 characters.', array(1 => CRM_SMS_Provider::MAX_SMS_CHAR)) . '</li>'; | |
412 | } | |
413 | if (!empty($dataErrors)) { | |
414 | $errors['textFile'] = ts('The following errors were detected in %1:', array( | |
408b79bf | 415 | 1 => $name, |
353ffa53 | 416 | )) . ' <ul>' . implode('', $dataErrors) . '</ul>'; |
6a488035 TO |
417 | } |
418 | } | |
419 | ||
c6327d7d | 420 | $templateName = CRM_Core_BAO_MessageTemplate::getMessageTemplates(); |
1e035d58 | 421 | if (!empty($params['SMSsaveTemplate']) && in_array(CRM_Utils_Array::value('SMSsaveTemplateName', $params), $templateName) |
6a488035 | 422 | ) { |
1e035d58 | 423 | $errors['SMSsaveTemplate'] = ts('Duplicate Template Name.'); |
6a488035 TO |
424 | } |
425 | return empty($errors) ? TRUE : $errors; | |
426 | } | |
427 | ||
428 | /** | |
fe482240 | 429 | * Display Name of the form. |
6a488035 | 430 | * |
6a488035 TO |
431 | * |
432 | * @return string | |
433 | */ | |
434 | public function getTitle() { | |
435 | return ts('SMS Content'); | |
436 | } | |
96025800 | 437 | |
6a488035 | 438 | } |