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

Add Activity In Institution related to institution Campaign #633

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Civi;

use Civi\Api4\Activity;
use Civi\Api4\Campaign;
use Civi\Core\Service\AutoSubscriber;

/**
*
*/
class InstitutionCampaignService extends AutoSubscriber {

/**
*
*/
public static function getSubscribedEvents() {
return [
'&hook_civicrm_post' => [
['linkCampaignToOrganization'],
],
];
}

/**
*
*/
public static function linkCampaignToOrganization(string $op, string $objectName, int $objectId, &$objectRef) {

if ($objectName != 'Campaign' || !$objectId) {
return;
}

$institutionCampaign = Campaign::get(TRUE)
->addSelect(
'id',
'Additional_Details.Institution',
'title'
)
->addWhere('id', '=', $objectId)
->execute();
Comment on lines +34 to +41
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Handle Potential Empty Result from Campaign::get()

There is no check to ensure that $institutionCampaign contains results before calling $institutionCampaign->first(). If the campaign with the given $objectId does not exist, calling first() may result in an error.

Apply this diff to add a check for empty results:

 $institutionCampaign = Campaign::get(TRUE)
   ->addSelect(
       'id',
       'Additional_Details.Institution',
       'title'
   )
   ->addWhere('id', '=', $objectId)
   ->execute();

+$currentInstitutionCampaign = $institutionCampaign->first();
+if (!$currentInstitutionCampaign) {
+  // Handle the case where the campaign is not found.
+  return;
+}

Committable suggestion skipped: line range outside the PR's diff.


$currentInstitutionCampaign = $institutionCampaign->first();
$currentInstitutionId = $currentInstitutionCampaign['Additional_Details.Institution'];
if (!$currentInstitutionId) {
return;
}
Comment on lines +44 to +47
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Check for Undefined Index in Data Retrieval

When accessing $currentInstitutionCampaign['Additional_Details.Institution'], there's a risk of an undefined index if the 'Additional_Details.Institution' key is missing. This could lead to a PHP notice or error.

Apply this diff to ensure the index exists before accessing it:

 $currentInstitutionId = $currentInstitutionCampaign['Additional_Details.Institution'];
+if (!isset($currentInstitutionCampaign['Additional_Details.Institution'])) {
+  return;
+}

Committable suggestion skipped: line range outside the PR's diff.

$campaignTitle = $currentInstitutionCampaign['title'];
$campaignId = $currentInstitutionCampaign['id'];

// Check for status change.
if ($currentInstitutionId) {
self::createInstitutionCampaignActivity($currentInstitutionId, $campaignTitle, $campaignId);
}
}

/**
* Log an activity in CiviCRM.
*/
private static function createInstitutionCampaignActivity($currentInstitutionId, $campaignTitle, $campaignId) {
try {
$results = Activity::create(FALSE)
->addValue('subject', $campaignTitle)
->addValue('activity_type_id:name', 'Institution Campaign')
->addValue('status_id:name', 'Completed')
->addValue('activity_date_time', date('Y-m-d H:i:s'))
->addValue('source_contact_id', $currentInstitutionId)
->addValue('target_contact_id', $currentInstitutionId)
->execute();

}
catch (\CiviCRM_API4_Exception $ex) {
\Civi::log()->debug("Exception while creating Institution Campaign activity: " . $ex->getMessage());
}
}

}