From b3e8d3dbbb2e8e4710833944f349603bc909b80e Mon Sep 17 00:00:00 2001 From: belwalshubham Date: Wed, 9 Oct 2024 20:08:05 +0530 Subject: [PATCH 1/7] fix(*): refactor qr code generate function --- .../Civi/CollectionCampService.php | 94 ++------------- .../goonjcustom/Civi/QrCodeService.php | 109 ++++++++++++++++++ 2 files changed, 119 insertions(+), 84 deletions(-) create mode 100644 wp-content/civi-extensions/goonjcustom/Civi/QrCodeService.php diff --git a/wp-content/civi-extensions/goonjcustom/Civi/CollectionCampService.php b/wp-content/civi-extensions/goonjcustom/Civi/CollectionCampService.php index 24b3c3d7c..a998ef3aa 100644 --- a/wp-content/civi-extensions/goonjcustom/Civi/CollectionCampService.php +++ b/wp-content/civi-extensions/goonjcustom/Civi/CollectionCampService.php @@ -19,6 +19,7 @@ use Civi\Api4\StateProvince; use Civi\Api4\Utils\CoreUtil; use Civi\Core\Service\AutoSubscriber; +use Civi\QrCodeService; /** * @@ -439,6 +440,7 @@ public static function linkCollectionCampToContact(string $op, string $objectNam $contactId = $currentCollectionCamp['Collection_Camp_Core_Details.Contact_Id']; $collectionCampTitle = $currentCollectionCamp['title']; $collectionCampId = $currentCollectionCamp['id']; + $collectionCampSubtype = $currentCollectionCamp['subtype:name']; // Check for status change. if ($currentStatus !== $newStatus) { @@ -493,104 +495,28 @@ public static function generateCollectionCampQr(string $op, string $objectName, } $collectionCamps = EckEntity::get('Collection_Camp', TRUE) - ->addSelect('Collection_Camp_Core_Details.Status', 'Collection_Camp_Core_Details.Contact_Id') + ->addSelect('Collection_Camp_Core_Details.Status', 'Collection_Camp_Core_Details.Contact_Id', 'subtype:name') ->addWhere('id', '=', $objectId) ->execute(); $currentCollectionCamp = $collectionCamps->first(); $currentStatus = $currentCollectionCamp['Collection_Camp_Core_Details.Status']; $collectionCampId = $currentCollectionCamp['id']; + $collectionCampSubtype = $currentCollectionCamp['subtype:name']; + + if (empty($collectionCampSubtype)) { + \Civi::log()->warning('Collection camp subtype is not set or is empty for Collection Camp ID: ' . $collectionCampId); + return; + } // Check for status change. if ($currentStatus !== $newStatus) { if ($newStatus === 'authorized') { - self::generateQrCode($collectionCampId); + QrCodeService::generateQrCode($collectionCampId, $collectionCampSubtype); } } } - /** - * - */ - public static function generateQrCode($collectionCampId) { - - try { - $baseUrl = \CRM_Core_Config::singleton()->userFrameworkBaseURL; - $url = "{$baseUrl}actions/collection-camp/{$collectionCampId}"; - - $options = new QROptions([ - 'version' => 5, - 'outputType' => QRCode::OUTPUT_IMAGE_PNG, - 'eccLevel' => QRCode::ECC_L, - 'scale' => 10, - ]); - - $qrcode = (new QRCode($options))->render($url); - - // Remove the base64 header and decode the image data. - $qrcode = str_replace('data:image/png;base64,', '', $qrcode); - - $qrcode = base64_decode($qrcode); - - $baseFileName = "qr_code_{$collectionCampId}.png"; - - $fileName = \CRM_Utils_File::makeFileName($baseFileName); - - $tempFilePath = \CRM_Utils_File::tempnam($baseFileName); - - $numBytes = file_put_contents($tempFilePath, $qrcode); - - if (!$numBytes) { - \CRM_Core_Error::debug_log_message('Failed to write QR code to temporary file for collection camp ID ' . $collectionCampId); - return FALSE; - } - - $customFields = CustomField::get(FALSE) - ->addSelect('id') - ->addWhere('custom_group_id:name', '=', 'Collection_Camp_QR_Code') - ->addWhere('name', '=', 'QR_Code') - ->setLimit(1) - ->execute(); - - $qrField = $customFields->first(); - - if (!$qrField) { - \CRM_Core_Error::debug_log_message('No field to save QR Code for collection camp ID ' . $collectionCampId); - return FALSE; - } - - $qrFieldId = 'custom_' . $qrField['id']; - - // Save the QR code as an attachment linked to the collection camp. - $params = [ - 'entity_id' => $collectionCampId, - 'name' => $fileName, - 'mime_type' => 'image/png', - 'field_name' => $qrFieldId, - 'options' => [ - 'move-file' => $tempFilePath, - ], - ]; - - $result = civicrm_api3('Attachment', 'create', $params); - - if (empty($result['id'])) { - \CRM_Core_Error::debug_log_message('Failed to create attachment for collection camp ID ' . $collectionCampId); - return FALSE; - } - - $attachment = $result['values'][$result['id']]; - - $attachmentUrl = $attachment['url']; - } - catch (\CiviCRM_API3_Exception $e) { - \CRM_Core_Error::debug_log_message('Error generating QR code: ' . $e->getMessage()); - return FALSE; - } - - return TRUE; - } - /** * This hook is called after a db write on entities. * diff --git a/wp-content/civi-extensions/goonjcustom/Civi/QrCodeService.php b/wp-content/civi-extensions/goonjcustom/Civi/QrCodeService.php new file mode 100644 index 000000000..d09ba10ca --- /dev/null +++ b/wp-content/civi-extensions/goonjcustom/Civi/QrCodeService.php @@ -0,0 +1,109 @@ +userFrameworkBaseURL; + + if ($collectionCampSubtype === 'Dropping_Center') { + $url = sprintf(self::DROPPING_CENTER_URL_PATTERN, $baseUrl, $collectionCampId); + } + else { + $url = sprintf(self::COLLECTION_CAMP_URL_PATTERN, $baseUrl, $collectionCampId); + } + + $options = new QROptions([ + 'version' => 5, + 'outputType' => QRCode::OUTPUT_IMAGE_PNG, + 'eccLevel' => QRCode::ECC_L, + 'scale' => 10, + ]); + + $qrcode = (new QRCode($options))->render($url); + + // Remove the base64 header and decode the image data. + $qrcode = str_replace('data:image/png;base64,', '', $qrcode); + $qrcode = base64_decode($qrcode); + + $baseFileName = "qr_code_{$collectionCampId}.png"; + $fileName = \CRM_Utils_File::makeFileName($baseFileName); + $tempFilePath = \CRM_Utils_File::tempnam($baseFileName); + + $numBytes = file_put_contents($tempFilePath, $qrcode); + + if (!$numBytes) { + \CRM_Core_Error::debug_log_message('Failed to write QR code to temporary file for collection camp ID ' . $collectionCampId); + return FALSE; + } + + $customFields = CustomField::get(FALSE) + ->addSelect('id') + ->addWhere('custom_group_id:name', '=', 'Collection_Camp_QR_Code') + ->addWhere('name', '=', 'QR_Code') + ->setLimit(1) + ->execute(); + + $qrField = $customFields->first(); + + if (!$qrField) { + \CRM_Core_Error::debug_log_message('No field to save QR Code for collection camp ID ' . $collectionCampId); + return FALSE; + } + + $qrFieldId = 'custom_' . $qrField['id']; + + // Save the QR code as an attachment linked to the collection camp. + $params = [ + 'entity_id' => $collectionCampId, + 'name' => $fileName, + 'mime_type' => 'image/png', + 'field_name' => $qrFieldId, + 'options' => [ + 'move-file' => $tempFilePath, + ], + ]; + + $result = civicrm_api3('Attachment', 'create', $params); + + if (empty($result['id'])) { + \CRM_Core_Error::debug_log_message('Failed to create attachment for collection camp ID ' . $collectionCampId); + return FALSE; + } + + $attachment = $result['values'][$result['id']]; + $attachmentUrl = $attachment['url']; + + } + catch (\CiviCRM_API3_Exception $e) { + \CRM_Core_Error::debug_log_message('Error generating QR code: ' . $e->getMessage()); + return FALSE; + } + + return TRUE; + } + +} From 4e51228b525c5be316192c6ba95d22688d11de53 Mon Sep 17 00:00:00 2001 From: belwalshubham Date: Wed, 9 Oct 2024 20:27:30 +0530 Subject: [PATCH 2/7] fix(*): code cleanups --- .../goonjcustom/Civi/CollectionCampService.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/wp-content/civi-extensions/goonjcustom/Civi/CollectionCampService.php b/wp-content/civi-extensions/goonjcustom/Civi/CollectionCampService.php index a998ef3aa..9bf21132a 100644 --- a/wp-content/civi-extensions/goonjcustom/Civi/CollectionCampService.php +++ b/wp-content/civi-extensions/goonjcustom/Civi/CollectionCampService.php @@ -538,18 +538,24 @@ public static function reGenerateCollectionCampQr(string $op, string $objectName try { $collectionCampId = $objectRef->id; $collectionCamp = EckEntity::get('Collection_Camp', TRUE) - ->addSelect('Collection_Camp_Core_Details.Status', 'Collection_Camp_QR_Code.QR_Code') + ->addSelect('Collection_Camp_Core_Details.Status', 'Collection_Camp_QR_Code.QR_Code', 'subtype:name') ->addWhere('id', '=', $collectionCampId) ->execute()->single(); $status = $collectionCamp['Collection_Camp_Core_Details.Status']; $collectionCampQr = $collectionCamp['Collection_Camp_QR_Code.QR_Code']; + $collectionCampSubtype = $collectionCamp['subtype:name']; + if (empty($collectionCampSubtype)) { + \Civi::log()->error('Collection camp subtype is not set or is empty for Collection Camp ID: ' . $collectionCampId); + return; + } + if ($status !== 'authorized' || $collectionCampQr !== NULL) { return; } - self::generateQrCode($collectionCampId); + QrCodeService::generateQrCode($collectionCampId, $collectionCampSubtype); } catch (\Exception $e) { From 9eb62a1952fd27a61d6874c331c77dc7bc712c33 Mon Sep 17 00:00:00 2001 From: belwalshubham Date: Wed, 9 Oct 2024 20:57:03 +0530 Subject: [PATCH 3/7] resolve feedbacks --- wp-content/civi-extensions/goonjcustom/Civi/QrCodeService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-content/civi-extensions/goonjcustom/Civi/QrCodeService.php b/wp-content/civi-extensions/goonjcustom/Civi/QrCodeService.php index d09ba10ca..a329777e1 100644 --- a/wp-content/civi-extensions/goonjcustom/Civi/QrCodeService.php +++ b/wp-content/civi-extensions/goonjcustom/Civi/QrCodeService.php @@ -89,7 +89,7 @@ public static function generateQrCode($collectionCampId, $collectionCampSubtype) $result = civicrm_api3('Attachment', 'create', $params); - if (empty($result['id'])) { + if (!empty($result['is_error'])) { \CRM_Core_Error::debug_log_message('Failed to create attachment for collection camp ID ' . $collectionCampId); return FALSE; } From 42eab609cc6c728bd4d73db7515b14c430434d2e Mon Sep 17 00:00:00 2001 From: belwalshubham Date: Wed, 9 Oct 2024 20:58:06 +0530 Subject: [PATCH 4/7] fix(*): cleanups --- wp-content/civi-extensions/goonjcustom/Civi/QrCodeService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-content/civi-extensions/goonjcustom/Civi/QrCodeService.php b/wp-content/civi-extensions/goonjcustom/Civi/QrCodeService.php index a329777e1..69ec27602 100644 --- a/wp-content/civi-extensions/goonjcustom/Civi/QrCodeService.php +++ b/wp-content/civi-extensions/goonjcustom/Civi/QrCodeService.php @@ -98,7 +98,7 @@ public static function generateQrCode($collectionCampId, $collectionCampSubtype) $attachmentUrl = $attachment['url']; } - catch (\CiviCRM_API3_Exception $e) { + catch (\Exception $e) { \CRM_Core_Error::debug_log_message('Error generating QR code: ' . $e->getMessage()); return FALSE; } From a962288965db4e831edca65f7644efa0296911f2 Mon Sep 17 00:00:00 2001 From: belwalshubham Date: Thu, 10 Oct 2024 10:37:45 +0530 Subject: [PATCH 5/7] Refactor date and address handling logic for collection camps and dropping center --- .../plugins/goonj-blocks/build/render.php | 31 ++++++++++++++++--- .../plugins/goonj-blocks/goonj-blocks.php | 3 ++ .../plugins/goonj-blocks/src/render.php | 31 ++++++++++++++++--- 3 files changed, 57 insertions(+), 8 deletions(-) diff --git a/wp-content/plugins/goonj-blocks/build/render.php b/wp-content/plugins/goonj-blocks/build/render.php index d0658b5e8..e1df1e1b1 100644 --- a/wp-content/plugins/goonj-blocks/build/render.php +++ b/wp-content/plugins/goonj-blocks/build/render.php @@ -35,11 +35,34 @@ $action_target['id'] ); -if ( in_array( $target, array( 'collection-camp', 'dropping-center' ) ) ) : - $start_date = new DateTime( $action_target['Collection_Camp_Intent_Details.Start_Date'] ); - $end_date = new DateTime( $action_target['Collection_Camp_Intent_Details.End_Date'] ); - $address = $action_target['Collection_Camp_Intent_Details.Location_Area_of_camp']; +$target_config = [ + 'dropping-center' => [ + 'start_time' => 'Dropping_Centre.Start_Time', + 'end_time' => 'Dropping_Centre.End_Time', + 'address' => 'Dropping_Centre.Where_do_you_wish_to_open_dropping_center_Address_', + 'contribution_link' => $dropping_center_material_contribution_link, + ], + 'collection-camp' => [ + 'start_time' => 'Collection_Camp_Intent_Details.Start_Date', + 'end_time' => 'Collection_Camp_Intent_Details.End_Date', + 'address' => 'Collection_Camp_Intent_Details.Location_Area_of_camp', + 'contribution_link' => $material_contribution_link, + ], + ]; +if (isset($target_config[$target])) : + try { + $config = $target_config[$target]; + $start_date = new DateTime($action_target[$config['start_time']]); + $end_date = new DateTime($action_target[$config['end_time']]); + $address = $action_target[$config['address']]; + $contribution_link = $config['contribution_link']; + } + catch (Exception $e) { + \Civi::log()->error('Invalid date format for start or end time', ['error' => $e->getMessage(), 'target' => $target]); + echo '
An error occurred. Please try again later.
'; + return; + } ?>

diff --git a/wp-content/plugins/goonj-blocks/goonj-blocks.php b/wp-content/plugins/goonj-blocks/goonj-blocks.php index 97e640a85..63a84a382 100644 --- a/wp-content/plugins/goonj-blocks/goonj-blocks.php +++ b/wp-content/plugins/goonj-blocks/goonj-blocks.php @@ -86,6 +86,9 @@ function gb_goonj_blocks_check_action_target_exists() { 'Collection_Camp_Intent_Details.Start_Date', 'Collection_Camp_Intent_Details.End_Date', 'Collection_Camp_Intent_Details.Location_Area_of_camp', + 'Dropping_Centre.Start_Time', + 'Dropping_Centre.End_Time', + 'Dropping_Centre.Where_do_you_wish_to_open_dropping_center_Address_', ); switch ( $target ) { diff --git a/wp-content/plugins/goonj-blocks/src/render.php b/wp-content/plugins/goonj-blocks/src/render.php index d0658b5e8..e1df1e1b1 100644 --- a/wp-content/plugins/goonj-blocks/src/render.php +++ b/wp-content/plugins/goonj-blocks/src/render.php @@ -35,11 +35,34 @@ $action_target['id'] ); -if ( in_array( $target, array( 'collection-camp', 'dropping-center' ) ) ) : - $start_date = new DateTime( $action_target['Collection_Camp_Intent_Details.Start_Date'] ); - $end_date = new DateTime( $action_target['Collection_Camp_Intent_Details.End_Date'] ); - $address = $action_target['Collection_Camp_Intent_Details.Location_Area_of_camp']; +$target_config = [ + 'dropping-center' => [ + 'start_time' => 'Dropping_Centre.Start_Time', + 'end_time' => 'Dropping_Centre.End_Time', + 'address' => 'Dropping_Centre.Where_do_you_wish_to_open_dropping_center_Address_', + 'contribution_link' => $dropping_center_material_contribution_link, + ], + 'collection-camp' => [ + 'start_time' => 'Collection_Camp_Intent_Details.Start_Date', + 'end_time' => 'Collection_Camp_Intent_Details.End_Date', + 'address' => 'Collection_Camp_Intent_Details.Location_Area_of_camp', + 'contribution_link' => $material_contribution_link, + ], + ]; +if (isset($target_config[$target])) : + try { + $config = $target_config[$target]; + $start_date = new DateTime($action_target[$config['start_time']]); + $end_date = new DateTime($action_target[$config['end_time']]); + $address = $action_target[$config['address']]; + $contribution_link = $config['contribution_link']; + } + catch (Exception $e) { + \Civi::log()->error('Invalid date format for start or end time', ['error' => $e->getMessage(), 'target' => $target]); + echo '
An error occurred. Please try again later.
'; + return; + } ?>

From c5d9e4562056f9c08d0dc8926e226b4dd03dbec6 Mon Sep 17 00:00:00 2001 From: belwalshubham Date: Thu, 10 Oct 2024 10:42:35 +0530 Subject: [PATCH 6/7] fix(*): code cleanups --- wp-content/plugins/goonj-blocks/build/render.php | 7 +++---- wp-content/plugins/goonj-blocks/src/render.php | 7 +++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/wp-content/plugins/goonj-blocks/build/render.php b/wp-content/plugins/goonj-blocks/build/render.php index f4a794bd3..7d92736bb 100644 --- a/wp-content/plugins/goonj-blocks/build/render.php +++ b/wp-content/plugins/goonj-blocks/build/render.php @@ -53,8 +53,7 @@ 'contribution_link' => $material_contribution_link, ], ]; - -if (isset($target_config[$target])) : + if (isset($target_config[$target])) : try { $config = $target_config[$target]; $start_date = new DateTime($action_target[$config['start_time']]); @@ -66,8 +65,8 @@ \Civi::log()->error('Invalid date format for start or end time', ['error' => $e->getMessage(), 'target' => $target]); echo '
An error occurred. Please try again later.
'; return; - } - ?> + } + ?>

diff --git a/wp-content/plugins/goonj-blocks/src/render.php b/wp-content/plugins/goonj-blocks/src/render.php index f4a794bd3..7d92736bb 100644 --- a/wp-content/plugins/goonj-blocks/src/render.php +++ b/wp-content/plugins/goonj-blocks/src/render.php @@ -53,8 +53,7 @@ 'contribution_link' => $material_contribution_link, ], ]; - -if (isset($target_config[$target])) : + if (isset($target_config[$target])) : try { $config = $target_config[$target]; $start_date = new DateTime($action_target[$config['start_time']]); @@ -66,8 +65,8 @@ \Civi::log()->error('Invalid date format for start or end time', ['error' => $e->getMessage(), 'target' => $target]); echo '
An error occurred. Please try again later.
'; return; - } - ?> + } + ?>

From 550660f3df86be8808b2b4aa17c6f70a63698696 Mon Sep 17 00:00:00 2001 From: belwalshubham Date: Thu, 10 Oct 2024 11:15:07 +0530 Subject: [PATCH 7/7] update contribution link --- wp-content/plugins/goonj-blocks/build/render.php | 8 +++++++- wp-content/plugins/goonj-blocks/src/render.php | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/wp-content/plugins/goonj-blocks/build/render.php b/wp-content/plugins/goonj-blocks/build/render.php index 7d92736bb..9221e257d 100644 --- a/wp-content/plugins/goonj-blocks/build/render.php +++ b/wp-content/plugins/goonj-blocks/build/render.php @@ -39,6 +39,12 @@ $action_target['id'] ); +$dropping_center_material_contribution_link = sprintf( + '/dropping-center-contribution?source=%s&target_id=%s', + $action_target['title'], + $action_target['id'], +); + $target_config = [ 'dropping-center' => [ 'start_time' => 'Dropping_Centre.Start_Time', @@ -90,7 +96,7 @@ - +
diff --git a/wp-content/plugins/goonj-blocks/src/render.php b/wp-content/plugins/goonj-blocks/src/render.php index 7d92736bb..9221e257d 100644 --- a/wp-content/plugins/goonj-blocks/src/render.php +++ b/wp-content/plugins/goonj-blocks/src/render.php @@ -39,6 +39,12 @@ $action_target['id'] ); +$dropping_center_material_contribution_link = sprintf( + '/dropping-center-contribution?source=%s&target_id=%s', + $action_target['title'], + $action_target['id'], +); + $target_config = [ 'dropping-center' => [ 'start_time' => 'Dropping_Centre.Start_Time', @@ -90,7 +96,7 @@ - +