Permalink
Browse files

Implemented SMS queuing functionality and back-end processing

  • Loading branch information...
1 parent c4fa64b commit 1bec7173f003ee54dd940fffba2ed9b8632153cf Mike Ho committed Mar 14, 2012
@@ -0,0 +1,28 @@
+<?php
+ // Up the Memory Limit
+ ini_set('memory_limit', '256M');
+
+ // Ensure we are NOT running
+ QApplication::CliProcessEnsureUnique();
+
+ // Setup a Lock File
+ QApplication::CliProcessSetupLockFile();
+
+ // Run the Logic/Code
+ while (QApplication::CliProcessIsLockFileValid()) {
+ // Analyze only 10 at a time, starting with the oldest (E.g. lowest ID) first
+ $objMessageArray = SmsMessage::LoadArrayByDateSent(
+ null,
+ array(
+ QQ::OrderBy(QQN::SmsMessage()->Id),
+ QQ::LimitInfo(10)
+ )
+ );
+
+ foreach ($objMessageArray as $objMessage) {
+ $objMessage->Send();
+ }
+
+ sleep(10);
+ }
+?>
@@ -56,6 +56,26 @@
* @var EmailMessageRoute
*/
public $objSelectedEmailMessageRoute;
+
+ /**
+ * @var SmsMessageDataGrid
+ */
+ public $dtgSmsMessage;
+
+ /**
+ * @var QTextBox
+ */
+ public $txtSmsTitle;
+
+ /**
+ * @var QTextBox
+ */
+ public $txtSmsBody;
+
+ /**
+ * @var QButton
+ */
+ public $btnSmsSend;
public $lblMinistry;
public $lblType;
@@ -108,6 +128,66 @@ public function __construct($objParentObject, $strControlId = null, Group $objGr
}
/**
+ * Sets up the SmsMessageDataGrid, and related controls to allow sending of SMSes
+ * @return void
+ */
+ protected function SetupSmsControls() {
+ $this->dtgSmsMessage = new SmsMessageDataGrid($this);
+ $objColumn = $this->dtgSmsMessage->MetaAddColumn('DateQueued', 'Width=120px', 'FontSize=10px', 'Name=Date Sent', 'Html=<?= $_CONTROL->ParentControl->RenderSmsDateSent($_ITEM); ?>');
+ $this->dtgSmsMessage->MetaAddColumn(QQN::SmsMessage()->Login->FirstName, 'Name=Sent By', 'Html=<?= $_ITEM->Login->Name; ?>', 'Width=150px', 'FontSize=10px');
+ $this->dtgSmsMessage->MetaAddColumn('Subject', 'Width=150px');
+ $this->dtgSmsMessage->MetaAddColumn('Body', 'Width=300px');
+ $this->dtgSmsMessage->SetDataBinder('dtgSmsMessage_Bind', $this);
+ $this->dtgSmsMessage->Paginator = new QPaginator($this->dtgSmsMessage);
+ $this->dtgSmsMessage->SortColumnIndex = 0;
+ $this->dtgSmsMessage->SortDirection = 1;
+ $this->dtgSmsMessage->NoDataHtml = '<em>No SMS Messages have yet been sent to this group.</em>';
+
+ $this->txtSmsTitle = new QTextBox($this);
+ $this->txtSmsTitle->Name = 'Subject';
+ $this->txtSmsTitle->HtmlAfter = '<br/><span class="na" style="font-size: 10px;">While it is recommended that you provide a subject,<br/>please note that some mobile providers do <strong>NOT</strong> show the subject to the recipient</span>';
+ $this->txtSmsTitle->MaxLength = 40;
+
+ $this->txtSmsBody = new QTextBox($this);
+ $this->txtSmsBody->Name = 'Message';
+ $this->txtSmsBody->Required = true;
+ $this->txtSmsBody->MaxLength = 140;
+ $this->txtSmsBody->HtmlAfter = '<br/><span class="na" style="font-size: 10px;">Please limit to 140 characters</span>';
+
+ $this->btnSmsSend = new QButton($this);
+ $this->btnSmsSend->Text = 'Send SMS';
+ $this->btnSmsSend->CssClass = 'primary';
+ $this->btnSmsSend->CausesValidation = $this->txtSmsBody;
+ $this->btnSmsSend->AddAction(new QClickEvent(), new QAjaxControlAction($this, 'btnSmsSend_Click'));
+ }
+
+ public function dtgSmsMessage_Bind() {
+ $objCondition = QQ::Equal(QQN::SmsMessage()->GroupId, $this->objGroup->Id);
+ $this->dtgSmsMessage->MetaDataBinder($objCondition);
+ }
+
+ public function RenderSmsDateSent(SmsMessage $objSmsMessage) {
+ if ($objSmsMessage->DateSent) {
+ return $objSmsMessage->DateSent->ToString('MMM D YYYY, h:mmz');
+ } else {
+ return 'sending...';
+ }
+ }
+
+ public function btnSmsSend_Click($strFormId, $strControlId, $strParameter) {
+ if (!QApplication::$Login->Email) {
+ QApplication::DisplayAlert('Your NOAH Account was not set up with your @alcf.net email address, so unfortunately you cannot yet send a SMS Message. Please contact it@alcf.net to have this fixed.');
+ return;
+ }
+
+ SmsMessage::QueueSmsForGroup($this->objGroup, QApplication::$Login, $this->txtSmsTitle->Text, $this->txtSmsBody->Text);
+ $this->dtgSmsMessage->Refresh();
+ $this->txtSmsTitle->Text = null;
+ $this->txtSmsBody->Text = null;
+ QApplication::DisplayAlert('Your SMS Message was queued to be sent! You will receive a copy of this SMS to your email at ' . QApplication::$Login->Email . '.');
+ }
+
+ /**
* Sets up the EmailMessageRouteDataGrid if there are messages associated with this group
* @return void
*/
@@ -23,6 +23,7 @@ protected function SetupPanel() {
$this->dtgGroups->SetDataBinder('dtgGroups_Bind', $this);
if ($this->objGroup->CountEmailMessageRoutes()) $this->SetupEmailMessageControls();
+ $this->SetupSmsControls();
}
public function RenderGroupName(Group $objGroup) {
@@ -60,6 +60,7 @@ protected function SetupPanel() {
$this->lblStructure->HtmlEntities = false;
if ($this->objGroup->CountEmailMessageRoutes()) $this->SetupEmailMessageControls();
+ $this->SetupSmsControls();
}
public function dtgMembers_Bind() {
@@ -7,6 +7,7 @@ protected function SetupPanel() {
$this->dtgMembers->SetDataBinder('dtgMembers_Bind', $this);
if ($this->objGroup->CountEmailMessageRoutes()) $this->SetupEmailMessageControls();
+ $this->SetupSmsControls();
}
public function dtgMembers_Bind() {
@@ -32,4 +32,22 @@
<h3>Email Message Archive</h3>
<div class="section"><?php $_CONTROL->dtgEmailMessageRoute->Render(); ?></div>
<?php $_CONTROL->dlgEmailMessage->Render(); ?>
-<?php } ?>
+<?php } ?>
+
+<?php if ($_CONTROL->dtgSmsMessage) { ?>
+ <br/>
+ <h3>SMS Messages</h3>
+ <div class="section">
+ <?php $_CONTROL->dtgSmsMessage->Render(); ?>
+
+ <div style="padding: 15px; border: 1px solid #999; margin-top: 15px;">
+ <h3>Send an SMS Message</h3>
+ <?php $_CONTROL->txtSmsTitle->RenderWithName(); ?>
+ <?php $_CONTROL->txtSmsBody->RenderWithName(); ?>
+ <div class="buttonBar">
+ <?php $_CONTROL->btnSmsSend->Render(); ?>
+ </div>
+ </div>
+ </div>
+<?php } ?>
+
@@ -14,6 +14,7 @@ protected function SetupPanel() {
$this->lblQuery->HtmlEntities = false;
if ($this->objGroup->CountEmailMessageRoutes()) $this->SetupEmailMessageControls();
+ $this->SetupSmsControls();
}
public function dtgMembers_Bind() {
@@ -981,7 +981,7 @@ public static function CreatePerson($strFirstName, $strMiddle, $strLastName, $bl
*
*/
public function GetSmsEnabledPhone() {
- if ($this->PrimaryPhone->IsSmsEnabled()) return $this->PrimaryPhone;
+ if ($this->PrimaryPhone && $this->PrimaryPhone->IsSmsEnabled()) return $this->PrimaryPhone;
return null;
}
@@ -27,6 +27,67 @@ public function __toString() {
return sprintf('SmsMessage Object %s', $this->intId);
}
+ /**
+ * Queues a new SMS Message to be sent out
+ * @param Group $objGroup
+ * @param Login $objLogin
+ * @param string $strSubject can be null/blank
+ * @param string $strBody
+ * @return SmsMessage the queued message that has been queued
+ */
+ public static function QueueSmsForGroup(Group $objGroup, Login $objLogin, $strSubject, $strBody) {
+ $objSmsMessage = new SmsMessage();
+ $objSmsMessage->Group = $objGroup;
+ $objSmsMessage->Login = $objLogin;
+ $objSmsMessage->Subject = trim($strSubject);
+ $objSmsMessage->Body = trim($strBody);
+ $objSmsMessage->DateQueued = QDateTime::Now();
+ $objSmsMessage->Save();
+ return $objSmsMessage;
+ }
+
+ public function Send() {
+ // Do **NOT** send again if it has already been sent!
+ if ($this->dttDateSent) return;
+
+ // Store an array of email addresses (SMS-based) to send to
+ $strBccArray = array();
+
+ // Get the Group, and get the array for CcArray
+ $intPersonIdArray = array();
+ foreach ($this->Group->GetActiveGroupParticipationArray() as $objGroupParticipation) {
+ // ONly at most one per person
+ if (!array_key_exists($objGroupParticipation->PersonId, $intPersonIdArray)) {
+ // Get the Person
+ $objPerson = $objGroupParticipation->Person;
+
+ // Do we have an SMS?
+ if ($objPhone = $objPerson->GetSmsEnabledPhone()) {
+ // Yep! Add it to the list
+ $strBccArray[] = $objPhone->SmsEmailAddress;
+ }
+
+ // Finally, let's make sure it only gets sent (at most) once per person
+ $intPersonIdArray[$objGroupParticipation->PersonId] = $objGroupParticipation->PersonId;
+ }
+ }
+
+ // Do we have any to send to?
+ if (count($strBccArray)) {
+ // Yes -- let's send it
+ $objEmailMessage = new QEmailMessage($this->Login->Email, $this->Login->Email, $this->strSubject, $this->strBody);
+ $objEmailMessage->Bcc = implode(', ', $strBccArray);
+ QEmailServer::Send($objEmailMessage);
+
+ $this->DateSent = QDateTime::Now();
+ $this->Save();
+ } else {
+ // No -- let's report it
+ $objEmailMessage = new QEmailMessage($this->Login->Email, $this->Login->Email, '[Failed to Send] ' . $this->strSubject, 'The following SMS did NOT send because there were no SMS-enabled group participants: ' . $this->strBody);
+ QEmailServer::Send($objEmailMessage);
+ $this->Delete();
+ }
+ }
// Override or Create New Load/Count methods
// (For obvious reasons, these methods are commented out...

0 comments on commit 1bec717

Please sign in to comment.