Ejemplo n.º 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;
 }
 /**
  * global validation rules for the form
  *
  * @param array $fields posted values of the form
  *
  * @return array list of errors to be posted back to the form
  * @static
  * @access public
  */
 static function formRule($fields)
 {
     $errors = array();
     // Validate include/exclude groups, mailings
     // Start
     if (isset($fields['includeGroups']) && isset($fields['excludeGroups']) && is_array($fields['excludeGroups'])) {
         $checkGroups = array();
         $checkGroups = in_array($fields['includeGroups'], $fields['excludeGroups']);
         if (!empty($checkGroups)) {
             $errors['excludeGroups'] = ts('Cannot have same groups in Include Group(s) and Exclude Group(s).');
         }
     }
     if (isset($fields['includeMailings']) && is_array($fields['includeMailings']) && isset($fields['excludeMailings']) && is_array($fields['excludeMailings'])) {
         $checkMailings = array();
         $checkMailings = array_intersect($fields['includeMailings'], $fields['excludeMailings']);
         if (!empty($checkMailings)) {
             $errors['excludeMailings'] = ts('Cannot have same mail in Include mailing(s) and Exclude mailing(s).');
         }
     }
     if (!empty($fields['search_id']) && empty($fields['group_id'])) {
         $errors['group_id'] = ts('You must select a group to filter on');
     }
     if (empty($fields['search_id']) && !empty($fields['group_id'])) {
         $errors['search_id'] = ts('You must select a search to filter');
     }
     // End
     // Validate message template html/text
     // Start
     $errors = array();
     $template = CRM_Core_Smarty::singleton();
     if (isset($fields['html_message'])) {
         $htmlMessage = str_replace(array("\n", "\r"), ' ', $fields['html_message']);
         $htmlMessage = str_replace("'", "\\'", $htmlMessage);
         $template->assign('htmlContent', $htmlMessage);
     }
     $domain = CRM_Core_BAO_Domain::getDomain();
     $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 ($fields["{$part}_id"]) {
             //echo "found<p>";
             $component = new CRM_Mailing_BAO_Component();
             $component->id = $fields["{$part}_id"];
             $component->find(TRUE);
             ${$part}['textFile'] = $component->body_text;
             ${$part}['htmlFile'] = $component->body_html;
             $component->free();
         } else {
             ${$part}['htmlFile'] = ${$part}['textFile'] = '';
         }
     }
     if (!CRM_Utils_Array::value('text_message', $fields) && !CRM_Utils_Array::value('html_message', $fields)) {
         $errors['html_message'] = ts('Please provide either a Text or HTML formatted message - or both.');
     }
     foreach (array('text', 'html') as $file) {
         $str = $fields[$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 */
         $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 . '_message'] = 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>';
         }
     }
     // End
     return empty($errors) ? TRUE : $errors;
 }
Ejemplo n.º 3
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;
 }
Ejemplo n.º 4
0
 /**
  * Function for validation
  *
  * @param array $params (ref.) an assoc array of name/value pairs
  *
  * @return mixed true or array of errors
  * @access public
  * @static
  */
 function dataRule(&$params, &$files, &$options)
 {
     if (CRM_Utils_Array::value('_qf_Import_refresh', $_POST)) {
         return true;
     }
     $errors = array();
     require_once 'CRM/Core/BAO/Domain.php';
     $domain =& CRM_Core_BAO_Domain::getCurrentDomain();
     $mailing = null;
     $session =& CRM_Core_Session::singleton();
     $values = array('contact_id' => $session->get('userID'));
     $contact = array();
     $ids = array();
     CRM_Contact_BAO_Contact::retrieve($values, $contact, $id);
     $verp = array_flip(array('optOut', 'reply', 'unsubscribe', 'owner'));
     foreach ($verp as $key => $value) {
         $verp[$key]++;
     }
     $urls = array_flip(array('forward'));
     foreach ($urls as $key => $value) {
         $urls[$key]++;
     }
     require_once 'CRM/Mailing/BAO/Component.php';
     $header =& new CRM_Mailing_BAO_Component();
     $header->id = $params['header_id'];
     $header->find(true);
     $footer =& new CRM_Mailing_BAO_Component();
     $footer->id = $params['footer_id'];
     $footer->find(true);
     list($headerBody['htmlFile'], $headerBody['textFile']) = array($header->body_html, $header->body_text);
     list($footerBody['htmlFile'], $footerBody['textFile']) = array($footer->body_html, $footer->body_text);
     require_once 'CRM/Utils/Token.php';
     if (!file_exists($files['textFile']['tmp_name'])) {
         $errors['textFile'] = ts('Please provide at least the text message.');
     }
     foreach (array('textFile', 'htmlFile') as $file) {
         if (!file_exists($files[$file]['tmp_name'])) {
             continue;
         }
         $str = file_get_contents($files[$file]['tmp_name']);
         $name = $files[$file]['name'];
         /* append header/footer */
         $str = $headerBody[$file] . $str . $footerBody[$file];
         $dataErrors = array();
         /* First look for missing tokens */
         $err = CRM_Utils_Token::requiredTokens($str);
         if ($err !== true) {
             foreach ($err as $token => $desc) {
                 $dataErrors[] = '<li>' . ts('Missing required token') . ' {' . $token . "}: {$desc}</li>";
             }
         }
         /* Do a full token replacement on a dummy verp, the current contact
          * and domain. */
         $str = CRM_Utils_Token::replaceDomainTokens($str, $domain);
         $str = CRM_Utils_Token::replaceMailingTokens($str, $mailing);
         $str = CRM_Utils_Token::replaceActionTokens($str, $verp, $urls);
         $str = CRM_Utils_Token::replaceContactTokens($str, $contact);
         $unmatched = CRM_Utils_Token::unmatchedTokens($str);
         if (!empty($unmatched)) {
             foreach ($unmatched as $token) {
                 $dataErrors[] = '<li>' . ts('Invalid token code') . ' {' . $token . '}</li>';
             }
         }
         if (!empty($dataErrors)) {
             $errors[$file] = ts('The following errors were detected in %1:', array(1 => $name)) . ': <ul>' . implode('', $dataErrors) . '</ul>';
         }
     }
     return empty($errors) ? true : $errors;
 }