implement TwingleShop integration
This commit is contained in:
parent
ea46e6a747
commit
8cfa270dff
60 changed files with 5200 additions and 106 deletions
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']];
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue