-
Notifications
You must be signed in to change notification settings - Fork 1
Test email #1560
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
base: dev
Are you sure you want to change the base?
Test email #1560
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1742,4 +1742,193 @@ public static function generateInvoiceNumber(string $op, string $objectName, int | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Send camp outcome acknowledgement email after 5 days. | ||
| */ | ||
| public static function sendCampOutcomeAckEmailAfter5Days($collectionCamp) { | ||
| $campId = $collectionCamp['id']; | ||
|
|
||
| // Fetch Event Volunteer | ||
| $volunteeringActivities = Activity::get(FALSE) | ||
| ->addSelect('activity_contact.contact_id') | ||
| ->addJoin('ActivityContact AS activity_contact', 'LEFT') | ||
| ->addWhere('activity_type_id:name', '=', 'Volunteering') | ||
| ->addWhere('Volunteering_Activity.Collection_Camp', '=', $campId) | ||
| ->addWhere('activity_contact.record_type_id', '=', 3) | ||
| ->execute(); | ||
|
|
||
| // Collect volunteer contact IDs | ||
| $volunteerContactIds = []; | ||
| foreach ($volunteeringActivities as $volunteer) { | ||
| $volunteerContactIds[] = $volunteer['activity_contact.contact_id']; | ||
| } | ||
|
|
||
| // Fetch volunteer emails (if any volunteers found) | ||
| $eventVolunteerEmails = []; | ||
| if (!empty($volunteerContactIds)) { | ||
| $volunteerContacts = Contact::get(FALSE) | ||
| ->addSelect('email.email') | ||
| ->addJoin('Email AS email', 'LEFT') | ||
| ->addWhere('id', 'IN', $volunteerContactIds) | ||
| ->execute(); | ||
|
|
||
| foreach ($volunteerContacts as $contact) { | ||
| if (!empty($contact['email.email'])) { | ||
| $eventVolunteerEmails[] = $contact['email.email']; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Convert to comma-separated string for CC | ||
| $eventVolunteerCC = implode(',', $eventVolunteerEmails); | ||
|
|
||
| $collectionCamps = EckEntity::get('Collection_Camp', FALSE) | ||
| ->addSelect('Collection_Camp_Intent_Details.Location_Area_of_camp', 'Core_Contribution_Details.Number_of_unique_contributors', 'Camp_Outcome.Rate_the_camp', 'Camp_Outcome.Total_Fundraised_form_Activity', 'Collection_Camp_Intent_Details.Start_Date', 'title', 'Collection_Camp_Intent_Details.End_Date') | ||
| ->addWhere('id', '=', $campId) | ||
| ->execute()->single(); | ||
|
|
||
| $contribution = Contribution::get(FALSE) | ||
| ->addSelect('total_amount') | ||
| ->addWhere('contribution_status_id:name', '=', 'Completed') | ||
| ->addWhere('Contribution_Details.Source', '=', $campId) | ||
| ->execute(); | ||
|
|
||
| $totalAmount = 0; | ||
|
|
||
| foreach ($contribution as $c) { | ||
| $totalAmount += $c['total_amount']; | ||
| } | ||
|
|
||
| $collectionSourceVehicleDispatche = EckEntity::get('Collection_Source_Vehicle_Dispatch', FALSE) | ||
| ->addSelect('Acknowledgement_For_Logistics.No_of_bags_received_at_PU_Office') | ||
| ->addWhere('Camp_Vehicle_Dispatch.Collection_Camp', '=', $campId) | ||
| ->execute(); | ||
|
|
||
| $materialGeneratedList = []; | ||
| foreach ($collectionSourceVehicleDispatche as $dispatch) { | ||
| $materialGeneratedList[] = $dispatch['Acknowledgement_For_Logistics.No_of_bags_received_at_PU_Office']; | ||
| } | ||
|
|
||
| $materialGeneratedHtml = ''; | ||
| if (!empty($collectionSourceVehicleDispatche)) { | ||
| // Outer bullet | ||
| $materialGeneratedHtml .= "<li>Material generated:<br>"; | ||
| // Inner numbered list with inline style for emails | ||
| $materialGeneratedHtml .= "<ol style='margin:0; padding-left:20px;'>"; | ||
| foreach ($collectionSourceVehicleDispatche as $dispatch) { | ||
| $materialGeneratedHtml .= "<li>" . $dispatch['Acknowledgement_For_Logistics.No_of_bags_received_at_PU_Office'] . "</li>"; | ||
| } | ||
| $materialGeneratedHtml .= "</ol></li>"; | ||
| } | ||
|
|
||
| $uniqueContributors = $collectionCamps['Core_Contribution_Details.Number_of_unique_contributors']; | ||
|
|
||
| $campRating = $collectionCamps['Camp_Outcome.Rate_the_camp']; | ||
| $fundsGenerated = $collectionCamps['Camp_Outcome.Total_Fundraised_form_Activity']; | ||
| $collectionCampTitle = $collectionCamps['title']; | ||
|
|
||
| $campAddress = $collectionCamps['Collection_Camp_Intent_Details.Location_Area_of_camp']; | ||
|
|
||
| $campStartDate = $collectionCamps['Collection_Camp_Intent_Details.Start_Date']; | ||
| $campEndDate = $collectionCamps['Collection_Camp_Intent_Details.End_Date']; | ||
|
|
||
| $campCompletionDate = $collectionCamp['Camp_Outcome.Camp_Status_Completion_Date']; | ||
| $campOrganiserId = $collectionCamp['Collection_Camp_Core_Details.Contact_Id']; | ||
|
|
||
| $campAttendedBy = Contact::get(FALSE) | ||
| ->addSelect('email.email', 'display_name') | ||
| ->addJoin('Email AS email', 'LEFT') | ||
| ->addWhere('id', '=', $campOrganiserId) | ||
| ->execute()->first(); | ||
|
|
||
| $attendeeEmail = $campAttendedBy['email.email']; | ||
| $attendeeName = $campAttendedBy['display_name']; | ||
|
|
||
| if (!$attendeeEmail) { | ||
| throw new \Exception('Attendee email missing'); | ||
| } | ||
|
|
||
| $mailParams = [ | ||
| 'subject' => $attendeeName . ' thankyou for organizing the camp! A quick snapshot.', | ||
| 'from' => self::getFromAddress(), | ||
| 'toEmail' => $attendeeEmail, | ||
| 'replyTo' => self::getFromAddress(), | ||
| 'html' => self::getCampOutcomeAckEmailAfter5Days($attendeeName, $campAddress, $campStartDate, $totalAmount, $materialGeneratedHtml, $uniqueContributors, $campRating, $fundsGenerated, $campId, $campEndDate, $campOrganiserId), | ||
| 'cc' => $eventVolunteerCC, | ||
| ]; | ||
|
|
||
| $emailSendResult = \CRM_Utils_Mail::send($mailParams); | ||
|
|
||
| if ($emailSendResult) { | ||
| \Civi::log()->info("Camp status email sent for collection camp: $campId"); | ||
| try { | ||
| EckEntity::update('Collection_Camp', FALSE) | ||
| ->addValue('Camp_Outcome.Five_Day_Email_Sent', 1) | ||
| ->addWhere('id', '=', $campId) | ||
| ->execute(); | ||
|
|
||
| } | ||
| catch (\CiviCRM_API4_Exception $ex) { | ||
| \Civi::log()->debug("Exception while creating update the email sent " . $ex->getMessage()); | ||
| } | ||
| try { | ||
| $results = Activity::create(FALSE) | ||
| ->addValue('subject', $collectionCampTitle) | ||
| ->addValue('activity_type_id:name', 'Camp summary email') | ||
| ->addValue('status_id:name', 'Authorized') | ||
| ->addValue('activity_date_time', date('Y-m-d H:i:s')) | ||
| ->addValue('source_contact_id', $campOrganiserId) | ||
| ->addValue('target_contact_id', $campOrganiserId) | ||
| ->addValue('Collection_Camp_Data.Collection_Camp_ID', $campId) | ||
| ->execute(); | ||
|
|
||
| } | ||
| catch (\CiviCRM_API4_Exception $ex) { | ||
| \Civi::log()->debug("Exception while creating Camp summary email activity: " . $ex->getMessage()); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * | ||
| */ | ||
| public static function getCampOutcomeAckEmailAfter5Days($attendeeName, $campAddress, $campStartDate, $totalAmount, $materialGeneratedHtml, $uniqueContributors, $campRating, $fundsGenerated, $campId, $campEndDate, $campOrganiserId) { | ||
| $homeUrl = \CRM_Utils_System::baseCMSURL(); | ||
| $campVolunteerFeedback = $homeUrl . 'volunteer-camp-feedback/#?Collection_Source_Feedback.Collection_Camp_Code=' . $campId . '&Collection_Source_Feedback.Collection_Camp_Address=' . urlencode($campAddress) . '&Collection_Source_Feedback.Filled_By=' . $campOrganiserId; | ||
| // Conditionally include funds raised | ||
| $fundsGeneratedHtml = ''; | ||
| if (!empty($fundsGenerated)) { | ||
| $fundsGeneratedHtml = "<li>Funds raised through activities: $fundsGenerated</li>"; | ||
| } | ||
|
|
||
| $formattedCampStartDate = date('d-m-Y', strtotime($campStartDate)); | ||
| $formattedCampEndDate = date('d-m-Y', strtotime($campEndDate)); | ||
|
|
||
| // Conditional date text | ||
| if ($formattedCampStartDate === $formattedCampEndDate) { | ||
| $campDateText = "on <strong>$formattedCampStartDate</strong>"; | ||
| } else { | ||
| $campDateText = "from <strong>$formattedCampStartDate</strong> to <strong>$formattedCampEndDate</strong>"; | ||
| } | ||
|
|
||
| $html = " | ||
| <p>Dear $attendeeName,</p> | ||
| <p>Thank you for organising the recent collection drive at <strong>$campAddress</strong> $campDateText! Your effort brought people together and added strength to this movement of mindful giving.</p> | ||
| <p>Here’s a quick snapshot of the camp:</p> | ||
| <ul> | ||
| $materialGeneratedHtml | ||
| <li>Footfall: $uniqueContributors</li> | ||
| $fundsGeneratedHtml | ||
| </ul> | ||
| <p>If you haven’t filled the feedback form yet, you can share your thoughts here: <a href='$campVolunteerFeedback'>Feedback Form</a></p> | ||
| <p>We would also love to hear about any highlights, challenges, or ideas you’d like us to know. Your reflections will help us make future drives even more impactful.</p> | ||
| <p>Looking forward to many more such collaborations ahead!</p> | ||
| <p>Warm regards,<br>Team Goonj</p> | ||
| "; | ||
|
|
||
| return $html; | ||
| } | ||
|
Comment on lines
+1897
to
+1932
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unused parameters These parameters are accepted but never used in the HTML body. Either incorporate them into the email template or remove them from the signature (and the call site on Line 1856). 🧰 Tools🪛 PHPMD (2.15.0)[warning] 1897-1897: Avoid unused parameters such as '$totalAmount'. (undefined) (UnusedFormalParameter) [warning] 1897-1897: Avoid unused parameters such as '$campRating'. (undefined) (UnusedFormalParameter) 🤖 Prompt for AI Agents |
||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sendCampOutcomeAckEmailAfter5Daysdoes too much and has several issues.This single method fetches volunteers, fetches camp details, sums contributions, builds dispatch HTML, fetches organiser contact, composes and sends the email, updates a flag, and creates an activity. Consider extracting helper methods for readability and testability.
Concrete issues:
$campCompletionDateis assigned but never read.$resultsfromActivity::createis never read.$collectionSourceVehicleDispatcheis iterated once to build$materialGeneratedList(which is itself never used), then iterated again to build$materialGeneratedHtml. Remove the first loop.Contact::geton Line 1838 throws or returns no result, the method will throw an unhandled exception before the email flag is set.Proposed fix for the unused code and double iteration
- $campCompletionDate = $collectionCamp['Camp_Outcome.Camp_Status_Completion_Date'];🧰 Tools
🪛 PHPMD (2.15.0)
[warning] 1835-1835: Avoid unused local variables such as '$campCompletionDate'. (undefined)
(UnusedLocalVariable)
[warning] 1875-1875: Avoid unused local variables such as '$results'. (undefined)
(UnusedLocalVariable)
🤖 Prompt for AI Agents