implement TwingleShop integration
This commit is contained in:
parent
ea46e6a747
commit
8cfa270dff
60 changed files with 5200 additions and 106 deletions
|
@ -254,7 +254,14 @@ function _civicrm_api3_twingle_donation_Submit_spec(&$params) {
|
|||
'title' => E::ts('Custom fields'),
|
||||
'type' => CRM_Utils_Type::T_STRING,
|
||||
'api.required' => 0,
|
||||
'description' => E::ts('Additional information for either the contact or the (recurring) contribution.'),
|
||||
'description' => E::ts('Additional information for either the contact or the (recurring) contribution.'),
|
||||
);
|
||||
$params['products'] = [
|
||||
'name' => 'products',
|
||||
'title' => E::ts('Products'),
|
||||
'type' => CRM_Utils_Type::T_STRING,
|
||||
'api.required' => 0,
|
||||
'description' => E::ts('Products ordered via TwingleShop'),
|
||||
];
|
||||
$params['remarks'] = [
|
||||
'name' => 'remarks',
|
||||
|
@ -634,6 +641,11 @@ function civicrm_api3_twingle_donation_Submit($params) {
|
|||
'total_amount' => $params['amount'] / 100,
|
||||
];
|
||||
|
||||
// If the submission contains products, do not auto-create a line item
|
||||
if (!empty($params['products']) && $profile->isShopEnabled()) {
|
||||
$contribution_data['skipLineItem'] = 1;
|
||||
}
|
||||
|
||||
// Add custom field values.
|
||||
if (isset($custom_fields['Contribution'])) {
|
||||
$contribution_data += $custom_fields['Contribution'];
|
||||
|
@ -727,6 +739,27 @@ function civicrm_api3_twingle_donation_Submit($params) {
|
|||
$mandate = civicrm_api3('SepaMandate', 'createfull', $mandate_data);
|
||||
|
||||
$result_values['sepa_mandate'] = reset($mandate['values']);
|
||||
|
||||
// Add contribution data to result_values for later use
|
||||
$contribution_id = $result_values['sepa_mandate']['entity_id'];
|
||||
if ($contribution_id) {
|
||||
$contribution = civicrm_api3(
|
||||
'Contribution',
|
||||
'getsingle',
|
||||
['id' => $contribution_id]
|
||||
);
|
||||
$result_values['contribution'] = $contribution;
|
||||
} else {
|
||||
$mandate_id = $result_values['sepa_mandate']['id'];
|
||||
$message = E::LONG_NAME . ": could not find contribution for sepa mandate $mandate_id";
|
||||
throw new CiviCRM_API3_Exception($message, 'api_error');
|
||||
}
|
||||
|
||||
// Add products as line items to the contribution
|
||||
if (!empty($params['products']) && $profile->isShopEnabled()) {
|
||||
$line_items = CRM_Twingle_Submission::createLineItems($result_values, $params, $profile);
|
||||
$result_values['contribution']['line_items'] = $line_items;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Set financial type depending on donation rhythm. This applies for
|
||||
|
@ -827,6 +860,12 @@ function civicrm_api3_twingle_donation_Submit($params) {
|
|||
}
|
||||
|
||||
$result_values['contribution'] = $contribution;
|
||||
|
||||
// Add products as line items to the contribution
|
||||
if (!empty($params['products']) && $profile->isShopEnabled()) {
|
||||
$line_items = CRM_Twingle_Submission::createLineItems($result_values, $params, $profile);
|
||||
$result_values['contribution']['line_items'] = $line_items;
|
||||
}
|
||||
}
|
||||
|
||||
// MEMBERSHIP CREATION
|
||||
|
|
138
api/v3/TwingleProduct/Create.php
Normal file
138
api/v3/TwingleProduct/Create.php
Normal file
|
@ -0,0 +1,138 @@
|
|||
<?php
|
||||
|
||||
use Civi\Twingle\Shop\Exceptions\ProductException;
|
||||
use CRM_Twingle_ExtensionUtil as E;
|
||||
use Civi\Twingle\Shop\BAO\TwingleProduct;
|
||||
|
||||
/**
|
||||
* TwingleProduct.Create API specification (optional)
|
||||
* This is used for documentation and validation.
|
||||
*
|
||||
* @param array $spec description of fields supported by this API call
|
||||
*
|
||||
* @see https://docs.civicrm.org/dev/en/latest/framework/api-architecture/
|
||||
*/
|
||||
function _civicrm_api3_twingle_product_Create_spec(&$spec) {
|
||||
$spec['id'] = [
|
||||
'name' => 'id',
|
||||
'title' => E::ts('TwingleProduct ID'),
|
||||
'type' => CRM_Utils_Type::T_INT,
|
||||
'api.required' => 0,
|
||||
'description' => E::ts('The TwingleProduct ID in the database'),
|
||||
];
|
||||
$spec['external_id'] = [
|
||||
'name' => 'external_id',
|
||||
'title' => E::ts('Twingle ID'),
|
||||
'type' => CRM_Utils_Type::T_INT,
|
||||
'api.required' => 1,
|
||||
'description' => E::ts('External product ID in Twingle database'),
|
||||
];
|
||||
$spec['project_id'] = [
|
||||
'name' => 'project_id',
|
||||
'title' => E::ts('Twingle Shop ID'),
|
||||
'type' => CRM_Utils_Type::T_INT,
|
||||
'api.required' => 1,
|
||||
'description' => E::ts('ID of the corresponding Twingle Shop'),
|
||||
];
|
||||
$spec['name'] = [
|
||||
'name' => 'name',
|
||||
'title' => E::ts('Product Name'),
|
||||
'type' => CRM_Utils_Type::T_STRING,
|
||||
'api.required' => 1,
|
||||
'description' => E::ts('Name of the product'),
|
||||
];
|
||||
$spec['is_active'] = [
|
||||
'name' => 'is_active',
|
||||
'title' => E::ts('Is active?'),
|
||||
'type' => CRM_Utils_Type::T_BOOLEAN,
|
||||
'api.required' => 0,
|
||||
'api.default' => 1,
|
||||
'description' => E::ts('Is the product active?'),
|
||||
];
|
||||
$spec['description'] = [
|
||||
'name' => 'description',
|
||||
'title' => E::ts('Product Description'),
|
||||
'type' => CRM_Utils_Type::T_TEXT,
|
||||
'api.required' => 0,
|
||||
'description' => E::ts('Short description of the product'),
|
||||
];
|
||||
$spec['price'] = [
|
||||
'name' => 'price',
|
||||
'title' => E::ts('Product Price'),
|
||||
'type' => CRM_Utils_Type::T_FLOAT,
|
||||
'api.required' => 0,
|
||||
'description' => E::ts('Price of the product'),
|
||||
];
|
||||
$spec['sort'] = [
|
||||
'name' => 'sort',
|
||||
'title' => E::ts('Sort'),
|
||||
'type' => CRM_Utils_Type::T_INT,
|
||||
'api.required' => 0,
|
||||
'description' => E::ts('Sort order of the product'),
|
||||
];
|
||||
$spec['financial_type_id'] = [
|
||||
'name' => 'financial_type_id',
|
||||
'title' => E::ts('Financial Type ID'),
|
||||
'type' => CRM_Utils_Type::T_INT,
|
||||
'api.required' => 1,
|
||||
'description' => E::ts('ID of the financial type of the product'),
|
||||
];
|
||||
$spec['twingle_shop_id'] = [
|
||||
'name' => 'twingle_shop_id',
|
||||
'title' => E::ts('FK to TwingleShop'),
|
||||
'type' => CRM_Utils_Type::T_INT,
|
||||
'api.required' => 1,
|
||||
'description' => E::ts('FK to TwingleShop'),
|
||||
];
|
||||
$spec['tw_updated_at'] = [
|
||||
'name' => 'tw_updated_at',
|
||||
'title' => E::ts('Twingle timestamp'),
|
||||
'type' => CRM_Utils_Type::T_INT,
|
||||
'api.required' => 1,
|
||||
'description' => E::ts('Timestamp of last update in Twingle db'),
|
||||
];
|
||||
$spec['price_field_id'] = [
|
||||
'name' => 'price_field_id',
|
||||
'title' => E::ts('FK to PriceField'),
|
||||
'type' => CRM_Utils_Type::T_INT,
|
||||
'api.required' => 0,
|
||||
'description' => E::ts('FK to PriceField'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* TwingleProduct.Create API
|
||||
*
|
||||
* @param array $params
|
||||
*
|
||||
* @return array
|
||||
* API result descriptor
|
||||
*
|
||||
* @see civicrm_api3_create_success
|
||||
*
|
||||
* @throws API_Exception
|
||||
* @throws \Exception
|
||||
*/
|
||||
function civicrm_api3_twingle_product_Create($params): array {
|
||||
// Filter for allowed params
|
||||
$allowed_params = [];
|
||||
_civicrm_api3_twingle_product_Create_spec($allowed_params);
|
||||
$params = array_intersect_key($params, $allowed_params);
|
||||
|
||||
try {
|
||||
// Create TwingleProduct and load params
|
||||
$product = new TwingleProduct();
|
||||
$product->load($params);
|
||||
|
||||
// Save TwingleProduct
|
||||
$product->add();
|
||||
$result = $product->getAttributes();
|
||||
return civicrm_api3_create_success($result, $params, 'TwingleProduct', 'Create');
|
||||
}
|
||||
catch (ProductException $e) {
|
||||
return civicrm_api3_create_error($e->getMessage(), [
|
||||
'error_code' => $e->getCode(),
|
||||
'params' => $params,
|
||||
]);
|
||||
}
|
||||
}
|
72
api/v3/TwingleProduct/Delete.php
Normal file
72
api/v3/TwingleProduct/Delete.php
Normal file
|
@ -0,0 +1,72 @@
|
|||
<?php
|
||||
|
||||
use CRM_Twingle_ExtensionUtil as E;
|
||||
use Civi\Twingle\Shop\BAO\TwingleProduct;
|
||||
|
||||
/**
|
||||
* TwingleProduct.Delete API specification (optional)
|
||||
* This is used for documentation and validation.
|
||||
*
|
||||
* @param array $spec description of fields supported by this API call
|
||||
*
|
||||
* @see https://docs.civicrm.org/dev/en/latest/framework/api-architecture/
|
||||
*/
|
||||
function _civicrm_api3_twingle_product_Delete_spec(&$spec) {
|
||||
$spec['id'] = [
|
||||
'name' => 'id',
|
||||
'title' => E::ts('TwingleProduct ID'),
|
||||
'type' => CRM_Utils_Type::T_INT,
|
||||
'api.required' => 0,
|
||||
'description' => E::ts('The TwingleProduct ID in CiviCRM'),
|
||||
];
|
||||
$spec['external_id'] = [
|
||||
'name' => 'external_id',
|
||||
'title' => E::ts('External TwingleProduct ID'),
|
||||
'type' => CRM_Utils_Type::T_INT,
|
||||
'api.required' => 0,
|
||||
'description' => E::ts('Twingle\'s ID of the product'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* TwingleProduct.Delete API
|
||||
*
|
||||
* @param array $params
|
||||
*
|
||||
* @return array
|
||||
* API result descriptor
|
||||
*
|
||||
* @throws API_Exception*@throws \Exception
|
||||
* @throws \Exception
|
||||
* @see civicrm_api3_create_success
|
||||
*
|
||||
*/
|
||||
function civicrm_api3_twingle_product_Delete($params) {
|
||||
// Filter for allowed params
|
||||
$allowed_params = [];
|
||||
_civicrm_api3_twingle_product_Delete_spec($allowed_params);
|
||||
$params = array_intersect_key($params, $allowed_params);
|
||||
|
||||
// Find TwingleProduct via getsingle API
|
||||
$product_data = civicrm_api3('TwingleProduct', 'getsingle', $params);
|
||||
if ($product_data['is_error']) {
|
||||
return civicrm_api3_create_error($product_data['error_message'],
|
||||
['error_code' => $product_data['error_code'], 'params' => $params]
|
||||
);
|
||||
}
|
||||
|
||||
// Get TwingleProduct object
|
||||
$product = TwingleProduct::findById($product_data['id']);
|
||||
|
||||
// Delete TwingleProduct and associated PriceField and PriceFieldValue
|
||||
$result = $product->delete();
|
||||
if ($result) {
|
||||
return civicrm_api3_create_success(1, $params, 'TwingleProduct', 'Delete');
|
||||
}
|
||||
else {
|
||||
return civicrm_api3_create_error(
|
||||
E::ts('TwingleProduct could not be deleted.'),
|
||||
['error_code' => 'delete_failed', 'params' => $params]
|
||||
);
|
||||
}
|
||||
}
|
137
api/v3/TwingleProduct/Get.php
Normal file
137
api/v3/TwingleProduct/Get.php
Normal file
|
@ -0,0 +1,137 @@
|
|||
<?php
|
||||
|
||||
use CRM_Twingle_ExtensionUtil as E;
|
||||
use Civi\Twingle\Shop\BAO\TwingleProduct;
|
||||
|
||||
/**
|
||||
* TwingleProduct.Get API specification (optional)
|
||||
* This is used for documentation and validation.
|
||||
*
|
||||
* @param array $spec description of fields supported by this API call
|
||||
*
|
||||
* @see https://docs.civicrm.org/dev/en/latest/framework/api-architecture/
|
||||
*/
|
||||
function _civicrm_api3_twingle_product_Get_spec(&$spec) {
|
||||
$spec['id'] = [
|
||||
'name' => 'id',
|
||||
'title' => E::ts('TwingleProduct ID'),
|
||||
'type' => CRM_Utils_Type::T_INT,
|
||||
'api.required' => 0,
|
||||
'description' => E::ts('The TwingleProduct ID in CiviCRM'),
|
||||
];
|
||||
$spec['external_id'] = [
|
||||
'name' => 'external_id',
|
||||
'title' => E::ts('External TwingleProduct ID'),
|
||||
'type' => CRM_Utils_Type::T_INT,
|
||||
'api.required' => 0,
|
||||
'description' => E::ts('Twingle\'s ID of the product'),
|
||||
];
|
||||
$spec['price_field_id'] = [
|
||||
'name' => 'Price Field ID',
|
||||
'title' => E::ts('Price Field ID'),
|
||||
'type' => CRM_Utils_Type::T_INT,
|
||||
'api.required' => 0,
|
||||
'description' => E::ts('FK to civicrm_price_field'),
|
||||
];
|
||||
$spec['twingle_shop_id'] = [
|
||||
'name' => 'twingle_shop_id',
|
||||
'title' => E::ts('TwingleShop ID'),
|
||||
'type' => CRM_Utils_Type::T_INT,
|
||||
'api.required' => 0,
|
||||
'description' => E::ts('The TwingleShop ID in CiviCRM'),
|
||||
];
|
||||
$spec['project_identifier'] = [
|
||||
'name' => 'project_identifier',
|
||||
'title' => E::ts('Project Identifier'),
|
||||
'type' => CRM_Utils_Type::T_STRING,
|
||||
'api.required' => 0,
|
||||
'description' => E::ts('Twingle project identifier'),
|
||||
];
|
||||
$spec['numerical_project_id'] = [
|
||||
'name' => 'numerical_project_id',
|
||||
'title' => E::ts('Numerical Project Identifier'),
|
||||
'type' => CRM_Utils_Type::T_INT,
|
||||
'api.required' => 0,
|
||||
'description' => E::ts('Twingle numerical project identifier'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* TwingleProduct.Get API
|
||||
*
|
||||
* @param array $params
|
||||
*
|
||||
* @return array
|
||||
* API result descriptor
|
||||
*
|
||||
* @throws API_Exception
|
||||
* @see civicrm_api3_create_success
|
||||
*
|
||||
*/
|
||||
function civicrm_api3_twingle_product_Get($params) {
|
||||
// Filter for allowed params
|
||||
$allowed_params = [];
|
||||
_civicrm_api3_twingle_product_Get_spec($allowed_params);
|
||||
$params = array_intersect_key($params, $allowed_params);
|
||||
|
||||
// Build query
|
||||
$query = 'SELECT ctp.* FROM civicrm_twingle_product ctp
|
||||
INNER JOIN civicrm_twingle_shop cts ON ctp.twingle_shop_id = cts.id';
|
||||
$query_params = [];
|
||||
|
||||
if (!empty($params)) {
|
||||
$query = $query . ' WHERE';
|
||||
$possible_params = [];
|
||||
_civicrm_api3_twingle_product_Get_spec($possible_params);
|
||||
$param_count = 1;
|
||||
$altered_params = [];
|
||||
|
||||
// Specify product fields to define table prefix
|
||||
$productFields = array_keys(TwingleProduct::fields());
|
||||
|
||||
// Alter params (prefix with table name)
|
||||
foreach ($possible_params as $param) {
|
||||
if (!empty($params[$param['name']])) {
|
||||
// Prefix with table name
|
||||
$table_prefix = in_array($param['name'], $productFields) ? 'ctp.' : 'cts.';
|
||||
$altered_params[] = [
|
||||
'name' => $table_prefix . $param['name'],
|
||||
'value' => $params[$param['name']],
|
||||
'type' => $param['type'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// Add altered params to query
|
||||
foreach ($altered_params as $param) {
|
||||
$query = $query . ' ' . $param['name'] . " = %$param_count AND";
|
||||
$query_params[$param_count] = [
|
||||
$param['value'],
|
||||
$param['type'] == CRM_Utils_Type::T_INT ? 'Integer' : 'String',
|
||||
];
|
||||
$param_count++;
|
||||
}
|
||||
}
|
||||
|
||||
// Cut away last 'AND'
|
||||
$query = substr($query, 0, -4);
|
||||
|
||||
// Execute query
|
||||
try {
|
||||
$dao = TwingleProduct::executeQuery($query, $query_params);
|
||||
}
|
||||
catch (Exception $e) {
|
||||
return civicrm_api3_create_error($e->getMessage(), [
|
||||
'error_code' => $e->getCode(),
|
||||
'params' => $params,
|
||||
]);
|
||||
}
|
||||
|
||||
// Prepare return values
|
||||
$returnValues = [];
|
||||
while ($dao->fetch()) {
|
||||
$returnValues[] = $dao->toArray();
|
||||
}
|
||||
|
||||
return civicrm_api3_create_success($returnValues, $params, 'TwingleProduct', 'Get');
|
||||
}
|
54
api/v3/TwingleProduct/Getsingle.php
Normal file
54
api/v3/TwingleProduct/Getsingle.php
Normal file
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
use CRM_Twingle_ExtensionUtil as E;
|
||||
|
||||
/**
|
||||
* TwingleProduct.Getsingle API specification (optional)
|
||||
* This is used for documentation and validation.
|
||||
*
|
||||
* @param array $spec description of fields supported by this API call
|
||||
*
|
||||
* @see https://docs.civicrm.org/dev/en/latest/framework/api-architecture/
|
||||
*/
|
||||
function _civicrm_api3_twingle_product_Getsingle_spec(&$spec) {
|
||||
_civicrm_api3_twingle_product_Get_spec($spec);
|
||||
}
|
||||
|
||||
/**
|
||||
* TwingleProduct.Getsingle API
|
||||
*
|
||||
* @param array $params
|
||||
*
|
||||
* @return array
|
||||
* API result descriptor
|
||||
*
|
||||
* @see civicrm_api3_create_success
|
||||
*
|
||||
* @throws API_Exception
|
||||
*/
|
||||
function civicrm_api3_twingle_product_Getsingle($params) {
|
||||
// Filter for allowed params
|
||||
$allowed_params = [];
|
||||
_civicrm_api3_twingle_product_Getsingle_spec($allowed_params);
|
||||
$params = array_intersect_key($params, $allowed_params);
|
||||
|
||||
// Check whether any parameters are set
|
||||
if (empty($params)) {
|
||||
return civicrm_api3_create_error(
|
||||
"At least one parameter must be set",
|
||||
['error_code' => 'missing_parameter', 'params' => $params]
|
||||
);
|
||||
}
|
||||
|
||||
// Find TwingleProduct via get API
|
||||
$returnValues = civicrm_api3('TwingleProduct', 'get', $params);
|
||||
$count = $returnValues['count'];
|
||||
|
||||
// Check whether only a single TwingleProduct is found
|
||||
if ($count != 1) {
|
||||
return civicrm_api3_create_error(
|
||||
"Expected one TwingleProduct but found $count",
|
||||
['error_code' => 'not_found', 'params' => $params]
|
||||
);
|
||||
}
|
||||
return $returnValues['values'][$returnValues['id']];
|
||||
}
|
80
api/v3/TwingleShop/Create.php
Normal file
80
api/v3/TwingleShop/Create.php
Normal file
|
@ -0,0 +1,80 @@
|
|||
<?php
|
||||
use CRM_Twingle_ExtensionUtil as E;
|
||||
use Civi\Twingle\Shop\BAO\TwingleShop;
|
||||
use Civi\Twingle\Shop\Exceptions\ShopException;
|
||||
|
||||
/**
|
||||
* TwingleShop.Create API specification (optional)
|
||||
* This is used for documentation and validation.
|
||||
*
|
||||
* @param array $spec description of fields supported by this API call
|
||||
*
|
||||
* @see https://docs.civicrm.org/dev/en/latest/framework/api-architecture/
|
||||
*/
|
||||
function _civicrm_api3_twingle_shop_Create_spec(&$spec) {
|
||||
$spec['project_identifier'] = [
|
||||
'name' => 'project_identifier',
|
||||
'title' => E::ts('Project Identifier'),
|
||||
'type' => CRM_Utils_Type::T_STRING,
|
||||
'api.required' => 1,
|
||||
'description' => E::ts('Twingle project identifier'),
|
||||
];
|
||||
$spec['numerical_project_id'] = [
|
||||
'name' => 'numerical_project_id',
|
||||
'title' => E::ts('Numerical Project Identifier'),
|
||||
'type' => CRM_Utils_Type::T_INT,
|
||||
'api.required' => 1,
|
||||
'description' => E::ts('Numerical Twingle project identifier'),
|
||||
];
|
||||
$spec['name'] = [
|
||||
'name' => 'name',
|
||||
'title' => E::ts('Shop Name'),
|
||||
'type' => CRM_Utils_Type::T_STRING,
|
||||
'api.required' => 1,
|
||||
'description' => E::ts('Name of the shop'),
|
||||
];
|
||||
$spec['financial_type_id'] = [
|
||||
'name' => 'financial_type_id',
|
||||
'title' => E::ts('Financial Type ID'),
|
||||
'type' => CRM_Utils_Type::T_INT,
|
||||
'api.required' => 1,
|
||||
'description' => E::ts('FK to civicrm_financial_type'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* TwingleShop.Create API
|
||||
*
|
||||
* @param array $params
|
||||
*
|
||||
* @return array
|
||||
* API result descriptor
|
||||
*
|
||||
* @see civicrm_api3_create_success
|
||||
*
|
||||
* @throws API_Exception
|
||||
*/
|
||||
function civicrm_api3_twingle_shop_Create($params) {
|
||||
// Filter for allowed params
|
||||
$allowed_params = [];
|
||||
_civicrm_api3_twingle_shop_Create_spec($allowed_params);
|
||||
$params = array_intersect_key($params, $allowed_params);
|
||||
|
||||
try {
|
||||
// Create TwingleShop and load params
|
||||
$shop = new TwingleShop();
|
||||
$shop->load($params);
|
||||
|
||||
// Save TwingleShop
|
||||
$result = $shop->add();
|
||||
|
||||
// Return success
|
||||
return civicrm_api3_create_success($result, $params, 'TwingleShop', 'Create');
|
||||
}
|
||||
catch (ShopException $e) {
|
||||
return civicrm_api3_create_error($e->getMessage(), [
|
||||
'error_code' => $e->getErrorCode(),
|
||||
'params' => $params,
|
||||
]);
|
||||
}
|
||||
}
|
79
api/v3/TwingleShop/Delete.php
Normal file
79
api/v3/TwingleShop/Delete.php
Normal file
|
@ -0,0 +1,79 @@
|
|||
<?php
|
||||
use CRM_Twingle_ExtensionUtil as E;
|
||||
use Civi\Twingle\Shop\BAO\TwingleShop;
|
||||
|
||||
/**
|
||||
* TwingleShop.Delete API specification (optional)
|
||||
* This is used for documentation and validation.
|
||||
*
|
||||
* @param array $spec description of fields supported by this API call
|
||||
*
|
||||
* @see https://docs.civicrm.org/dev/en/latest/framework/api-architecture/
|
||||
*/
|
||||
function _civicrm_api3_twingle_shop_Delete_spec(&$spec) {
|
||||
$spec['id'] = [
|
||||
'name' => 'id',
|
||||
'title' => E::ts('TwingleShop ID'),
|
||||
'type' => CRM_Utils_Type::T_INT,
|
||||
'api.required' => 0,
|
||||
'description' => E::ts('The TwingleShop ID in CiviCRM'),
|
||||
];
|
||||
$spec['project_identifier'] = [
|
||||
'name' => 'project_identifier',
|
||||
'title' => E::ts('Project Identifier'),
|
||||
'type' => CRM_Utils_Type::T_STRING,
|
||||
'api.required' => 0,
|
||||
'description' => E::ts('Twingle project identifier'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* TwingleShop.Delete API
|
||||
*
|
||||
* @param array $params
|
||||
*
|
||||
* @return array
|
||||
* API result descriptor
|
||||
*
|
||||
* @throws \API_Exception
|
||||
* @throws \Civi\Twingle\Shop\Exceptions\ShopException
|
||||
* @throws \Exception
|
||||
* @see civicrm_api3_create_success
|
||||
*
|
||||
*/
|
||||
function civicrm_api3_twingle_shop_Delete($params) {
|
||||
// Filter for allowed params
|
||||
$allowed_params = [];
|
||||
_civicrm_api3_twingle_shop_Delete_spec($allowed_params);
|
||||
$params = array_intersect_key($params, $allowed_params);
|
||||
|
||||
// Find TwingleShop via getsingle API
|
||||
$shop_data = civicrm_api3('TwingleShop', 'getsingle', $params);
|
||||
if ($shop_data['is_error']) {
|
||||
return civicrm_api3_create_error($shop_data['error_message'],
|
||||
['error_code' => $shop_data['error_code'], 'params' => $params]
|
||||
);
|
||||
}
|
||||
|
||||
// Get TwingleShop object
|
||||
$shop = TwingleShop::findById($shop_data['id']);
|
||||
|
||||
// Delete TwingleShop
|
||||
/* @var \Civi\Twingle\Shop\BAO\TwingleShop $shop */
|
||||
$result = $shop->deleteByConstraint();
|
||||
if ($result) {
|
||||
return civicrm_api3_create_success(1, $params, 'TwingleShop', 'Delete');
|
||||
}
|
||||
elseif ($result === 0) {
|
||||
return civicrm_api3_create_error(
|
||||
E::ts('TwingleShop could not be found.'),
|
||||
['error_code' => 'not_found', 'params' => $params]
|
||||
);
|
||||
}
|
||||
else {
|
||||
return civicrm_api3_create_error(
|
||||
E::ts('TwingleShop could not be deleted.'),
|
||||
['error_code' => 'delete_failed', 'params' => $params]
|
||||
);
|
||||
}
|
||||
}
|
97
api/v3/TwingleShop/Fetch.php
Normal file
97
api/v3/TwingleShop/Fetch.php
Normal file
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
|
||||
use CRM_Twingle_ExtensionUtil as E;
|
||||
use Civi\Twingle\Shop\BAO\TwingleShop;
|
||||
use Civi\Twingle\Shop\Exceptions\ApiCallError;
|
||||
use Civi\Twingle\Shop\Exceptions\ProductException;
|
||||
use Civi\Twingle\Shop\Exceptions\ShopException;
|
||||
|
||||
/**
|
||||
* TwingleShop.Fetch API specification (optional)
|
||||
* This is used for documentation and validation.
|
||||
*
|
||||
* @param array $spec description of fields supported by this API call
|
||||
*
|
||||
* @see https://docs.civicrm.org/dev/en/latest/framework/api-architecture/
|
||||
*/
|
||||
function _civicrm_api3_twingle_shop_Fetch_spec(&$spec) {
|
||||
$spec['project_identifiers'] = [
|
||||
'name' => 'project_identifiers',
|
||||
'title' => E::ts('Project Identifiers'),
|
||||
'type' => CRM_Utils_Type::T_STRING,
|
||||
'api.required' => 1,
|
||||
'description' => E::ts('Comma separated list of Twingle project identifiers.'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* TwingleShop.Fetch API
|
||||
*
|
||||
* @param array $params
|
||||
*
|
||||
* @return array
|
||||
* API result descriptor
|
||||
*
|
||||
* @see civicrm_api3_create_success
|
||||
*
|
||||
* @throws API_Exception
|
||||
*/
|
||||
function civicrm_api3_twingle_shop_Fetch($params) {
|
||||
// Filter for allowed params
|
||||
$allowed_params = [];
|
||||
_civicrm_api3_twingle_shop_Fetch_spec($allowed_params);
|
||||
$params = array_intersect_key($params, $allowed_params);
|
||||
|
||||
$returnValues = [];
|
||||
|
||||
// Explode string with project IDs and trim
|
||||
$projectIds = array_map(
|
||||
function ($projectId) {
|
||||
return trim($projectId);
|
||||
},
|
||||
explode(',', $params['project_identifiers'])
|
||||
);
|
||||
|
||||
// Get products for all projects of type 'shop'
|
||||
foreach ($projectIds as $projectId) {
|
||||
try {
|
||||
$shop = TwingleShop::findByProjectIdentifier($projectId);
|
||||
$products = $shop->fetchProducts();
|
||||
$returnValues[$projectId] = [];
|
||||
$returnValues[$projectId] += $shop->getAttributes();
|
||||
$returnValues[$projectId]['products'] = array_map(function ($product) {
|
||||
return $product->getAttributes();
|
||||
}, $products);
|
||||
}
|
||||
catch (ShopException|ApiCallError|ProductException $e) {
|
||||
// If this project identifier doesn't belong to a project of type
|
||||
// 'shop', just skip it
|
||||
if ($e->getErrorCode() == ShopException::ERROR_CODE_NOT_A_SHOP) {
|
||||
$returnValues[$projectId] = "project is not of type 'shop'";
|
||||
continue;
|
||||
}
|
||||
// Else, log error and throw exception
|
||||
else {
|
||||
Civi::log()->error(
|
||||
$e->getMessage(),
|
||||
[
|
||||
'project_identifier' => $projectId,
|
||||
'params' => $params,
|
||||
]
|
||||
);
|
||||
return civicrm_api3_create_error($e->getMessage(), [
|
||||
'error_code' => $e->getErrorCode(),
|
||||
'project_identifier' => $projectId,
|
||||
'params' => $params,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return civicrm_api3_create_success(
|
||||
$returnValues,
|
||||
$params,
|
||||
'TwingleShop',
|
||||
'Fetch'
|
||||
);
|
||||
}
|
111
api/v3/TwingleShop/Get.php
Normal file
111
api/v3/TwingleShop/Get.php
Normal file
|
@ -0,0 +1,111 @@
|
|||
<?php
|
||||
use CRM_Twingle_ExtensionUtil as E;
|
||||
use Civi\Twingle\Shop\BAO\TwingleShop;
|
||||
|
||||
/**
|
||||
* TwingleShop.Get API specification (optional)
|
||||
* This is used for documentation and validation.
|
||||
*
|
||||
* @param array $spec description of fields supported by this API call
|
||||
*
|
||||
* @see https://docs.civicrm.org/dev/en/latest/framework/api-architecture/
|
||||
*/
|
||||
function _civicrm_api3_twingle_shop_Get_spec(&$spec) {
|
||||
$spec['id'] = [
|
||||
'name' => 'id',
|
||||
'title' => E::ts('TwingleShop ID'),
|
||||
'type' => CRM_Utils_Type::T_INT,
|
||||
'api.required' => 0,
|
||||
'description' => E::ts('The TwingleShop ID in CiviCRM'),
|
||||
];
|
||||
$spec['project_identifier'] = [
|
||||
'name' => 'project_identifier',
|
||||
'title' => E::ts('Project Identifier'),
|
||||
'type' => CRM_Utils_Type::T_STRING,
|
||||
'api.required' => 0,
|
||||
'description' => E::ts('Twingle project identifier'),
|
||||
];
|
||||
$spec['numerical_project_id'] = [
|
||||
'name' => 'numerical_project_id',
|
||||
'title' => E::ts('Numerical Project Identifier'),
|
||||
'type' => CRM_Utils_Type::T_INT,
|
||||
'api.required' => 0,
|
||||
'description' => E::ts('Twingle numerical project identifier'),
|
||||
];
|
||||
$spec['name'] = [
|
||||
'name' => 'name',
|
||||
'title' => E::ts('Name'),
|
||||
'type' => CRM_Utils_Type::T_STRING,
|
||||
'api.required' => 0,
|
||||
'description' => E::ts('Name of the TwingleShop'),
|
||||
];
|
||||
$spec['price_set_id'] = [
|
||||
'name' => 'price_set_id',
|
||||
'title' => E::ts('Price Set ID'),
|
||||
'type' => CRM_Utils_Type::T_INT,
|
||||
'api.required' => 0,
|
||||
'description' => E::ts('FK to civicrm_price_set'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* TwingleShop.Get API
|
||||
*
|
||||
* @param array $params
|
||||
*
|
||||
* @return array
|
||||
* API result descriptor
|
||||
*
|
||||
* @see civicrm_api3_create_success
|
||||
*
|
||||
* @throws API_Exception
|
||||
*/
|
||||
function civicrm_api3_twingle_shop_Get($params) {
|
||||
// Filter for allowed params
|
||||
$allowed_params = [];
|
||||
_civicrm_api3_twingle_shop_Get_spec($allowed_params);
|
||||
$params = array_intersect_key($params, $allowed_params);
|
||||
|
||||
// Build query
|
||||
$query = 'SELECT * FROM civicrm_twingle_shop';
|
||||
$query_params = [];
|
||||
|
||||
if (!empty($params)) {
|
||||
$query = $query . ' WHERE';
|
||||
$possible_params = [];
|
||||
_civicrm_api3_twingle_shop_Get_spec($possible_params);
|
||||
$param_count = 1;
|
||||
|
||||
foreach ($possible_params as $param) {
|
||||
if (!empty($params[$param['name']])) {
|
||||
$query = $query . ' ' . $param['name'] . " = %$param_count AND";
|
||||
$query_params[$param_count] = [
|
||||
$params[$param['name']],
|
||||
$param['type'] == CRM_Utils_Type::T_INT ? 'Integer' : 'String'
|
||||
];
|
||||
$param_count++;
|
||||
}
|
||||
}
|
||||
// Cut away last 'AND'
|
||||
$query = substr($query, 0, -4);
|
||||
}
|
||||
|
||||
// Execute query
|
||||
try {
|
||||
$dao = TwingleShop::executeQuery($query, $query_params);
|
||||
}
|
||||
catch (\Exception $e) {
|
||||
return civicrm_api3_create_error($e->getMessage(), [
|
||||
'error_code' => $e->getCode(),
|
||||
'params' => $params,
|
||||
]);
|
||||
}
|
||||
|
||||
// Prepare return values
|
||||
$returnValues = [];
|
||||
while ($dao->fetch()) {
|
||||
$returnValues[] = $dao->toArray();
|
||||
}
|
||||
|
||||
return civicrm_api3_create_success($returnValues, $params, 'TwingleShop', 'Get');
|
||||
}
|
54
api/v3/TwingleShop/Getsingle.php
Normal file
54
api/v3/TwingleShop/Getsingle.php
Normal file
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
use CRM_Twingle_ExtensionUtil as E;
|
||||
|
||||
/**
|
||||
* TwingleShop.Getsingle API specification (optional)
|
||||
* This is used for documentation and validation.
|
||||
*
|
||||
* @param array $spec description of fields supported by this API call
|
||||
*
|
||||
* @see https://docs.civicrm.org/dev/en/latest/framework/api-architecture/
|
||||
*/
|
||||
function _civicrm_api3_twingle_shop_Getsingle_spec(&$spec) {
|
||||
_civicrm_api3_twingle_shop_Get_spec($spec);
|
||||
}
|
||||
|
||||
/**
|
||||
* TwingleShop.Getsingle API
|
||||
*
|
||||
* @param array $params
|
||||
*
|
||||
* @return array
|
||||
* API result descriptor
|
||||
*
|
||||
* @see civicrm_api3_create_success
|
||||
*
|
||||
* @throws API_Exception
|
||||
*/
|
||||
function civicrm_api3_twingle_shop_Getsingle($params) {
|
||||
// Filter for allowed params
|
||||
$allowed_params = [];
|
||||
_civicrm_api3_twingle_shop_Getsingle_spec($allowed_params);
|
||||
$params = array_intersect_key($params, $allowed_params);
|
||||
|
||||
// Check whether any parameters are set
|
||||
if (empty($params)) {
|
||||
return civicrm_api3_create_error(
|
||||
"At least one parameter must be set",
|
||||
['error_code' => 'missing_parameter', 'params' => $params]
|
||||
);
|
||||
}
|
||||
|
||||
// Find TwingleShop via get API
|
||||
$returnValues = civicrm_api3('TwingleShop', 'get', $params);
|
||||
$count = $returnValues['count'];
|
||||
|
||||
// Check whether only a single TwingleShop is found
|
||||
if ($count != 1) {
|
||||
return civicrm_api3_create_error(
|
||||
"Expected one TwingleShop but found $count",
|
||||
['error_code' => 'not_found', 'params' => $params]
|
||||
);
|
||||
}
|
||||
return $returnValues['values'][$returnValues['id']];
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue