| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.4 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2013 | |
| 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 |
| 31 | * @copyright CiviCRM LLC (c) 2004-2013 |
| 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 | * |
| 55 | * @return void |
| 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']; |
| 88 | $messageTemplate = new CRM_Core_DAO_MessageTemplate(); |
| 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 | * |
| 125 | * @return void |
| 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) { |
| 200 | if (!empty($formValues[$key])) { |
| 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) { |
| 241 | if (!empty($formValues[$key])) { |
| 242 | $composeParams[$key] = $formValues[$key]; |
| 243 | $this->set($key, $formValues[$key]); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | if (!empty($composeParams['updateTemplate'])) { |
| 248 | $templateParams = array( |
| 249 | 'msg_text' => $text_message, |
| 250 | 'is_active' => TRUE, |
| 251 | ); |
| 252 | |
| 253 | $templateParams['id'] = $formValues['template']; |
| 254 | |
| 255 | $msgTemplate = CRM_Core_BAO_MessageTemplate::add($templateParams); |
| 256 | } |
| 257 | |
| 258 | if (!empty($composeParams['saveTemplate'])) { |
| 259 | $templateParams = array( |
| 260 | 'msg_text' => $text_message, |
| 261 | 'is_active' => TRUE, |
| 262 | ); |
| 263 | |
| 264 | $templateParams['msg_title'] = $composeParams['saveTemplateName']; |
| 265 | |
| 266 | $msgTemplate = CRM_Core_BAO_MessageTemplate::add($templateParams); |
| 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 | * |
| 295 | * @return mixed true or array of errors |
| 296 | * @access public |
| 297 | * @static |
| 298 | */ |
| 299 | static function formRule($params, $files, $self) { |
| 300 | if (!empty($_POST['_qf_Import_refresh'])) { |
| 301 | return TRUE; |
| 302 | } |
| 303 | $errors = array(); |
| 304 | $template = CRM_Core_Smarty::singleton(); |
| 305 | |
| 306 | |
| 307 | $domain = CRM_Core_BAO_Domain::getDomain(); |
| 308 | |
| 309 | $mailing = new CRM_Mailing_BAO_Mailing(); |
| 310 | $mailing->id = $self->_mailingID; |
| 311 | $mailing->find(TRUE); |
| 312 | |
| 313 | $session = CRM_Core_Session::singleton(); |
| 314 | $values = array('contact_id' => $session->get('userID'), |
| 315 | 'version' => 3, |
| 316 | ); |
| 317 | require_once 'api/api.php'; |
| 318 | $contact = civicrm_api('contact', 'get', $values); |
| 319 | |
| 320 | //CRM-4524 |
| 321 | $contact = reset($contact['values']); |
| 322 | |
| 323 | $verp = array_flip(array('optOut', 'reply', 'unsubscribe', 'resubscribe', 'owner')); |
| 324 | foreach ($verp as $key => $value) { |
| 325 | $verp[$key]++; |
| 326 | } |
| 327 | |
| 328 | $urls = array_flip(array('forward', 'optOutUrl', 'unsubscribeUrl', 'resubscribeUrl')); |
| 329 | foreach ($urls as $key => $value) { |
| 330 | $urls[$key]++; |
| 331 | } |
| 332 | |
| 333 | |
| 334 | $skipTextFile = $self->get('skipTextFile'); |
| 335 | |
| 336 | if (!$params['upload_type']) { |
| 337 | if ((!isset($files['textFile']) || !file_exists($files['textFile']['tmp_name']))) { |
| 338 | if (!($skipTextFile)) { |
| 339 | $errors['textFile'] = ts('Please provide a Text'); |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | else { |
| 344 | if (empty($params['text_message'])) { |
| 345 | $errors['text_message'] = ts('Please provide a Text'); |
| 346 | } |
| 347 | else { |
| 348 | if (!empty($params['text_message'])) { |
| 349 | $messageCheck = CRM_Utils_Array::value('text_message', $params); |
| 350 | if ($messageCheck && (strlen($messageCheck) > CRM_SMS_Provider::MAX_SMS_CHAR)) { |
| 351 | $errors['text_message'] = ts("You can configure the SMS message body up to %1 characters", array(1 => CRM_SMS_Provider::MAX_SMS_CHAR)); |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | if (!empty($params['saveTemplate']) && empty($params['saveTemplateName'])) { |
| 356 | $errors['saveTemplateName'] = ts('Please provide a Template Name.'); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | if (($params['upload_type'] || file_exists(CRM_Utils_Array::value('tmp_name', $files['textFile']))) || |
| 361 | (!$params['upload_type'] && $params['text_message']) |
| 362 | ) { |
| 363 | |
| 364 | if (!$params['upload_type']) { |
| 365 | $str = file_get_contents($files['textFile']['tmp_name']); |
| 366 | $name = $files['textFile']['name']; |
| 367 | } |
| 368 | else { |
| 369 | $str = $params['text_message']; |
| 370 | $name = 'text message'; |
| 371 | } |
| 372 | |
| 373 | $dataErrors = array(); |
| 374 | |
| 375 | /* Do a full token replacement on a dummy verp, the current |
| 376 | * contact and domain, and the first organization. */ |
| 377 | |
| 378 | |
| 379 | // here we make a dummy mailing object so that we |
| 380 | // can retrieve the tokens that we need to replace |
| 381 | // so that we do get an invalid token error |
| 382 | // this is qute hacky and I hope that there might |
| 383 | // be a suggestion from someone on how to |
| 384 | // make it a bit more elegant |
| 385 | |
| 386 | $dummy_mail = new CRM_Mailing_BAO_Mailing(); |
| 387 | $mess = "body_text"; |
| 388 | $dummy_mail->$mess = $str; |
| 389 | $tokens = $dummy_mail->getTokens(); |
| 390 | |
| 391 | $str = CRM_Utils_Token::replaceSubscribeInviteTokens($str); |
| 392 | $str = CRM_Utils_Token::replaceDomainTokens($str, $domain, NULL, $tokens['text']); |
| 393 | $str = CRM_Utils_Token::replaceMailingTokens($str, $mailing, NULL, $tokens['text']); |
| 394 | $str = CRM_Utils_Token::replaceOrgTokens($str, $org); |
| 395 | $str = CRM_Utils_Token::replaceActionTokens($str, $verp, $urls, NULL, $tokens['text']); |
| 396 | $str = CRM_Utils_Token::replaceContactTokens($str, $contact, NULL, $tokens['text']); |
| 397 | |
| 398 | $unmatched = CRM_Utils_Token::unmatchedTokens($str); |
| 399 | $contentCheck = CRM_Utils_String::htmlToText($str); |
| 400 | |
| 401 | if (!empty($unmatched) && 0) { |
| 402 | foreach ($unmatched as $token) { |
| 403 | $dataErrors[] = '<li>' . ts('Invalid token code') . ' {' . $token . '}</li>'; |
| 404 | } |
| 405 | } |
| 406 | if (strlen($contentCheck) > CRM_SMS_Provider::MAX_SMS_CHAR) { |
| 407 | $dataErrors[] = '<li>' . ts('The body of the SMS cannot exceed %1 characters.', array(1 => CRM_SMS_Provider::MAX_SMS_CHAR)) . '</li>'; |
| 408 | } |
| 409 | if (!empty($dataErrors)) { |
| 410 | $errors['textFile'] = ts('The following errors were detected in %1:', array( |
| 411 | 1 => $name)) . ' <ul>' . implode('', $dataErrors) . '</ul>'; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | $templateName = CRM_Core_BAO_MessageTemplate::getMessageTemplates(); |
| 416 | if (!empty($params['saveTemplate']) && in_array(CRM_Utils_Array::value('saveTemplateName', $params), $templateName) |
| 417 | ) { |
| 418 | $errors['saveTemplate'] = ts('Duplicate Template Name.'); |
| 419 | } |
| 420 | return empty($errors) ? TRUE : $errors; |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * Display Name of the form |
| 425 | * |
| 426 | * @access public |
| 427 | * |
| 428 | * @return string |
| 429 | */ |
| 430 | public function getTitle() { |
| 431 | return ts('SMS Content'); |
| 432 | } |
| 433 | } |
| 434 | |