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