예제 #1
0
 /**
  *  Assert the content of document
  *
  * @param array $formValues
  * @param array $type
  */
 public function _testDocumentContent($formValues, $type)
 {
     $html = array();
     $form = new CRM_Contact_Form_Task_PDFLetterCommon();
     list($formValues, $categories, $html_message, $messageToken, $returnProperties) = $form->processMessageTemplate($formValues);
     list($html_message, $zip) = CRM_Utils_PDF_Document::unzipDoc($formValues['document_file_path'], $formValues['document_type']);
     foreach ($this->_contactIds as $item => $contactId) {
         $params = array('contact_id' => $contactId);
         list($contact) = CRM_Utils_Token::getTokenDetails($params, $returnProperties, FALSE, FALSE, NULL, $messageToken, 'CRM_Contact_Form_Task_PDFLetterCommon');
         $html[] = CRM_Utils_Token::replaceContactTokens($html_message, $contact[$contactId], TRUE, $messageToken);
     }
     $returnContent = CRM_Utils_PDF_Document::printDocuments($formValues['document_file_path'], $html, $type, $zip, TRUE);
     $returnContent = strip_tags($returnContent);
     $this->assertTrue(strpos($returnContent, 'Hello Antonia D`souza') !== 0);
     $this->assertTrue(strpos($returnContent, 'Hello Anthony Collins') !== 0);
 }
 /**
  * generate htmlfor pdf letters
  *
  * @param array $membershipIDs
  * @param array $returnProperties
  * @param bool $skipOnHold
  * @param bool $skipDeceased
  * @param unknown_type $messageToken
  * @param $html_message
  * @param $categories
  *
  * @return unknown
  */
 static function generateHTML($membershipIDs, $returnProperties, $skipOnHold, $skipDeceased, $messageToken, $html_message, $categories)
 {
     $memberships = CRM_Utils_Token::getMembershipTokenDetails($membershipIDs);
     foreach ($membershipIDs as $membershipID) {
         $membership = $memberships[$membershipID];
         // get contact information
         $contactId = $membership['contact_id'];
         $params = array('contact_id' => $contactId);
         //getTokenDetails is much like calling the api contact.get function - but - with some minor
         // special handlings. It preceeds the existence of the api
         list($contacts) = CRM_Utils_Token::getTokenDetails($params, $returnProperties, $skipOnHold, $skipDeceased, NULL, $messageToken, 'CRM_Contribution_Form_Task_PDFLetterCommon');
         $tokenHtml = CRM_Utils_Token::replaceContactTokens($html_message, $contacts[$contactId], TRUE, $messageToken);
         $tokenHtml = CRM_Utils_Token::replaceEntityTokens('membership', $membership, $tokenHtml, $messageToken);
         $tokenHtml = CRM_Utils_Token::replaceHookTokens($tokenHtml, $contacts[$contactId], $categories, TRUE);
         $tokenHtml = CRM_Utils_Token::parseThroughSmarty($tokenHtml, $contacts[$contactId]);
         $html[] = $tokenHtml;
     }
     return $html;
 }
예제 #3
0
 /**
  * Run this page (figure out the action needed and perform it).
  *
  * @return void
  */
 public function run()
 {
     $session = CRM_Core_Session::singleton();
     $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', CRM_Core_DAO::$_nullObject, FALSE, 'text');
     $type = CRM_Utils_Request::retrieve('type', 'String', CRM_Core_DAO::$_nullObject, FALSE, 'text');
     $options = array();
     $session->getVars($options, "CRM_Mailing_Controller_Send_{$qfKey}");
     //get the options if control come from search context, CRM-3711
     if (empty($options)) {
         $session->getVars($options, "CRM_Contact_Controller_Search_{$qfKey}");
     }
     // FIXME: the below and CRM_Mailing_Form_Test::testMail()
     // should be refactored
     $fromEmail = NULL;
     $mailing = new CRM_Mailing_BAO_Mailing();
     if (!empty($options)) {
         $mailing->id = $options['mailing_id'];
         $fromEmail = CRM_Utils_Array::value('from_email', $options);
     }
     $mailing->find(TRUE);
     CRM_Mailing_BAO_Mailing::tokenReplace($mailing);
     // get and format attachments
     $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_mailing', $mailing->id);
     //get details of contact with token value including Custom Field Token Values.CRM-3734
     $returnProperties = $mailing->getReturnProperties();
     $params = array('contact_id' => $session->get('userID'));
     $details = CRM_Utils_Token::getTokenDetails($params, $returnProperties, TRUE, TRUE, NULL, $mailing->getFlattenedTokens(), get_class($this));
     $mime =& $mailing->compose(NULL, NULL, NULL, $session->get('userID'), $fromEmail, $fromEmail, TRUE, $details[0][$session->get('userID')], $attachments);
     if ($type == 'html') {
         CRM_Utils_System::setHttpHeader('Content-Type', 'text/html; charset=utf-8');
         print $mime->getHTMLBody();
     } else {
         CRM_Utils_System::setHttpHeader('Content-Type', 'text/plain; charset=utf-8');
         print $mime->getTXTBody();
     }
     CRM_Utils_System::civiExit();
 }
예제 #4
0
 /**
  * Generate the contribution array from the form, we fill in the contact details and determine any aggregation
  * around contact_id of contribution_recur_id
  *
  * @param string $groupBy
  * @param array $contributionIDs
  * @param array $returnProperties
  * @param bool $skipOnHold
  * @param bool $skipDeceased
  * @param array $messageToken
  * @param string $task
  * @param string $separator
  * @param bool $isIncludeSoftCredits
  *
  * @return array
  */
 public static function buildContributionArray($groupBy, $contributionIDs, $returnProperties, $skipOnHold, $skipDeceased, $messageToken, $task, $separator, $isIncludeSoftCredits)
 {
     $contributions = $contacts = $notSent = array();
     foreach ($contributionIDs as $item => $contributionId) {
         // get contribution information
         $contribution = CRM_Utils_Token::getContributionTokenDetails(array('contribution_id' => $contributionId), $returnProperties, NULL, $messageToken, $task);
         $contribution = $contributions[$contributionId] = $contribution[$contributionId];
         if ($isIncludeSoftCredits) {
             //@todo find out why this happens & add comments
             list($contactID) = explode('-', $item);
             $contactID = (int) $contactID;
         } else {
             $contactID = $contribution['contact_id'];
         }
         if (!isset($contacts[$contactID])) {
             $contacts[$contactID] = array();
             $contacts[$contactID]['contact_aggregate'] = 0;
             $contacts[$contactID]['combined'] = $contacts[$contactID]['contribution_ids'] = array();
         }
         $contacts[$contactID]['contact_aggregate'] += $contribution['total_amount'];
         $groupByID = empty($contribution[$groupBy]) ? 0 : $contribution[$groupBy];
         $contacts[$contactID]['contribution_ids'][$groupBy][$groupByID][$contributionId] = TRUE;
         if (!isset($contacts[$contactID]['combined'][$groupBy]) || !isset($contacts[$contactID]['combined'][$groupBy][$groupByID])) {
             $contacts[$contactID]['combined'][$groupBy][$groupByID] = $contribution;
             $contacts[$contactID]['aggregates'][$groupBy][$groupByID] = $contribution['total_amount'];
         } else {
             $contacts[$contactID]['combined'][$groupBy][$groupByID] = self::combineContributions($contacts[$contactID]['combined'][$groupBy][$groupByID], $contribution, $separator);
             $contacts[$contactID]['aggregates'][$groupBy][$groupByID] += $contribution['total_amount'];
         }
     }
     // Assign the available contributions before calling tokens so hooks parsing smarty can access it.
     // Note that in core code you can only use smarty here if enable if for the whole site, incl
     // CiviMail, with a big performance impact.
     // Hooks allow more nuanced smarty usage here.
     CRM_Core_Smarty::singleton()->assign('contributions', $contributions);
     foreach ($contacts as $contactID => $contact) {
         $tokenResolvedContacts = CRM_Utils_Token::getTokenDetails(array('contact_id' => $contactID), $returnProperties, $skipOnHold, $skipDeceased, NULL, $messageToken, $task);
         $contacts[$contactID] = array_merge($tokenResolvedContacts[0][$contactID], $contact);
     }
     return array($contributions, $contacts);
 }
 protected function replaceTokens($input, $contact_id)
 {
     //get contact
     $params = array(array('contact_id', '=', $contact_id, 0, 0));
     list($contact, $_) = CRM_Contact_BAO_Query::apiQuery($params);
     $contact = reset($contact);
     if (!$contact || is_a($contact, 'CRM_Core_Error')) {
         throw new API_Exception('Could not find contact with ID: ' . $params['contact_id']);
     }
     $tokens = CRM_Utils_Token::getTokens($input);
     // get replacement text for these tokens
     $returnProperties = array('sort_name' => 1, 'email' => 1, 'do_not_email' => 1, 'is_deceased' => 1, 'on_hold' => 1, 'display_name' => 1, 'preferred_mail_format' => 1);
     if (isset($tokens['contact'])) {
         foreach ($tokens['contact'] as $key => $value) {
             $returnProperties[$value] = 1;
         }
     }
     list($details) = CRM_Utils_Token::getTokenDetails(array($contact_id), $returnProperties, false, false, null, $tokens);
     $contact = reset($details);
     // call token hook
     $hookTokens = array();
     CRM_Utils_Hook::tokens($hookTokens);
     $categories = array_keys($hookTokens);
     CRM_Utils_Token::replaceGreetingTokens($input, NULL, $contact['contact_id']);
     $input = CRM_Utils_Token::replaceDomainTokens($input, $domain, true, $tokens, true);
     $input = CRM_Utils_Token::replaceContactTokens($input, $contact, false, $tokens, false, true);
     $input = CRM_Utils_Token::replaceComponentTokens($input, $contact, $tokens, true);
     $input = CRM_Utils_Token::replaceHookTokens($input, $contact, $categories, true);
     return $input;
 }
예제 #6
0
파일: SMSCommon.php 프로젝트: kidaa30/yes
 /**
  * Build the form object.
  *
  *
  * @param CRM_Core_Form $form
  *
  * @return void
  */
 public static function buildQuickForm(&$form)
 {
     $toArray = array();
     $providers = CRM_SMS_BAO_Provider::getProviders(NULL, NULL, TRUE, 'is_default desc');
     $providerSelect = array();
     foreach ($providers as $provider) {
         $providerSelect[$provider['id']] = $provider['title'];
     }
     $suppressedSms = 0;
     //here we are getting logged in user id as array but we need target contact id. CRM-5988
     $cid = $form->get('cid');
     if ($cid) {
         $form->_contactIds = array($cid);
     }
     $to = $form->add('text', 'to', ts('To'), array('class' => 'huge'), TRUE);
     $form->add('text', 'activity_subject', ts('Name The SMS'), array('class' => 'huge'), TRUE);
     $toSetDefault = TRUE;
     if (property_exists($form, '_context') && $form->_context == 'standalone') {
         $toSetDefault = FALSE;
     }
     // when form is submitted recompute contactIds
     $allToSMS = array();
     if ($to->getValue()) {
         $allToPhone = explode(',', $to->getValue());
         $form->_contactIds = array();
         foreach ($allToPhone as $value) {
             list($contactId, $phone) = explode('::', $value);
             if ($contactId) {
                 $form->_contactIds[] = $contactId;
                 $form->_toContactPhone[] = $phone;
             }
         }
         $toSetDefault = TRUE;
     }
     //get the group of contacts as per selected by user in case of Find Activities
     if (!empty($form->_activityHolderIds)) {
         $extendTargetContacts = 0;
         $invalidActivity = 0;
         $validActivities = 0;
         foreach ($form->_activityHolderIds as $key => $id) {
             //valid activity check
             if (CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $id, 'subject', 'id') != self::RECIEVED_SMS_ACTIVITY_SUBJECT) {
                 $invalidActivity++;
                 continue;
             }
             $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
             $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
             //target contacts limit check
             $ids = array_keys(CRM_Activity_BAO_ActivityContact::getNames($id, $targetID));
             if (count($ids) > 1) {
                 $extendTargetContacts++;
                 continue;
             }
             $validActivities++;
             $form->_contactIds = empty($form->_contactIds) ? $ids : array_unique(array_merge($form->_contactIds, $ids));
         }
         if (!$validActivities) {
             $errorMess = "";
             if ($extendTargetContacts) {
                 $errorMess = ts('One selected activity consists of more than one target contact.', array('count' => $extendTargetContacts, 'plural' => '%count selected activities consist of more than one target contact.'));
             }
             if ($invalidActivity) {
                 $errorMess = $errorMess ? ' ' : '';
                 $errorMess .= ts('The selected activity is invalid.', array('count' => $invalidActivity, 'plural' => '%count selected activities are invalid.'));
             }
             CRM_Core_Error::statusBounce(ts("%1: SMS Reply will not be sent.", array(1 => $errorMess)));
         }
     }
     if (is_array($form->_contactIds) && !empty($form->_contactIds) && $toSetDefault) {
         $returnProperties = array('sort_name' => 1, 'phone' => 1, 'do_not_sms' => 1, 'is_deceased' => 1, 'display_name' => 1);
         list($form->_contactDetails) = CRM_Utils_Token::getTokenDetails($form->_contactIds, $returnProperties, FALSE, FALSE);
         // make a copy of all contact details
         $form->_allContactDetails = $form->_contactDetails;
         foreach ($form->_contactIds as $key => $contactId) {
             $value = $form->_contactDetails[$contactId];
             //to check if the phone type is "Mobile"
             $phoneTypes = CRM_Core_OptionGroup::values('phone_type', TRUE, FALSE, FALSE, NULL, 'name');
             if (CRM_Utils_System::getClassName($form) == 'CRM_Activity_Form_Task_SMS') {
                 //to check for "if the contact id belongs to a specified activity type"
                 $actDetails = CRM_Activity_BAO_Activity::getContactActivity($contactId);
                 if (self::RECIEVED_SMS_ACTIVITY_SUBJECT != CRM_Utils_Array::retrieveValueRecursive($actDetails, 'subject')) {
                     $suppressedSms++;
                     unset($form->_contactDetails[$contactId]);
                     continue;
                 }
             }
             if (isset($value['phone_type_id']) && $value['phone_type_id'] != CRM_Utils_Array::value('Mobile', $phoneTypes) || $value['do_not_sms'] || empty($value['phone']) || !empty($value['is_deceased'])) {
                 //if phone is not primary check if non-primary phone is "Mobile"
                 if (!empty($value['phone']) && $value['phone_type_id'] != CRM_Utils_Array::value('Mobile', $phoneTypes) && empty($value['is_deceased'])) {
                     $filter = array('do_not_sms' => 0);
                     $contactPhones = CRM_Core_BAO_Phone::allPhones($contactId, FALSE, 'Mobile', $filter);
                     if (count($contactPhones) > 0) {
                         $mobilePhone = CRM_Utils_Array::retrieveValueRecursive($contactPhones, 'phone');
                         $form->_contactDetails[$contactId]['phone_id'] = CRM_Utils_Array::retrieveValueRecursive($contactPhones, 'id');
                         $form->_contactDetails[$contactId]['phone'] = $mobilePhone;
                         $form->_contactDetails[$contactId]['phone_type_id'] = CRM_Utils_Array::value('Mobile', $phoneTypes);
                     } else {
                         $suppressedSms++;
                         unset($form->_contactDetails[$contactId]);
                         continue;
                     }
                 } else {
                     $suppressedSms++;
                     unset($form->_contactDetails[$contactId]);
                     continue;
                 }
             }
             if (isset($mobilePhone)) {
                 $phone = $mobilePhone;
             } elseif (empty($form->_toContactPhone)) {
                 $phone = $value['phone'];
             } else {
                 $phone = CRM_Utils_Array::value($key, $form->_toContactPhone);
             }
             if ($phone) {
                 $toArray[] = array('text' => '"' . $value['sort_name'] . '" (' . $phone . ')', 'id' => "{$contactId}::{$phone}");
             }
         }
         if (empty($toArray)) {
             CRM_Core_Error::statusBounce(ts('Selected contact(s) do not have a valid Phone, or communication preferences specify DO NOT SMS, or they are deceased'));
         }
     }
     //activity related variables
     if (isset($invalidActivity)) {
         $form->assign('invalidActivity', $invalidActivity);
     }
     if (isset($extendTargetContacts)) {
         $form->assign('extendTargetContacts', $extendTargetContacts);
     }
     $form->assign('toContact', json_encode($toArray));
     $form->assign('suppressedSms', $suppressedSms);
     $form->assign('totalSelectedContacts', count($form->_contactIds));
     $form->add('select', 'sms_provider_id', ts('From'), $providerSelect, TRUE);
     CRM_Mailing_BAO_Mailing::commonCompose($form);
     if ($form->_single) {
         // also fix the user context stack
         if ($form->_context) {
             $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
         } else {
             $url = CRM_Utils_System::url('civicrm/contact/view', "&show=1&action=browse&cid={$form->_contactIds[0]}&selectedChild=activity");
         }
         $session = CRM_Core_Session::singleton();
         $session->replaceUserContext($url);
         $form->addDefaultButtons(ts('Send SMS'), 'upload', 'cancel');
     } else {
         $form->addDefaultButtons(ts('Send SMS'), 'upload');
     }
     $form->addFormRule(array('CRM_Contact_Form_Task_SMSCommon', 'formRule'), $form);
 }
 static function sendSMS(&$contactDetails, &$activityParams, &$smsParams = array(), &$contactIds, $userID = NULL)
 {
     if ($userID == NULL) {
         $session = CRM_Core_Session::singleton();
         $userID = $session->get('userID');
     }
     $text =& $activityParams['text_message'];
     $html =& $activityParams['html_message'];
     // CRM-4575
     // token replacement of addressee/email/postal greetings
     // get the tokens added in subject and message
     $messageToken = CRM_Utils_Token::getTokens($text);
     $messageToken = array_merge($messageToken, CRM_Utils_Token::getTokens($html));
     //create the meta level record first ( sms activity )
     $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type', 'SMS', 'name');
     // CRM-6265: save both text and HTML parts in details (if present)
     if ($html and $text) {
         $details = "-ALTERNATIVE ITEM 0-\n{$html}\n-ALTERNATIVE ITEM 1-\n{$text}\n-ALTERNATIVE END-\n";
     } else {
         $details = $html ? $html : $text;
     }
     $activitySubject = $activityParams['activity_subject'];
     $activityParams = array('source_contact_id' => $userID, 'activity_type_id' => $activityTypeID, 'activity_date_time' => date('YmdHis'), 'subject' => $activitySubject, 'details' => $details, 'status_id' => 2);
     $activity = self::create($activityParams);
     $activityID = $activity->id;
     $returnProperties = array();
     if (isset($messageToken['contact'])) {
         foreach ($messageToken['contact'] as $key => $value) {
             $returnProperties[$value] = 1;
         }
     }
     // get token details for contacts, call only if tokens are used
     $details = array();
     if (!empty($returnProperties)) {
         list($details) = CRM_Utils_Token::getTokenDetails($contactIds, $returnProperties, NULL, NULL, FALSE, $messageToken, 'CRM_Activity_BAO_Activity');
     }
     // call token hook
     $tokens = array();
     CRM_Utils_Hook::tokens($tokens);
     $categories = array_keys($tokens);
     $escapeSmarty = $sent = FALSE;
     foreach ($contactDetails as $values) {
         $contactId = $values['contact_id'];
         if (!empty($details) && is_array($details["{$contactId}"])) {
             // unset email from details since it always returns primary email address
             unset($details["{$contactId}"]['email']);
             unset($details["{$contactId}"]['email_id']);
             $values = array_merge($values, $details["{$contactId}"]);
         }
         $tokenText = CRM_Utils_Token::replaceContactTokens($text, $values, FALSE, $messageToken, FALSE, $escapeSmarty);
         $tokenText = CRM_Utils_Token::replaceHookTokens($tokenText, $values, $categories, FALSE, $escapeSmarty);
         $tokenHtml = CRM_Utils_Token::replaceContactTokens($html, $values, TRUE, $messageToken, FALSE, $escapeSmarty);
         $tokenHtml = CRM_Utils_Token::replaceHookTokens($tokenHtml, $values, $categories, TRUE, $escapeSmarty);
         $smsParams['To'] = $values['phone'];
         if (self::sendSMSMessage($contactId, $tokenText, $tokenHtml, $smsParams, $activityID)) {
             // even a single successful delivery should set this falg to true
             $sent = TRUE;
         }
     }
     return array($sent, $activity->id);
 }
예제 #8
0
 /**
  * Cancel this participant and finish, send cancellation email. At this point no
  * auto-cancellation of payment is handled, so payment needs to be manually cancelled
  *
  * return @void
  */
 public function cancelParticipant($params)
 {
     //set participant record status to Cancelled, refund payment if possible
     // send email to participant and admin, and log Activity
     $value = array();
     $value['id'] = $this->_participant_id;
     $cancelledId = array_search('Cancelled', CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Negative'"));
     $value['status_id'] = $cancelledId;
     CRM_Event_BAO_Participant::create($value);
     $domainValues = array();
     $domain = CRM_Core_BAO_Domain::getDomain();
     $tokens = array('domain' => array('name', 'phone', 'address', 'email'), 'contact' => CRM_Core_SelectValues::contactTokens());
     foreach ($tokens['domain'] as $token) {
         $domainValues[$token] = CRM_Utils_Token::getDomainTokenReplacement($token, $domain);
     }
     $participantRoles = array();
     $participantRoles = CRM_Event_PseudoConstant::participantRole();
     $participantDetails = array();
     $query = "SELECT * FROM civicrm_participant WHERE id = {$this->_participant_id}";
     $dao = CRM_Core_DAO::executeQuery($query);
     while ($dao->fetch()) {
         $participantDetails[$dao->id] = array('id' => $dao->id, 'role' => $participantRoles[$dao->role_id], 'is_test' => $dao->is_test, 'event_id' => $dao->event_id, 'status_id' => $dao->status_id, 'fee_amount' => $dao->fee_amount, 'contact_id' => $dao->contact_id, 'register_date' => $dao->register_date, 'registered_by_id' => $dao->registered_by_id);
     }
     $eventDetails = array();
     $eventParams = array('id' => $this->_event_id);
     CRM_Event_BAO_Event::retrieve($eventParams, $eventDetails[$this->_event_id]);
     //get default participant role.
     $eventDetails[$this->_event_id]['participant_role'] = CRM_Utils_Array::value($eventDetails[$this->_event_id]['default_role_id'], $participantRoles);
     //get the location info
     $locParams = array('entity_id' => $this->_event_id, 'entity_table' => 'civicrm_event');
     $eventDetails[$this->_event_id]['location'] = CRM_Core_BAO_Location::getValues($locParams, TRUE);
     //get contact details
     $contactIds[$this->_contact_id] = $this->_contact_id;
     list($currentContactDetails) = CRM_Utils_Token::getTokenDetails($contactIds, NULL, FALSE, FALSE, NULL, array(), 'CRM_Event_BAO_Participant');
     foreach ($currentContactDetails as $contactId => $contactValues) {
         $contactDetails[$this->_contact_id] = $contactValues;
     }
     //send a 'cancelled' email to user, and cc the event's cc_confirm email
     $mail = CRM_Event_BAO_Participant::sendTransitionParticipantMail($this->_participant_id, $participantDetails[$this->_participant_id], $eventDetails[$this->_event_id], $contactDetails[$this->_contact_id], $domainValues, "Cancelled", "");
     $statusMsg = ts('Event registration information for %1 has been updated.', array(1 => $this->_contact_name));
     $statusMsg .= ' ' . ts('A cancellation email has been sent to %1.', array(1 => $this->_contact_email));
     CRM_Core_Session::setStatus($statusMsg, ts('Saved'), 'success');
     $url = CRM_Utils_System::url('civicrm/event/info', "reset=1&id={$this->_event_id}&noFullMsg=true");
     CRM_Utils_System::redirect($url);
 }
예제 #9
0
 /**
  * Send SMS.
  *
  * @param array $contactDetails
  * @param array $activityParams
  * @param array $smsParams
  * @param $contactIds
  * @param int $userID
  *
  * @return array
  * @throws CRM_Core_Exception
  */
 public static function sendSMS(&$contactDetails, &$activityParams, &$smsParams = array(), &$contactIds, $userID = NULL)
 {
     if ($userID == NULL) {
         $session = CRM_Core_Session::singleton();
         $userID = $session->get('userID');
     }
     $text =& $activityParams['sms_text_message'];
     // CRM-4575
     // token replacement of addressee/email/postal greetings
     // get the tokens added in subject and message
     $messageToken = CRM_Utils_Token::getTokens($text);
     // Create the meta level record first ( sms activity )
     $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type', 'SMS', 'name');
     $details = $text;
     $activitySubject = $activityParams['activity_subject'];
     $activityParams = array('source_contact_id' => $userID, 'activity_type_id' => $activityTypeID, 'activity_date_time' => date('YmdHis'), 'subject' => $activitySubject, 'details' => $details, 'status_id' => 2);
     $activity = self::create($activityParams);
     $activityID = $activity->id;
     $returnProperties = array();
     if (isset($messageToken['contact'])) {
         foreach ($messageToken['contact'] as $key => $value) {
             $returnProperties[$value] = 1;
         }
     }
     // call token hook
     $tokens = array();
     CRM_Utils_Hook::tokens($tokens);
     $categories = array_keys($tokens);
     // get token details for contacts, call only if tokens are used
     $details = array();
     if (!empty($returnProperties) || !empty($tokens)) {
         list($details) = CRM_Utils_Token::getTokenDetails($contactIds, $returnProperties, NULL, NULL, FALSE, $messageToken, 'CRM_Activity_BAO_Activity');
     }
     $success = 0;
     $escapeSmarty = FALSE;
     $errMsgs = array();
     foreach ($contactDetails as $values) {
         $contactId = $values['contact_id'];
         if (!empty($details) && is_array($details["{$contactId}"])) {
             // unset email from details since it always returns primary email address
             unset($details["{$contactId}"]['email']);
             unset($details["{$contactId}"]['email_id']);
             $values = array_merge($values, $details["{$contactId}"]);
         }
         $tokenText = CRM_Utils_Token::replaceContactTokens($text, $values, FALSE, $messageToken, FALSE, $escapeSmarty);
         $tokenText = CRM_Utils_Token::replaceHookTokens($tokenText, $values, $categories, FALSE, $escapeSmarty);
         // Only send if the phone is of type mobile
         $phoneTypes = CRM_Core_OptionGroup::values('phone_type', TRUE, FALSE, FALSE, NULL, 'name');
         if ($values['phone_type_id'] == CRM_Utils_Array::value('Mobile', $phoneTypes)) {
             $smsParams['To'] = $values['phone'];
         } else {
             $smsParams['To'] = '';
         }
         $sendResult = self::sendSMSMessage($contactId, $tokenText, $smsParams, $activityID, $userID);
         if (PEAR::isError($sendResult)) {
             // Collect all of the PEAR_Error objects
             $errMsgs[] = $sendResult;
         } else {
             $success++;
         }
     }
     // If at least one message was sent and no errors
     // were generated then return a boolean value of TRUE.
     // Otherwise, return FALSE (no messages sent) or
     // and array of 1 or more PEAR_Error objects.
     $sent = FALSE;
     if ($success > 0 && count($errMsgs) == 0) {
         $sent = TRUE;
     } elseif (count($errMsgs) > 0) {
         $sent = $errMsgs;
     }
     return array($sent, $activity->id, $success);
 }
예제 #10
0
 /**
  * process the form after the input has been submitted and validated
  *
  * @access public
  *
  * @return void
  */
 public function postProcess()
 {
     // get all the details needed to generate a receipt
     $contribIDs = implode(',', $this->_contributionIds);
     $details = CRM_Contribute_Form_Task_Status::getDetails($contribIDs);
     $baseIPN = new CRM_Core_Payment_BaseIPN();
     $message = array();
     $template = CRM_Core_Smarty::singleton();
     $params = $this->controller->exportValues($this->_name);
     $createPdf = FALSE;
     if ($params['output'] == "pdf_receipt") {
         $createPdf = TRUE;
     }
     $excludeContactIds = array();
     if (!$createPdf) {
         $returnProperties = array('email' => 1, 'do_not_email' => 1, 'is_deceased' => 1, 'on_hold' => 1);
         list($contactDetails) = CRM_Utils_Token::getTokenDetails($this->_contactIds, $returnProperties, FALSE, FALSE);
         $suppressedEmails = 0;
         foreach ($contactDetails as $id => $values) {
             if (empty($values['email']) || !empty($values['do_not_email']) || CRM_Utils_Array::value('is_deceased', $values) || !empty($values['on_hold'])) {
                 $suppressedEmails++;
                 $excludeContactIds[] = $values['contact_id'];
             }
         }
     }
     foreach ($details as $contribID => $detail) {
         $input = $ids = $objects = array();
         if (in_array($detail['contact'], $excludeContactIds)) {
             continue;
         }
         $input['component'] = $detail['component'];
         $ids['contact'] = $detail['contact'];
         $ids['contribution'] = $contribID;
         $ids['contributionRecur'] = NULL;
         $ids['contributionPage'] = NULL;
         $ids['membership'] = CRM_Utils_Array::value('membership', $detail);
         $ids['participant'] = CRM_Utils_Array::value('participant', $detail);
         $ids['event'] = CRM_Utils_Array::value('event', $detail);
         if (!$baseIPN->validateData($input, $ids, $objects, FALSE)) {
             CRM_Core_Error::fatal();
         }
         $contribution =& $objects['contribution'];
         // CRM_Core_Error::debug('o',$objects);
         // set some fake input values so we can reuse IPN code
         $input['amount'] = $contribution->total_amount;
         $input['is_test'] = $contribution->is_test;
         $input['fee_amount'] = $contribution->fee_amount;
         $input['net_amount'] = $contribution->net_amount;
         $input['trxn_id'] = $contribution->trxn_id;
         $input['trxn_date'] = isset($contribution->trxn_date) ? $contribution->trxn_date : NULL;
         // CRM_Contribute_BAO_Contribution::composeMessageArray expects mysql formatted date
         $objects['contribution']->receive_date = CRM_Utils_Date::isoToMysql($objects['contribution']->receive_date);
         // CRM_Core_Error::debug('input',$input);
         $values = array();
         $mail = $baseIPN->sendMail($input, $ids, $objects, $values, FALSE, $createPdf);
         if ($mail['html']) {
             $message[] = $mail['html'];
         } else {
             $message[] = nl2br($mail['body']);
         }
         // reset template values before processing next transactions
         $template->clearTemplateVars();
     }
     if ($createPdf) {
         CRM_Utils_PDF_Utils::html2pdf($message, 'civicrmContributionReceipt.pdf', FALSE, $params['pdf_format_id']);
         CRM_Utils_System::civiExit();
     } else {
         if ($suppressedEmails) {
             $status = ts('Email was NOT sent to %1 contacts (no email address on file, or communication preferences specify DO NOT EMAIL, or contact is deceased).', array(1 => $suppressedEmails));
             $msgTitle = ts('Email Error');
             $msgType = 'error';
         } else {
             $status = ts('Your mail has been sent.');
             $msgTitle = ts('Sent');
             $msgType = 'success';
         }
         CRM_Core_Session::setStatus($status, $msgTitle, $msgType);
     }
 }
 /**
  * Build the form
  *
  * @access public
  *
  * @return void
  */
 static function buildQuickForm(&$form)
 {
     $toArray = $ccArray = $bccArray = array();
     $suppressedEmails = 0;
     //here we are getting logged in user id as array but we need target contact id. CRM-5988
     $cid = $form->get('cid');
     if ($cid) {
         $form->_contactIds = array($cid);
     }
     $to = $form->add('text', 'to', ts('To'), '', TRUE);
     $cc = $form->add('text', 'cc_id', ts('CC'));
     $bcc = $form->add('text', 'bcc_id', ts('BCC'));
     $elements = array('cc', 'bcc');
     foreach ($elements as $element) {
         if (${$element}->getValue()) {
             preg_match_all('!"(.*?)"\\s+<\\s*(.*?)\\s*>!', ${$element}->getValue(), $matches);
             $elementValues = array();
             for ($i = 0; $i < count($matches[0]); $i++) {
                 $name = '"' . $matches[1][$i] . '" &lt;' . $matches[2][$i] . '&gt;';
                 $elementValues[] = array('name' => $name, 'id' => $matches[0][$i]);
             }
             $var = "{$element}Contact";
             $form->assign($var, json_encode($elementValues));
         }
     }
     $toSetDefault = TRUE;
     if (property_exists($form, '_context') && $form->_context == 'standalone') {
         $toSetDefault = FALSE;
     }
     // when form is submitted recompute contactIds
     $allToEmails = array();
     if ($to->getValue()) {
         $allToEmails = explode(',', $to->getValue());
         $form->_contactIds = array();
         foreach ($allToEmails as $value) {
             list($contactId, $email) = explode('::', $value);
             if ($contactId) {
                 $form->_contactIds[] = $contactId;
                 $form->_toContactEmails[] = $email;
             }
         }
         $toSetDefault = TRUE;
     }
     //get the group of contacts as per selected by user in case of Find Activities
     if (!empty($form->_activityHolderIds)) {
         $contact = $form->get('contacts');
         $form->_contactIds = $contact;
     }
     if (is_array($form->_contactIds) && $toSetDefault) {
         $returnProperties = array('sort_name' => 1, 'email' => 1, 'do_not_email' => 1, 'is_deceased' => 1, 'on_hold' => 1, 'display_name' => 1, 'preferred_mail_format' => 1);
         list($form->_contactDetails) = CRM_Utils_Token::getTokenDetails($form->_contactIds, $returnProperties, FALSE, FALSE);
         // make a copy of all contact details
         $form->_allContactDetails = $form->_contactDetails;
         foreach ($form->_contactIds as $key => $contactId) {
             $value = $form->_contactDetails[$contactId];
             if ($value['do_not_email'] || empty($value['email']) || CRM_Utils_Array::value('is_deceased', $value) || $value['on_hold']) {
                 $suppressedEmails++;
                 // unset contact details for contacts that we won't be sending email. This is prevent extra computation
                 // during token evaluation etc.
                 unset($form->_contactDetails[$contactId]);
             } else {
                 if (empty($form->_toContactEmails)) {
                     $email = $value['email'];
                 } else {
                     $email = $form->_toContactEmails[$key];
                 }
                 $toArray[] = array('name' => '"' . $value['sort_name'] . '" &lt;' . $email . '&gt;', 'id' => "{$contactId}::{$email}");
             }
         }
         if (empty($toArray)) {
             CRM_Core_Error::statusBounce(ts('Selected contact(s) do not have a valid email address, or communication preferences specify DO NOT EMAIL, or they are deceased or Primary email address is On Hold.'));
         }
     }
     $form->assign('toContact', json_encode($toArray));
     $form->assign('suppressedEmails', $suppressedEmails);
     $form->assign('totalSelectedContacts', count($form->_contactIds));
     $form->add('text', 'subject', ts('Subject'), 'size=50 maxlength=254', TRUE);
     $form->add('select', 'fromEmailAddress', ts('From'), $form->_fromEmails, TRUE);
     CRM_Mailing_BAO_Mailing::commonCompose($form);
     // add attachments
     CRM_Core_BAO_File::buildAttachment($form, NULL);
     if ($form->_single) {
         // also fix the user context stack
         if ($form->_caseId) {
             $ccid = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseContact', $form->_caseId, 'contact_id', 'case_id');
             $url = CRM_Utils_System::url('civicrm/contact/view/case', "&reset=1&action=view&cid={$ccid}&id={$form->_caseId}");
         } elseif ($form->_context) {
             $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
         } else {
             $url = CRM_Utils_System::url('civicrm/contact/view', "&show=1&action=browse&cid={$form->_contactIds[0]}&selectedChild=activity");
         }
         $session = CRM_Core_Session::singleton();
         $session->replaceUserContext($url);
         $form->addDefaultButtons(ts('Send Email'), 'upload', 'cancel');
     } else {
         $form->addDefaultButtons(ts('Send Email'), 'upload');
     }
     $form->addFormRule(array('CRM_Contact_Form_Task_EmailCommon', 'formRule'), $form);
 }
예제 #12
0
 /**
  * Run this page (figure out the action needed and perform it).
  *
  * @param int $id
  * @param int $contactID
  * @param bool $print
  * @param bool $allowID
  */
 public function run($id = NULL, $contactID = NULL, $print = TRUE, $allowID = FALSE)
 {
     if (is_numeric($id)) {
         $this->_mailingID = $id;
     } else {
         $print = TRUE;
         $this->_mailingID = CRM_Utils_Request::retrieve('id', 'String', CRM_Core_DAO::$_nullObject, TRUE);
     }
     // # CRM-7651
     // override contactID from the function level if passed in
     if (isset($contactID) && is_numeric($contactID)) {
         $this->_contactID = $contactID;
     } else {
         $session = CRM_Core_Session::singleton();
         $this->_contactID = $session->get('userID');
     }
     // mailing key check
     if (Civi::settings()->get('hash_mailing_url')) {
         $this->_mailing = new CRM_Mailing_BAO_Mailing();
         if (!is_numeric($this->_mailingID)) {
             $this->_mailing->hash = $this->_mailingID;
         } elseif (is_numeric($this->_mailingID)) {
             $this->_mailing->id = $this->_mailingID;
             // if mailing is present and associated hash is present
             // while 'hash' is not been used for mailing view : throw 'permissionDenied'
             if ($this->_mailing->find() && CRM_Core_DAO::getFieldValue('CRM_Mailing_BAO_Mailing', $this->_mailingID, 'hash', 'id') && !$allowID) {
                 CRM_Utils_System::permissionDenied();
                 return;
             }
         }
     } else {
         $this->_mailing = new CRM_Mailing_BAO_Mailing();
         $this->_mailing->id = $this->_mailingID;
     }
     if (!$this->_mailing->find(TRUE) || !$this->checkPermission()) {
         CRM_Utils_System::permissionDenied();
         return;
     }
     CRM_Mailing_BAO_Mailing::tokenReplace($this->_mailing);
     // get and format attachments
     $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_mailing', $this->_mailing->id);
     // get contact detail and compose if contact id exists
     $returnProperties = $this->_mailing->getReturnProperties();
     if (isset($this->_contactID)) {
         // get details of contact with token value including Custom Field Token Values.CRM-3734
         $params = array('contact_id' => $this->_contactID);
         $details = CRM_Utils_Token::getTokenDetails($params, $returnProperties, FALSE, TRUE, NULL, $this->_mailing->getFlattenedTokens(), get_class($this));
         $details = $details[0][$this->_contactID];
         $contactId = $this->_contactID;
     } else {
         // get tokens that are not contact specific resolved
         $params = array('contact_id' => 0);
         $details = CRM_Utils_Token::getAnonymousTokenDetails($params, $returnProperties, TRUE, TRUE, NULL, $this->_mailing->getFlattenedTokens(), get_class($this));
         $details = CRM_Utils_Array::value(0, $details[0]);
         $contactId = 0;
     }
     $mime =& $this->_mailing->compose(NULL, NULL, NULL, $contactId, $this->_mailing->from_email, $this->_mailing->from_email, TRUE, $details, $attachments);
     $title = NULL;
     if (isset($this->_mailing->body_html) && empty($_GET['text'])) {
         $header = 'text/html; charset=utf-8';
         $content = $mime->getHTMLBody();
         if (strpos($content, '<head>') === FALSE && strpos($content, '<title>') === FALSE) {
             $title = '<head><title>' . $this->_mailing->subject . '</title></head>';
         }
     } else {
         $header = 'text/plain; charset=utf-8';
         $content = $mime->getTXTBody();
     }
     CRM_Utils_System::setTitle($this->_mailing->subject);
     if (CRM_Utils_Array::value('snippet', $_GET) === 'json') {
         CRM_Core_Page_AJAX::returnJsonResponse($content);
     }
     if ($print) {
         CRM_Utils_System::setHttpHeader('Content-Type', $header);
         print $title;
         print $content;
         CRM_Utils_System::civiExit();
     } else {
         return $content;
     }
 }
 /**
  * process the form after the input has been submitted and validated
  *
  * @access public
  *
  * @return None
  */
 static function postProcess(&$form)
 {
     list($formValues, $categories, $html_message, $messageToken, $returnProperties) = self::processMessageTemplate($form);
     $skipOnHold = isset($form->skipOnHold) ? $form->skipOnHold : FALSE;
     $skipDeceased = isset($form->skipDeceased) ? $form->skipDeceased : TRUE;
     foreach ($form->_contactIds as $item => $contactId) {
         $params = array('contact_id' => $contactId);
         list($contact) = CRM_Utils_Token::getTokenDetails($params, $returnProperties, $skipOnHold, $skipDeceased, NULL, $messageToken, 'CRM_Contact_Form_Task_PDFLetterCommon');
         if (civicrm_error($contact)) {
             $notSent[] = $contactId;
             continue;
         }
         $tokenHtml = CRM_Utils_Token::replaceContactTokens($html_message, $contact[$contactId], TRUE, $messageToken);
         $tokenHtml = CRM_Utils_Token::replaceHookTokens($tokenHtml, $contact[$contactId], $categories, TRUE);
         if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) {
             $smarty = CRM_Core_Smarty::singleton();
             // also add the contact tokens to the template
             $smarty->assign_by_ref('contact', $contact);
             $tokenHtml = $smarty->fetch("string:{$tokenHtml}");
         }
         $html[] = $tokenHtml;
     }
     self::createActivities($form, $html_message, $form->_contactIds);
     CRM_Utils_PDF_Utils::html2pdf($html, "CiviLetter.pdf", FALSE, $formValues);
     $form->postProcessHook();
     CRM_Utils_System::civiExit(1);
 }
예제 #14
0
 /**
  * Send confirmation of cancellation to source participant
  *
  * return @ void
  */
 public function sendCancellation()
 {
     $domainValues = array();
     $domain = CRM_Core_BAO_Domain::getDomain();
     $tokens = array('domain' => array('name', 'phone', 'address', 'email'), 'contact' => CRM_Core_SelectValues::contactTokens());
     foreach ($tokens['domain'] as $token) {
         $domainValues[$token] = CRM_Utils_Token::getDomainTokenReplacement($token, $domain);
     }
     $participantRoles = array();
     $participantRoles = CRM_Event_PseudoConstant::participantRole();
     $participantDetails = array();
     $query = "SELECT * FROM civicrm_participant WHERE id = {$this->_from_participant_id}";
     $dao = CRM_Core_DAO::executeQuery($query);
     while ($dao->fetch()) {
         $participantDetails[$dao->id] = array('id' => $dao->id, 'role' => $participantRoles[$dao->role_id], 'is_test' => $dao->is_test, 'event_id' => $dao->event_id, 'status_id' => $dao->status_id, 'fee_amount' => $dao->fee_amount, 'contact_id' => $dao->contact_id, 'register_date' => $dao->register_date, 'registered_by_id' => $dao->registered_by_id);
     }
     $eventDetails = array();
     $eventParams = array('id' => $this->_event_id);
     CRM_Event_BAO_Event::retrieve($eventParams, $eventDetails[$this->_event_id]);
     //get default participant role.
     $eventDetails[$this->_event_id]['participant_role'] = CRM_Utils_Array::value($eventDetails[$this->_event_id]['default_role_id'], $participantRoles);
     //get the location info
     $locParams = array('entity_id' => $this->_event_id, 'entity_table' => 'civicrm_event');
     $eventDetails[$this->_event_id]['location'] = CRM_Core_BAO_Location::getValues($locParams, TRUE);
     //get contact details
     $contactIds[$this->_from_contact_id] = $this->_from_contact_id;
     list($currentContactDetails) = CRM_Utils_Token::getTokenDetails($contactIds, NULL, FALSE, FALSE, NULL, array(), 'CRM_Event_BAO_Participant');
     foreach ($currentContactDetails as $contactId => $contactValues) {
         $contactDetails[$this->_from_contact_id] = $contactValues;
     }
     //send a 'cancelled' email to user, and cc the event's cc_confirm email
     $mail = CRM_Event_BAO_Participant::sendTransitionParticipantMail($this->_from_participant_id, $participantDetails[$this->_from_participant_id], $eventDetails[$this->_event_id], $contactDetails[$this->_from_contact_id], $domainValues, "Transferred", "");
     $statusMsg = ts('Event registration information for %1 has been updated.', array(1 => $this->_contact_name));
     $statusMsg .= ' ' . ts('A cancellation email has been sent to %1.', array(1 => $this->_contact_email));
     CRM_Core_Session::setStatus($statusMsg, ts('Thanks'), 'success');
 }
 /**
  * process the form after the input has been submitted and validated
  *
  * @access public
  *
  * @return void
  */
 static function postProcess(&$form)
 {
     list($formValues, $categories, $html_message, $messageToken, $returnProperties) = self::processMessageTemplate($form);
     // update dates ?
     $receipt_update = isset($formValues['receipt_update']) ? $formValues['receipt_update'] : FALSE;
     $thankyou_update = isset($formValues['thankyou_update']) ? $formValues['thankyou_update'] : FALSE;
     $nowDate = date('YmdHis');
     $receipts = 0;
     $thanks = 0;
     $updateStatus = '';
     // skip some contacts ?
     $skipOnHold = isset($form->skipOnHold) ? $form->skipOnHold : FALSE;
     $skipDeceased = isset($form->skipDeceased) ? $form->skipDeceased : TRUE;
     foreach ($form->getVar('_contributionIds') as $item => $contributionId) {
         // get contact information
         $contactId = civicrm_api("Contribution", "getvalue", array('version' => '3', 'id' => $contributionId, 'return' => 'contact_id'));
         $params = array('contact_id' => $contactId);
         list($contact) = CRM_Utils_Token::getTokenDetails($params, $returnProperties, $skipOnHold, $skipDeceased, NULL, $messageToken, 'CRM_Contribution_Form_Task_PDFLetterCommon');
         if (civicrm_error($contact)) {
             $notSent[] = $contributionId;
             continue;
         }
         // get contribution information
         $params = array('contribution_id' => $contributionId);
         $contribution = CRM_Utils_Token::getContributionTokenDetails($params, $returnProperties, NULL, $messageToken, 'CRM_Contribution_Form_Task_PDFLetterCommon');
         if (civicrm_error($contribution)) {
             $notSent[] = $contributionId;
             continue;
         }
         $tokenHtml = CRM_Utils_Token::replaceContactTokens($html_message, $contact[$contactId], TRUE, $messageToken);
         $tokenHtml = CRM_Utils_Token::replaceContributionTokens($tokenHtml, $contribution[$contributionId], TRUE, $messageToken);
         $tokenHtml = CRM_Utils_Token::replaceHookTokens($tokenHtml, $contact[$contactId], $categories, TRUE);
         if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) {
             $smarty = CRM_Core_Smarty::singleton();
             // also add the tokens to the template
             $smarty->assign_by_ref('contact', $contact);
             $tokenHtml = $smarty->fetch("string:{$tokenHtml}");
         }
         $html[] = $tokenHtml;
         // update dates (do it for each contribution including grouped recurring contribution)
         if ($receipt_update) {
             $result = CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'receipt_date', $nowDate);
             // We can't use CRM_Core_Error::fatal here because the api error elevates the exception level. FIXME. dgg
             if ($result) {
                 $receipts++;
             }
         }
         if ($thankyou_update) {
             $result = CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'thankyou_date', $nowDate);
             // We can't use CRM_Core_Error::fatal here because the api error elevates the exception level. FIXME. dgg
             if ($result) {
                 $thanks++;
             }
         }
     }
     self::createActivities($form, $html_message, $form->_contactIds);
     CRM_Utils_PDF_Utils::html2pdf($html, "CiviLetter.pdf", FALSE, $formValues);
     $form->postProcessHook();
     if ($receipts) {
         $updateStatus = ts('Receipt date has been updated for %1 contributions.', array(1 => $receipts));
     }
     if ($thanks) {
         $updateStatus .= ' ' . ts('Thank-you date has been updated for %1 contributions.', array(1 => $thanks));
     }
     if ($updateStatus) {
         CRM_Core_Session::setStatus($updateStatus);
     }
     CRM_Utils_System::civiExit(1);
 }
예제 #16
0
 /**
  * Send an email from the specified template based on an array of params
  *
  * @param array $params  a string-keyed array of function params, see function body for details
  *
  * @return array  of four parameters: a boolean whether the email was sent, and the subject, text and HTML templates
  */
 static function sendTemplate($params)
 {
     $defaults = array('groupName' => NULL, 'valueName' => NULL, 'messageTemplateID' => NULL, 'contactId' => NULL, 'tplParams' => array(), 'from' => NULL, 'toName' => NULL, 'toEmail' => NULL, 'cc' => NULL, 'bcc' => NULL, 'replyTo' => NULL, 'attachments' => NULL, 'isTest' => FALSE, 'PDFFilename' => NULL);
     $params = array_merge($defaults, $params);
     if ((!$params['groupName'] || !$params['valueName']) && !$params['messageTemplateID']) {
         CRM_Core_Error::fatal(ts("Message template's option group and/or option value or ID missing."));
     }
     if ($params['messageTemplateID']) {
         // fetch the three elements from the db based on id
         $query = 'SELECT msg_subject subject, msg_text text, msg_html html, pdf_format_id format
                   FROM civicrm_msg_template mt
                   WHERE mt.id = %1 AND mt.is_default = 1';
         $sqlParams = array(1 => array($params['messageTemplateID'], 'String'));
     } else {
         // fetch the three elements from the db based on option_group and option_value names
         $query = 'SELECT msg_subject subject, msg_text text, msg_html html, pdf_format_id format
                   FROM civicrm_msg_template mt
                   JOIN civicrm_option_value ov ON workflow_id = ov.id
                   JOIN civicrm_option_group og ON ov.option_group_id = og.id
                   WHERE og.name = %1 AND ov.name = %2 AND mt.is_default = 1';
         $sqlParams = array(1 => array($params['groupName'], 'String'), 2 => array($params['valueName'], 'String'));
     }
     $dao = CRM_Core_DAO::executeQuery($query, $sqlParams);
     $dao->fetch();
     if (!$dao->N) {
         if ($params['messageTemplateID']) {
             CRM_Core_Error::fatal(ts('No such message template: id=%1.', array(1 => $params['messageTemplateID'])));
         } else {
             CRM_Core_Error::fatal(ts('No such message template: option group %1, option value %2.', array(1 => $params['groupName'], 2 => $params['valueName'])));
         }
     }
     $subject = $dao->subject;
     $text = $dao->text;
     $html = $dao->html;
     $format = $dao->format;
     $dao->free();
     // add the test banner (if requested)
     if ($params['isTest']) {
         $query = "SELECT msg_subject subject, msg_text text, msg_html html\n                      FROM civicrm_msg_template mt\n                      JOIN civicrm_option_value ov ON workflow_id = ov.id\n                      JOIN civicrm_option_group og ON ov.option_group_id = og.id\n                      WHERE og.name = 'msg_tpl_workflow_meta' AND ov.name = 'test_preview' AND mt.is_default = 1";
         $testDao = CRM_Core_DAO::executeQuery($query);
         $testDao->fetch();
         $subject = $testDao->subject . $subject;
         $text = $testDao->text . $text;
         $html = preg_replace('/<body(.*)$/im', "<body\\1\n{$testDao->html}", $html);
         $testDao->free();
     }
     // replace tokens in the three elements (in subject as if it was the text body)
     $domain = CRM_Core_BAO_Domain::getDomain();
     $hookTokens = array();
     $mailing = new CRM_Mailing_BAO_Mailing();
     $mailing->body_text = $text;
     $mailing->body_html = $html;
     $tokens = $mailing->getTokens();
     CRM_Utils_Hook::tokens($hookTokens);
     $categories = array_keys($hookTokens);
     $contactID = CRM_Utils_Array::value('contactId', $params);
     if ($contactID) {
         $contactParams = array('contact_id' => $contactID);
         $returnProperties = array();
         if (isset($tokens['text']['contact'])) {
             foreach ($tokens['text']['contact'] as $name) {
                 $returnProperties[$name] = 1;
             }
         }
         if (isset($tokens['html']['contact'])) {
             foreach ($tokens['html']['contact'] as $name) {
                 $returnProperties[$name] = 1;
             }
         }
         list($contact) = CRM_Utils_Token::getTokenDetails($contactParams, $returnProperties, FALSE, FALSE, NULL, CRM_Utils_Token::flattenTokens($tokens), 'CRM_Core_BAO_MessageTemplate');
         $contact = $contact[$contactID];
     }
     $subject = CRM_Utils_Token::replaceDomainTokens($subject, $domain, TRUE, $tokens['text'], TRUE);
     $text = CRM_Utils_Token::replaceDomainTokens($text, $domain, TRUE, $tokens['text'], TRUE);
     $html = CRM_Utils_Token::replaceDomainTokens($html, $domain, TRUE, $tokens['html'], TRUE);
     if ($contactID) {
         $subject = CRM_Utils_Token::replaceContactTokens($subject, $contact, FALSE, $tokens['text'], FALSE, TRUE);
         $text = CRM_Utils_Token::replaceContactTokens($text, $contact, FALSE, $tokens['text'], FALSE, TRUE);
         $html = CRM_Utils_Token::replaceContactTokens($html, $contact, FALSE, $tokens['html'], FALSE, TRUE);
         $contactArray = array($contactID => $contact);
         CRM_Utils_Hook::tokenValues($contactArray, array($contactID), NULL, CRM_Utils_Token::flattenTokens($tokens), 'CRM_Core_BAO_MessageTemplate');
         $contact = $contactArray[$contactID];
         $subject = CRM_Utils_Token::replaceHookTokens($subject, $contact, $categories, TRUE);
         $text = CRM_Utils_Token::replaceHookTokens($text, $contact, $categories, TRUE);
         $html = CRM_Utils_Token::replaceHookTokens($html, $contact, $categories, TRUE);
     }
     // strip whitespace from ends and turn into a single line
     $subject = "{strip}{$subject}{/strip}";
     // parse the three elements with Smarty
     $smarty = CRM_Core_Smarty::singleton();
     foreach ($params['tplParams'] as $name => $value) {
         $smarty->assign($name, $value);
     }
     foreach (array('subject', 'text', 'html') as $elem) {
         ${$elem} = $smarty->fetch("string:{${$elem}}");
     }
     // send the template, honouring the target user’s preferences (if any)
     $sent = FALSE;
     // create the params array
     $params['subject'] = $subject;
     $params['text'] = $text;
     $params['html'] = $html;
     if ($params['toEmail']) {
         $contactParams = array(array('email', 'LIKE', $params['toEmail'], 0, 1));
         list($contact, $_) = CRM_Contact_BAO_Query::apiQuery($contactParams);
         $prefs = array_pop($contact);
         if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] == 'HTML') {
             $params['text'] = NULL;
         }
         if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] == 'Text') {
             $params['html'] = NULL;
         }
         $config = CRM_Core_Config::singleton();
         $pdf_filename = '';
         if ($config->doNotAttachPDFReceipt && $params['PDFFilename'] && $params['html']) {
             $pdf_filename = $config->templateCompileDir . CRM_Utils_File::makeFileName($params['PDFFilename']);
             //FIXME : CRM-7894
             //xmlns attribute is required in XHTML but it is invalid in HTML,
             //Also the namespace "xmlns=http://www.w3.org/1999/xhtml" is default,
             //and will be added to the <html> tag even if you do not include it.
             $html = preg_replace('/(<html)(.+?xmlns=["\'].[^\\s]+["\'])(.+)?(>)/', '\\1\\3\\4', $params['html']);
             file_put_contents($pdf_filename, CRM_Utils_PDF_Utils::html2pdf($html, $params['PDFFilename'], TRUE, $format));
             if (empty($params['attachments'])) {
                 $params['attachments'] = array();
             }
             $params['attachments'][] = array('fullPath' => $pdf_filename, 'mime_type' => 'application/pdf', 'cleanName' => $params['PDFFilename']);
         }
         $sent = CRM_Utils_Mail::send($params);
         if ($pdf_filename) {
             unlink($pdf_filename);
         }
     }
     return array($sent, $subject, $text, $html);
 }
 public function updateGreeting()
 {
     $config = CRM_Core_Config::singleton();
     $contactType = CRM_Utils_Request::retrieve('ct', 'String', CRM_Core_DAO::$_nullArray, FALSE, NULL, 'REQUEST');
     if (!in_array($contactType, array('Individual', 'Household', 'Organization'))) {
         CRM_Core_Error::fatal(ts('Invalid Contact Type.'));
     }
     $greeting = CRM_Utils_Request::retrieve('gt', 'String', CRM_Core_DAO::$_nullArray, FALSE, NULL, 'REQUEST');
     if (!in_array($greeting, array('email_greeting', 'postal_greeting', 'addressee'))) {
         CRM_Core_Error::fatal(ts('Invalid Greeting Type.'));
     }
     if (in_array($greeting, array('email_greeting', 'postal_greeting')) && $contactType == 'Organization') {
         CRM_Core_Error::fatal(ts('You cannot use %1 for contact type %2.', array(1 => $greeting, 2 => $contactType)));
     }
     $valueID = $id = CRM_Utils_Request::retrieve('id', 'Positive', CRM_Core_DAO::$_nullArray, FALSE, NULL, 'REQUEST');
     // if valueID is not passed use default value
     if (!$valueID) {
         require_once 'CRM/Core/OptionGroup.php';
         $contactTypeFilters = array(1 => 'Individual', 2 => 'Household', 3 => 'Organization');
         $filter = CRM_Utils_Array::key($contactType, $contactTypeFilters);
         $defaulValueID = CRM_Core_OptionGroup::values($greeting, NULL, NULL, NULL, " AND is_default = 1 AND ( filter = {$filter} OR filter = 0 )", "value");
         $valueID = array_pop($defaulValueID);
     }
     $filter = array('contact_type' => $contactType, 'greeting_type' => $greeting);
     $allGreetings = CRM_Core_PseudoConstant::greeting($filter);
     $originalGreetingString = $greetingString = CRM_Utils_Array::value($valueID, $allGreetings);
     if (!$greetingString) {
         CRM_Core_Error::fatal(ts('Incorrect greeting value id %1.', array(1 => $valueID)));
     }
     // build return properties based on tokens
     require_once 'CRM/Utils/Token.php';
     $greetingTokens = CRM_Utils_Token::getTokens($greetingString);
     $tokens = CRM_Utils_Array::value('contact', $greetingTokens);
     $greetingsReturnProperties = array();
     if (is_array($tokens)) {
         $greetingsReturnProperties = array_fill_keys(array_values($tokens), 1);
     }
     //process all contacts only when force pass.
     $force = CRM_Utils_Request::retrieve('force', 'String', CRM_Core_DAO::$_nullArray, FALSE, NULL, 'REQUEST');
     $processAll = $processOnlyIdSet = FALSE;
     if (in_array($force, array(1, 'true'))) {
         $processAll = TRUE;
     } elseif ($force == 2) {
         $processOnlyIdSet = TRUE;
     }
     //FIXME : apiQuery should handle these clause.
     $filterContactFldIds = $filterIds = array();
     if (!$processAll) {
         $idFldName = $displayFldName = NULL;
         if ($greeting == 'email_greeting' || $greeting == 'postal_greeting' || $greeting == 'addressee') {
             $idFldName = $greeting . '_id';
             $displayFldName = $greeting . '_display';
         }
         if ($idFldName) {
             $sql = "\nSELECT DISTINCT id, {$idFldName}\n  FROM civicrm_contact\n WHERE contact_type = %1\n   AND ( {$idFldName} IS NULL OR\n         ( {$idFldName} IS NOT NULL AND {$displayFldName} IS NULL ) )\n   ";
             $dao = CRM_Core_DAO::executeQuery($sql, array(1 => array($contactType, 'String')));
             while ($dao->fetch()) {
                 $filterContactFldIds[$dao->id] = $dao->{$idFldName};
                 if (!CRM_Utils_System::isNull($dao->{$idFldName})) {
                     $filterIds[$dao->id] = $dao->{$idFldName};
                 }
             }
         }
         if (empty($filterContactFldIds)) {
             $filterContactFldIds[] = 0;
         }
     }
     if (empty($filterContactFldIds)) {
         return;
     }
     // retrieve only required contact information
     require_once 'CRM/Utils/Token.php';
     $extraParams[] = array('contact_type', '=', $contactType, 0, 0);
     // we do token replacement in the replaceGreetingTokens hook
     list($greetingDetails) = CRM_Utils_Token::getTokenDetails(array_keys($filterContactFldIds), $greetingsReturnProperties, FALSE, FALSE, $extraParams);
     // perform token replacement and build update SQL
     $contactIds = array();
     $cacheFieldQuery = "UPDATE civicrm_contact SET {$greeting}_display = CASE id ";
     foreach ($greetingDetails as $contactID => $contactDetails) {
         if (!$processAll && !array_key_exists($contactID, $filterContactFldIds)) {
             continue;
         }
         if ($processOnlyIdSet) {
             if (!array_key_exists($contactID, $filterIds)) {
                 continue;
             }
             if ($id) {
                 $greetingString = $originalGreetingString;
                 $contactIds[] = $contactID;
             } else {
                 if ($greetingBuffer = CRM_Utils_Array::value($filterContactFldIds[$contactID], $allGreetings)) {
                     $greetingString = $greetingBuffer;
                 }
             }
             $allContactIds[] = $contactID;
         } else {
             $greetingString = $originalGreetingString;
             if ($greetingBuffer = CRM_Utils_Array::value($filterContactFldIds[$contactID], $allGreetings)) {
                 $greetingString = $greetingBuffer;
             } else {
                 $contactIds[] = $contactID;
             }
         }
         CRM_Contact_BAO_Contact_Utils::processGreetingTemplate($greetingString, $contactDetails, $contactID, 'CRM_UpdateGreeting');
         $greetingString = CRM_Core_DAO::escapeString($greetingString);
         $cacheFieldQuery .= " WHEN {$contactID} THEN '{$greetingString}' ";
         $allContactIds[] = $contactID;
     }
     if (!empty($allContactIds)) {
         $cacheFieldQuery .= " ELSE {$greeting}_display\n                              END;";
         if (!empty($contactIds)) {
             // need to update greeting _id field.
             $queryString = "\nUPDATE civicrm_contact\n   SET {$greeting}_id = {$valueID}\n WHERE id IN (" . implode(',', $contactIds) . ")";
             CRM_Core_DAO::executeQuery($queryString);
         }
         // now update cache field
         CRM_Core_DAO::executeQuery($cacheFieldQuery);
     }
 }
예제 #18
0
 /**
  * @param array $params
  *
  * @throws Exception
  */
 public static function updateGreeting($params)
 {
     $contactType = $params['ct'];
     $greeting = $params['gt'];
     $valueID = $id = CRM_Utils_Array::value('id', $params);
     $force = CRM_Utils_Array::value('force', $params);
     $limit = CRM_Utils_Array::value('limit', $params);
     // if valueID is not passed use default value
     if (!$valueID) {
         $valueID = $id = self::defaultGreeting($contactType, $greeting);
     }
     $filter = array('contact_type' => $contactType, 'greeting_type' => $greeting);
     $allGreetings = CRM_Core_PseudoConstant::greeting($filter);
     $originalGreetingString = $greetingString = CRM_Utils_Array::value($valueID, $allGreetings);
     if (!$greetingString) {
         CRM_Core_Error::fatal(ts('Incorrect greeting value id %1, or no default greeting for this contact type and greeting type.', array(1 => $valueID)));
     }
     // build return properties based on tokens
     $greetingTokens = CRM_Utils_Token::getTokens($greetingString);
     $tokens = CRM_Utils_Array::value('contact', $greetingTokens);
     $greetingsReturnProperties = array();
     if (is_array($tokens)) {
         $greetingsReturnProperties = array_fill_keys(array_values($tokens), 1);
     }
     // Process ALL contacts only when force=1 or force=2 is passed. Else only contacts with NULL greeting or addressee value are updated.
     $processAll = $processOnlyIdSet = FALSE;
     if ($force == 1) {
         $processAll = TRUE;
     } elseif ($force == 2) {
         $processOnlyIdSet = TRUE;
     }
     //FIXME : apiQuery should handle these clause.
     $filterContactFldIds = $filterIds = array();
     $idFldName = $displayFldName = NULL;
     if (in_array($greeting, CRM_Contact_BAO_Contact::$_greetingTypes)) {
         $idFldName = $greeting . '_id';
         $displayFldName = $greeting . '_display';
     }
     if ($idFldName) {
         $queryParams = array(1 => array($contactType, 'String'));
         // if $force == 1 then update all contacts else only
         // those with NULL greeting or addressee value CRM-9476
         if ($processAll) {
             $sql = "SELECT DISTINCT id, {$idFldName} FROM civicrm_contact WHERE contact_type = %1 ";
         } else {
             $sql = "\n          SELECT DISTINCT id, {$idFldName}\n          FROM civicrm_contact\n          WHERE contact_type = %1\n          AND ({$idFldName} IS NULL\n          OR ( {$idFldName} IS NOT NULL AND ({$displayFldName} IS NULL OR {$displayFldName} = '')) )";
         }
         if ($limit) {
             $sql .= " LIMIT 0, %2";
             $queryParams += array(2 => array($limit, 'Integer'));
         }
         $dao = CRM_Core_DAO::executeQuery($sql, $queryParams);
         while ($dao->fetch()) {
             $filterContactFldIds[$dao->id] = $dao->{$idFldName};
             if (!CRM_Utils_System::isNull($dao->{$idFldName})) {
                 $filterIds[$dao->id] = $dao->{$idFldName};
             }
         }
     }
     if (empty($filterContactFldIds)) {
         $filterContactFldIds[] = 0;
     }
     // retrieve only required contact information
     $extraParams[] = array('contact_type', '=', $contactType, 0, 0);
     // we do token replacement in the replaceGreetingTokens hook
     list($greetingDetails) = CRM_Utils_Token::getTokenDetails(array_keys($filterContactFldIds), $greetingsReturnProperties, FALSE, FALSE, $extraParams);
     // perform token replacement and build update SQL
     $contactIds = array();
     $cacheFieldQuery = "UPDATE civicrm_contact SET {$greeting}_display = CASE id ";
     foreach ($greetingDetails as $contactID => $contactDetails) {
         if (!$processAll && !array_key_exists($contactID, $filterContactFldIds)) {
             continue;
         }
         if ($processOnlyIdSet && !array_key_exists($contactID, $filterIds)) {
             continue;
         }
         if ($id) {
             $greetingString = $originalGreetingString;
             $contactIds[] = $contactID;
         } else {
             if ($greetingBuffer = CRM_Utils_Array::value($filterContactFldIds[$contactID], $allGreetings)) {
                 $greetingString = $greetingBuffer;
             }
         }
         self::processGreetingTemplate($greetingString, $contactDetails, $contactID, 'CRM_UpdateGreeting');
         $greetingString = CRM_Core_DAO::escapeString($greetingString);
         $cacheFieldQuery .= " WHEN {$contactID} THEN '{$greetingString}' ";
         $allContactIds[] = $contactID;
     }
     if (!empty($allContactIds)) {
         $cacheFieldQuery .= " ELSE {$greeting}_display\n                              END;";
         if (!empty($contactIds)) {
             // need to update greeting _id field.
             // reset greeting _custom
             $resetCustomGreeting = '';
             if ($valueID != 4) {
                 $resetCustomGreeting = ", {$greeting}_custom = NULL ";
             }
             $queryString = "\nUPDATE civicrm_contact\nSET {$greeting}_id = {$valueID}\n    {$resetCustomGreeting}\nWHERE id IN (" . implode(',', $contactIds) . ")";
             CRM_Core_DAO::executeQuery($queryString);
         }
         // now update cache field
         CRM_Core_DAO::executeQuery($cacheFieldQuery);
     }
 }
예제 #19
0
/**
 * Preview mailing.
 *
 * @param array $params
 *   Array per getfields metadata.
 *
 * @return array
 * @throws \API_Exception
 */
function civicrm_api3_mailing_preview($params)
{
    civicrm_api3_verify_mandatory($params, 'CRM_Mailing_DAO_Mailing', array('id'), FALSE);
    $fromEmail = NULL;
    if (!empty($params['from_email'])) {
        $fromEmail = $params['from_email'];
    }
    $session = CRM_Core_Session::singleton();
    $mailing = new CRM_Mailing_BAO_Mailing();
    $mailing->id = $params['id'];
    $mailing->find(TRUE);
    CRM_Mailing_BAO_Mailing::tokenReplace($mailing);
    // get and format attachments
    $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_mailing', $mailing->id);
    $returnProperties = $mailing->getReturnProperties();
    $contactID = CRM_Utils_Array::value('contact_id', $params);
    if (!$contactID) {
        $contactID = $session->get('userID');
    }
    $mailingParams = array('contact_id' => $contactID);
    $details = CRM_Utils_Token::getTokenDetails($mailingParams, $returnProperties, TRUE, TRUE, NULL, $mailing->getFlattenedTokens());
    $mime =& $mailing->compose(NULL, NULL, NULL, $session->get('userID'), $fromEmail, $fromEmail, TRUE, $details[0][$contactID], $attachments);
    return civicrm_api3_create_success(array('id' => $params['id'], 'contact_id' => $contactID, 'subject' => $mime->_headers['Subject'], 'body_html' => $mime->getHTMLBody(), 'body_text' => $mime->getTXTBody()));
}
 function postProcess()
 {
     $values = $this->_contactIds;
     $config = CRM_Core_Config::singleton();
     $msg_id = $this->_submitValues['message_template'];
     if (!empty($msg_id)) {
         $mysql = " SELECT * FROM veda_civicrm_wordmailmerge WHERE msg_template_id = %1";
         $params = array(1 => array($msg_id, 'Integer'));
         $dao = CRM_Core_DAO::executeQuery($mysql, $params);
         //$dao = CRM_Core_DAO::executeQuery($mysql);
         while ($dao->fetch()) {
             $fileId = $dao->file_id;
         }
         $sql = "SELECT * FROM civicrm_file WHERE id = %1";
         $params = array(1 => array($fileId, 'Integer'));
         $dao = CRM_Core_DAO::executeQuery($sql, $params);
         //$dao = CRM_Core_DAO::executeQuery($sql);
         while ($dao->fetch()) {
             $default['fileID'] = $dao->id;
             $default['mime_type'] = $dao->mime_type;
             $default['fileName'] = $dao->uri;
             $default['cleanName'] = CRM_Utils_File::cleanFileName($dao->uri);
             $default['fullPath'] = $config->customFileUploadDir . DIRECTORY_SEPARATOR . $dao->uri;
             $default['deleteURLArgs'] = CRM_Core_BAO_File::deleteURLArgs('civicrm_file', $msg_id, $dao->id);
         }
         $defaults[$dao->id] = $default;
         $this->assign('defaults', $defaults);
         $noofContact = count($this->_contactIds);
         require_once $config->extensionsDir . '/uk.co.vedaconsulting.module.wordmailmerge/tinybutstrong/tbs_class.php';
         require_once $config->extensionsDir . '/uk.co.vedaconsulting.module.wordmailmerge/tinybutstrong-opentbs/tbs_plugin_opentbs.php';
         $TBS = new clsTinyButStrong();
         // new instance of TBS
         $TBS->Plugin(TBS_INSTALL, OPENTBS_PLUGIN);
         // load the OpenTBS plugin
         $template = $default['fullPath'];
         // contactrows to check for duplicate address
         $contactrows = array();
         foreach ($values as $key => $value) {
             $SelectedcontactID = $values[$key];
             // get the details for all selected contacts
             list($contactDetails) = CRM_Utils_Token::getTokenDetails(array($SelectedcontactID), $this->_returnProperties, NULL, NULL, FALSE, $this->_allTokens);
             // populate contactrows array to check dupliacte address
             $contactrows[$SelectedcontactID] = $contactDetails[$SelectedcontactID];
         }
         // if merge_letter_for_same_address selected check for duplicate address
         if (isset($this->_submitValues['merge_letter_for_same_address']) && $this->_submitValues['merge_letter_for_same_address']) {
             CRM_Core_BAO_Address::mergeSameAddress($contactrows);
         }
         foreach ($values as $key => $value) {
             if ($key < $noofContact) {
                 $selectedCID = $values[$key];
                 $contactFormatted = array();
                 // if contact_id found in filtered contactrows array get contact details from contactrows
                 if (array_key_exists($selectedCID, $contactrows)) {
                     $contactFormatted[$selectedCID] = $contactrows[$selectedCID];
                     $membershipFormatted = array();
                     if ($this->_searchFrom == 'member' && isset($contactFormatted[$selectedCID]['membership_id'])) {
                         $membershipFormatted = CRM_Utils_Token::getMembershipTokenDetails($contactFormatted[$selectedCID]['membership_id']);
                     }
                     foreach ($this->_tokenMerge as $atKey => $atValue) {
                         // Replace hook tokens
                         $explodedTokenName = explode('.', $atValue['token_name']);
                         // this is fixed by assigning 'address_block' token into 'contact' token array // gopi@vedaconsulting.co.uk
                         //need to do proper fix seems token named as contact.address_block
                         // $atValue['token_name'] = ($atValue['token_name'] == 'address_block') ? 'contact.'.$atValue['token_name'] : $atValue['token_name'];
                         if (array_key_exists($atValue['token_name'], $contactFormatted[$selectedCID])) {
                             if (!empty($explodedTokenName[1]) && $explodedTokenName[0] != 'contact') {
                                 $vars[$key][$explodedTokenName[0]][$explodedTokenName[1]] = $contactFormatted[$selectedCID][$atValue['token_name']];
                             } else {
                                 $vars[$key][$atValue['token_name']] = $contactFormatted[$selectedCID][$atValue['token_name']];
                             }
                         } else {
                             if ($explodedTokenName[0] == 'membership') {
                                 $explodedTokenName[1] = $explodedTokenName[1] == 'membership_id' ? 'id' : $explodedTokenName[1];
                                 $vars[$key][$explodedTokenName[0]][$explodedTokenName[1]] = CRM_Utils_Token::getMembershipTokenReplacement($explodedTokenName[1], $membershipFormatted[$contactFormatted[$selectedCID]['membership_id']]);
                             } else {
                                 $vars[$key][$atValue['token_name']] = CRM_Utils_Token::getContactTokenReplacement($atValue['token_name'], $contactFormatted[$selectedCID], FALSE, FALSE);
                             }
                         }
                         //need to do proper fix, token_name.date seems not returning null value if not found
                         if ($explodedTokenName[0] == 'token_name' && !is_array($vars[$key]['token_name'])) {
                             $vars[$key][$atValue['token_name']] = '';
                         }
                     }
                     //to skip error, if by chance using membership token in 'find contact' search
                     if ($this->_searchFrom != 'member') {
                         foreach (CRM_Core_SelectValues::membershipTokens() as $token => $label) {
                             $token = str_replace(array('{', '}'), "", $token);
                             $tokenNames = explode('.', $token);
                             $vars[$key]['membership'][$tokenNames[1]] = $label;
                         }
                     }
                     foreach ($vars[$key] as $varKey => $varValue) {
                         $explodeValues = explode('.', $varKey);
                         if (isset($explodeValues[1]) && !empty($explodeValues[1])) {
                             $vars[$key][$explodeValues[0]][$explodeValues[1]] = $vars[$key][$varKey];
                             unset($vars[$key][$varKey]);
                         }
                     }
                     // blank lines removed while creating the address_block - gopi@vedaconsulting.co.uk
                     /*if (!empty($vars[$key]['contact']['address_block'])) {
                         $vars[$key]['contact']['address_block'] = str_replace('<br />', "", $vars[$key]['contact']['address_block']);
                       }*/
                     $TBS->LoadTemplate($template, OPENTBS_ALREADY_UTF8);
                     $TBS->MergeBlock(self::TOKEN_VAR_NAME, $vars);
                 }
             }
         }
         $output_file_name = 'CiviCRMWordExport.docx';
         $TBS->Show(OPENTBS_DOWNLOAD, $output_file_name);
         CRM_Utils_System::civiExit();
     }
     parent::postProcess();
 }
예제 #21
0
 public function buildQuickForm()
 {
     $session = CRM_Core_Session::singleton();
     $this->add('text', 'test_email', ts('Send to This Address'));
     $defaults['test_email'] = $session->get('ufUniqID');
     $qfKey = $this->get('qfKey');
     $this->add('select', 'test_group', ts('Send to This Group'), array('' => ts('- none -')) + CRM_Core_PseudoConstant::group('Mailing'));
     $this->setDefaults($defaults);
     $this->add('submit', 'sendtest', ts('Send a Test Mailing'));
     $name = ts('Next');
     if (CRM_Mailing_Info::workflowEnabled()) {
         if (!CRM_Core_Permission::check('schedule mailings') && CRM_Core_Permission::check('create mailings')) {
             $name = ts('Inform Scheduler');
         }
     }
     $buttons = array(array('type' => 'back', 'name' => ts('Previous')), array('type' => 'next', 'name' => $name, 'spacing' => '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;', 'isDefault' => TRUE), array('type' => 'submit', 'name' => ts('Save & Continue Later')), array('type' => 'cancel', 'name' => ts('Cancel')));
     $this->addButtons($buttons);
     $mailingID = $this->get('mailing_id');
     $textFile = $this->get('textFile');
     $htmlFile = $this->get('htmlFile');
     $this->addFormRule(array('CRM_Mailing_Form_Test', 'testMail'), $this);
     $preview = array();
     if ($textFile) {
         $preview['text_link'] = CRM_Utils_System::url('civicrm/mailing/preview', "type=text&qfKey={$qfKey}");
     }
     if ($htmlFile) {
         $preview['html_link'] = CRM_Utils_System::url('civicrm/mailing/preview', "type=html&qfKey={$qfKey}");
     }
     $preview['attachment'] = CRM_Core_BAO_File::attachmentInfo('civicrm_mailing', $mailingID);
     $this->assign('preview', $preview);
     //Token Replacement of Subject in preview mailing
     $options = array();
     $prefix = "CRM_Mailing_Controller_Send_{$qfKey}";
     if ($this->_searchBasedMailing) {
         $prefix = "CRM_Contact_Controller_Search_{$qfKey}";
     }
     $session->getVars($options, $prefix);
     $mailing = new CRM_Mailing_BAO_Mailing();
     $mailing->id = $options['mailing_id'];
     $mailing->find(TRUE);
     $fromEmail = $mailing->from_email;
     $replyToEmail = $mailing->replyto_email;
     $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_mailing', $mailing->id);
     $returnProperties = $mailing->getReturnProperties();
     $userID = $session->get('userID');
     $params = array('contact_id' => $userID);
     $details = CRM_Utils_Token::getTokenDetails($params, $returnProperties, TRUE, TRUE, NULL, $mailing->getFlattenedTokens(), get_class($this));
     $allDetails =& $mailing->compose(NULL, NULL, NULL, $userID, $fromEmail, $fromEmail, TRUE, $details[0][$userID], $attachments);
     $this->assign('subject', $allDetails->_headers['Subject']);
 }
예제 #22
0
 /**
  * run this page (figure out the action needed and perform it).
  *
  * @return void
  */
 function run($id = NULL, $contact_id = NULL, $print = TRUE)
 {
     if (is_numeric($id)) {
         $this->_mailingID = $id;
     } else {
         $print = TRUE;
         $this->_mailingID = CRM_Utils_Request::retrieve('id', 'Integer', CRM_Core_DAO::$_nullObject, TRUE);
     }
     // # CRM-7651
     // override contactID from the function level if passed in
     if (isset($contactID) && is_numeric($contactID)) {
         $this->_contactID = $contactID;
     } else {
         $session = CRM_Core_Session::singleton();
         $this->_contactID = $session->get('userID');
     }
     $this->_mailing = new CRM_Mailing_BAO_Mailing();
     $this->_mailing->id = $this->_mailingID;
     if (!$this->_mailing->find(TRUE) || !$this->checkPermission()) {
         CRM_Utils_System::permissionDenied();
         return;
     }
     CRM_Mailing_BAO_Mailing::tokenReplace($this->_mailing);
     // get and format attachments
     $attachments =& CRM_Core_BAO_File::getEntityFile('civicrm_mailing', $this->_mailing->id);
     // get contact detail and compose if contact id exists
     if (isset($this->_contactID)) {
         //get details of contact with token value including Custom Field Token Values.CRM-3734
         $returnProperties = $this->_mailing->getReturnProperties();
         $params = array('contact_id' => $this->_contactID);
         $details = CRM_Utils_Token::getTokenDetails($params, $returnProperties, TRUE, TRUE, NULL, $this->_mailing->getFlattenedTokens(), get_class($this));
         $details = $details[0][$this->_contactID];
     } else {
         $details = array('test');
     }
     $mime =& $this->_mailing->compose(NULL, NULL, NULL, 0, $this->_mailing->from_email, $this->_mailing->from_email, TRUE, $details, $attachments);
     if (isset($this->_mailing->body_html)) {
         $header = 'Content-Type: text/html; charset=utf-8';
         $content = $mime->getHTMLBody();
     } else {
         $header = 'Content-Type: text/plain; charset=utf-8';
         $content = $mime->getTXTBody();
     }
     if ($print) {
         header($header);
         print $content;
         CRM_Utils_System::civiExit();
     } else {
         return $content;
     }
 }
예제 #23
0
 /**
  * @param $fields
  * @param $mailing
  * @param $mailer
  * @param $job_date
  * @param $attachments
  *
  * @return bool|null
  * @throws Exception
  */
 public function deliverGroup(&$fields, &$mailing, &$mailer, &$job_date, &$attachments)
 {
     static $smtpConnectionErrors = 0;
     if (!is_object($mailer) || empty($fields)) {
         CRM_Core_Error::fatal();
     }
     // get the return properties
     $returnProperties = $mailing->getReturnProperties();
     $params = $targetParams = $deliveredParams = array();
     $count = 0;
     /**
      * CRM-15702: Sending bulk sms to contacts without e-mail address fails.
      * Solution is to skip checking for on hold
      */
     $skipOnHold = TRUE;
     //do include a statement to check wether e-mail address is on hold
     if ($mailing->sms_provider_id) {
         $skipOnHold = FALSE;
         //do not include a statement to check wether e-mail address is on hold
     }
     foreach ($fields as $key => $field) {
         $params[] = $field['contact_id'];
     }
     $details = CRM_Utils_Token::getTokenDetails($params, $returnProperties, $skipOnHold, TRUE, NULL, $mailing->getFlattenedTokens(), get_class($this), $this->id);
     $config = CRM_Core_Config::singleton();
     foreach ($fields as $key => $field) {
         $contactID = $field['contact_id'];
         if (!array_key_exists($contactID, $details[0])) {
             $details[0][$contactID] = array();
         }
         /* Compose the mailing */
         $recipient = $replyToEmail = NULL;
         $replyValue = strcmp($mailing->replyto_email, $mailing->from_email);
         if ($replyValue) {
             $replyToEmail = $mailing->replyto_email;
         }
         $message =& $mailing->compose($this->id, $field['id'], $field['hash'], $field['contact_id'], $field['email'], $recipient, FALSE, $details[0][$contactID], $attachments, FALSE, NULL, $replyToEmail);
         if (empty($message)) {
             // lets keep the message in the queue
             // most likely a permissions related issue with smarty templates
             // or a bad contact id? CRM-9833
             continue;
         }
         /* Send the mailing */
         $body =& $message->get();
         $headers =& $message->headers();
         if ($mailing->sms_provider_id) {
             $provider = CRM_SMS_Provider::singleton(array('mailing_id' => $mailing->id));
             $body = $provider->getMessage($message, $field['contact_id'], $details[0][$contactID]);
             $headers = $provider->getRecipientDetails($field, $details[0][$contactID]);
         }
         // make $recipient actually be the *encoded* header, so as not to baffle Mail_RFC822, CRM-5743
         $recipient = $headers['To'];
         $result = NULL;
         // disable error reporting on real mailings (but leave error reporting for tests), CRM-5744
         if ($job_date) {
             $errorScope = CRM_Core_TemporaryErrorScope::ignoreException();
         }
         $result = $mailer->send($recipient, $headers, $body, $this->id);
         if ($job_date) {
             unset($errorScope);
         }
         if (is_a($result, 'PEAR_Error') && !$mailing->sms_provider_id) {
             // CRM-9191
             $message = $result->getMessage();
             if (strpos($message, 'Failed to write to socket') !== FALSE || strpos($message, 'Failed to set sender') !== FALSE) {
                 // lets log this message and code
                 $code = $result->getCode();
                 CRM_Core_Error::debug_log_message("SMTP Socket Error or failed to set sender error. Message: {$message}, Code: {$code}");
                 // these are socket write errors which most likely means smtp connection errors
                 // lets skip them
                 $smtpConnectionErrors++;
                 if ($smtpConnectionErrors <= 5) {
                     continue;
                 }
                 // seems like we have too many of them in a row, we should
                 // write stuff to disk and abort the cron job
                 $this->writeToDB($deliveredParams, $targetParams, $mailing, $job_date);
                 CRM_Core_Error::debug_log_message("Too many SMTP Socket Errors. Exiting");
                 CRM_Utils_System::civiExit();
             }
             /* Register the bounce event */
             $params = array('event_queue_id' => $field['id'], 'job_id' => $this->id, 'hash' => $field['hash']);
             $params = array_merge($params, CRM_Mailing_BAO_BouncePattern::match($result->getMessage()));
             CRM_Mailing_Event_BAO_Bounce::create($params);
         } elseif (is_a($result, 'PEAR_Error') && $mailing->sms_provider_id) {
             // Handle SMS errors: CRM-15426
             $job_id = intval($this->id);
             $mailing_id = intval($mailing->id);
             CRM_Core_Error::debug_log_message("Failed to send SMS message. Vars: mailing_id: {$mailing_id}, job_id: {$job_id}. Error message follows.");
             CRM_Core_Error::debug_log_message($result->getMessage());
         } else {
             /* Register the delivery event */
             $deliveredParams[] = $field['id'];
             $targetParams[] = $field['contact_id'];
             $count++;
             if ($count % CRM_Core_DAO::BULK_MAIL_INSERT_COUNT == 0) {
                 $this->writeToDB($deliveredParams, $targetParams, $mailing, $job_date);
                 $count = 0;
                 // hack to stop mailing job at run time, CRM-4246.
                 // to avoid making too many DB calls for this rare case
                 // lets do it when we snapshot
                 $status = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_MailingJob', $this->id, 'status', 'id', TRUE);
                 if ($status != 'Running') {
                     return FALSE;
                 }
             }
         }
         unset($result);
         // seems like a successful delivery or bounce, lets decrement error count
         // only if we have smtp connection errors
         if ($smtpConnectionErrors > 0) {
             $smtpConnectionErrors--;
         }
         // If we have enabled the Throttle option, this is the time to enforce it.
         if (isset($config->mailThrottleTime) && $config->mailThrottleTime > 0) {
             usleep((int) $config->mailThrottleTime);
         }
     }
     $result = $this->writeToDB($deliveredParams, $targetParams, $mailing, $job_date);
     return $result;
 }
예제 #24
0
 /**
  * Send an email from the specified template based on an array of params.
  *
  * @param array $params
  *   A string-keyed array of function params, see function body for details.
  *
  * @return array
  *   Array of four parameters: a boolean whether the email was sent, and the subject, text and HTML templates
  */
 public static function sendTemplate($params)
 {
     $defaults = array('groupName' => NULL, 'valueName' => NULL, 'messageTemplateID' => NULL, 'contactId' => NULL, 'tplParams' => array(), 'from' => NULL, 'toName' => NULL, 'toEmail' => NULL, 'cc' => NULL, 'bcc' => NULL, 'replyTo' => NULL, 'attachments' => NULL, 'isTest' => FALSE, 'PDFFilename' => NULL);
     $params = array_merge($defaults, $params);
     CRM_Utils_Hook::alterMailParams($params, 'messageTemplate');
     if ((!$params['groupName'] || !$params['valueName']) && !$params['messageTemplateID']) {
         CRM_Core_Error::fatal(ts("Message template's option group and/or option value or ID missing."));
     }
     if ($params['messageTemplateID']) {
         // fetch the three elements from the db based on id
         $query = 'SELECT msg_subject subject, msg_text text, msg_html html, pdf_format_id format
                   FROM civicrm_msg_template mt
                   WHERE mt.id = %1 AND mt.is_default = 1';
         $sqlParams = array(1 => array($params['messageTemplateID'], 'String'));
     } else {
         // fetch the three elements from the db based on option_group and option_value names
         $query = 'SELECT msg_subject subject, msg_text text, msg_html html, pdf_format_id format
                   FROM civicrm_msg_template mt
                   JOIN civicrm_option_value ov ON workflow_id = ov.id
                   JOIN civicrm_option_group og ON ov.option_group_id = og.id
                   WHERE og.name = %1 AND ov.name = %2 AND mt.is_default = 1';
         $sqlParams = array(1 => array($params['groupName'], 'String'), 2 => array($params['valueName'], 'String'));
     }
     $dao = CRM_Core_DAO::executeQuery($query, $sqlParams);
     $dao->fetch();
     if (!$dao->N) {
         if ($params['messageTemplateID']) {
             CRM_Core_Error::fatal(ts('No such message template: id=%1.', array(1 => $params['messageTemplateID'])));
         } else {
             CRM_Core_Error::fatal(ts('No such message template: option group %1, option value %2.', array(1 => $params['groupName'], 2 => $params['valueName'])));
         }
     }
     $mailContent = array('subject' => $dao->subject, 'text' => $dao->text, 'html' => $dao->html, 'format' => $dao->format);
     $dao->free();
     CRM_Utils_Hook::alterMailContent($mailContent);
     // add the test banner (if requested)
     if ($params['isTest']) {
         $query = "SELECT msg_subject subject, msg_text text, msg_html html\n                      FROM civicrm_msg_template mt\n                      JOIN civicrm_option_value ov ON workflow_id = ov.id\n                      JOIN civicrm_option_group og ON ov.option_group_id = og.id\n                      WHERE og.name = 'msg_tpl_workflow_meta' AND ov.name = 'test_preview' AND mt.is_default = 1";
         $testDao = CRM_Core_DAO::executeQuery($query);
         $testDao->fetch();
         $mailContent['subject'] = $testDao->subject . $mailContent['subject'];
         $mailContent['text'] = $testDao->text . $mailContent['text'];
         $mailContent['html'] = preg_replace('/<body(.*)$/im', "<body\\1\n{$testDao->html}", $mailContent['html']);
         $testDao->free();
     }
     // replace tokens in the three elements (in subject as if it was the text body)
     $domain = CRM_Core_BAO_Domain::getDomain();
     $hookTokens = array();
     $mailing = new CRM_Mailing_BAO_Mailing();
     $mailing->subject = $mailContent['subject'];
     $mailing->body_text = $mailContent['text'];
     $mailing->body_html = $mailContent['html'];
     $tokens = $mailing->getTokens();
     CRM_Utils_Hook::tokens($hookTokens);
     $categories = array_keys($hookTokens);
     $contactID = CRM_Utils_Array::value('contactId', $params);
     if ($contactID) {
         $contactParams = array('contact_id' => $contactID);
         $returnProperties = array();
         if (isset($tokens['subject']['contact'])) {
             foreach ($tokens['subject']['contact'] as $name) {
                 $returnProperties[$name] = 1;
             }
         }
         if (isset($tokens['text']['contact'])) {
             foreach ($tokens['text']['contact'] as $name) {
                 $returnProperties[$name] = 1;
             }
         }
         if (isset($tokens['html']['contact'])) {
             foreach ($tokens['html']['contact'] as $name) {
                 $returnProperties[$name] = 1;
             }
         }
         // @todo CRM-17253 don't resolve contact details if there are no tokens
         // effectively comment out this next (performance-expensive) line
         // but unfortunately testing is a bit think on the ground to that needs to
         // be added.
         list($contact) = CRM_Utils_Token::getTokenDetails($contactParams, $returnProperties, FALSE, FALSE, NULL, CRM_Utils_Token::flattenTokens($tokens), 'CRM_Core_BAO_MessageTemplate');
         $contact = $contact[$contactID];
     }
     $mailContent['subject'] = CRM_Utils_Token::replaceDomainTokens($mailContent['subject'], $domain, FALSE, $tokens['text'], TRUE);
     $mailContent['text'] = CRM_Utils_Token::replaceDomainTokens($mailContent['text'], $domain, FALSE, $tokens['text'], TRUE);
     $mailContent['html'] = CRM_Utils_Token::replaceDomainTokens($mailContent['html'], $domain, TRUE, $tokens['html'], TRUE);
     if ($contactID) {
         $mailContent['subject'] = CRM_Utils_Token::replaceContactTokens($mailContent['subject'], $contact, FALSE, $tokens['text'], FALSE, TRUE);
         $mailContent['text'] = CRM_Utils_Token::replaceContactTokens($mailContent['text'], $contact, FALSE, $tokens['text'], FALSE, TRUE);
         $mailContent['html'] = CRM_Utils_Token::replaceContactTokens($mailContent['html'], $contact, FALSE, $tokens['html'], FALSE, TRUE);
         $contactArray = array($contactID => $contact);
         CRM_Utils_Hook::tokenValues($contactArray, array($contactID), NULL, CRM_Utils_Token::flattenTokens($tokens), 'CRM_Core_BAO_MessageTemplate');
         $contact = $contactArray[$contactID];
         $mailContent['subject'] = CRM_Utils_Token::replaceHookTokens($mailContent['subject'], $contact, $categories, TRUE);
         $mailContent['text'] = CRM_Utils_Token::replaceHookTokens($mailContent['text'], $contact, $categories, TRUE);
         $mailContent['html'] = CRM_Utils_Token::replaceHookTokens($mailContent['html'], $contact, $categories, TRUE);
     }
     // strip whitespace from ends and turn into a single line
     $mailContent['subject'] = "{strip}{$mailContent['subject']}{/strip}";
     // parse the three elements with Smarty
     $smarty = CRM_Core_Smarty::singleton();
     foreach ($params['tplParams'] as $name => $value) {
         $smarty->assign($name, $value);
     }
     foreach (array('subject', 'text', 'html') as $elem) {
         $mailContent[$elem] = $smarty->fetch("string:{$mailContent[$elem]}");
     }
     // send the template, honouring the target user’s preferences (if any)
     $sent = FALSE;
     // create the params array
     $params['subject'] = $mailContent['subject'];
     $params['text'] = $mailContent['text'];
     $params['html'] = $mailContent['html'];
     if ($params['toEmail']) {
         $contactParams = array(array('email', 'LIKE', $params['toEmail'], 0, 1));
         list($contact, $_) = CRM_Contact_BAO_Query::apiQuery($contactParams);
         $prefs = array_pop($contact);
         if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] == 'HTML') {
             $params['text'] = NULL;
         }
         if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] == 'Text') {
             $params['html'] = NULL;
         }
         $config = CRM_Core_Config::singleton();
         if (isset($params['isEmailPdf']) && $params['isEmailPdf'] == 1) {
             $pdfHtml = CRM_Contribute_BAO_ContributionPage::addInvoicePdfToEmail($params['contributionId'], $params['contactId']);
             if (empty($params['attachments'])) {
                 $params['attachments'] = array();
             }
             $params['attachments'][] = CRM_Utils_Mail::appendPDF('Invoice.pdf', $pdfHtml, $mailContent['format']);
         }
         $pdf_filename = '';
         if ($config->doNotAttachPDFReceipt && $params['PDFFilename'] && $params['html']) {
             if (empty($params['attachments'])) {
                 $params['attachments'] = array();
             }
             $params['attachments'][] = CRM_Utils_Mail::appendPDF($params['PDFFilename'], $params['html'], $mailContent['format']);
             if (isset($params['tplParams']['email_comment'])) {
                 $params['html'] = $params['tplParams']['email_comment'];
                 $params['text'] = strip_tags($params['tplParams']['email_comment']);
             }
         }
         $sent = CRM_Utils_Mail::send($params);
         if ($pdf_filename) {
             unlink($pdf_filename);
         }
     }
     return array($sent, $mailContent['subject'], $mailContent['text'], $mailContent['html']);
 }
예제 #25
0
파일: Pledge.php 프로젝트: hguru/224Civi
 public static function updatePledgeStatus($params)
 {
     $returnMessages = array();
     $sendReminders = CRM_Utils_Array::value('send_reminders', $params, FALSE);
     $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
     //unset statues that we never use for pledges
     foreach (array('Completed', 'Cancelled', 'Failed') as $statusKey) {
         if ($key = CRM_Utils_Array::key($statusKey, $allStatus)) {
             unset($allStatus[$key]);
         }
     }
     $statusIds = implode(',', array_keys($allStatus));
     $updateCnt = 0;
     $query = "\nSELECT  pledge.contact_id              as contact_id,\n        pledge.id                      as pledge_id,\n        pledge.amount                  as amount,\n        payment.scheduled_date         as scheduled_date,\n        pledge.create_date             as create_date,\n        payment.id                     as payment_id,\n        pledge.currency                as currency,\n        pledge.contribution_page_id    as contribution_page_id,\n        payment.reminder_count         as reminder_count,\n        pledge.max_reminders           as max_reminders,\n        payment.reminder_date          as reminder_date,\n        pledge.initial_reminder_day    as initial_reminder_day,\n        pledge.additional_reminder_day as additional_reminder_day,\n        pledge.status_id               as pledge_status,\n        payment.status_id              as payment_status,\n        pledge.is_test                 as is_test,\n        pledge.campaign_id             as campaign_id,\n        SUM(payment.scheduled_amount)  as amount_due,\n        ( SELECT sum(civicrm_pledge_payment.actual_amount)\n        FROM civicrm_pledge_payment\n        WHERE civicrm_pledge_payment.status_id = 1\n        AND  civicrm_pledge_payment.pledge_id = pledge.id\n        ) as amount_paid\n        FROM      civicrm_pledge pledge, civicrm_pledge_payment payment\n        WHERE     pledge.id = payment.pledge_id\n        AND     payment.status_id IN ( {$statusIds} ) AND pledge.status_id IN ( {$statusIds} )\n        GROUP By  payment.id\n        ";
     $dao = CRM_Core_DAO::executeQuery($query);
     $now = date('Ymd');
     $pledgeDetails = $contactIds = $pledgePayments = $pledgeStatus = array();
     while ($dao->fetch()) {
         $checksumValue = CRM_Contact_BAO_Contact_Utils::generateChecksum($dao->contact_id);
         $pledgeDetails[$dao->payment_id] = array('scheduled_date' => $dao->scheduled_date, 'amount_due' => $dao->amount_due, 'amount' => $dao->amount, 'amount_paid' => $dao->amount_paid, 'create_date' => $dao->create_date, 'contact_id' => $dao->contact_id, 'pledge_id' => $dao->pledge_id, 'checksumValue' => $checksumValue, 'contribution_page_id' => $dao->contribution_page_id, 'reminder_count' => $dao->reminder_count, 'max_reminders' => $dao->max_reminders, 'reminder_date' => $dao->reminder_date, 'initial_reminder_day' => $dao->initial_reminder_day, 'additional_reminder_day' => $dao->additional_reminder_day, 'pledge_status' => $dao->pledge_status, 'payment_status' => $dao->payment_status, 'is_test' => $dao->is_test, 'currency' => $dao->currency, 'campaign_id' => $dao->campaign_id);
         $contactIds[$dao->contact_id] = $dao->contact_id;
         $pledgeStatus[$dao->pledge_id] = $dao->pledge_status;
         if (CRM_Utils_Date::overdue(CRM_Utils_Date::customFormat($dao->scheduled_date, '%Y%m%d'), $now) && $dao->payment_status != array_search('Overdue', $allStatus)) {
             $pledgePayments[$dao->pledge_id][$dao->payment_id] = $dao->payment_id;
         }
     }
     // process the updating script...
     foreach ($pledgePayments as $pledgeId => $paymentIds) {
         // 1. update the pledge /pledge payment status. returns new status when an update happens
         $returnMessages[] = "Checking if status update is needed for Pledge Id: {$pledgeId} (current status is {$allStatus[$pledgeStatus[$pledgeId]]})";
         $newStatus = CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeId, $paymentIds, array_search('Overdue', $allStatus), NULL, 0, FALSE, TRUE);
         if ($newStatus != $pledgeStatus[$pledgeId]) {
             $returnMessages[] = "- status updated to: {$allStatus[$newStatus]}";
             $updateCnt += 1;
         }
     }
     if ($sendReminders) {
         // retrieve domain tokens
         $domain = CRM_Core_BAO_Domain::getDomain();
         $tokens = array('domain' => array('name', 'phone', 'address', 'email'), 'contact' => CRM_Core_SelectValues::contactTokens());
         $domainValues = array();
         foreach ($tokens['domain'] as $token) {
             $domainValues[$token] = CRM_Utils_Token::getDomainTokenReplacement($token, $domain);
         }
         //get the domain email address, since we don't carry w/ object.
         $domainValue = CRM_Core_BAO_Domain::getNameAndEmail();
         $domainValues['email'] = $domainValue[1];
         // retrieve contact tokens
         // this function does NOT return Deceased contacts since we don't want to send them email
         list($contactDetails) = CRM_Utils_Token::getTokenDetails($contactIds, NULL, FALSE, FALSE, NULL, $tokens, 'CRM_UpdatePledgeRecord');
         // assign domain values to template
         $template = CRM_Core_Smarty::singleton();
         $template->assign('domain', $domainValues);
         //set receipt from
         $receiptFrom = '"' . $domainValues['name'] . '" <' . $domainValues['email'] . '>';
         foreach ($pledgeDetails as $paymentId => $details) {
             if (array_key_exists($details['contact_id'], $contactDetails)) {
                 $contactId = $details['contact_id'];
                 $pledgerName = $contactDetails[$contactId]['display_name'];
             } else {
                 continue;
             }
             if (empty($details['reminder_date'])) {
                 $nextReminderDate = new DateTime($details['scheduled_date']);
                 $nextReminderDate->modify("-" . $details['initial_reminder_day'] . "day");
                 $nextReminderDate = $nextReminderDate->format("Ymd");
             } else {
                 $nextReminderDate = new DateTime($details['reminder_date']);
                 $nextReminderDate->modify("+" . $details['additional_reminder_day'] . "day");
                 $nextReminderDate = $nextReminderDate->format("Ymd");
             }
             if ($details['reminder_count'] < $details['max_reminders'] && $nextReminderDate <= $now) {
                 $toEmail = $doNotEmail = $onHold = NULL;
                 if (!empty($contactDetails[$contactId]['email'])) {
                     $toEmail = $contactDetails[$contactId]['email'];
                 }
                 if (!empty($contactDetails[$contactId]['do_not_email'])) {
                     $doNotEmail = $contactDetails[$contactId]['do_not_email'];
                 }
                 if (!empty($contactDetails[$contactId]['on_hold'])) {
                     $onHold = $contactDetails[$contactId]['on_hold'];
                 }
                 // 2. send acknowledgement mail
                 if ($toEmail && !($doNotEmail || $onHold)) {
                     //assign value to template
                     $template->assign('amount_paid', $details['amount_paid'] ? $details['amount_paid'] : 0);
                     $template->assign('contact', $contactDetails[$contactId]);
                     $template->assign('next_payment', $details['scheduled_date']);
                     $template->assign('amount_due', $details['amount_due']);
                     $template->assign('checksumValue', $details['checksumValue']);
                     $template->assign('contribution_page_id', $details['contribution_page_id']);
                     $template->assign('pledge_id', $details['pledge_id']);
                     $template->assign('scheduled_payment_date', $details['scheduled_date']);
                     $template->assign('amount', $details['amount']);
                     $template->assign('create_date', $details['create_date']);
                     $template->assign('currency', $details['currency']);
                     list($mailSent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate(array('groupName' => 'msg_tpl_workflow_pledge', 'valueName' => 'pledge_reminder', 'contactId' => $contactId, 'from' => $receiptFrom, 'toName' => $pledgerName, 'toEmail' => $toEmail));
                     // 3. update pledge payment details
                     if ($mailSent) {
                         CRM_Pledge_BAO_PledgePayment::updateReminderDetails($paymentId);
                         $activityType = 'Pledge Reminder';
                         $activityParams = array('subject' => $subject, 'source_contact_id' => $contactId, 'source_record_id' => $paymentId, 'assignee_contact_id' => $contactId, 'activity_type_id' => CRM_Core_OptionGroup::getValue('activity_type', $activityType, 'name'), 'activity_date_time' => CRM_Utils_Date::isoToMysql($now), 'due_date_time' => CRM_Utils_Date::isoToMysql($details['scheduled_date']), 'is_test' => $details['is_test'], 'status_id' => 2, 'campaign_id' => $details['campaign_id']);
                         if (is_a(civicrm_api('activity', 'create', $activityParams), 'CRM_Core_Error')) {
                             $returnMessages[] = "Failed creating Activity for acknowledgment";
                             return array('is_error' => 1, 'message' => $returnMessages);
                         }
                         $returnMessages[] = "Payment reminder sent to: {$pledgerName} - {$toEmail}";
                     }
                 }
             }
         }
         // end foreach on $pledgeDetails
     }
     // end if ( $sendReminders )
     $returnMessages[] = "{$updateCnt} records updated.";
     return array('is_error' => 0, 'messages' => implode("\n\r", $returnMessages));
 }
예제 #26
0
 /**
  * Build the form object.
  *
  * @param CRM_Core_Form $form
  */
 public static function buildQuickForm(&$form)
 {
     $toArray = $ccArray = $bccArray = array();
     $suppressedEmails = 0;
     //here we are getting logged in user id as array but we need target contact id. CRM-5988
     $cid = $form->get('cid');
     if ($cid) {
         $form->_contactIds = explode(',', $cid);
     }
     if (count($form->_contactIds) > 1) {
         $form->_single = FALSE;
     }
     $emailAttributes = array('class' => 'huge');
     $to = $form->add('text', 'to', ts('To'), $emailAttributes, TRUE);
     $cc = $form->add('text', 'cc_id', ts('CC'), $emailAttributes);
     $bcc = $form->add('text', 'bcc_id', ts('BCC'), $emailAttributes);
     $setDefaults = TRUE;
     if (property_exists($form, '_context') && $form->_context == 'standalone') {
         $setDefaults = FALSE;
     }
     $elements = array('to', 'cc', 'bcc');
     $form->_allContactIds = $form->_toContactIds = $form->_contactIds;
     foreach ($elements as $element) {
         if (${$element}->getValue()) {
             $allEmails = explode(',', ${$element}->getValue());
             if ($element == 'to') {
                 $form->_toContactIds = $form->_contactIds = array();
             }
             foreach ($allEmails as $value) {
                 list($contactId, $email) = explode('::', $value);
                 if ($contactId) {
                     switch ($element) {
                         case 'to':
                             $form->_contactIds[] = $form->_toContactIds[] = $contactId;
                             $form->_toContactEmails[] = $email;
                             break;
                         case 'cc':
                             $form->_ccContactIds[] = $contactId;
                             break;
                         case 'bcc':
                             $form->_bccContactIds[] = $contactId;
                             break;
                     }
                     $form->_allContactIds[] = $contactId;
                 }
             }
             $setDefaults = TRUE;
         }
     }
     //get the group of contacts as per selected by user in case of Find Activities
     if (!empty($form->_activityHolderIds)) {
         $contact = $form->get('contacts');
         $form->_allContactIds = $form->_contactIds = $contact;
     }
     // check if we need to setdefaults and check for valid contact emails / communication preferences
     if (is_array($form->_allContactIds) && $setDefaults) {
         $returnProperties = array('sort_name' => 1, 'email' => 1, 'do_not_email' => 1, 'is_deceased' => 1, 'on_hold' => 1, 'display_name' => 1, 'preferred_mail_format' => 1);
         // get the details for all selected contacts ( to, cc and bcc contacts )
         list($form->_contactDetails) = CRM_Utils_Token::getTokenDetails($form->_allContactIds, $returnProperties, FALSE, FALSE);
         // make a copy of all contact details
         $form->_allContactDetails = $form->_contactDetails;
         // perform all validations
         foreach ($form->_allContactIds as $key => $contactId) {
             $value = $form->_contactDetails[$contactId];
             if ($value['do_not_email'] || empty($value['email']) || !empty($value['is_deceased']) || $value['on_hold']) {
                 $suppressedEmails++;
                 // unset contact details for contacts that we won't be sending email. This is prevent extra computation
                 // during token evaluation etc.
                 unset($form->_contactDetails[$contactId]);
             } else {
                 $email = $value['email'];
                 // build array's which are used to setdefaults
                 if (in_array($contactId, $form->_toContactIds)) {
                     $form->_toContactDetails[$contactId] = $form->_contactDetails[$contactId];
                     // If a particular address has been specified as the default, use that instead of contact's primary email
                     if (!empty($form->_toEmail) && $form->_toEmail['contact_id'] == $contactId) {
                         $email = $form->_toEmail['email'];
                     }
                     $toArray[] = array('text' => '"' . $value['sort_name'] . '" <' . $email . '>', 'id' => "{$contactId}::{$email}");
                 } elseif (in_array($contactId, $form->_ccContactIds)) {
                     $ccArray[] = array('text' => '"' . $value['sort_name'] . '" <' . $email . '>', 'id' => "{$contactId}::{$email}");
                 } elseif (in_array($contactId, $form->_bccContactIds)) {
                     $bccArray[] = array('text' => '"' . $value['sort_name'] . '" <' . $email . '>', 'id' => "{$contactId}::{$email}");
                 }
             }
         }
         if (empty($toArray)) {
             CRM_Core_Error::statusBounce(ts('Selected contact(s) do not have a valid email address, or communication preferences specify DO NOT EMAIL, or they are deceased or Primary email address is On Hold.'));
         }
     }
     $form->assign('toContact', json_encode($toArray));
     $form->assign('ccContact', json_encode($ccArray));
     $form->assign('bccContact', json_encode($bccArray));
     $form->assign('suppressedEmails', $suppressedEmails);
     $form->assign('totalSelectedContacts', count($form->_contactIds));
     $form->add('text', 'subject', ts('Subject'), 'size=50 maxlength=254', TRUE);
     $form->add('select', 'fromEmailAddress', ts('From'), $form->_fromEmails, TRUE, array('class' => 'crm-select2 huge'));
     CRM_Mailing_BAO_Mailing::commonCompose($form);
     // add attachments
     CRM_Core_BAO_File::buildAttachment($form, NULL);
     if ($form->_single) {
         // also fix the user context stack
         if ($form->_caseId) {
             $ccid = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseContact', $form->_caseId, 'contact_id', 'case_id');
             $url = CRM_Utils_System::url('civicrm/contact/view/case', "&reset=1&action=view&cid={$ccid}&id={$form->_caseId}");
         } elseif ($form->_context) {
             $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
         } else {
             $url = CRM_Utils_System::url('civicrm/contact/view', "&show=1&action=browse&cid={$form->_contactIds[0]}&selectedChild=activity");
         }
         $session = CRM_Core_Session::singleton();
         $session->replaceUserContext($url);
         $form->addDefaultButtons(ts('Send Email'), 'upload', 'cancel');
     } else {
         $form->addDefaultButtons(ts('Send Email'), 'upload');
     }
     $fields = array('followup_assignee_contact_id' => array('type' => 'entityRef', 'label' => ts('Assigned to'), 'attributes' => array('multiple' => TRUE, 'create' => TRUE, 'api' => array('params' => array('is_deceased' => 0)))), 'followup_activity_type_id' => array('type' => 'select', 'label' => ts('Followup Activity'), 'attributes' => array('' => '- ' . ts('select activity') . ' -') + CRM_Core_PseudoConstant::ActivityType(FALSE), 'extra' => array('class' => 'crm-select2')), 'followup_activity_subject' => array('type' => 'text', 'label' => ts('Subject'), 'attributes' => CRM_Core_DAO::getAttribute('CRM_Activity_DAO_Activity', 'subject')));
     //add followup date
     $form->addDateTime('followup_date', ts('in'), FALSE, array('formatType' => 'activityDateTime'));
     foreach ($fields as $field => $values) {
         if (!empty($fields[$field])) {
             $attribute = CRM_Utils_Array::value('attributes', $values);
             $required = !empty($values['required']);
             if ($values['type'] == 'select' && empty($attribute)) {
                 $form->addSelect($field, array('entity' => 'activity'), $required);
             } elseif ($values['type'] == 'entityRef') {
                 $form->addEntityRef($field, $values['label'], $attribute, $required);
             } else {
                 $form->add($values['type'], $field, $values['label'], $attribute, $required, CRM_Utils_Array::value('extra', $values));
             }
         }
     }
     $form->addFormRule(array('CRM_Contact_Form_Task_EmailCommon', 'formRule'), $form);
     CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'templates/CRM/Contact/Form/Task/EmailCommon.js', 0, 'html-header');
 }
예제 #27
0
 /**
  * Function takes participant ids and statuses
  * update status from $fromStatusId to $toStatusId
  * and send mail + create activities.
  *
  * @param array $participantIds
  *   Participant ids.
  * @param int $toStatusId
  *   Update status id.
  * @param int $fromStatusId
  *   From status id.
  * @param bool $returnResult
  * @param bool $skipCascadeRule
  *
  * @return array|NULL
  */
 public static function transitionParticipants($participantIds, $toStatusId, $fromStatusId = NULL, $returnResult = FALSE, $skipCascadeRule = FALSE)
 {
     if (!is_array($participantIds) || empty($participantIds) || !$toStatusId) {
         return NULL;
     }
     //thumb rule is if we triggering  primary participant need to triggered additional
     $allParticipantIds = $primaryANDAdditonalIds = array();
     foreach ($participantIds as $id) {
         $allParticipantIds[] = $id;
         if (self::isPrimaryParticipant($id)) {
             //filter additional as per status transition rules, CRM-5403
             if ($skipCascadeRule) {
                 $additionalIds = self::getAdditionalParticipantIds($id);
             } else {
                 $additionalIds = self::getValidAdditionalIds($id, $fromStatusId, $toStatusId);
             }
             if (!empty($additionalIds)) {
                 $allParticipantIds = array_merge($allParticipantIds, $additionalIds);
                 $primaryANDAdditonalIds[$id] = $additionalIds;
             }
         }
     }
     //get the unique participant ids,
     $allParticipantIds = array_unique($allParticipantIds);
     //pull required participants, contacts, events  data, if not in hand
     static $eventDetails = array();
     static $domainValues = array();
     static $contactDetails = array();
     $contactIds = $eventIds = $participantDetails = array();
     $statusTypes = CRM_Event_PseudoConstant::participantStatus();
     $participantRoles = CRM_Event_PseudoConstant::participantRole();
     $pendingStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Pending'");
     //first thing is pull all necessory data from db.
     $participantIdClause = '(' . implode(',', $allParticipantIds) . ')';
     //get all participants data.
     $query = "SELECT * FROM civicrm_participant WHERE id IN {$participantIdClause}";
     $dao = CRM_Core_DAO::executeQuery($query);
     while ($dao->fetch()) {
         $participantDetails[$dao->id] = array('id' => $dao->id, 'role' => $participantRoles[$dao->role_id], 'is_test' => $dao->is_test, 'event_id' => $dao->event_id, 'status_id' => $dao->status_id, 'fee_amount' => $dao->fee_amount, 'contact_id' => $dao->contact_id, 'register_date' => $dao->register_date, 'registered_by_id' => $dao->registered_by_id);
         if (!array_key_exists($dao->contact_id, $contactDetails)) {
             $contactIds[$dao->contact_id] = $dao->contact_id;
         }
         if (!array_key_exists($dao->event_id, $eventDetails)) {
             $eventIds[$dao->event_id] = $dao->event_id;
         }
     }
     //get the domain values.
     if (empty($domainValues)) {
         // making all tokens available to templates.
         $domain = CRM_Core_BAO_Domain::getDomain();
         $tokens = array('domain' => array('name', 'phone', 'address', 'email'), 'contact' => CRM_Core_SelectValues::contactTokens());
         foreach ($tokens['domain'] as $token) {
             $domainValues[$token] = CRM_Utils_Token::getDomainTokenReplacement($token, $domain);
         }
     }
     //get all required contacts detail.
     if (!empty($contactIds)) {
         // get the contact details.
         list($currentContactDetails) = CRM_Utils_Token::getTokenDetails($contactIds, NULL, FALSE, FALSE, NULL, array(), 'CRM_Event_BAO_Participant');
         foreach ($currentContactDetails as $contactId => $contactValues) {
             $contactDetails[$contactId] = $contactValues;
         }
     }
     //get all required events detail.
     if (!empty($eventIds)) {
         foreach ($eventIds as $eventId) {
             //retrieve event information
             $eventParams = array('id' => $eventId);
             CRM_Event_BAO_Event::retrieve($eventParams, $eventDetails[$eventId]);
             //get default participant role.
             $eventDetails[$eventId]['participant_role'] = CRM_Utils_Array::value($eventDetails[$eventId]['default_role_id'], $participantRoles);
             //get the location info
             $locParams = array('entity_id' => $eventId, 'entity_table' => 'civicrm_event');
             $eventDetails[$eventId]['location'] = CRM_Core_BAO_Location::getValues($locParams, TRUE);
         }
     }
     //now we are ready w/ all required data.
     //take a decision as per statuses.
     $emailType = NULL;
     $toStatus = $statusTypes[$toStatusId];
     $fromStatus = CRM_Utils_Array::value($fromStatusId, $statusTypes);
     switch ($toStatus) {
         case 'Pending from waitlist':
         case 'Pending from approval':
             switch ($fromStatus) {
                 case 'On waitlist':
                 case 'Awaiting approval':
                     $emailType = 'Confirm';
                     break;
             }
             break;
         case 'Expired':
             //no matter from where u come send expired mail.
             $emailType = $toStatus;
             break;
         case 'Cancelled':
             //no matter from where u come send cancel mail.
             $emailType = $toStatus;
             break;
     }
     //as we process additional w/ primary, there might be case if user
     //select primary as well as additionals, so avoid double processing.
     $processedParticipantIds = array();
     $mailedParticipants = array();
     //send mails and update status.
     foreach ($participantDetails as $participantId => $participantValues) {
         $updateParticipantIds = array();
         if (in_array($participantId, $processedParticipantIds)) {
             continue;
         }
         //check is it primary and has additional.
         if (array_key_exists($participantId, $primaryANDAdditonalIds)) {
             foreach ($primaryANDAdditonalIds[$participantId] as $additonalId) {
                 if ($emailType) {
                     $mail = self::sendTransitionParticipantMail($additonalId, $participantDetails[$additonalId], $eventDetails[$participantDetails[$additonalId]['event_id']], $contactDetails[$participantDetails[$additonalId]['contact_id']], $domainValues, $emailType);
                     //get the mail participant ids
                     if ($mail) {
                         $mailedParticipants[$additonalId] = $contactDetails[$participantDetails[$additonalId]['contact_id']]['display_name'];
                     }
                 }
                 $updateParticipantIds[] = $additonalId;
                 $processedParticipantIds[] = $additonalId;
             }
         }
         //now send email appropriate mail to primary.
         if ($emailType) {
             $mail = self::sendTransitionParticipantMail($participantId, $participantValues, $eventDetails[$participantValues['event_id']], $contactDetails[$participantValues['contact_id']], $domainValues, $emailType);
             //get the mail participant ids
             if ($mail) {
                 $mailedParticipants[$participantId] = $contactDetails[$participantValues['contact_id']]['display_name'];
             }
         }
         //now update status of group/one at once.
         $updateParticipantIds[] = $participantId;
         //update the register date only when we,
         //move participant to pending class, CRM-6496
         $updateRegisterDate = FALSE;
         if (array_key_exists($toStatusId, $pendingStatuses)) {
             $updateRegisterDate = TRUE;
         }
         self::updateStatus($updateParticipantIds, $toStatusId, $updateRegisterDate);
         $processedParticipantIds[] = $participantId;
     }
     //return result for cron.
     if ($returnResult) {
         $results = array('mailedParticipants' => $mailedParticipants, 'updatedParticipantIds' => $processedParticipantIds);
         return $results;
     }
 }
예제 #28
0
 /**
  * Generate the contribution array from the form, we fill in the contact details and determine any aggregation
  * around contact_id of contribution_recur_id
  *
  * @param string $groupBy
  * @param CRM_Contribute_Form_Task $form
  * @param array $returnProperties
  * @param bool $skipOnHold
  * @param bool $skipDeceased
  * @param array $messageToken
  * @param string $task
  * @param string $separator
  *
  * @return array
  */
 public static function buildContributionArray($groupBy, $form, $returnProperties, $skipOnHold, $skipDeceased, $messageToken, $task, $separator)
 {
     $contributions = $contacts = $notSent = array();
     $contributionIDs = $form->getVar('_contributionIds');
     if ($form->_includesSoftCredits) {
         //@todo - comment on what is stored there
         $contributionIDs = $form->getVar('_contributionContactIds');
     }
     foreach ($contributionIDs as $item => $contributionId) {
         // get contribution information
         $contribution = CRM_Utils_Token::getContributionTokenDetails(array('contribution_id' => $contributionId), $returnProperties, NULL, $messageToken, $task);
         $contribution = $contributions[$contributionId] = $contribution[$contributionId];
         if ($form->_includesSoftCredits) {
             //@todo find out why this happens & add comments
             list($contactID) = explode('-', $item);
             $contactID = (int) $contactID;
         } else {
             $contactID = $contribution['contact_id'];
         }
         if (!isset($contacts[$contactID])) {
             list($contact) = CRM_Utils_Token::getTokenDetails(array('contact_id' => $contactID), $returnProperties, $skipOnHold, $skipDeceased, NULL, $messageToken, $task);
             $contacts[$contactID] = $contact[$contactID];
             $contacts[$contactID]['contact_aggregate'] = 0;
             $contacts[$contactID]['combined'] = $contacts[$contactID]['contribution_ids'] = array();
         }
         $contacts[$contactID]['contact_aggregate'] += $contribution['total_amount'];
         $groupByID = empty($contribution[$groupBy]) ? 0 : $contribution[$groupBy];
         $contacts[$contactID]['contribution_ids'][$groupBy][$groupByID][$contributionId] = TRUE;
         if (!isset($contacts[$contactID]['combined'][$groupBy]) || !isset($contacts[$contactID]['combined'][$groupBy][$groupByID])) {
             $contacts[$contactID]['combined'][$groupBy][$groupByID] = $contribution;
             $contacts[$contactID]['aggregates'][$groupBy][$groupByID] = $contribution['total_amount'];
         } else {
             $contacts[$contactID]['combined'][$groupBy][$groupByID] = self::combineContributions($contacts[$contactID]['combined'][$groupBy][$groupByID], $contribution, $separator);
             $contacts[$contactID]['aggregates'][$groupBy][$groupByID] += $contribution['total_amount'];
         }
     }
     return array($contributions, $contacts);
 }
예제 #29
0
 /**
  * Declaration of common variables for Invoice and PDF.
  *
  *
  * @param array $contribIds
  *   Contribution Id.
  * @param array $params
  *   Parameter for pdf or email invoices.
  * @param array $contactIds
  *   Contact Id.
  *
  * @return array
  *   array of common elements
  *
  */
 public static function getElements($contribIds, $params, $contactIds)
 {
     $pdfElements = array();
     $pdfElements['contribIDs'] = implode(',', $contribIds);
     $pdfElements['details'] = CRM_Contribute_Form_Task_Status::getDetails($pdfElements['contribIDs']);
     $pdfElements['baseIPN'] = new CRM_Core_Payment_BaseIPN();
     $pdfElements['params'] = $params;
     $pdfElements['createPdf'] = FALSE;
     if (!empty($pdfElements['params']['output']) && ($pdfElements['params']['output'] == "pdf_invoice" || $pdfElements['params']['output'] == "pdf_receipt")) {
         $pdfElements['createPdf'] = TRUE;
     }
     $excludeContactIds = array();
     if (!$pdfElements['createPdf']) {
         $returnProperties = array('email' => 1, 'do_not_email' => 1, 'is_deceased' => 1, 'on_hold' => 1);
         list($contactDetails) = CRM_Utils_Token::getTokenDetails($contactIds, $returnProperties, FALSE, FALSE);
         $pdfElements['suppressedEmails'] = 0;
         $suppressedEmails = 0;
         foreach ($contactDetails as $id => $values) {
             if (empty($values['email']) || empty($params['override_privacy']) && !empty($values['do_not_email']) || CRM_Utils_Array::value('is_deceased', $values) || !empty($values['on_hold'])) {
                 $suppressedEmails++;
                 $pdfElements['suppressedEmails'] = $suppressedEmails;
                 $excludeContactIds[] = $values['contact_id'];
             }
         }
     }
     $pdfElements['excludeContactIds'] = $excludeContactIds;
     return $pdfElements;
 }
예제 #30
0
 /**
  * Process the form after the input has been submitted and validated.
  *
  * @param CRM_Core_Form $form
  */
 public static function postProcess(&$form)
 {
     $formValues = $form->controller->exportValues($form->getName());
     list($formValues, $categories, $html_message, $messageToken, $returnProperties) = self::processMessageTemplate($formValues);
     $buttonName = $form->controller->getButtonName();
     $skipOnHold = isset($form->skipOnHold) ? $form->skipOnHold : FALSE;
     $skipDeceased = isset($form->skipDeceased) ? $form->skipDeceased : TRUE;
     $html = $document = array();
     // CRM-16725 Skip creation of activities if user is previewing their PDF letter(s)
     if ($buttonName == '_qf_PDF_upload') {
         self::createActivities($form, $html_message, $form->_contactIds);
     }
     if (!empty($formValues['document_file_path'])) {
         list($html_message, $zip) = CRM_Utils_PDF_Document::unzipDoc($formValues['document_file_path'], $formValues['document_type']);
     }
     foreach ($form->_contactIds as $item => $contactId) {
         $caseId = NULL;
         $params = array('contact_id' => $contactId);
         list($contact) = CRM_Utils_Token::getTokenDetails($params, $returnProperties, $skipOnHold, $skipDeceased, NULL, $messageToken, 'CRM_Contact_Form_Task_PDFLetterCommon');
         if (civicrm_error($contact)) {
             $notSent[] = $contactId;
             continue;
         }
         $tokenHtml = CRM_Utils_Token::replaceContactTokens($html_message, $contact[$contactId], TRUE, $messageToken);
         if (!empty($form->_caseId)) {
             $caseId = $form->_caseId;
         }
         if (empty($caseId) && !empty($form->_caseIds[$item])) {
             $caseId = $form->_caseIds[$item];
         }
         if ($caseId) {
             $tokenHtml = CRM_Utils_Token::replaceCaseTokens($caseId, $tokenHtml, $messageToken);
         }
         $tokenHtml = CRM_Utils_Token::replaceHookTokens($tokenHtml, $contact[$contactId], $categories, TRUE);
         if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) {
             $smarty = CRM_Core_Smarty::singleton();
             // also add the contact tokens to the template
             $smarty->assign_by_ref('contact', $contact);
             $tokenHtml = $smarty->fetch("string:{$tokenHtml}");
         }
         $html[] = $tokenHtml;
     }
     $type = $formValues['document_type'];
     if ($type == 'pdf') {
         CRM_Utils_PDF_Utils::html2pdf($html, "CiviLetter.pdf", FALSE, $formValues);
     } elseif (!empty($formValues['document_file_path'])) {
         CRM_Utils_PDF_Document::printDocuments($formValues['document_file_path'], $html, $type, $zip);
     } else {
         CRM_Utils_PDF_Document::html2doc($html, "CiviLetter.{$type}", $formValues);
     }
     $form->postProcessHook();
     CRM_Utils_System::civiExit(1);
 }