/**
  * send the message to all the contacts and also insert a
  * contact activity in each contacts record
  *
  * @param array  $contactIds   the array of contact ids to send the email
  * @param string $subject      the subject of the message
  * @param string $message      the message contents
  * @param string $emailAddress use this 'to' email address instead of the default Primary address
  *
  * @return array             (total, added, notAdded) count of emails sent
  * @access public
  * @static
  */
 function sendEmail(&$contactIds, &$subject, &$message, $emailAddress)
 {
     $session =& CRM_Core_Session::singleton();
     $userID = $session->get('userID');
     list($fromDisplayName, $fromEmail, $fromDoNotEmail) = CRM_Contact_BAO_Contact::getContactDetails($userID);
     if (!$fromEmail) {
         return array(count($contactIds), 0, count($contactIds));
     }
     if (!trim($fromDisplayName)) {
         $fromDisplayName = $fromEmail;
     }
     $from = CRM_Utils_Mail::encodeAddressHeader($fromDisplayName, $fromEmail);
     // create the meta level record first
     $email =& new CRM_Core_BAO_EmailHistory();
     $email->subject = $subject;
     $email->message = $message;
     $email->contact_id = $userID;
     $email->sent_date = date('Ymd');
     $email->save();
     $sent = $notSent = 0;
     foreach ($contactIds as $contactId) {
         if (CRM_Core_BAO_EmailHistory::sendMessage($from, $contactId, $subject, $message, $emailAddress, $email->id)) {
             $sent++;
         } else {
             $notSent++;
         }
     }
     return array(count($contactIds), $sent, $notSent);
 }
Esempio n. 2
0
 /**
  * process the form after the input has been submitted and validated
  *
  * @access public
  * @return None
  */
 function postProcess()
 {
     $emailAddress = null;
     if ($this->_single) {
         $emailAddress = $this->controller->exportValue('Email', 'to');
     }
     if ($this->_noEmails) {
         $emailAddress = $this->controller->exportValue('Email', 'emailAddress');
         // for adding the email-id to the primary address
         $cid = CRM_Utils_Request::retrieve('cid', $this, false);
         if ($cid) {
             $location =& CRM_Contact_BAO_Contact::getEmailDetails($cid);
             if ($location[3]) {
                 $locationID = $location[3];
                 $email =& new CRM_Core_DAO_Email();
                 $email->location_id = $locationID;
                 $email->is_primary = 1;
                 $email->email = $emailAddress;
                 $email->save();
             } else {
                 require_once 'CRM/Core/BAO/LocationType.php';
                 $ids = $params = $locID = array();
                 $params['contact_id'] = $cid;
                 $locType = CRM_Core_BAO_LocationType::getDefault();
                 $params['location'][1]['location_type_id'] = $locType->id;
                 $params['location'][1]['is_primary'] = 1;
                 $params['location'][1]['email'][1]['email'] = $emailAddress;
                 CRM_Core_BAO_Location::add($params, $ids, 1);
             }
         }
     }
     $subject = $this->controller->exportValue('Email', 'subject');
     $message = $this->controller->exportValue('Email', 'message');
     require_once 'CRM/Core/BAO/EmailHistory.php';
     list($total, $sent, $notSent) = CRM_Core_BAO_EmailHistory::sendEmail($this->_contactIds, $subject, $message, $emailAddress);
     $status = array('', ts('Total Selected Contact(s): %1', array(1 => $total)));
     if ($sent) {
         $status[] = ts('Email sent to contact(s): %1', array(1 => $sent));
     }
     if ($notSent) {
         $status[] = ts('Email not sent to contact(s) (no email address on file or communication preferences specify DO NOT EMAIL): %1', array(1 => $notSent));
     }
     CRM_Core_Session::setStatus($status);
 }
Esempio n. 3
0
 /**
  * Delete a contact and all its associated records
  * 
  * @param  int  $id id of the contact to delete
  *
  * @return boolean true if contact deleted, false otherwise
  * @access public
  * @static
  */
 function deleteContact($id)
 {
     require_once 'CRM/Core/BAO/EmailHistory.php';
     require_once 'CRM/Core/BAO/Meeting.php';
     require_once 'CRM/Core/BAO/Phonecall.php';
     // make sure we have edit permission for this contact
     // before we delete
     if (!CRM_Contact_BAO_Contact::permissionedContact($id, CRM_CORE_PERMISSION_EDIT)) {
         return false;
     }
     require_once 'CRM/Utils/Hook.php';
     $contact =& new CRM_Contact_DAO_Contact();
     $contact->id = $id;
     if (!$contact->find(true)) {
         return false;
     }
     $contactType = $contact->contact_type;
     CRM_Utils_Hook::pre('delete', $contactType, $id);
     CRM_Core_DAO::transaction('BEGIN');
     // do a top down deletion
     CRM_Mailing_Event_BAO_Subscribe::deleteContact($id);
     CRM_Contact_BAO_GroupContact::deleteContact($id);
     CRM_Contact_BAO_SubscriptionHistory::deleteContact($id);
     CRM_Contact_BAO_Relationship::deleteContact($id);
     CRM_Contribute_BAO_Contribution::deleteContact($id);
     CRM_Core_BAO_Note::deleteContact($id);
     CRM_Core_DAO::deleteEntityContact('CRM_Core_DAO_CustomValue', $id);
     CRM_Core_DAO::deleteEntityContact('CRM_Core_DAO_ActivityHistory', $id);
     require_once 'CRM/Core/BAO/UFMatch.php';
     CRM_Core_BAO_UFMatch::deleteContact($id);
     // need to remove them from email, meeting and phonecall
     CRM_Core_BAO_EmailHistory::deleteContact($id);
     CRM_Core_BAO_Meeting::deleteContact($id);
     CRM_Core_BAO_Phonecall::deleteContact($id);
     // location shld be deleted after phonecall, since fields in phonecall are
     // fkeyed into location/phone.
     CRM_Core_BAO_Location::deleteContact($id);
     // fix household and org primary contact ids
     foreach ($GLOBALS['_CRM_CONTACT_BAO_CONTACT']['misc'] as $name) {
         require_once str_replace('_', DIRECTORY_SEPARATOR, "CRM_Contact_DAO_" . $name) . ".php";
         eval('$object =& new CRM_Contact_DAO_' . $name . '( );');
         $object->primary_contact_id = $id;
         $object->find();
         while ($object->fetch()) {
             // we need to set this to null explicitly
             $object->primary_contact_id = 'null';
             $object->save();
         }
     }
     require_once str_replace('_', DIRECTORY_SEPARATOR, "CRM_Contact_BAO_" . $contactType) . ".php";
     eval('$object =& new CRM_Contact_BAO_' . $contactType . '( );');
     $object->contact_id = $contact->id;
     $object->delete();
     $contact->delete();
     //delete the contact id from recently view
     CRM_Utils_Recent::del($id);
     CRM_Core_DAO::transaction('COMMIT');
     CRM_Utils_Hook::post('delete', $contactType, $contact->id, $contact);
     return true;
 }