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

Feat/assign to chapter group #617

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Expand Up @@ -35,6 +35,7 @@ public static function getSubscribedEvents() {
'&hook_civicrm_pre' => [
['assignChapterGroupToIndividual'],
['generateInstitutionCollectionCampQr'],
['linkInstitutionCollectionCampToContact'],
],
'&hook_civicrm_custom' => [
['setOfficeDetails'],
Expand All @@ -44,6 +45,78 @@ public static function getSubscribedEvents() {
];
}

/**
*
*/
public static function linkInstitutionCollectionCampToContact(string $op, string $objectName, $objectId, &$objectRef) {
if ($objectName !== 'Eck_Collection_Camp' || !$objectId) {
return;
}

$newStatus = $objectRef['Collection_Camp_Core_Details.Status'] ?? '';
if (!$newStatus) {
return;
}

$collectionCamps = EckEntity::get('Collection_Camp', FALSE)
->addSelect('Collection_Camp_Core_Details.Status', 'Institution_Collection_Camp_Intent.Organization_Name', 'title', 'Institution_Collection_Camp_Intent.Institution_POC')
->addWhere('id', '=', $objectId)
->execute();

$currentCollectionCamp = $collectionCamps->first();
$currentStatus = $currentCollectionCamp['Collection_Camp_Core_Details.Status'];
$PocId = $currentCollectionCamp['Institution_Collection_Camp_Intent.Institution_POC'];
$organizationId = $currentCollectionCamp['Institution_Collection_Camp_Intent.Organization_Name'];

if (!$PocId && !$organizationId) {
return;
}

$collectionCampTitle = $currentCollectionCamp['title'];
$collectionCampId = $currentCollectionCamp['id'];

if ($currentStatus !== $newStatus && $newStatus === 'authorized') {
self::createCollectionCampOrganizeActivity($PocId, $organizationId, $collectionCampTitle, $collectionCampId);
}
}

/**
*
*/
private static function createCollectionCampOrganizeActivity($PocId, $organizationId, $collectionCampTitle, $collectionCampId) {
try {

// Create activity for PocId.
self::createActivity($PocId, $collectionCampTitle, $collectionCampId);

// Create activity for organizationId, only if it's different from PocId.
if ($organizationId !== $PocId) {
self::createActivity($organizationId, $collectionCampTitle, $collectionCampId);
}

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

/**
*
*/
private static function createActivity($contactId, $collectionCampTitle, $collectionCampId) {
Activity::create(FALSE)
->addValue('subject', $collectionCampTitle)
->addValue('activity_type_id:name', 'Organize Collection Camp')
->addValue('status_id:name', 'Authorized')
->addValue('activity_date_time', date('Y-m-d H:i:s'))
->addValue('source_contact_id', $contactId)
->addValue('target_contact_id', $contactId)
->addValue('Collection_Camp_Data.Collection_Camp_ID', $collectionCampId)
->execute();

\Civi::log()->info("Activity created for contact {$contactId} for Institution Collection Camp {$collectionCampTitle}");
}

/**
*
*/
Expand Down
71 changes: 49 additions & 22 deletions wp-content/civi-extensions/goonjcustom/Civi/InstitutionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Civi\Api4\CustomField;
use Civi\Api4\Organization;
use Civi\Api4\Relationship;
use Civi\Api4\Group;
use Civi\Api4\GroupContact;
use Civi\Core\Service\AutoSubscriber;
use Civi\Traits\CollectionSource;

Expand All @@ -26,7 +28,7 @@ public static function getSubscribedEvents() {
'&hook_civicrm_post' => [
['organizationCreated'],
['setOfficeDetails'],
['assignChapterGroupToIndividual'],
// ['assignChapterGroupToIndividual'],
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Avoid commenting out event subscriptions without proper cleanup

The commented-out event subscription could lead to:

  1. Dead code if not properly cleaned up
  2. Breaking changes if other parts of the system depend on this hook
  3. Confusion about the intended state of the feature

Either remove the line completely or document why it's temporarily disabled.

],
];
}
Expand Down Expand Up @@ -87,28 +89,53 @@ private static function getChapterGroupForState($stateId) {
/**
*
*/
public static function assignChapterGroupToIndividual(string $op, string $objectName, int $objectId, &$objectRef) {
return;
if ($objectName !== 'Eck_Collection_Camp' || empty($objectRef['title']) || $objectRef['title'] !== 'Institution Collection Camp') {
return FALSE;
}
$stateId = $objectRef['Institution_Collection_Camp_Intent.State'];
$contactId = $objectRef['Institution_Collection_Camp_Intent.Institution_POC'];
$organizationId = $objectRef['Institution_Collection_Camp_Intent.Organization_Name'];

if (!$stateId || !$contactId) {
\Civi::log()->info("Missing Contact ID and State ID");
return FALSE;
}
$groupId = self::getChapterGroupForState($stateId);
// public static function assignChapterGroupToIndividual(string $op, string $objectName, $objectId, &$objectRef) {
// // Define the assignments array
// $assignments = [
// 'Institution Collection Camp' => [
// 'stateField' => 'Institution_Collection_Camp_Intent.State',
// 'contactField' => 'Institution_Collection_Camp_Intent.Institution_POC',
// 'organizationField' => 'Institution_Collection_Camp_Intent.Organization_Name'
// ],
// 'Institution Dropping Center' => [
// 'stateField' => 'Institution_Dropping_Center_Intent.State',
// 'contactField' => 'Institution_Dropping_Center_Intent.Institution_POC',
// 'organizationField' => 'Institution_Dropping_Center_Intent.Organization_Name'
// ]
// ];



// // Validate objectName and objectRef
// if ($objectName !== 'Eck_Collection_Camp' || empty($objectRef['title']) || !isset($assignments[$objectRef['title']])) {
// return FALSE;
// }

// $assignment = $assignments[$objectRef['title']];

// // Access fields using array syntax
// $stateId = $objectRef[$assignment['stateField']] ?? NULL;
// $contactId = $objectRef[$assignment['contactField']] ?? NULL;
// $organizationId = $objectRef[$assignment['organizationField']] ?? NULL;

// if (!$stateId || !$contactId) {
// \Civi::log()->info("Missing Contact ID or State ID for " . $objectRef['title']);
// return FALSE;
// }

// // Get the group and add contacts
// $groupId = self::getChapterGroupForState($stateId);

// if ($groupId) {
// self::addContactToGroup($contactId, $groupId);
// if ($organizationId) {
// self::addContactToGroup($organizationId, $groupId);
// }
// }

// return TRUE;
// }
Comment on lines +92 to +137
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Refactor commented implementation for better maintainability

The commented-out implementation has several areas for improvement:

  1. Move the assignments array to a configuration file or class constant to improve maintainability
  2. Break down the method into smaller, focused methods following Single Responsibility Principle
  3. Consider using enums or constants for the title strings to prevent typos

Here's a suggested refactor:

private const INSTITUTION_ASSIGNMENTS = [
    self::INSTITUTION_COLLECTION_CAMP => [
        'stateField' => 'Institution_Collection_Camp_Intent.State',
        'contactField' => 'Institution_Collection_Camp_Intent.Institution_POC',
        'organizationField' => 'Institution_Collection_Camp_Intent.Organization_Name'
    ],
    self::INSTITUTION_DROPPING_CENTER => [
        'stateField' => 'Institution_Dropping_Center_Intent.State',
        'contactField' => 'Institution_Dropping_Center_Intent.Institution_POC',
        'organizationField' => 'Institution_Dropping_Center_Intent.Organization_Name'
    ]
];

public static function assignChapterGroupToIndividual(string $op, string $objectName, $objectId, &$objectRef) {
    if (!self::isValidAssignment($objectName, $objectRef)) {
        return FALSE;
    }

    $assignment = self::INSTITUTION_ASSIGNMENTS[$objectRef['title']];
    $ids = self::extractAssignmentIds($objectRef, $assignment);
    
    if (!self::areIdsValid($ids)) {
        return FALSE;
    }

    return self::processGroupAssignment($ids);
}

private static function isValidAssignment(string $objectName, array $objectRef): bool {
    return $objectName === 'Eck_Collection_Camp' 
        && !empty($objectRef['title']) 
        && isset(self::INSTITUTION_ASSIGNMENTS[$objectRef['title']]);
}

private static function extractAssignmentIds(array $objectRef, array $assignment): array {
    return [
        'stateId' => $objectRef[$assignment['stateField']] ?? NULL,
        'contactId' => $objectRef[$assignment['contactField']] ?? NULL,
        'organizationId' => $objectRef[$assignment['organizationField']] ?? NULL,
    ];
}

private static function areIdsValid(array $ids): bool {
    if (!$ids['stateId'] || !$ids['contactId']) {
        \Civi::log()->info("Missing Contact ID or State ID");
        return FALSE;
    }
    return TRUE;
}

private static function processGroupAssignment(array $ids): bool {
    $groupId = self::getChapterGroupForState($ids['stateId']);
    if (!$groupId) {
        return FALSE;
    }

    self::addContactToGroup($ids['contactId'], $groupId);
    if ($ids['organizationId']) {
        self::addContactToGroup($ids['organizationId'], $groupId);
    }

    return TRUE;
}


if ($groupId) {
self::addContactToGroup($contactId, $groupId);
if ($organizationId) {
self::addContactToGroup($organizationId, $groupId);
}
}
}

/**
*
Expand Down