Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SMS Trait] Consolidate 2 foreach loops into 1 in sms trait #29629

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 6 additions & 23 deletions CRM/Contact/Form/Task/SMSTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ trait CRM_Contact_Form_Task_SMSTrait {
*
* @throws \CRM_Core_Exception
*/
public function postProcess() {
public function postProcess(): void {
$this->postProcessSms();
}

Expand Down Expand Up @@ -122,7 +122,7 @@ protected function buildSmsForm(): void {
$form->add('text', 'activity_subject', ts('Name The SMS'), ['class' => 'huge'], TRUE);

$toSetDefault = TRUE;
if (property_exists($form, '_context') && $form->_context == 'standalone') {
if (property_exists($form, '_context') && $form->_context === 'standalone') {
$toSetDefault = FALSE;
}

Expand Down Expand Up @@ -254,28 +254,11 @@ protected function postProcessSms(): void {

// format contact details array to handle multiple sms from same contact
$formattedContactDetails = [];
$tempPhones = [];
$phonesToSendTo = explode(',', $this->getSubmittedValue('to'));
$contactIds = $phones = [];
foreach ($phonesToSendTo as $phone) {
[$contactId, $phone] = explode('::', $phone);
if ($contactId) {
$contactIds[] = $contactId;
$phones[] = $phone;
}
}
foreach ($contactIds as $key => $contactId) {
$phone = $phones[$key];

if ($phone) {
$phoneKey = "{$contactId}::{$phone}";
if (!in_array($phoneKey, $tempPhones)) {
$tempPhones[] = $phoneKey;
if (!empty($form->_contactDetails[$contactId])) {
$formattedContactDetails[] = $form->_contactDetails[$contactId];
$contactIds[] = $contactId;
}
}
[$contactId] = explode('::', $phone);
if ($contactId && !empty($form->_contactDetails[$contactId])) {
$formattedContactDetails[] = $form->_contactDetails[$contactId];
}
}

Expand Down Expand Up @@ -315,7 +298,7 @@ protected function postProcessSms(): void {
$not_sent[] = "<a href='$contactViewUrl' title='$displayName'>$displayName</a>";
}
$status = '(' . ts('because no phone number on file or communication preferences specify DO NOT SMS or Contact is deceased');
if (CRM_Utils_System::getClassName($form) == 'CRM_Activity_Form_Task_SMS') {
if (CRM_Utils_System::getClassName($form) === 'CRM_Activity_Form_Task_SMS') {
$status .= ' ' . ts("or the contact is not part of the activity '%1'", [1 => $this->getActivityName()]);
}
$status .= ')<ul><li>' . implode('</li><li>', $not_sent) . '</li></ul>';
Expand Down