Skip to content
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

Update translation template and fix incorrect use of ts() #101

Merged
merged 6 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
<?php

namespace Civi\Twingle\Shop\BAO;

use Civi\Api4\PriceField;
use Civi\Twingle\Shop\DAO\TwingleProduct as TwingleProductDAO;
use Civi\Twingle\Shop\DAO\TwingleShop as TwingleShopDAO;
use Civi\Api4\PriceFieldValue;
use Civi\Twingle\Shop\Exceptions\ProductException;
use Civi\Twingle\Shop\Exceptions\ShopException;
use CRM_Core_Exception;
use CRM_Core_Transaction;
use CRM_Twingle_ExtensionUtil as E;
use CRM_Utils_Type;
use function Civi\Twingle\Shop\Utils\convert_int_to_bool;
use function Civi\Twingle\Shop\Utils\convert_str_to_date;
use function Civi\Twingle\Shop\Utils\convert_str_to_int;
Expand All @@ -24,7 +18,7 @@
* TwingleProduct BAO class.
* This class is used to implement the logic for the TwingleProduct entity.
*/
class TwingleProduct extends TwingleProductDAO {
class CRM_Twingle_BAO_TwingleProduct extends CRM_Twingle_DAO_TwingleProduct {

/**
* Name of this product.
Expand Down Expand Up @@ -421,14 +415,14 @@ public function getAttributes() {
* @param int $external_id
* External id of the product (by Twingle)
*
* @return TwingleProduct|null
* @return CRM_Twingle_BAO_TwingleProduct|null
* TwingleProduct object or NULL if not found
*
* @throws \Civi\Twingle\Shop\Exceptions\ProductException
* @throws \Civi\Core\Exception\DBQueryException
*/
public static function findByExternalId($external_id) {
$dao = TwingleShopDAO::executeQuery("SELECT * FROM civicrm_twingle_product WHERE external_id = %1",
$dao = CRM_Twingle_BAO_TwingleShop::executeQuery("SELECT * FROM civicrm_twingle_product WHERE external_id = %1",
[1 => [$external_id, 'String']]);
if ($dao->fetch()) {
$product = new self();
Expand Down Expand Up @@ -456,23 +450,24 @@ public function add($mode = 'create') {

// Try to lookup object in database
try {
$dao = TwingleShopDAO::executeQuery("SELECT * FROM civicrm_twingle_product WHERE external_id = %1",
$dao = CRM_Twingle_BAO_TwingleShop::executeQuery("SELECT * FROM civicrm_twingle_product WHERE external_id = %1",
[1 => [$this->external_id, 'String']]);
if ($dao->fetch()) {
$this->copyValues(array_merge($dao->toArray(), $this->getAttributes()));
}
}
catch (\Civi\Core\Exception\DBQueryException $e) {
throw new ProductException(
E::ts('Could not find TwingleProduct in database: ' . $e->getMessage()),
E::ts('Could not find TwingleProduct in database: %1', [1 => $e->getMessage()]),
ShopException::ERROR_CODE_COULD_NOT_FIND_SHOP_IN_DB);
}

// Register pre-hook
$twingle_product_values = $this->getAttributes();
try {
\CRM_Utils_Hook::pre($mode, 'TwingleProduct', $this->id, $twingle_product_values);
} catch (\Exception $e) {
}
catch (\Exception $e) {
$tx->rollback();
throw $e;
}
Expand All @@ -487,14 +482,15 @@ public function add($mode = 'create') {
// Save object to database
try {
$this->save();
} catch (\Exception $e) {
}
catch (\Exception $e) {
$tx->rollback();
throw new ProductException(
E::ts('Could not save TwingleProduct to database: ' . $e->getMessage()),
E::ts('Could not save TwingleProduct to database: %1', [1 => $e->getMessage()]),
ProductException::ERROR_CODE_COULD_NOT_CREATE_PRODUCT);
}
$result = self::findById($this->id);
/* @var self $result */
/** @var self $result */
$this->load($result->getAttributes());

// Register post-hook
Expand All @@ -514,7 +510,7 @@ public function add($mode = 'create') {
/**
* Delete TwingleProduct along with associated PriceField and PriceFieldValue.
*
* @override \Civi\Twingle\Shop\DAO\TwingleProduct::delete
* @override \CRM_Twingle_DAO_TwingleProduct::delete
* @throws \CRM_Core_Exception
* @throws \Civi\Twingle\Shop\Exceptions\ProductException
*/
Expand Down Expand Up @@ -570,7 +566,7 @@ public function checkOutdated($product_from_twingle) {
/**
* Compare two products
*
* @param TwingleProduct $product_to_compare_with
* @param CRM_Twingle_BAO_TwingleProduct $product_to_compare_with
* Product from database
*
* @return bool
Expand All @@ -592,7 +588,7 @@ public function equals($product_to_compare_with) {
*/
public function getFinancialTypeId(): ?int {
if (!empty($this->price_field_id)) {
$price_set = \Civi\Api4\PriceField::get()
$price_set = PriceField::get()
->addSelect('financial_type_id')
->addWhere('id', '=', $this->price_field_id)
->execute()
Expand All @@ -610,7 +606,7 @@ public function getFinancialTypeId(): ?int {
*/
public function getPriceFieldValueId() {
if (!empty($this->price_field_id)) {
$price_field_value = \Civi\Api4\PriceFieldValue::get()
$price_field_value = PriceFieldValue::get()
->addSelect('id')
->addWhere('price_field_id', '=', $this->price_field_id)
->execute()
Expand All @@ -635,15 +631,15 @@ public function deletePriceField(): void {
}
catch (CRM_Core_Exception $e) {
throw new ProductException(
E::ts('An Error occurred while searching for the associated PriceFieldValue: ' . $e->getMessage()),
E::ts('An Error occurred while searching for the associated PriceFieldValue: %1', [1 => $e->getMessage()]),
ProductException::ERROR_CODE_PRICE_FIELD_VALUE_NOT_FOUND);
}
try {
civicrm_api3('PriceFieldValue', 'delete', ['id' => $result['id']]);
}
catch (CRM_Core_Exception $e) {
throw new ProductException(
E::ts('Could not delete associated PriceFieldValue: ' . $e->getMessage()),
E::ts('Could not delete associated PriceFieldValue: %1', [1 => $e->getMessage()]),
ProductException::ERROR_CODE_COULD_NOT_DELETE_PRICE_FIELD_VALUE);
}

Expand All @@ -667,13 +663,14 @@ public function deletePriceField(): void {
}
catch (CRM_Core_Exception $e) {
throw new ProductException(
E::ts('An Error occurred while searching for the associated PriceField: ' . $e->getMessage()),
E::ts('An Error occurred while searching for the associated PriceField: %1', [1 => $e->getMessage()]),
ProductException::ERROR_CODE_PRICE_FIELD_NOT_FOUND);
}
throw new ProductException(
E::ts('Could not delete associated PriceField: ' . $e->getMessage()),
E::ts('Could not delete associated PriceField: %1', [1 => $e->getMessage()]),
ProductException::ERROR_CODE_COULD_NOT_DELETE_PRICE_FIELD);
}
$this->price_field_id = NULL;
}

}
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
<?php

namespace Civi\Twingle\Shop\BAO;

// phpcs:disable
use CRM_Twingle_ExtensionUtil as E;
use Civi\Twingle\Shop\DAO\TwingleShop as TwingleShopDAO;
use Civi\Twingle\Shop\BAO\TwingleProduct as TwingleProductBAO;
use Civi\Twingle\Shop\ApiCall;
use Civi\Twingle\Shop\Exceptions\ShopException;
use Civi\Twingle\Shop\Exceptions\ProductException;
use Exception;
use function Civi\Twingle\Shop\Utils\filter_attributes;
use function Civi\Twingle\Shop\Utils\convert_str_to_int;
use function Civi\Twingle\Shop\Utils\validate_data_types;
// phpcs:enable

require_once E::path() . '/Civi/Twingle/Shop/Utils/TwingleShopUtils.php';

class TwingleShop extends TwingleShopDAO {
class CRM_Twingle_BAO_TwingleShop extends CRM_Twingle_DAO_TwingleShop {

public const ALLOWED_ATTRIBUTES = [
'id' => \CRM_Utils_Type::T_INT,
Expand Down Expand Up @@ -64,14 +59,14 @@ public function __construct() {
* @param string $project_identifier
* Twingle project identifier
*
* @return TwingleShop
* @return CRM_Twingle_BAO_TwingleShop
*
* @throws ShopException
* @throws \Civi\Twingle\Shop\Exceptions\ApiCallError
* @throws \CRM_Core_Exception
*/
public static function findByProjectIdentifier(string $project_identifier) {
$shop = new TwingleShop();
$shop = new CRM_Twingle_BAO_TwingleShop();
$shop->get('project_identifier', $project_identifier);
if (!$shop->id) {
$shop->fetchDataFromTwingle($project_identifier);
Expand Down Expand Up @@ -147,14 +142,15 @@ public function add($mode = 'create') {

// Try to lookup object in database
try {
$dao = TwingleShopDAO::executeQuery("SELECT * FROM civicrm_twingle_shop WHERE project_identifier = %1",
$dao = self::executeQuery("SELECT * FROM civicrm_twingle_shop WHERE project_identifier = %1",
[1 => [$this->project_identifier, 'String']]);
if ($dao->fetch()) {
$this->load($dao->toArray());
}
} catch (\Civi\Core\Exception\DBQueryException $e) {
}
catch (\Civi\Core\Exception\DBQueryException $e) {
throw new ShopException(
E::ts('Could not find TwingleShop in database: ' . $e->getMessage()),
E::ts('Could not find TwingleShop in database: %1', [1 => $e->getMessage()]),
ShopException::ERROR_CODE_COULD_NOT_FIND_SHOP_IN_DB);
}

Expand Down Expand Up @@ -196,7 +192,7 @@ function deleteByConstraint() {
catch (\CRM_Core_Exception $e) {
if ($e->getMessage() != 'Expected one PriceSet but found 0') {
throw new ShopException(
E::ts('Could not find associated PriceSet: ' . $e->getMessage()),
E::ts('Could not find associated PriceSet: %1', [1 => $e->getMessage()]),
ShopException::ERROR_CODE_PRICE_SET_NOT_FOUND);
}
else {
Expand All @@ -212,7 +208,7 @@ function deleteByConstraint() {
['id' => $this->price_set_id]);
} catch (\CRM_Core_Exception $e) {
throw new ShopException(
E::ts('Could not delete associated PriceSet: ' . $e->getMessage()),
E::ts('Could not delete associated PriceSet: %1', [1 => $e->getMessage()]),
ShopException::ERROR_CODE_COULD_NOT_DELETE_PRICE_SET);
}

Expand Down Expand Up @@ -259,7 +255,7 @@ public function fetchProducts(): array {
}, []);

foreach ($products_from_db as $product) {
/* @var TwingleProductBAO $product */
/* @var CRM_Twingle_BAO_TwingleProduct $product */

// Find orphaned products which are in the database but not in Twingle
$found = array_key_exists($product->external_id, $products_from_twingle);
Expand All @@ -286,8 +282,8 @@ public function fetchProducts(): array {
foreach ($products_from_twingle as $product_from_twingle) {
$found = array_key_exists($product_from_twingle['id'], $products);
if (!$found) {
$product = new TwingleProduct();
$product->load(TwingleProduct::renameTwingleAttrs($product_from_twingle));
$product = new CRM_Twingle_BAO_TwingleProduct();
$product->load(CRM_Twingle_BAO_TwingleProduct::renameTwingleAttrs($product_from_twingle));
$product->twingle_shop_id = $this->id;
$this->products[] = $product;
}
Expand All @@ -305,13 +301,13 @@ public function fetchProducts(): array {
public function getProducts() {
$products = [];

$result = TwingleProductBAO::executeQuery(
$result = CRM_Twingle_BAO_TwingleProduct::executeQuery(
"SELECT * FROM civicrm_twingle_product WHERE twingle_shop_id = %1",
[1 => [$this->id, 'Integer']]
);

while ($result->fetch()) {
$product = new TwingleProductBAO();
$product = new CRM_Twingle_BAO_TwingleProduct();
$product->load($result->toArray());
$products[] = $product;
}
Expand Down Expand Up @@ -456,9 +452,10 @@ function($project) {
public function deleteProducts() {
try {
$products = $this->getProducts();
} catch (\Civi\Core\Exception\DBQueryException $e) {
}
catch (\Civi\Core\Exception\DBQueryException $e) {
throw new ProductException(
E::ts('Could not retrieve associated products: ' . $e->getMessage()),
E::ts('Could not retrieve associated products: %1', [1 => $e->getMessage()]),
ProductException::ERROR_CODE_COULD_NOT_GET_PRODUCTS
);
}
Expand All @@ -469,8 +466,8 @@ public function deleteProducts() {
}
catch (ProductException $e) {
throw new ProductException(
E::ts('Could not delete associated products: ' . $e->getMessage()),
ProductException::ERROR_CODE_COULD_NOT_DELETE_PRICE_SET
E::ts('Could not delete associated products: %1', [1 => $e->getMessage()]),
ProductException::ERROR_CODE_COULD_NOT_DELETE_PRICE_SET,
);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

namespace Civi\Twingle\Shop\DAO;

/**
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
Expand All @@ -15,7 +13,7 @@
/**
* Database access object for the TwingleProduct entity.
*/
class TwingleProduct extends \CRM_Core_DAO {
class CRM_Twingle_DAO_TwingleProduct extends CRM_Core_DAO {
const EXT = E::LONG_NAME;
const TABLE_ADDED = '';

Expand Down Expand Up @@ -144,7 +142,7 @@ public static function &fields() {
'where' => 'civicrm_twingle_product.id',
'table_name' => 'civicrm_twingle_product',
'entity' => 'TwingleProduct',
'bao' => 'Civi\Twingle\Shop\DAO\TwingleProduct',
'bao' => 'CRM_Twingle_DAO_TwingleProduct',
'localizable' => 0,
'html' => [
'type' => 'Number',
Expand All @@ -167,7 +165,7 @@ public static function &fields() {
'where' => 'civicrm_twingle_product.external_id',
'table_name' => 'civicrm_twingle_product',
'entity' => 'TwingleProduct',
'bao' => 'Civi\Twingle\Shop\DAO\TwingleProduct',
'bao' => 'CRM_Twingle_DAO_TwingleProduct',
'localizable' => 0,
'html' => [
'type' => 'Number',
Expand All @@ -189,7 +187,7 @@ public static function &fields() {
'where' => 'civicrm_twingle_product.price_field_id',
'table_name' => 'civicrm_twingle_product',
'entity' => 'TwingleProduct',
'bao' => 'Civi\Twingle\Shop\DAO\TwingleProduct',
'bao' => 'CRM_Twingle_DAO_TwingleProduct',
'localizable' => 0,
'FKClassName' => 'CRM_Contact_DAO_Contact',
'add' => NULL,
Expand All @@ -208,9 +206,9 @@ public static function &fields() {
'where' => 'civicrm_twingle_product.twingle_shop_id',
'table_name' => 'civicrm_twingle_product',
'entity' => 'TwingleProduct',
'bao' => 'Civi\Twingle\Shop\DAO\TwingleProduct',
'bao' => 'CRM_Twingle_DAO_TwingleProduct',
'localizable' => 0,
'FKClassName' => 'Civi\Twingle\Shop\DAO\TwingleShop',
'FKClassName' => 'CRM_Twingle_DAO_TwingleShop',
'add' => NULL,
],
'created_at' => [
Expand All @@ -228,7 +226,7 @@ public static function &fields() {
'where' => 'civicrm_twingle_product.created_at',
'table_name' => 'civicrm_twingle_product',
'entity' => 'TwingleProduct',
'bao' => 'Civi\Twingle\Shop\DAO\TwingleProduct',
'bao' => 'CRM_Twingle_DAO_TwingleProduct',
'localizable' => 0,
'add' => NULL,
],
Expand All @@ -247,7 +245,7 @@ public static function &fields() {
'where' => 'civicrm_twingle_product.updated_at',
'table_name' => 'civicrm_twingle_product',
'entity' => 'TwingleProduct',
'bao' => 'Civi\Twingle\Shop\DAO\TwingleProduct',
'bao' => 'CRM_Twingle_DAO_TwingleProduct',
'localizable' => 0,
'add' => NULL,
],
Expand Down
Loading
Loading