diff --git a/wp-content/civi-extensions/goonjcustom/Civi/InstitutionCollectionCampService.php b/wp-content/civi-extensions/goonjcustom/Civi/InstitutionCollectionCampService.php index 2ba0155f5..679771c82 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'], + ['linkInstitutionCollectionCampToContact'], ], '&hook_civicrm_custom' => [ ['setOfficeDetails'], @@ -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}"); + } + /** * */ diff --git a/wp-content/civi-extensions/goonjcustom/Civi/InstitutionService.php b/wp-content/civi-extensions/goonjcustom/Civi/InstitutionService.php index 1fbc0158d..14ac12ed3 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; @@ -26,7 +28,7 @@ public static function getSubscribedEvents() { '&hook_civicrm_post' => [ ['organizationCreated'], ['setOfficeDetails'], - ['assignChapterGroupToIndividual'], + // ['assignChapterGroupToIndividual'], ], ]; } @@ -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; +// } - if ($groupId) { - self::addContactToGroup($contactId, $groupId); - if ($organizationId) { - self::addContactToGroup($organizationId, $groupId); - } - } - } /** *