Example #1
0
 /**
  * Validation.
  *
  * @param array $params
  *   (ref.) an assoc array of name/value pairs.
  *
  * @param $files
  * @param $self
  *
  * @return bool|array
  *   mixed true or array of errors
  */
 public static function formRule($params, $files, $self)
 {
     if (!empty($_POST['_qf_Import_refresh'])) {
         return TRUE;
     }
     $errors = array();
     $template = CRM_Core_Smarty::singleton();
     if (isset($params['html_message'])) {
         $htmlMessage = str_replace(array("\n", "\r"), ' ', $params['html_message']);
         $htmlMessage = str_replace("'", "\\'", $htmlMessage);
         $template->assign('htmlContent', $htmlMessage);
     }
     $domain = CRM_Core_BAO_Domain::getDomain();
     $mailing = new CRM_Mailing_BAO_Mailing();
     $mailing->id = $self->_mailingID;
     $mailing->find(TRUE);
     $session = CRM_Core_Session::singleton();
     $values = array('contact_id' => $session->get('userID'), 'version' => 3);
     require_once 'api/api.php';
     $contact = civicrm_api('contact', 'get', $values);
     //CRM-4524
     $contact = reset($contact['values']);
     $verp = array_flip(array('optOut', 'reply', 'unsubscribe', 'resubscribe', 'owner'));
     foreach ($verp as $key => $value) {
         $verp[$key]++;
     }
     $urls = array_flip(array('forward', 'optOutUrl', 'unsubscribeUrl', 'resubscribeUrl'));
     foreach ($urls as $key => $value) {
         $urls[$key]++;
     }
     // set $header and $footer
     foreach (array('header', 'footer') as $part) {
         ${$part} = array();
         if ($params["{$part}_id"]) {
             //echo "found<p>";
             $component = new CRM_Mailing_BAO_Component();
             $component->id = $params["{$part}_id"];
             $component->find(TRUE);
             ${$part}['textFile'] = $component->body_text;
             ${$part}['htmlFile'] = $component->body_html;
             $component->free();
         } else {
             ${$part}['htmlFile'] = ${$part}['textFile'] = '';
         }
     }
     $skipTextFile = $self->get('skipTextFile');
     $skipHtmlFile = $self->get('skipHtmlFile');
     if (!$params['upload_type']) {
         if ((!isset($files['textFile']) || !file_exists($files['textFile']['tmp_name'])) && (!isset($files['htmlFile']) || !file_exists($files['htmlFile']['tmp_name']))) {
             if (!($skipTextFile || $skipHtmlFile)) {
                 $errors['textFile'] = ts('Please provide either a Text or HTML formatted message - or both.');
             }
         }
     } else {
         if (empty($params['text_message']) && empty($params['html_message'])) {
             $errors['html_message'] = ts('Please provide either a Text or HTML formatted message - or both.');
         }
         if (!empty($params['saveTemplate']) && empty($params['saveTemplateName'])) {
             $errors['saveTemplateName'] = ts('Please provide a Template Name.');
         }
     }
     foreach (array('text', 'html') as $file) {
         if (!$params['upload_type'] && !file_exists(CRM_Utils_Array::value('tmp_name', $files[$file . 'File']))) {
             continue;
         }
         if ($params['upload_type'] && !$params[$file . '_message']) {
             continue;
         }
         if (!$params['upload_type']) {
             $str = file_get_contents($files[$file . 'File']['tmp_name']);
             $name = $files[$file . 'File']['name'];
         } else {
             $str = $params[$file . '_message'];
             $str = $file == 'html' ? str_replace('%7B', '{', str_replace('%7D', '}', $str)) : $str;
             $name = $file . ' message';
         }
         /* append header/footer */
         $str = $header[$file . 'File'] . $str . $footer[$file . 'File'];
         $dataErrors = array();
         /* First look for missing tokens */
         if (!CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, 'disable_mandatory_tokens_check')) {
             $err = CRM_Utils_Token::requiredTokens($str);
             if ($err !== TRUE) {
                 foreach ($err as $token => $desc) {
                     $dataErrors[] = '<li>' . ts('This message is missing a required token - {%1}: %2', array(1 => $token, 2 => $desc)) . '</li>';
                 }
             }
         }
         /* Do a full token replacement on a dummy verp, the current
          * contact and domain, and the first organization. */
         // here we make a dummy mailing object so that we
         // can retrieve the tokens that we need to replace
         // so that we do get an invalid token error
         // this is qute hacky and I hope that there might
         // be a suggestion from someone on how to
         // make it a bit more elegant
         $dummy_mail = new CRM_Mailing_BAO_Mailing();
         $mess = "body_{$file}";
         $dummy_mail->{$mess} = $str;
         $tokens = $dummy_mail->getTokens();
         $str = CRM_Utils_Token::replaceSubscribeInviteTokens($str);
         $str = CRM_Utils_Token::replaceDomainTokens($str, $domain, NULL, $tokens[$file]);
         $str = CRM_Utils_Token::replaceMailingTokens($str, $mailing, NULL, $tokens[$file]);
         $str = CRM_Utils_Token::replaceOrgTokens($str, $org);
         $str = CRM_Utils_Token::replaceActionTokens($str, $verp, $urls, NULL, $tokens[$file]);
         $str = CRM_Utils_Token::replaceContactTokens($str, $contact, NULL, $tokens[$file]);
         $unmatched = CRM_Utils_Token::unmatchedTokens($str);
         if (!empty($unmatched) && 0) {
             foreach ($unmatched as $token) {
                 $dataErrors[] = '<li>' . ts('Invalid token code') . ' {' . $token . '}</li>';
             }
         }
         if (!empty($dataErrors)) {
             $errors[$file . 'File'] = ts('The following errors were detected in %1:', array(1 => $name)) . ' <ul>' . implode('', $dataErrors) . '</ul><br /><a href="' . CRM_Utils_System::docURL2('Sample CiviMail Messages', TRUE, NULL, NULL, NULL, "wiki") . '" target="_blank">' . ts('More information on required tokens...') . '</a>';
         }
     }
     $templateName = CRM_Core_BAO_MessageTemplate::getMessageTemplates();
     if (!empty($params['saveTemplate']) && in_array(CRM_Utils_Array::value('saveTemplateName', $params), $templateName)) {
         $errors['saveTemplate'] = ts('Duplicate Template Name.');
     }
     return empty($errors) ? TRUE : $errors;
 }
Example #2
0
 /**
  * Function for validation
  *
  * @param array $params (ref.) an assoc array of name/value pairs
  *
  * @param $files
  * @param $self
  *
  * @return mixed true or array of errors
  * @access public
  * @static
  */
 static function formRule($params, $files, $self)
 {
     if (!empty($_POST['_qf_Import_refresh'])) {
         return TRUE;
     }
     $errors = array();
     $template = CRM_Core_Smarty::singleton();
     $domain = CRM_Core_BAO_Domain::getDomain();
     $mailing = new CRM_Mailing_BAO_Mailing();
     $mailing->id = $self->_mailingID;
     $mailing->find(TRUE);
     $session = CRM_Core_Session::singleton();
     $values = array('contact_id' => $session->get('userID'), 'version' => 3);
     require_once 'api/api.php';
     $contact = civicrm_api('contact', 'get', $values);
     //CRM-4524
     $contact = reset($contact['values']);
     $verp = array_flip(array('optOut', 'reply', 'unsubscribe', 'resubscribe', 'owner'));
     foreach ($verp as $key => $value) {
         $verp[$key]++;
     }
     $urls = array_flip(array('forward', 'optOutUrl', 'unsubscribeUrl', 'resubscribeUrl'));
     foreach ($urls as $key => $value) {
         $urls[$key]++;
     }
     $skipTextFile = $self->get('skipTextFile');
     if (!$params['upload_type']) {
         if (!isset($files['textFile']) || !file_exists($files['textFile']['tmp_name'])) {
             if (!$skipTextFile) {
                 $errors['textFile'] = ts('Please provide a Text');
             }
         }
     } else {
         if (empty($params['text_message'])) {
             $errors['text_message'] = ts('Please provide a Text');
         } else {
             if (!empty($params['text_message'])) {
                 $messageCheck = CRM_Utils_Array::value('text_message', $params);
                 if ($messageCheck && strlen($messageCheck) > CRM_SMS_Provider::MAX_SMS_CHAR) {
                     $errors['text_message'] = ts("You can configure the SMS message body up to %1 characters", array(1 => CRM_SMS_Provider::MAX_SMS_CHAR));
                 }
             }
         }
         if (!empty($params['saveTemplate']) && empty($params['saveTemplateName'])) {
             $errors['saveTemplateName'] = ts('Please provide a Template Name.');
         }
     }
     if ($params['upload_type'] || file_exists(CRM_Utils_Array::value('tmp_name', $files['textFile'])) || !$params['upload_type'] && $params['text_message']) {
         if (!$params['upload_type']) {
             $str = file_get_contents($files['textFile']['tmp_name']);
             $name = $files['textFile']['name'];
         } else {
             $str = $params['text_message'];
             $name = 'text message';
         }
         $dataErrors = array();
         /* Do a full token replacement on a dummy verp, the current
          * contact and domain, and the first organization. */
         // here we make a dummy mailing object so that we
         // can retrieve the tokens that we need to replace
         // so that we do get an invalid token error
         // this is qute hacky and I hope that there might
         // be a suggestion from someone on how to
         // make it a bit more elegant
         $dummy_mail = new CRM_Mailing_BAO_Mailing();
         $mess = "body_text";
         $dummy_mail->{$mess} = $str;
         $tokens = $dummy_mail->getTokens();
         $str = CRM_Utils_Token::replaceSubscribeInviteTokens($str);
         $str = CRM_Utils_Token::replaceDomainTokens($str, $domain, NULL, $tokens['text']);
         $str = CRM_Utils_Token::replaceMailingTokens($str, $mailing, NULL, $tokens['text']);
         $str = CRM_Utils_Token::replaceOrgTokens($str, $org);
         $str = CRM_Utils_Token::replaceActionTokens($str, $verp, $urls, NULL, $tokens['text']);
         $str = CRM_Utils_Token::replaceContactTokens($str, $contact, NULL, $tokens['text']);
         $unmatched = CRM_Utils_Token::unmatchedTokens($str);
         $contentCheck = CRM_Utils_String::htmlToText($str);
         if (!empty($unmatched) && 0) {
             foreach ($unmatched as $token) {
                 $dataErrors[] = '<li>' . ts('Invalid token code') . ' {' . $token . '}</li>';
             }
         }
         if (strlen($contentCheck) > CRM_SMS_Provider::MAX_SMS_CHAR) {
             $dataErrors[] = '<li>' . ts('The body of the SMS cannot exceed %1 characters.', array(1 => CRM_SMS_Provider::MAX_SMS_CHAR)) . '</li>';
         }
         if (!empty($dataErrors)) {
             $errors['textFile'] = ts('The following errors were detected in %1:', array(1 => $name)) . ' <ul>' . implode('', $dataErrors) . '</ul>';
         }
     }
     $templateName = CRM_Core_BAO_MessageTemplate::getMessageTemplates();
     if (!empty($params['saveTemplate']) && in_array(CRM_Utils_Array::value('saveTemplateName', $params), $templateName)) {
         $errors['saveTemplate'] = ts('Duplicate Template Name.');
     }
     return empty($errors) ? TRUE : $errors;
 }
 /**
  * Build the  compose mail form.
  *
  * @param CRM_Core_Form $form
  *
  * @return void
  */
 public static function commonCompose(&$form)
 {
     //get the tokens.
     $tokens = array();
     if (method_exists($form, 'listTokens')) {
         $tokens = array_merge($form->listTokens(), $tokens);
     }
     //sorted in ascending order tokens by ignoring word case
     $form->assign('tokens', CRM_Utils_Token::formatTokensForDisplay($tokens));
     $templates = array();
     $textFields = array('text_message' => ts('HTML Format'), 'sms_text_message' => ts('SMS Message'));
     $modePrefixes = array('Mail' => NULL, 'SMS' => 'SMS');
     $className = CRM_Utils_System::getClassName($form);
     if ($className != 'CRM_SMS_Form_Upload' && $className != 'CRM_Contact_Form_Task_SMS' && $className != 'CRM_Contact_Form_Task_SMS') {
         $form->add('wysiwyg', 'html_message', ts('HTML Format'), array('cols' => '80', 'rows' => '8', 'onkeyup' => "return verify(this)"));
         if ($className != 'CRM_Admin_Form_ScheduleReminders') {
             unset($modePrefixes['SMS']);
         }
     } else {
         unset($textFields['text_message']);
         unset($modePrefixes['Mail']);
     }
     //insert message Text by selecting "Select Template option"
     foreach ($textFields as $id => $label) {
         $prefix = NULL;
         if ($id == 'sms_text_message') {
             $prefix = "SMS";
             $form->assign('max_sms_length', CRM_SMS_Provider::MAX_SMS_CHAR);
         }
         $form->add('textarea', $id, $label, array('cols' => '80', 'rows' => '8', 'onkeyup' => "return verify(this, '{$prefix}')"));
     }
     foreach ($modePrefixes as $prefix) {
         if ($prefix == 'SMS') {
             $templates[$prefix] = CRM_Core_BAO_MessageTemplate::getMessageTemplates(FALSE, TRUE);
         } else {
             $templates[$prefix] = CRM_Core_BAO_MessageTemplate::getMessageTemplates(FALSE);
         }
         if (!empty($templates[$prefix])) {
             $form->assign('templates', TRUE);
             $form->add('select', "{$prefix}template", ts('Use Template'), array('' => ts('- select -')) + $templates[$prefix], FALSE, array('onChange' => "selectValue( this.value, '{$prefix}');"));
         }
         $form->add('checkbox', "{$prefix}updateTemplate", ts('Update Template'), NULL);
         $form->add('checkbox', "{$prefix}saveTemplate", ts('Save As New Template'), NULL, FALSE, array('onclick' => "showSaveDetails(this, '{$prefix}');"));
         $form->add('text', "{$prefix}saveTemplateName", ts('Template Title'));
     }
     // I'm not sure this is ever called.
     $action = CRM_Utils_Request::retrieve('action', 'String', $form, FALSE);
     if (CRM_Utils_System::getClassName($form) == 'CRM_Contact_Form_Task_PDF' && $action == CRM_Core_Action::VIEW) {
         $form->freeze('html_message');
     }
 }
 /**
  * Function to build the  compose PDF letter form
  *
  * @param   $form
  *
  * @return None
  * @access public
  */
 public static function commonLetterCompose(&$form)
 {
     //get the tokens.
     $tokens = CRM_Core_SelectValues::contactTokens();
     if (CRM_Utils_System::getClassName($form) == 'CRM_Mailing_Form_Upload') {
         $tokens = array_merge(CRM_Core_SelectValues::mailingTokens(), $tokens);
     }
     //@todo move this fn onto the form
     if (CRM_Utils_System::getClassName($form) == 'CRM_Contribute_Form_Task_PDFLetter') {
         $tokens = array_merge(CRM_Core_SelectValues::contributionTokens(), $tokens);
     }
     if (method_exists($form, 'listTokens')) {
         $tokens = array_merge($form->listTokens(), $tokens);
     }
     //sorted in ascending order tokens by ignoring word case
     natcasesort($tokens);
     $form->assign('tokens', json_encode($tokens));
     $form->add('select', 'token1', ts('Insert Tokens'), $tokens, FALSE, array('size' => "5", 'multiple' => TRUE, 'onchange' => "return tokenReplHtml(this);"));
     $form->_templates = CRM_Core_BAO_MessageTemplate::getMessageTemplates(FALSE);
     if (!empty($form->_templates)) {
         $form->assign('templates', TRUE);
         $form->add('select', 'template', ts('Select Template'), array('' => ts('- select -')) + $form->_templates, FALSE, array('onChange' => "selectValue( this.value );"));
         $form->add('checkbox', 'updateTemplate', ts('Update Template'), NULL);
     }
     $form->add('checkbox', 'saveTemplate', ts('Save As New Template'), NULL, FALSE, array('onclick' => "showSaveDetails(this);"));
     $form->add('text', 'saveTemplateName', ts('Template Title'));
     $form->addWysiwyg('html_message', ts('Your Letter'), array('cols' => '80', 'rows' => '8', 'onkeyup' => "return verify(this)"));
     $action = CRM_Utils_Request::retrieve('action', 'String', $form, FALSE);
     if (CRM_Utils_System::getClassName($form) == 'CRM_Contact_Form_Task_PDF' && $action == CRM_Core_Action::VIEW) {
         $form->freeze('html_message');
     }
 }