Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
A common task in conference management is accepting and rejecting talks proposed by the various speakers. This involves reviewing the proposals sent in, making decisions on whether to accept or reject them, and then notifying the speakers either way on the decision. While SugarCRM cannot take away the human decision element of this process, it can automate the notification process. Let’s see how:
<?php // Do not store anything in this file that is not part of the array or the hook //version. This file will be automatically rebuilt in the future. $hook_version = 1; $hook_array = Array(); // position, file, function $hook_array['before_save'] = Array(); $hook_array['before_save'][] = Array(1, 'pos_Sessions push feed', 'custom/modules/pos_Sessions/SugarFeeds/SessionsFeed.php','SessionsFeed', 'pushFeed'); $hook_array['before_save'][] = Array(1, 'Session Accepted Hook', 'custom/modules/pos_Sessions/NotifySpeakerHook.php', 'NotifySpeakerHook', 'sendAcceptEmail'); $hook_array['before_save'][] = Array(2, 'Session Declined Hook', 'custom/modules/pos_Sessions/NotifySpeakerHook.php', 'NotifySpeakerHook', 'sendDeclineEmail'); ?>
First off, we’ll need to add to the existing logic_hooks.php file in the custom/modules/pos_Sessions/ directory, adding two entries for before_save logic hooks to send out these emails.
For the hook itself, we’ll add a new class named NotifySpeakerHook, which will be defined in the file custom/modules/pos_Sessions/NotifySpeakerHook.php.
<?php
class NotifySpeakerHook
{
public function sendAcceptEmail(SugarBean $bean, $event, $arguments)
{
if (!empty($bean->fetched_row['status'] )
&& $bean->fetched_row['status'] != $bean->status
&& $bean->status == 'Accepted') {
$job = new SchedulersJob();
$job->data = serialize(array(
$bean->id,
'Your proposal has been accepted!',
"Congratulations, your proposal entitled '{$bean->name} for the " .
"conference has been accepted.",
));
$job->execute_time = TimeDate::getInstance()->nowDb();
$job->retry_count = 3;
$job->name = "Send Speaker Email for {$bean->name}";
$job->target = 'class::EmailSpeakerNotices';
$queue = new SugarJobQueue();
$queue->submitJob($job);
}
}
public function sendDeclineEmail(SugarBean $bean, $event, $arguments)
{
if(!empty($bean->fetched_row['status'] )
&& $bean->fetched_row['status'] != $bean->status
&& $bean->status == 'Declined'){
$job = new SchedulersJob();
$job->data = serialize(array(
$bean->id,
'Your proposal has not been accepted',
"We are sorry to inform you that your proposal entitled ".
"{$bean->name} has not been accepted for the conference."
);
$job->execute_time = TimeDate::getInstance()->nowDb();
$job->retry_count = 3;
$job->name = "Send Speaker Email for {$bean->name}";
$job->target = 'class::EmailSpeakerNotices';
$queue = new SugarJobQueue();
$queue->submitJob($job);
}
}
}
There are two hook functions for sending either the acceptance or rejection email based upon whether the status has changed to ‘Accepted’ or ‘Declined’. Both of these methods trigger sending the email, which to help with save performance we’ll do asyncronously leveraging the Job Queue, which is a new feature in Sugar 6.5
Using the job queue requires having the Scheduler being setup and running on your instance. You can find instructions for doing this at http://support.sugarcrm.com/04_Find_Answers/02KB/02Administration/100Schedulers/Introduction_to_Cron_Jobs.
Using the Job Queue requires us to define the action for the Job to execute in a seperate class, which impliments the RunnableSchedulerJob interface. This interface requires us to impliment two methods, setJob which is simply us defining the job to execute, and then run() which actually runs the job. Define this class in the custom/Extensions/modules/Schedulers/Ext/ScheduledTasks/ directory with the filename EmailSpeakerNotices.php.
<?php
class EmailSpeakerNotices implements RunnableSchedulerJob
{
public function setJob(SchedulersJob $job)
{
$this->schedulerJob = $job;
}
public function run($data)
{
list($recordID, $emailSubject, $emailBody) = unserialize($data);
$bean = BeanFactory::getBean('pos_Sessions',$recordID);
if ( $bean === FALSE ) {
$this->schedulerJob->retry_count = 0; // Don't retry since the underlying data is bad
$this->schedulerJob->failJob('Session Record ID not found');
}
$emailObj = BeanFactory::getBean('Emails');
$defaults = $emailObj->getSystemDefaultEmail();
$mail = new SugarPHPMailer();
$mail->setMailerForSystem();
$mail->From = $defaults['email'];
$mail->FromName = $defaults['name'];
$mail->ClearAllRecipients();
$mail->ClearReplyTos();
$mail->Subject=from_html($emailSubject);
$mail->Body=from_html($emailBody);
$mail->prepForOutbound();
$speaker = BeanFactory::getBean('pos_Speakers',$bean->pos_speake680dpeakers_ida);
$speaker->retrieve($bean->pos_speake680dpeakers_ida);
if ( $speaker !== FALSE && !empty($speaker->email1) ) {
$mail->AddAddress($speaker->email1);
}
//Now create the Email record to record what was sent by this job.
if (@$mail->Send()) {
$emailObj->to_addrs= '';
$emailObj->type= 'archived';
$emailObj->deleted = '0';
$emailObj->name = $mail->Subject ;
$emailObj->description = $mail->Body;
$emailObj->description_html = null;
$emailObj->from_addr = $mail->From;
$emailObj->parent_type = $speaker->module_dir;
$emailObj->parent_id= $speaker->id;
$emailObj->date_sent = TimeDate::getInstance()->nowDb();
$emailObj->modified_user_id= '1';
$emailObj->created_by = '1';
$emailObj->team_id= '1';
$emailObj->status = 'sent';
$emailObj->save();
}
else {
$this->schedulerJob->failJob('Email could not be sent to '.$speaker->email1);
}
}
}Just like any time you add files inside the custom/Extensions/ directory, you will need to run the ‘Quick Rebuild and Repair’ process in the Admin -> Repair section to have the changes you make here take effect.
The job goes ahead and does the work of sending out the email to recipent for us in the background when the Scheduler process runs. One thing we do in this process is create an archived email in the application underneath the speaker record. This gives us a record of the email being successfully being sent out to the Speaker.