public function action_sendinvitemails() { global $db; global $sugar_config; global $mod_strings; $id = $_GET['record']; //get event $event = new FP_events(); $event->retrieve($id); $event->load_relationship('fp_events_contacts'); // get related contacts $event->load_relationship('fp_events_prospects_1'); //get related targets $event->load_relationship('fp_events_leads_1'); //get related leads //Count the number of delegates linked to the event that have not yet been invited $query = "SELECT * FROM fp_events_contacts_c WHERE fp_events_contactsfp_events_ida='" . $event->id . "' AND (invite_status='Not Invited' OR invite_status='' OR invite_status IS NULL) AND deleted='0'"; $result = $db->query($query); $contact_count = $db->getRowCount($result); //count contacts $query = "SELECT * FROM fp_events_prospects_1_c WHERE fp_events_prospects_1fp_events_ida='" . $event->id . "' AND (invite_status='Not Invited' OR invite_status='' OR invite_status IS NULL) AND deleted='0'"; $result = $db->query($query); $prospect_count = $db->getRowCount($result); //count targets $query = "SELECT * FROM fp_events_leads_1_c WHERE fp_events_leads_1fp_events_ida='" . $event->id . "' AND (invite_status='Not Invited' OR invite_status='' OR invite_status IS NULL) AND deleted='0'"; $result = $db->query($query); $lead_count = $db->getRowCount($result); //count leads $delegate_count = $contact_count + $prospect_count + $lead_count; //Total up delegates $invite_count = 0; //used to count the number of emails sent $error_count = 0; //used to count the number of failed email attempts //loop through related contacts foreach ($event->fp_events_contacts->getBeans() as $contact) { //Get accept status of contact $query = 'SELECT invite_status FROM fp_events_contacts_c WHERE fp_events_contactsfp_events_ida="' . $event->id . '" AND fp_events_contactscontacts_idb="' . $contact->id . '"'; $status = $db->getOne($query); if ($status == null || $status == '' || $status == 'Not Invited') { $invite_count++; //set email links $event->link = "<a href='" . $sugar_config['site_url'] . "/index.php?entryPoint=responseEntryPoint&event=" . $event->id . "&delegate=" . $contact->id . "&type=c&response=accept'>Accept</a>"; $event->link_declined = "<a href='" . $sugar_config['site_url'] . "/index.php?entryPoint=responseEntryPoint&event=" . $event->id . "&delegate=" . $contact->id . "&type=c&response=decline'>Decline</a>"; //Get the TO name and e-mail address for the message $rcpt_name = $contact->first_name . ' ' . $contact->last_name; $rcpt_email = $contact->email1; $emailTemp = new EmailTemplate(); $emailTemp->disable_row_level_security = true; $emailTemp->retrieve($event->invite_templates); //Use the ID value of the email template record //check email template is set, if not return error if ($emailTemp->id == '') { SugarApplication::appendErrorMessage($mod_strings['LBL_ERROR_MSG_5']); SugarApplication::redirect("index.php?module=FP_events&return_module=FP_events&action=DetailView&record=" . $event->id); die; } //parse the lead varibales first $firstpass = $emailTemp->parse_template_bean($emailTemp->body_html, 'Contacts', $contact); $email_subject = $emailTemp->parse_template_bean($emailTemp->subject, 'FP_events', $event); $email_body = from_html($emailTemp->parse_template_bean($firstpass, 'FP_events', $event)); $alt_emailbody = wordwrap($emailTemp->parse_template_bean($firstpass, 'FP_events', $event), 900); //get attachments $attachmentBean = new Note(); $attachment_list = $attachmentBean->get_full_list('', "parent_type = 'Emails' AND parent_id = '" . $event->invite_templates . "'"); $attachments = array(); if ($attachment_list != null) { foreach ($attachment_list as $attachment) { $attachments[] = $attachment; } } //send the email $send_invite = $this->sendEmail($rcpt_email, $email_subject, $rcpt_name, $email_body, $alt_emailbody, $contact, $attachments); //Send the message, log if error occurs if (!$send_invite) { $GLOBALS['log']->fatal('ERROR: Invite email failed to send to: ' . $rcpt_name . ' at ' . $rcpt_email); $error_count++; } else { //update contact to invites $query = 'UPDATE fp_events_contacts_c SET invite_status="Invited" WHERE fp_events_contactsfp_events_ida="' . $event->id . '" AND fp_events_contactscontacts_idb="' . $contact->id . '"'; $res = $db->query($query); } } } //loop through related targets foreach ($event->fp_events_prospects_1->getBeans() as $target) { //Get accept status of contact $query = 'SELECT invite_status FROM fp_events_prospects_1_c WHERE fp_events_prospects_1fp_events_ida="' . $event->id . '" AND fp_events_prospects_1prospects_idb="' . $target->id . '"'; $status = $db->getOne($query); if ($status == null || $status == '' || $status == 'Not Invited') { $invite_count++; //set email links $event->link = "<a href='" . $sugar_config['site_url'] . "/index.php?entryPoint=responseEntryPoint&event=" . $event->id . "&delegate=" . $target->id . "&type=t&response=accept'>Accept</a>"; $event->link_declined = "<a href='" . $sugar_config['site_url'] . "/index.php?entryPoint=responseEntryPoint&event=" . $event->id . "&delegate=" . $target->id . "&type=t&response=decline'>Decline</a>"; //Get the TO name and e-mail address for the message $rcpt_name = $target->first_name . ' ' . $target->last_name; $rcpt_email = $target->email1; $emailTemp = new EmailTemplate(); $emailTemp->disable_row_level_security = true; $emailTemp->retrieve($event->invite_templates); //Use the ID value of the email template record //parse the lead varibales first $firstpass = $emailTemp->parse_template_bean($emailTemp->body_html, 'Contacts', $target); $email_subject = $emailTemp->parse_template_bean($emailTemp->subject, 'FP_events', $event); $email_body = from_html($emailTemp->parse_template_bean($firstpass, 'FP_events', $event)); $alt_emailbody = wordwrap($emailTemp->parse_template_bean($firstpass, 'FP_events', $event), 900); //get attachments $attachmentBean = new Note(); $attachment_list = $attachmentBean->get_full_list('', "parent_type = 'Emails' AND parent_id = '" . $event->invite_templates . "'"); $attachments = array(); if ($attachment_list != null) { foreach ($attachment_list as $attachment) { $attachments[] = $attachment; } } //send the email $send_invite = $this->sendEmail($rcpt_email, $email_subject, $rcpt_name, $email_body, $alt_emailbody, $target, $attachments); //Send the message, log if error occurs if (!$send_invite) { $GLOBALS['log']->fatal('ERROR: Invite email failed to send to: ' . $rcpt_name . ' at ' . $rcpt_email); $error_count++; } else { //update contact to invites $query = 'UPDATE fp_events_prospects_1_c SET invite_status="Invited" WHERE fp_events_prospects_1fp_events_ida="' . $event->id . '" AND fp_events_prospects_1prospects_idb="' . $target->id . '"'; $res = $db->query($query); } } } //loop through related leads foreach ($event->fp_events_leads_1->getBeans() as $lead) { //Get accept status of contact $query = 'SELECT invite_status FROM fp_events_leads_1_c WHERE fp_events_leads_1fp_events_ida="' . $event->id . '" AND fp_events_leads_1leads_idb="' . $lead->id . '"'; $status = $db->getOne($query); if ($status == null || $status == '' || $status == 'Not Invited') { $invite_count++; //set email links $event->link = "<a href='" . $sugar_config['site_url'] . "/index.php?entryPoint=responseEntryPoint&event=" . $event->id . "&delegate=" . $lead->id . "&type=l&response=accept'>Accept</a>"; $event->link_declined = "<a href='" . $sugar_config['site_url'] . "/index.php?entryPoint=responseEntryPoint&event=" . $event->id . "&delegate=" . $lead->id . "&type=l&response=decline'>Decline</a>"; //Get the TO name and e-mail address for the message $rcpt_name = $lead->first_name . ' ' . $lead->last_name; $rcpt_email = $lead->email1; $emailTemp = new EmailTemplate(); $emailTemp->disable_row_level_security = true; $emailTemp->retrieve($event->invite_templates); //Use the ID value of the email template record //parse the lead varibales first $firstpass = $emailTemp->parse_template_bean($emailTemp->body_html, 'Contacts', $lead); $email_subject = $emailTemp->parse_template_bean($emailTemp->subject, 'FP_events', $event); $email_body = from_html($emailTemp->parse_template_bean($firstpass, 'FP_events', $event)); $alt_emailbody = wordwrap($emailTemp->parse_template_bean($firstpass, 'FP_events', $event), 900); //get attachments $attachmentBean = new Note(); $attachment_list = $attachmentBean->get_full_list('', "parent_type = 'Emails' AND parent_id = '" . $event->invite_templates . "'"); $attachments = array(); if ($attachment_list != null) { foreach ($attachment_list as $attachment) { $attachments[] = $attachment; } } //send the email $send_invite = $this->sendEmail($rcpt_email, $email_subject, $rcpt_name, $email_body, $alt_emailbody, $lead, $attachments); //Send the message, log if error occurs if (!$send_invite) { $GLOBALS['log']->fatal('ERROR: Invite email failed to send to: ' . $rcpt_name . ' at ' . $rcpt_email); $error_count++; } else { //update contact to invites $query = 'UPDATE fp_events_leads_1_c SET invite_status="Invited" WHERE fp_events_leads_1fp_events_ida="' . $event->id . '" AND fp_events_leads_1leads_idb="' . $lead->id . '"'; $res = $db->query($query); } } } //Redirect with error message if all linked contacts have already been invited if ($invite_count == 0) { SugarApplication::appendErrorMessage($mod_strings['LBL_ERROR_MSG_1']); SugarApplication::redirect("index.php?module=FP_events&return_module=FP_events&action=DetailView&record=" . $event->id); } //Redirect if all emails fail to send if ($error_count == $delegate_count) { $_SESSION['user_error_message'] = array(); //clear the error message array SugarApplication::appendErrorMessage($mod_strings['LBL_ERROR_MSG_2'] . $delegate_count); SugarApplication::redirect("index.php?module=FP_events&return_module=FP_events&action=DetailView&record=" . $event->id); } else { if ($error_count > 0 && $error_count <= 10) { //redirect with failed email count. $_SESSION['user_error_message'] = array(); SugarApplication::appendErrorMessage($error_count . $mod_strings['LBL_ERROR_MSG_4']); SugarApplication::redirect("index.php?module=FP_events&return_module=FP_events&action=DetailView&record=" . $event->id); } else { if ($error_count > 10) { $_SESSION['user_error_message'] = array(); SugarApplication::appendErrorMessage($mod_strings['LBL_ERROR_MSG_3']); SugarApplication::redirect("index.php?module=FP_events&return_module=FP_events&action=DetailView&record=" . $event->id); } else { SugarApplication::appendErrorMessage($mod_strings['LBL_SUCCESS_MSG']); SugarApplication::redirect("index.php?module=FP_events&return_module=FP_events&action=DetailView&record=" . $event->id); } } } }
<?php if (!defined('sugarEntry') || !sugarEntry) { die('Not A Valid Entry Point'); } global $db; $even_id = $_GET['event']; $delegate_id = $_GET['delegate']; $type = $_GET['type']; $response = $_GET['response']; //get event $event = new FP_events(); $event->retrieve($even_id); if ($type == 'c') { $event->load_relationship('fp_events_contacts'); // get related contacts if ($response == 'accept') { //check to see if they have already responded to the email $check_q = 'SELECT email_responded FROM fp_events_contacts_c WHERE fp_events_contactsfp_events_ida="' . $event->id . '" AND fp_events_contactscontacts_idb="' . $delegate_id . '"'; $check = $db->getOne($check_q); //update contact to accepted $query = 'UPDATE fp_events_contacts_c SET accept_status="Accepted", email_responded="1" WHERE fp_events_contactsfp_events_ida="' . $event->id . '" AND fp_events_contactscontacts_idb="' . $delegate_id . '" AND email_responded="0"'; if ($db->query($query) && $check != '1') { if (!IsNullOrEmptyString($event->accept_redirect)) { $url = $event->accept_redirect; header('Location: ' . $url); } else { echo 'Thank you for accepting'; } } else { echo 'You have already responded to the invitation or there was a problem with the link. Please contact the sender of the invite for help.';