/**
  * Function to fetch the template text/html messages
  */
 function template()
 {
     $templateId = CRM_Utils_Type::escape($_POST['tid'], 'Integer');
     $messageTemplate = new CRM_Core_DAO_MessageTemplates();
     $messageTemplate->id = $templateId;
     $messageTemplate->selectAdd();
     $messageTemplate->selectAdd('msg_text, msg_html, msg_subject, pdf_format_id');
     $messageTemplate->find(TRUE);
     $messages = array('subject' => $messageTemplate->msg_subject, 'msg_text' => $messageTemplate->msg_text, 'msg_html' => $messageTemplate->msg_html, 'pdf_format_id' => $messageTemplate->pdf_format_id);
     echo json_encode($messages);
     CRM_Utils_System::civiExit();
 }
 /**
  * This function sets the default values for the form.
  * the default values are retrieved from the database
  *
  * @access public
  *
  * @return None
  */
 function setDefaultValues()
 {
     $mailingID = CRM_Utils_Request::retrieve('mid', 'Integer', $this, FALSE, NULL);
     //need to differentiate new/reuse mailing, CRM-2873
     $reuseMailing = FALSE;
     if ($mailingID) {
         $reuseMailing = TRUE;
     } else {
         $mailingID = $this->_mailingID;
     }
     $count = $this->get('count');
     $this->assign('count', $count);
     $this->set('skipTextFile', FALSE);
     $defaults = array();
     if ($mailingID) {
         $dao = new CRM_Mailing_DAO_Mailing();
         $dao->id = $mailingID;
         $dao->find(TRUE);
         $dao->storeValues($dao, $defaults);
         //we don't want to retrieve template details once it is
         //set in session
         $templateId = $this->get('template');
         $this->assign('templateSelected', $templateId ? $templateId : 0);
         if (isset($defaults['msg_template_id']) && !$templateId) {
             $defaults['template'] = $defaults['msg_template_id'];
             $messageTemplate = new CRM_Core_DAO_MessageTemplates();
             $messageTemplate->id = $defaults['msg_template_id'];
             $messageTemplate->selectAdd();
             $messageTemplate->selectAdd('msg_text');
             $messageTemplate->find(TRUE);
             $defaults['text_message'] = $messageTemplate->msg_text;
         }
         if (isset($defaults['body_text'])) {
             $defaults['text_message'] = $defaults['body_text'];
             $this->set('textFile', $defaults['body_text']);
             $this->set('skipTextFile', TRUE);
         }
     }
     //fix for CRM-2873
     if (!$reuseMailing) {
         $textFilePath = $this->get('textFilePath');
         if ($textFilePath && file_exists($textFilePath)) {
             $defaults['text_message'] = file_get_contents($textFilePath);
             if (strlen($defaults['text_message']) > 0) {
                 $this->set('skipTextFile', TRUE);
             }
         }
     }
     $defaults['upload_type'] = 1;
     return $defaults;
 }
Exemple #3
0
 /**
  * Function to fetch the template text/html messages
  */
 function template()
 {
     require_once 'CRM/Utils/Type.php';
     $templateId = CRM_Utils_Type::escape($_POST['tid'], 'Integer');
     require_once "CRM/Core/DAO/MessageTemplates.php";
     $messageTemplate = new CRM_Core_DAO_MessageTemplates();
     $messageTemplate->id = $templateId;
     $messageTemplate->selectAdd();
     $messageTemplate->selectAdd('msg_text, msg_html, msg_subject');
     $messageTemplate->find(true);
     $messages = array('subject' => $messageTemplate->msg_subject, 'msg_text' => $messageTemplate->msg_text, 'msg_html' => $messageTemplate->msg_html);
     echo json_encode($messages);
     CRM_Utils_System::civiExit();
 }
Exemple #4
0
 /**
  * This function sets the default values for the form.
  * the default values are retrieved from the database
  * 
  * @access public
  * @return None
  */
 function setDefaultValues()
 {
     $mailingID = CRM_Utils_Request::retrieve('mid', 'Integer', $this, false, null);
     //need to differentiate new/reuse mailing, CRM-2873
     $reuseMailing = false;
     if ($mailingID) {
         $reuseMailing = true;
     } else {
         $mailingID = $this->_mailingID;
     }
     $count = $this->get('count');
     $this->assign('count', $count);
     $this->set('skipTextFile', false);
     $this->set('skipHtmlFile', false);
     $defaults = array();
     $htmlMessage = null;
     if ($mailingID) {
         require_once 'CRM/Mailing/DAO/Mailing.php';
         $dao = new CRM_Mailing_DAO_Mailing();
         $dao->id = $mailingID;
         $dao->find(true);
         $dao->storeValues($dao, $defaults);
         //we don't want to retrieve template details once it is
         //set in session
         $templateId = $this->get('template');
         $this->assign('templateSelected', $templateId ? $templateId : 0);
         if (isset($defaults['msg_template_id']) && !$templateId) {
             $defaults['template'] = $defaults['msg_template_id'];
             $messageTemplate = new CRM_Core_DAO_MessageTemplates();
             $messageTemplate->id = $defaults['msg_template_id'];
             $messageTemplate->selectAdd();
             $messageTemplate->selectAdd('msg_text, msg_html');
             $messageTemplate->find(true);
             $defaults['text_message'] = $messageTemplate->msg_text;
             $htmlMessage = $messageTemplate->msg_html;
         }
         if (isset($defaults['body_text'])) {
             $defaults['text_message'] = $defaults['body_text'];
             $this->set('textFile', $defaults['body_text']);
             $this->set('skipTextFile', true);
         }
         if (isset($defaults['body_html'])) {
             $htmlMessage = $defaults['body_html'];
             $this->set('htmlFile', $defaults['body_html']);
             $this->set('skipHtmlFile', true);
         }
         //set default from email address.
         require_once 'CRM/Core/OptionGroup.php';
         if (CRM_Utils_Array::value('from_name', $defaults) && CRM_Utils_Array::value('from_email', $defaults)) {
             $defaults['from_email_address'] = array_search('"' . $defaults['from_name'] . '" <' . $defaults['from_email'] . '>', CRM_Core_OptionGroup::values('from_email_address'));
         } else {
             //get the default from email address.
             $defaultAddress = CRM_Core_OptionGroup::values('from_email_address', null, null, null, ' AND is_default = 1');
             foreach ($defaultAddress as $id => $value) {
                 $defaults['from_email_address'] = $id;
             }
         }
     }
     //fix for CRM-2873
     if (!$reuseMailing) {
         $textFilePath = $this->get('textFilePath');
         if ($textFilePath && file_exists($textFilePath)) {
             $defaults['text_message'] = file_get_contents($textFilePath);
             if (strlen($defaults['text_message']) > 0) {
                 $this->set('skipTextFile', true);
             }
         }
         $htmlFilePath = $this->get('htmlFilePath');
         if ($htmlFilePath && file_exists($htmlFilePath)) {
             $defaults['html_message'] = file_get_contents($htmlFilePath);
             if (strlen($defaults['html_message']) > 0) {
                 $htmlMessage = $defaults['html_message'];
                 $this->set('skipHtmlFile', true);
             }
         }
     }
     if ($this->get('html_message')) {
         $htmlMessage = $this->get('html_message');
     }
     $htmlMessage = str_replace(array("\n", "\r"), ' ', $htmlMessage);
     $htmlMessage = str_replace("'", "\\'", $htmlMessage);
     $this->assign('message_html', $htmlMessage);
     $defaults['upload_type'] = 1;
     if (isset($defaults['body_html'])) {
         $defaults['html_message'] = $defaults['body_html'];
     }
     //CRM-4678 setdefault to default component when composing new mailing.
     if (!$reuseMailing) {
         $componentFields = array('header_id' => 'Header', 'footer_id' => 'Footer');
         foreach ($componentFields as $componentVar => $componentType) {
             $defaults[$componentVar] = CRM_Mailing_PseudoConstant::defaultComponent($componentType, '');
         }
     }
     return $defaults;
 }
Exemple #5
0
 /**
  * function to get the Message Templates
  *
  * @access public
  * @static 
  * @return object
  */
 static function getMessageTemplates($all = true)
 {
     $msgTpls = array();
     $messageTemplates = new CRM_Core_DAO_MessageTemplates();
     $messageTemplates->is_active = 1;
     if (!$all) {
         $messageTemplates->workflow_id = 'NULL';
     }
     $messageTemplates->find();
     while ($messageTemplates->fetch()) {
         $msgTpls[$messageTemplates->id] = $messageTemplates->msg_title;
     }
     asort($msgTpls);
     return $msgTpls;
 }
 /**
  * returns the list of fields that can be exported
  *
  * @access public
  * return array
  * @static
  */
 static function &export($prefix = false)
 {
     if (!self::$_export) {
         self::$_export = array();
         $fields = self::fields();
         foreach ($fields as $name => $field) {
             if (CRM_Utils_Array::value('export', $field)) {
                 if ($prefix) {
                     self::$_export['msg_template'] =& $fields[$name];
                 } else {
                     self::$_export[$name] =& $fields[$name];
                 }
             }
         }
     }
     return self::$_export;
 }
 /**
  * The function checks and updates the status of all membership records for a given domain using the
  * calc_membership_status and update_contact_membership APIs.
  *
  * IMPORTANT:
  * It uses the default Domain FROM Name and FROM Email Address as the From email address for emails sent by this api.
  * Verify that this value has been properly set from Administer > Configure > Domain Information
  * If you want to use some other FROM email address, modify line 125 and set your valid email address.
  *
  * @return array $result
  * @access public
  */
 static function updateAllMembershipStatus()
 {
     require_once 'api/api.php';
     //get all active statuses of membership, CRM-3984
     $allStatus = CRM_Member_PseudoConstant::membershipStatus();
     $statusLabels = CRM_Member_PseudoConstant::membershipStatus(NULL, NULL, 'label');
     $allTypes = CRM_Member_PseudoConstant::membershipType();
     $contribStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
     $query = "\nSELECT     civicrm_membership.id                    as membership_id,\n           civicrm_membership.is_override           as is_override,\n           civicrm_membership.reminder_date         as reminder_date,\n           civicrm_membership.membership_type_id    as membership_type_id,\n           civicrm_membership.status_id             as status_id,\n           civicrm_membership.join_date             as join_date,\n           civicrm_membership.start_date            as start_date,\n           civicrm_membership.end_date              as end_date,\n           civicrm_membership.source                as source,\n           civicrm_contact.id                       as contact_id,\n           civicrm_contact.is_deceased              as is_deceased,\n           civicrm_membership.owner_membership_id   as owner_membership_id,\n           civicrm_membership.contribution_recur_id as recur_id\nFROM       civicrm_membership\nINNER JOIN civicrm_contact ON ( civicrm_membership.contact_id = civicrm_contact.id )\nWHERE      civicrm_membership.is_test = 0";
     $params = array();
     $dao = CRM_Core_DAO::executeQuery($query, $params);
     $today = date("Y-m-d");
     $processCount = 0;
     $updateCount = 0;
     $reminderCount = 0;
     $smarty = CRM_Core_Smarty::singleton();
     $domainValues = CRM_Core_BAO_Domain::getNameAndEmail();
     $domainFromEmail = "{$domainValues['0']} <{$domainValues['1']}>";
     //use domain email address as a default From email.
     $fromEmailAddress = $domainFromEmail;
     while ($dao->fetch()) {
         // echo ".";
         $processCount++;
         /**
                $count++;
                echo $dao->contact_id . ', '. CRM_Utils_System::memory( ) . "<p>\n";
         
                CRM_Core_Error::debug( 'fBegin', count( $GLOBALS['_DB_DATAOBJECT']['RESULTS'] ) );
                if ( $count > 2 ) {
                foreach ( $GLOBALS['_DB_DATAOBJECT']['RESULTS'] as $r ) {
                CRM_Core_Error::debug( 'r', $r->query );
                }
                // CRM_Core_Error::debug( 'f', $GLOBALS['_DB_DATAOBJECT']['RESULTS'] );
                exit( );
                }
                **/
         // Put common parameters into array for easy access
         $memberParams = array('id' => $dao->membership_id, 'status_id' => $dao->status_id, 'contact_id' => $dao->contact_id, 'membership_type_id' => $dao->membership_type_id, 'membership_type' => $allTypes[$dao->membership_type_id], 'join_date' => $dao->join_date, 'start_date' => $dao->start_date, 'end_date' => $dao->end_date, 'reminder_date' => $dao->reminder_date, 'source' => $dao->source, 'skipStatusCal' => TRUE, 'skipRecentView' => TRUE);
         $smarty->assign_by_ref('memberParams', $memberParams);
         //update membership record to Deceased if contact is deceased
         if ($dao->is_deceased) {
             // check for 'Deceased' membership status, CRM-5636
             $deceaseStatusId = array_search('Deceased', $allStatus);
             if (!$deceaseStatusId) {
                 CRM_Core_Error::fatal(ts("Deceased Membership status is missing or not active. <a href='%1'>Click here to check</a>.", array(1 => CRM_Utils_System::url('civicrm/admin/member/membershipStatus', 'reset=1'))));
             }
             //process only when status change.
             if ($dao->status_id != $deceaseStatusId) {
                 //take all params that need to save.
                 $deceasedMembership = $memberParams;
                 $deceasedMembership['status_id'] = $deceaseStatusId;
                 $deceasedMembership['createActivity'] = TRUE;
                 $deceasedMembership['version'] = 3;
                 //since there is change in status.
                 $statusChange = array('status_id' => $deceaseStatusId);
                 $smarty->append_by_ref('memberParams', $statusChange, TRUE);
                 //process membership record.
                 civicrm_api('membership', 'create', $deceasedMembership);
             }
             continue;
         }
         //we fetch related, since we need to check for deceased
         //now further processing is handle w/ main membership record.
         if ($dao->owner_membership_id) {
             continue;
         }
         //update membership records where status is NOT - Pending OR Cancelled.
         //as well as membership is not override.
         //skipping Expired membership records -> reduced extra processing( kiran )
         if (!$dao->is_override && !in_array($dao->status_id, array(array_search('Pending', $allStatus), array_search('Cancelled', $allStatus), array_search('Expired', $allStatus)))) {
             // CRM-7248: added excludeIsAdmin param to the following fn call to prevent moving to admin statuses
             //get the membership status as per id.
             $newStatus = civicrm_api('membership_status', 'calc', array('membership_id' => $dao->membership_id, 'version' => 3, 'ignore_admin_only' => FALSE), TRUE);
             $statusId = CRM_Utils_Array::value('id', $newStatus);
             //process only when status change.
             if ($statusId && $statusId != $dao->status_id) {
                 //take all params that need to save.
                 $memParams = $memberParams;
                 $memParams['status_id'] = $statusId;
                 $memParams['createActivity'] = TRUE;
                 $memParams['version'] = 3;
                 //since there is change in status.
                 $statusChange = array('status_id' => $statusId);
                 $smarty->append_by_ref('memberParams', $statusChange, TRUE);
                 //process member record.
                 civicrm_api('membership', 'create', $memParams);
                 $updateCount++;
             }
         }
         //convert date from string format to timestamp format
         $reminder_date = CRM_Utils_DATE::unixTime($dao->reminder_date);
         $today_date = CRM_Utils_DATE::unixTime($today);
         //send reminder for membership renewal
         if ($dao->reminder_date && $dao->reminder_date != '0000-00-00' && $reminder_date <= $today_date) {
             $memType = new CRM_Member_BAO_MembershipType();
             $memType->id = $dao->membership_type_id;
             $memType->find(TRUE);
             $renewalMsgId = $memType->renewal_msg_id;
             if ($memType->autorenewal_msg_id && $dao->recur_id) {
                 $contribStatusId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionRecur', $dao->recur_id, 'contribution_status_id');
                 if ($contribStatusId != array_search('Cancelled', $contribStatus)) {
                     $renewalMsgId = $memType->autorenewal_msg_id;
                 }
             }
             if ($renewalMsgId) {
                 $toEmail = CRM_Contact_BAO_Contact::getPrimaryEmail($dao->contact_id);
                 if ($toEmail) {
                     $sendResult = CRM_Core_BAO_MessageTemplates::sendReminder($dao->contact_id, $toEmail, $renewalMsgId, $fromEmailAddress);
                     if (!$sendResult || is_a($sendResult, 'PEAR_Error')) {
                         // we could not send an email, for now we ignore
                         // CRM-3406
                         // at some point we might decide to do something
                     } else {
                         $reminderCount++;
                     }
                     //set membership reminder date to NULL since we've sent the reminder.
                     CRM_Core_DAO::setFieldValue('CRM_Member_DAO_Membership', $dao->membership_id, 'reminder_date', 'null');
                     // insert the activity log record.
                     $config = CRM_Core_Config::singleton();
                     $activityParams = array();
                     $activityParams['subject'] = $allTypes[$dao->membership_type_id] . ": Status - " . $statusLabels[$newStatus['id']] . ", End Date - " . CRM_Utils_Date::customFormat(CRM_Utils_Date::isoToMysql($dao->end_date), $config->dateformatFull);
                     $activityParams['source_record_id'] = $dao->membership_id;
                     $session = CRM_Core_Session::singleton();
                     $activityParams['source_contact_id'] = $session->get('userID') ? $session->get('userID') : $dao->contact_id;
                     $activityParams['assignee_contact_id'] = $dao->contact_id;
                     $activityParams['activity_date_time'] = date('YmdHis');
                     static $actRelIds = array();
                     if (!isset($actRelIds['activity_type_id'])) {
                         $actRelIds['activity_type_id'] = CRM_Core_OptionGroup::getValue('activity_type', 'Membership Renewal Reminder', 'name');
                     }
                     $activityParams['activity_type_id'] = $actRelIds['activity_type_id'];
                     if (!isset($actRelIds['activity_status_id'])) {
                         $actRelIds['activity_status_id'] = CRM_Core_OptionGroup::getValue('activity_status', 'Completed', 'name');
                     }
                     $activityParams['status_id'] = $actRelIds['activity_status_id'];
                     static $msgTpl = array();
                     if (!isset($msgTpl[$memType->renewal_msg_id])) {
                         $msgTpl[$memType->renewal_msg_id] = array();
                         $messageTemplate = new CRM_Core_DAO_MessageTemplates();
                         $messageTemplate->id = $memType->renewal_msg_id;
                         if ($messageTemplate->find(TRUE)) {
                             $msgTpl[$memType->renewal_msg_id]['subject'] = $messageTemplate->msg_subject;
                             $msgTpl[$memType->renewal_msg_id]['details'] = $messageTemplate->msg_text;
                         }
                         $messageTemplate->free();
                     }
                     $activityParams['details'] = "Subject: {$msgTpl[$memType->renewal_msg_id]['subject']}\nMessage: {$msgTpl[$memType->renewal_msg_id]['details']}\n";
                     $activity = CRM_Activity_BAO_Activity::create($activityParams);
                 }
             }
             $memType->free();
         }
         // CRM_Core_Error::debug( 'fEnd', count( $GLOBALS['_DB_DATAOBJECT']['RESULTS'] ) );
     }
     $result['is_error'] = 0;
     $result['messages'] = ts('Processed %1 membership records. Updated %2 records. Sent %3 renewal reminders.', array(1 => $processCount, 2 => $updateCount, 3 => $reminderCount));
     return $result;
 }
 static function sendReminder($contactId, $email, $messageTemplateID, $from)
 {
     $messageTemplates = new CRM_Core_DAO_MessageTemplates();
     $messageTemplates->id = $messageTemplateID;
     $domain = CRM_Core_BAO_Domain::getDomain();
     $result = NULL;
     $hookTokens = array();
     if ($messageTemplates->find(TRUE)) {
         $body_text = $messageTemplates->msg_text;
         $body_html = $messageTemplates->msg_html;
         $body_subject = $messageTemplates->msg_subject;
         if (!$body_text) {
             $body_text = CRM_Utils_String::htmlToText($body_html);
         }
         $params = array(array('contact_id', '=', $contactId, 0, 0));
         list($contact, $_) = CRM_Contact_BAO_Query::apiQuery($params);
         //CRM-4524
         $contact = reset($contact);
         if (!$contact || is_a($contact, 'CRM_Core_Error')) {
             return NULL;
         }
         //CRM-5734
         // get tokens to be replaced
         $tokens = array_merge(CRM_Utils_Token::getTokens($body_text), CRM_Utils_Token::getTokens($body_html), CRM_Utils_Token::getTokens($body_subject));
         // get replacement text for these tokens
         $returnProperties = array("preferred_mail_format" => 1);
         if (isset($tokens['contact'])) {
             foreach ($tokens['contact'] as $key => $value) {
                 $returnProperties[$value] = 1;
             }
         }
         list($details) = CRM_Utils_Token::getTokenDetails(array($contactId), $returnProperties, null, null, false, $tokens, 'CRM_Core_BAO_MessageTemplates');
         $contact = reset($details);
         // call token hook
         $hookTokens = array();
         CRM_Utils_Hook::tokens($hookTokens);
         $categories = array_keys($hookTokens);
         // do replacements in text and html body
         $type = array('html', 'text');
         foreach ($type as $key => $value) {
             $bodyType = "body_{$value}";
             if (${$bodyType}) {
                 CRM_Utils_Token::replaceGreetingTokens(${$bodyType}, NULL, $contact['contact_id']);
                 ${$bodyType} = CRM_Utils_Token::replaceDomainTokens(${$bodyType}, $domain, true, $tokens, true);
                 ${$bodyType} = CRM_Utils_Token::replaceContactTokens(${$bodyType}, $contact, false, $tokens, false, true);
                 ${$bodyType} = CRM_Utils_Token::replaceComponentTokens(${$bodyType}, $contact, $tokens, true);
                 ${$bodyType} = CRM_Utils_Token::replaceHookTokens(${$bodyType}, $contact, $categories, true);
             }
         }
         $html = $body_html;
         $text = $body_text;
         $smarty = CRM_Core_Smarty::singleton();
         foreach (array('text', 'html') as $elem) {
             ${$elem} = $smarty->fetch("string:{${$elem}}");
         }
         // do replacements in message subject
         $messageSubject = CRM_Utils_Token::replaceContactTokens($body_subject, $contact, false, $tokens);
         $messageSubject = CRM_Utils_Token::replaceDomainTokens($messageSubject, $domain, true, $tokens);
         $messageSubject = CRM_Utils_Token::replaceComponentTokens($messageSubject, $contact, $tokens, true);
         $messageSubject = CRM_Utils_Token::replaceHookTokens($messageSubject, $contact, $categories, true);
         $messageSubject = $smarty->fetch("string:{$messageSubject}");
         // set up the parameters for CRM_Utils_Mail::send
         $mailParams = array('groupName' => 'Scheduled Reminder Sender', 'from' => $from, 'toName' => $contact['display_name'], 'toEmail' => $email, 'subject' => $messageSubject);
         if (!$html || $contact['preferred_mail_format'] == 'Text' || $contact['preferred_mail_format'] == 'Both') {
             // render the &amp; entities in text mode, so that the links work
             $mailParams['text'] = str_replace('&amp;', '&', $text);
         }
         if ($html && ($contact['preferred_mail_format'] == 'HTML' || $contact['preferred_mail_format'] == 'Both')) {
             $mailParams['html'] = $html;
         }
         $result = CRM_Utils_Mail::send($mailParams);
     }
     $messageTemplates->free();
     return $result;
 }