diff --git a/wp-content/civi-extensions/goonjcustom/Civi/CollectionCampService.php b/wp-content/civi-extensions/goonjcustom/Civi/CollectionCampService.php index 24b3c3d7c..9bf21132a 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. * @@ -612,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) { 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..69ec27602 --- /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['is_error'])) { + \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 (\Exception $e) { + \CRM_Core_Error::debug_log_message('Error generating QR code: ' . $e->getMessage()); + return FALSE; + } + + return TRUE; + } + +} diff --git a/wp-content/plugins/goonj-blocks/build/render.php b/wp-content/plugins/goonj-blocks/build/render.php index fb4efc9f9..9221e257d 100644 --- a/wp-content/plugins/goonj-blocks/build/render.php +++ b/wp-content/plugins/goonj-blocks/build/render.php @@ -39,12 +39,40 @@ $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']; +$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', + '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; + } + ?>

@@ -68,7 +96,7 @@ - + diff --git a/wp-content/plugins/goonj-blocks/goonj-blocks.php b/wp-content/plugins/goonj-blocks/goonj-blocks.php index 8785d9f5f..0020b7112 100644 --- a/wp-content/plugins/goonj-blocks/goonj-blocks.php +++ b/wp-content/plugins/goonj-blocks/goonj-blocks.php @@ -88,6 +88,9 @@ function gb_goonj_blocks_check_action_target_exists() { 'Collection_Camp_Intent_Details.Location_Area_of_camp', 'Collection_Camp_Intent_Details.City', 'Collection_Camp_Intent_Details.State', + '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 fb4efc9f9..9221e257d 100644 --- a/wp-content/plugins/goonj-blocks/src/render.php +++ b/wp-content/plugins/goonj-blocks/src/render.php @@ -39,12 +39,40 @@ $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']; +$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', + '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; + } + ?>

@@ -68,7 +96,7 @@ - +