From: demeritcowboy Date: Mon, 8 Aug 2022 21:02:18 +0000 (-0400) Subject: Merge pull request #24178 from demeritcowboy/php81-frontend3 X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=0fa05f712d83ee89a627e62afc1f181d1f9a7b96;hp=86029386c06e30dedeb2e9fede9421d6bd809104;p=civicrm-core.git Merge pull request #24178 from demeritcowboy/php81-frontend3 [php 8.1 compat] Avoid passing null to strlen --- diff --git a/CRM/Financial/BAO/Order.php b/CRM/Financial/BAO/Order.php index 2397f483ce..941f65e9e4 100644 --- a/CRM/Financial/BAO/Order.php +++ b/CRM/Financial/BAO/Order.php @@ -458,12 +458,6 @@ class CRM_Financial_BAO_Order { foreach ($this->getPriceOptions() as $fieldID => $valueID) { $this->setPriceSetIDFromSelectedField($fieldID); } - if (!$this->priceSetID && $this->getTemplateContributionID()) { - // Load the line items from the contribution. - foreach ($this->getLineItems() as $lineItem) { - return $lineItem['price_field_id.price_set_id']; - } - } } return $this->priceSetID; } @@ -580,7 +574,7 @@ class CRM_Financial_BAO_Order { * @return array */ public function getPriceFieldSpec(int $id) :array { - return $this->getPriceFieldsMetadata()[$id]; + return $this->getPriceFieldsMetadata()[$id] ?? $this->getPriceFieldMetadata($id); } /** @@ -613,6 +607,28 @@ class CRM_Financial_BAO_Order { return $this->priceFieldMetadata; } + /** + * Get the metadata for the given price field. + * + * Note this uses a different method to getPriceFieldMetadata. + * + * There is an assumption in the code currently that all purchases + * are within a single price set. However, discussions have been around + * the idea that when form-builder supports contributions price sets will + * not be used as form-builder in itself is a configuration unit. + * + * Currently there are couple of unit tests that mix & match & rather than + * updating the tests to avoid notices when orders are loaded for receipting, + * the migration to this new method is starting.... + * + * @param int $id + * + * @return array + */ + public function getPriceFieldMetadata(int $id): array { + return CRM_Price_BAO_PriceField::getPriceField($id); + } + /** * Set the metadata for the order. * @@ -819,6 +835,10 @@ class CRM_Financial_BAO_Order { $params['financial_type_id'] = 0; if ($this->getTemplateContributionID()) { $lineItems = $this->getLinesFromTemplateContribution(); + // Set the price set ID from the first line item (we need to set this here + // to prevent a loop later when we retrieve the price field metadata to + // set the 'title' (as accessed from workflow message templates). + $this->setPriceSetID($lineItems[0]['price_field_id.price_set_id']); } else { foreach ($this->getPriceOptions() as $fieldID => $valueID) { @@ -855,7 +875,7 @@ class CRM_Financial_BAO_Order { elseif ($taxRate) { $lineItem['tax_amount'] = ($taxRate / 100) * $lineItem['line_total']; } - $lineItem['title'] = $lineItem['label']; + $lineItem['title'] = $this->getLineItemTitle($lineItem); } return $lineItems; } @@ -1001,15 +1021,7 @@ class CRM_Financial_BAO_Order { } } if (empty($lineItem['title'])) { - // Title is used in output for workflow templates. - $htmlType = !empty($this->priceFieldMetadata) ? $this->getPriceFieldSpec($lineItem['price_field_id'])['html_type'] : NULL; - $lineItem['title'] = (!$htmlType || $htmlType === 'Text') ? $lineItem['label'] : $this->getPriceFieldSpec($lineItem['price_field_id'])['label'] . ' : ' . $lineItem['label']; - if (!empty($lineItem['price_field_value_id'])) { - $description = $this->priceFieldValueMetadata[$lineItem['price_field_value_id']]['description'] ?? ''; - if ($description) { - $lineItem['title'] .= ' ' . CRM_Utils_String::ellipsify($description, 30); - } - } + $lineItem['title'] = $this->getLineItemTitle($lineItem); } $this->lineItems[$index] = $lineItem; } @@ -1211,4 +1223,27 @@ class CRM_Financial_BAO_Order { $lineItem = array_merge($defaults, $lineItem); } + /** + * Get a 'title' for the line item. + * + * This descriptor is used in message templates. It could conceivably + * by used elsewhere but if so determination would likely move to the api. + * + * @param array $lineItem + * + * @return string + */ + private function getLineItemTitle(array $lineItem): string { + // Title is used in output for workflow templates. + $htmlType = $this->getPriceFieldSpec($lineItem['price_field_id'])['html_type'] ?? NULL; + $lineItemTitle = (!$htmlType || $htmlType === 'Text') ? $lineItem['label'] : $this->getPriceFieldSpec($lineItem['price_field_id'])['label'] . ' - ' . $lineItem['label']; + if (!empty($lineItem['price_field_value_id'])) { + $description = $this->priceFieldValueMetadata[$lineItem['price_field_value_id']]['description'] ?? ''; + if ($description) { + $lineItemTitle .= ' ' . CRM_Utils_String::ellipsify($description, 30); + } + } + return $lineItemTitle; + } + } diff --git a/CRM/Price/BAO/PriceField.php b/CRM/Price/BAO/PriceField.php index aa03d74aee..9a3d87b762 100644 --- a/CRM/Price/BAO/PriceField.php +++ b/CRM/Price/BAO/PriceField.php @@ -9,6 +9,8 @@ +--------------------------------------------------------------------+ */ +use Civi\Api4\PriceField; + /** * Business objects for managing price fields. * @@ -164,6 +166,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField { } $transaction->commit(); + Civi::cache('metadata')->flush(); return $priceField; } @@ -872,4 +875,22 @@ WHERE id IN (" . implode(',', array_keys($priceFields)) . ')'; return 0; } + /** + * Get a specific price field (leveraging the cache). + * + * @param int $id + * + * @return array + * @noinspection PhpUnhandledExceptionInspection + * @noinspection PhpDocMissingThrowsInspection + */ + public static function getPriceField(int $id): array { + $cacheString = __CLASS__ . __FUNCTION__ . $id . CRM_Core_Config::domainID() . '_' . CRM_Core_I18n::getLocale(); + if (!Civi::cache('metadata')->has($cacheString)) { + $field = PriceField::get(FALSE)->addWhere('id', '=', $id)->execute()->first(); + Civi::cache('metadata')->set($cacheString, $field); + } + return Civi::cache('metadata')->get($cacheString); + } + } diff --git a/tests/phpunit/CRM/Contribute/Form/ContributionTest.php b/tests/phpunit/CRM/Contribute/Form/ContributionTest.php index 50febfabb8..e09880b290 100644 --- a/tests/phpunit/CRM/Contribute/Form/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/Form/ContributionTest.php @@ -712,7 +712,7 @@ Financial Type: Donation --------------------------------------------------------- Item Qty Each Subtotal Tax Rate Tax Amount Total ---------------------------------------------------------- -Price Field 1 1 $100.00 $100.00 10.00 % $10.00 $110.00 +Price Field - Price Field 1 1 $100.00 $100.00 10.00 % $10.00 $110.00 Amount before Tax : $100.00 @@ -737,7 +737,7 @@ Financial Type: Donation --------------------------------------------------------- Item Qty Each Total ---------------------------------------------------------- -Price Field 1 1 $100.00 $100.00 +Price Field - Price Field 1 1 $100.00 $100.00