-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: develop
Are you sure you want to change the base?
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 |
---|---|---|
@@ -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(); | ||
|
||
$currentInstitutionCampaign = $institutionCampaign->first(); | ||
$currentInstitutionId = $currentInstitutionCampaign['Additional_Details.Institution']; | ||
if (!$currentInstitutionId) { | ||
return; | ||
} | ||
Comment on lines
+44
to
+47
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. Check for Undefined Index in Data Retrieval When accessing Apply this diff to ensure the index exists before accessing it: $currentInstitutionId = $currentInstitutionCampaign['Additional_Details.Institution'];
+if (!isset($currentInstitutionCampaign['Additional_Details.Institution'])) {
+ return;
+}
|
||
$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()); | ||
} | ||
} | ||
|
||
} |
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.
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, callingfirst()
may result in an error.Apply this diff to add a check for empty results: