From e1d05b213a5156d0cd76b0860f3d03fbe30bbd5f Mon Sep 17 00:00:00 2001 From: belwalshubham Date: Fri, 13 Dec 2024 15:02:38 +0530 Subject: [PATCH 1/3] assign conatct to chapter group --- .../goonjcustom/Civi/InstitutionService.php | 55 ++++++++++++++----- 1 file changed, 41 insertions(+), 14 deletions(-) diff --git a/wp-content/civi-extensions/goonjcustom/Civi/InstitutionService.php b/wp-content/civi-extensions/goonjcustom/Civi/InstitutionService.php index 1fbc0158d..19ccb2f3c 100644 --- a/wp-content/civi-extensions/goonjcustom/Civi/InstitutionService.php +++ b/wp-content/civi-extensions/goonjcustom/Civi/InstitutionService.php @@ -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; @@ -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; + 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; } - $stateId = $objectRef['Institution_Collection_Camp_Intent.State']; - $contactId = $objectRef['Institution_Collection_Camp_Intent.Institution_POC']; - $organizationId = $objectRef['Institution_Collection_Camp_Intent.Organization_Name']; + + $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 and State ID"); - return FALSE; + \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); - } + self::addContactToGroup($contactId, $groupId); + if ($organizationId) { + self::addContactToGroup($organizationId, $groupId); + } } - } + + return TRUE; +} + /** * From 937ce81ea6b2dff3e7a4e3bd06550c5e27770c1b Mon Sep 17 00:00:00 2001 From: belwalshubham Date: Fri, 13 Dec 2024 16:14:55 +0530 Subject: [PATCH 2/3] code cleanups --- .../Civi/InstitutionCollectionCampService.php | 67 +++++++++++++ .../goonjcustom/Civi/InstitutionService.php | 94 +++++++++---------- 2 files changed, 114 insertions(+), 47 deletions(-) diff --git a/wp-content/civi-extensions/goonjcustom/Civi/InstitutionCollectionCampService.php b/wp-content/civi-extensions/goonjcustom/Civi/InstitutionCollectionCampService.php index 2ba0155f5..7a3b0e466 100644 --- a/wp-content/civi-extensions/goonjcustom/Civi/InstitutionCollectionCampService.php +++ b/wp-content/civi-extensions/goonjcustom/Civi/InstitutionCollectionCampService.php @@ -35,6 +35,7 @@ public static function getSubscribedEvents() { '&hook_civicrm_pre' => [ ['assignChapterGroupToIndividual'], ['generateInstitutionCollectionCampQr'], + ['linkCollectionCampToContact'], ], '&hook_civicrm_custom' => [ ['setOfficeDetails'], @@ -44,6 +45,72 @@ public static function getSubscribedEvents() { ]; } + public static function linkCollectionCampToContact(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']; + + // Check for status change and handle activity creation + if ($currentStatus !== $newStatus && $newStatus === 'authorized') { + self::createCollectionCampOrganizeActivity($PocId, $organizationId, $collectionCampTitle, $collectionCampId); + } +} + +private static function createCollectionCampOrganizeActivity($PocId, $organizationId, $collectionCampTitle, $collectionCampId) { + try { + // Prepare the activity data + $activityData = [ + 'subject' => $collectionCampTitle, + 'activity_type_id:name' => 'Organize Collection Camp', + 'status_id:name' => 'Authorized', + 'activity_date_time' => date('Y-m-d H:i:s'), + 'Collection_Camp_Data.Collection_Camp_ID' => $collectionCampId + ]; + + // Create activity for PocId + self::createActivity($PocId, $activityData); + + // Create activity for organizationId, only if it's different from PocId + if ($organizationId !== $PocId) { + self::createActivity($organizationId, $activityData); + } + + } catch (\CiviCRM_API4_Exception $ex) { + \Civi::log()->debug("Exception while creating Organize Collection Camp activity: " . $ex->getMessage()); + } +} + +private static function createActivity($contactId, $activityData) { + Activity::create(FALSE) + ->addValue('source_contact_id', $contactId) + ->addValue('target_contact_id', $contactId) + ->addValues($activityData) + ->execute(); +} + + /** * */ diff --git a/wp-content/civi-extensions/goonjcustom/Civi/InstitutionService.php b/wp-content/civi-extensions/goonjcustom/Civi/InstitutionService.php index 19ccb2f3c..14ac12ed3 100644 --- a/wp-content/civi-extensions/goonjcustom/Civi/InstitutionService.php +++ b/wp-content/civi-extensions/goonjcustom/Civi/InstitutionService.php @@ -28,7 +28,7 @@ public static function getSubscribedEvents() { '&hook_civicrm_post' => [ ['organizationCreated'], ['setOfficeDetails'], - ['assignChapterGroupToIndividual'], + // ['assignChapterGroupToIndividual'], ], ]; } @@ -89,52 +89,52 @@ private static function 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; -} +// 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; +// } /** From 5d0c1b173bce9bb97e53f43e23405daea80ec7b9 Mon Sep 17 00:00:00 2001 From: belwalshubham Date: Fri, 13 Dec 2024 21:47:28 +0530 Subject: [PATCH 3/3] link institution collection camp to contact --- .../Civi/InstitutionCollectionCampService.php | 82 ++++++++++--------- 1 file changed, 44 insertions(+), 38 deletions(-) diff --git a/wp-content/civi-extensions/goonjcustom/Civi/InstitutionCollectionCampService.php b/wp-content/civi-extensions/goonjcustom/Civi/InstitutionCollectionCampService.php index 7a3b0e466..679771c82 100644 --- a/wp-content/civi-extensions/goonjcustom/Civi/InstitutionCollectionCampService.php +++ b/wp-content/civi-extensions/goonjcustom/Civi/InstitutionCollectionCampService.php @@ -35,7 +35,7 @@ public static function getSubscribedEvents() { '&hook_civicrm_pre' => [ ['assignChapterGroupToIndividual'], ['generateInstitutionCollectionCampQr'], - ['linkCollectionCampToContact'], + ['linkInstitutionCollectionCampToContact'], ], '&hook_civicrm_custom' => [ ['setOfficeDetails'], @@ -45,71 +45,77 @@ public static function getSubscribedEvents() { ]; } - public static function linkCollectionCampToContact(string $op, string $objectName, $objectId, &$objectRef) { + /** + * + */ + public static function linkInstitutionCollectionCampToContact(string $op, string $objectName, $objectId, &$objectRef) { if ($objectName !== 'Eck_Collection_Camp' || !$objectId) { - return; + return; } $newStatus = $objectRef['Collection_Camp_Core_Details.Status'] ?? ''; if (!$newStatus) { - return; + 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(); + ->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; + return; } $collectionCampTitle = $currentCollectionCamp['title']; $collectionCampId = $currentCollectionCamp['id']; - // Check for status change and handle activity creation if ($currentStatus !== $newStatus && $newStatus === 'authorized') { - self::createCollectionCampOrganizeActivity($PocId, $organizationId, $collectionCampTitle, $collectionCampId); + self::createCollectionCampOrganizeActivity($PocId, $organizationId, $collectionCampTitle, $collectionCampId); } -} + } -private static function createCollectionCampOrganizeActivity($PocId, $organizationId, $collectionCampTitle, $collectionCampId) { + /** + * + */ + private static function createCollectionCampOrganizeActivity($PocId, $organizationId, $collectionCampTitle, $collectionCampId) { try { - // Prepare the activity data - $activityData = [ - 'subject' => $collectionCampTitle, - 'activity_type_id:name' => 'Organize Collection Camp', - 'status_id:name' => 'Authorized', - 'activity_date_time' => date('Y-m-d H:i:s'), - 'Collection_Camp_Data.Collection_Camp_ID' => $collectionCampId - ]; - - // Create activity for PocId - self::createActivity($PocId, $activityData); - - // Create activity for organizationId, only if it's different from PocId - if ($organizationId !== $PocId) { - self::createActivity($organizationId, $activityData); - } - } catch (\CiviCRM_API4_Exception $ex) { - \Civi::log()->debug("Exception while creating Organize Collection Camp activity: " . $ex->getMessage()); + // 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, $activityData) { + /** + * + */ + private static function createActivity($contactId, $collectionCampTitle, $collectionCampId) { Activity::create(FALSE) - ->addValue('source_contact_id', $contactId) - ->addValue('target_contact_id', $contactId) - ->addValues($activityData) - ->execute(); -} + ->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}"); + } /** *