-
Notifications
You must be signed in to change notification settings - Fork 1
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
Material contibution #354
base: develop
Are you sure you want to change the base?
Material contibution #354
Changes from all commits
b3e8d3d
4e51228
9eb62a1
42eab60
a962288
34398b4
c5d9e45
550660f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
} | ||
Comment on lines
+547
to
+552
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Eliminate code duplication by using a helper method The code in lines 547-552 duplicates the logic for retrieving and checking Update the - $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;
- }
+ $collectionCampSubtype = self::getCollectionCampSubtype($collectionCampId);
+ if ($collectionCampSubtype === null) {
+ return;
+ } Ensure that the new helper method handles logging appropriately based on the context (e.g., use different log levels if necessary).
|
||
|
||
if ($status !== 'authorized' || $collectionCampQr !== NULL) { | ||
return; | ||
} | ||
|
||
self::generateQrCode($collectionCampId); | ||
QrCodeService::generateQrCode($collectionCampId, $collectionCampSubtype); | ||
|
||
} | ||
catch (\Exception $e) { | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,109 @@ | ||||||||||||||||||
<?php | ||||||||||||||||||
|
||||||||||||||||||
namespace Civi; | ||||||||||||||||||
|
||||||||||||||||||
use Civi\Api4\CustomField; | ||||||||||||||||||
use Civi\Core\Service\AutoSubscriber; | ||||||||||||||||||
use chillerlan\QRCode\QRCode; | ||||||||||||||||||
use chillerlan\QRCode\QROptions; | ||||||||||||||||||
|
||||||||||||||||||
/** | ||||||||||||||||||
* | ||||||||||||||||||
*/ | ||||||||||||||||||
class QrCodeService extends AutoSubscriber { | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary Extension of The |
||||||||||||||||||
|
||||||||||||||||||
const DROPPING_CENTER_URL_PATTERN = "%sactions/dropping-center/%s"; | ||||||||||||||||||
const COLLECTION_CAMP_URL_PATTERN = "%sactions/collection-camp/%s"; | ||||||||||||||||||
|
||||||||||||||||||
/** | ||||||||||||||||||
* | ||||||||||||||||||
*/ | ||||||||||||||||||
public static function getSubscribedEvents() { | ||||||||||||||||||
return []; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
/** | ||||||||||||||||||
* | ||||||||||||||||||
*/ | ||||||||||||||||||
public static function generateQrCode($collectionCampId, $collectionCampSubtype) { | ||||||||||||||||||
try { | ||||||||||||||||||
$baseUrl = \CRM_Core_Config::singleton()->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; | ||||||||||||||||||
} | ||||||||||||||||||
Comment on lines
+92
to
+95
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improve Error Logging for API Failures When the API call to create the attachment fails, the error message lacks specific details. Modify the error logging to include the API error message: if (!empty($result['is_error'])) {
- \CRM_Core_Error::debug_log_message('Failed to create attachment for collection camp ID ' . $collectionCampId);
+ \CRM_Core_Error::debug_log_message('Failed to create attachment for collection camp ID ' . $collectionCampId . '. Error: ' . $result['error_message']);
return FALSE;
} This provides clearer insight into the cause of the failure, aiding in faster debugging. 📝 Committable suggestion
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
$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; | ||||||||||||||||||
} | ||||||||||||||||||
Comment on lines
+28
to
+107
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Method The
Consider refactoring this method into smaller, dedicated methods for each responsibility. This will enhance readability, maintainability, and testability of the code. Refactoring suggestions:
By breaking down the method, each part becomes more modular and easier to maintain. |
||||||||||||||||||
|
||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+42
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Refactor link generation to eliminate duplicate code The link generation for 'dropping-center' and 'collection-camp' shares similar structures. To adhere to the DRY (Don't Repeat Yourself) principle and enhance maintainability, consider abstracting this logic into a reusable function. You can create a function like this: function generate_contribution_link($type, $action_target) {
if ($type === 'collection-camp') {
return sprintf(
'/collection-camp-contribution?source=%s&target_id=%s&state_province_id=%s&city=%s',
$action_target['title'],
$action_target['id'],
$action_target['Collection_Camp_Intent_Details.State'],
$action_target['Collection_Camp_Intent_Details.City'],
);
} elseif ($type === 'dropping-center') {
return sprintf(
'/dropping-center-contribution?source=%s&target_id=%s',
$action_target['title'],
$action_target['id'],
);
}
return '';
} Then, update the assignments: -$material_contribution_link = sprintf(
- '/collection-camp-contribution?source=%s&target_id=%s&state_province_id=%s&city=%s',
- $action_target['title'],
- $action_target['id'],
- $action_target['Collection_Camp_Intent_Details.State'],
- $action_target['Collection_Camp_Intent_Details.City'],
-);
-
-$dropping_center_material_contribution_link = sprintf(
- '/dropping-center-contribution?source=%s&target_id=%s',
- $action_target['title'],
- $action_target['id'],
-);
+ $contribution_link = generate_contribution_link($target, $action_target); And adjust the |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
?> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
$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 '<div class="error">An error occurred. Please try again later.</div>'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+62
to
+74
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improve exception handling to protect sensitive information While the try-catch block effectively handles exceptions, logging the error message may expose sensitive information. Consider logging only essential details and providing a user-friendly message. Apply this change to safeguard sensitive data: catch (Exception $e) {
- \Civi::log()->error('Invalid date format for start or end time', ['error' => $e->getMessage(), 'target' => $target]);
+ \Civi::log()->error('Invalid date format for start or end time', ['target' => $target]);
echo '<div class="error">An error occurred. Please try again later.</div>';
return;
} This modification ensures that detailed exception messages aren't exposed, aligning with best security practices. 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
?> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
<div class="wp-block-gb-heading-wrapper"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
<h2 class="wp-block-gb-heading"><?php echo esc_html($heading_text); ?></h2> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
</div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -68,7 +96,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||
<a href="<?php echo esc_url( $register_link ); ?>" class="wp-block-gb-action-button"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
<?php esc_html_e( 'Volunteer with Goonj', 'goonj-blocks' ); ?> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
</a> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
<a href="<?php echo esc_url( $material_contribution_link ); ?>" class="wp-block-gb-action-button"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
<a href="<?php echo esc_url( $contribution_link ); ?>" class="wp-block-gb-action-button"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
<?php esc_html_e( 'Record your Material Contribution', 'goonj-blocks' ); ?> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
</a> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
</div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
], | ||
]; | ||
Comment on lines
+48
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider including 'processing-center' in The current implementation handles 'dropping-center' and 'collection-camp' using the Here's a suggested change: ],
'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,
],
+ 'processing-center' => [
+ 'address' => $action_target['address'],
+ 'visit_link' => $pu_visit_check_link,
+ 'contribution_link' => $pu_material_contribution_check_link,
+ ],
]; Adjust the subsequent code to utilize the
|
||
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 '<div class="error">An error occurred. Please try again later.</div>'; | ||
return; | ||
} | ||
Comment on lines
+63
to
+74
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove unnecessary error handling around Previous team discussions concluded that error handling for Apply this diff to remove the -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 '<div class="error">An error occurred. Please try again later.</div>';
- return;
- }
+if (isset($target_config[$target])) :
+ $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']; This change removes the unnecessary error handling, streamlining the code.
|
||
?> | ||
<div class="wp-block-gb-heading-wrapper"> | ||
<h2 class="wp-block-gb-heading"><?php echo esc_html($heading_text); ?></h2> | ||
</div> | ||
|
@@ -68,7 +96,7 @@ | |
<a href="<?php echo esc_url( $register_link ); ?>" class="wp-block-gb-action-button"> | ||
<?php esc_html_e( 'Volunteer with Goonj', 'goonj-blocks' ); ?> | ||
</a> | ||
<a href="<?php echo esc_url( $material_contribution_link ); ?>" class="wp-block-gb-action-button"> | ||
<a href="<?php echo esc_url( $contribution_link ); ?>" class="wp-block-gb-action-button"> | ||
<?php esc_html_e( 'Record your Material Contribution', 'goonj-blocks' ); ?> | ||
</a> | ||
</div> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Refactor duplicated code for handling
$collectionCampSubtype
The logic in lines 505-510 for retrieving and validating
$collectionCampSubtype
is duplicated in thereGenerateCollectionCampQr
method (lines 547-552). To adhere to the DRY (Don't Repeat Yourself) principle and improve maintainability, consider extracting this code into a reusable private method.Apply this diff to refactor the duplicated code:
Then update the
generateCollectionCampQr
method:📝 Committable suggestion