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

GP-43974 Use EntityReferences in CustomGroups #51

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 9 additions & 3 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
name: "Run unit tests"

on: ["push"]
on:
push:
branches:
- master
tags:
pull_request:

env:
CIVI_CI_CIVICRM: ${{ vars.CIVI_CI_CIVICRM || '["master"]' }}
Expand Down Expand Up @@ -50,9 +55,8 @@ jobs:
steps:
- id: clone-repo
name: "Clone the repository"
uses: "actions/checkout@v3"
uses: "actions/checkout@v4"
with:
ref: "${{ github.ref_name }}"
path: "de.systopia.contract"
- id: build-ci
uses: greenpeace-cee/civi-ci@main
Expand All @@ -71,6 +75,8 @@ jobs:
cp -R "$GITHUB_WORKSPACE/de.systopia.contract" "$EXT_DIR/de.systopia.contract"
git clone https://github.com/Project60/org.project60.banking.git "$EXT_DIR/org.project60.banking"
git clone https://github.com/Project60/org.project60.sepa.git "$EXT_DIR/org.project60.sepa"
git clone https://lab.civicrm.org/extensions/mjwshared.git "$EXT_DIR/mjwshared"
git clone https://github.com/greenpeace-cee/adyen.git "$EXT_DIR/adyen"
cv en de.systopia.contract
- id: run-tests
name: "Run Tests"
Expand Down
155 changes: 0 additions & 155 deletions CRM/Contract/CustomData.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,161 +47,6 @@ protected function log($level, $message) {
}
}

/**
* will take a JSON source file and synchronise the
* generic entity data with those specs
*/
public function syncEntities($source_file) {
$data = json_decode(file_get_contents($source_file), TRUE);
if (empty($data)) {
throw new Exception("syncEntities: Invalid specs");
}

foreach ($data['_entities'] as $entity_data) {
$this->translateStrings($entity_data);
$entity = $this->identifyEntity($data['entity'], $entity_data);

if (empty($entity)) {
// create OptionValue
$entity = $this->createEntity($data['entity'], $entity_data);
} elseif ($entity == 'FAILED') {
// Couldn't identify:
$this->log(CUSTOM_DATA_HELPER_LOG_ERROR, "Couldn't create/update {$data['entity']}: " . json_encode($entity_data));
} else {
// update OptionValue
$this->updateEntity($data['entity'], $entity_data, $entity);
}
}
}

/**
* will take a JSON source file and synchronise the
* OptionGroup/OptionValue data in the system with
* those specs
*/
public function syncOptionGroup($source_file) {
$data = json_decode(file_get_contents($source_file), TRUE);
if (empty($data)) {
throw new Exception("syncOptionGroup: Invalid specs");
}

// first: find or create option group
$this->translateStrings($data);
$optionGroup = $this->identifyEntity('OptionGroup', $data);
if (empty($optionGroup)) {
// create OptionGroup
$optionGroup = $this->createEntity('OptionGroup', $data);
} elseif ($optionGroup == 'FAILED') {
// Couldn't identify:
$this->log(CUSTOM_DATA_HELPER_LOG_ERROR, "Couldn't create/update OptionGroup: " . json_encode($data));
return;
} else {
// update OptionGroup
$this->updateEntity('OptionGroup', $data, $optionGroup, array('is_active'));
}

// now run the update for the OptionValues
foreach ($data['_values'] as $optionValueSpec) {
$this->translateStrings($optionValueSpec);
$optionValueSpec['option_group_id'] = $optionGroup['id'];
$optionValueSpec['_lookup'][] = 'option_group_id';
$optionValue = $this->identifyEntity('OptionValue', $optionValueSpec);

if (empty($optionValue)) {
// create OptionValue
$optionValue = $this->createEntity('OptionValue', $optionValueSpec);
} elseif ($optionValue == 'FAILED') {
// Couldn't identify:
$this->log(CUSTOM_DATA_HELPER_LOG_ERROR, "Couldn't create/update OptionValue: " . json_encode($optionValueSpec));
} else {
// update OptionValue
$this->updateEntity('OptionValue', $optionValueSpec, $optionValue, array('is_active'));
}
}
}


/**
* will take a JSON source file and synchronise the
* CustomGroup/CustomField data in the system with
* those specs
*/
public function syncCustomGroup($source_file) {
$force_update = FALSE;
$data = json_decode(file_get_contents($source_file), TRUE);
if (empty($data)) {
throw new Exception("CRM_Utils_CustomData::syncCustomGroup: Invalid custom specs");
}

// if extends_entity_column_value, make sure it's sensible data
if (isset($data['extends_entity_column_value'])) {
$force_update = TRUE; // this doesn't get returned by the API, so differences couldn't be detected
if ($data['extends'] == 'Activity') {
$extends_list = array();
foreach ($data['extends_entity_column_value'] as $activity_type) {
if (!is_numeric($activity_type)) {
$activity_type = CRM_Core_PseudoConstant::getKey(
'CRM_Activity_BAO_Activity',
'activity_type_id',
$activity_type
);
}
if ($activity_type) {
$extends_list[] = $activity_type;
}
}
$data['extends_entity_column_value'] = $extends_list;
}

if (is_array($data['extends_entity_column_value'])) {
$data['extends_entity_column_value'] = CRM_Utils_Array::implodePadded($data['extends_entity_column_value']);
}
}


// first: find or create custom group
$this->translateStrings($data);
$customGroup = $this->identifyEntity('CustomGroup', $data);
if (empty($customGroup)) {
// create CustomGroup
$customGroup = $this->createEntity('CustomGroup', $data);
} elseif ($customGroup == 'FAILED') {
// Couldn't identify:
$this->log(CUSTOM_DATA_HELPER_LOG_ERROR, "Couldn't create/update CustomGroup: " . json_encode($data));
return;
} else {
// update CustomGroup
$this->updateEntity('CustomGroup', $data, $customGroup, array('extends', 'style', 'is_active', 'title', 'extends_entity_column_value'), $force_update);
}

// now run the update for the CustomFields
foreach ($data['_fields'] as $customFieldSpec) {
$this->translateStrings($customFieldSpec);
$customFieldSpec['custom_group_id'] = $customGroup['id'];
$customFieldSpec['_lookup'][] = 'custom_group_id';
if (!empty($customFieldSpec['option_group_id']) && !is_numeric($customFieldSpec['option_group_id'])) {
// look up custom group id
$optionGroup = $this->getEntityID('OptionGroup', array('name' => $customFieldSpec['option_group_id']));
if ($optionGroup == 'FAILED' || $optionGroup==NULL) {
$this->log(CUSTOM_DATA_HELPER_LOG_ERROR, "Couldn't create/update CustomField, bad option_group: {$customFieldSpec['option_group_id']}");
return;
}
$customFieldSpec['option_group_id'] = $optionGroup['id'];
}
$customField = $this->identifyEntity('CustomField', $customFieldSpec);
if (empty($customField)) {
// create CustomField
$customField = $this->createEntity('CustomField', $customFieldSpec);
} elseif ($customField == 'FAILED') {
// Couldn't identify:
$this->log(CUSTOM_DATA_HELPER_LOG_ERROR, "Couldn't create/update CustomField: " . json_encode($customFieldSpec));
} else {
// update CustomField
$this->updateEntity('CustomField', $customFieldSpec, $customField, array('in_selector', 'is_view', 'is_searchable', 'html_type', 'data_type', 'custom_group_id'));
}
}
}

/**
* return the ID of the given entity (if exists)
*/
Expand Down
4 changes: 2 additions & 2 deletions CRM/Contract/FormUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function replaceIdWithLabel($name, $entity) {
if (isset($details[$result['custom_group_id']])) {
$customGroupTableId = key($details[$result['custom_group_id']]);
if (!empty($details[$result['custom_group_id']][$customGroupTableId]['fields'][$result['id']]['field_value'])) {
$entityId = $details[$result['custom_group_id']][$customGroupTableId]['fields'][$result['id']]['field_value'];
$entityId = $details[$result['custom_group_id']][$customGroupTableId]['fields'][$result['id']]['data'];
if ($entity == 'ContributionRecur') {
try {
$entityResult = civicrm_api3($entity, 'getsingle', ['id' => $entityId]);
Expand Down Expand Up @@ -87,7 +87,7 @@ public function setPaymentAmountCurrency() {
]);
$details = $this->form->get_template_vars('viewCustomData');
$customGroupTableId = key($details[$result['custom_group_id']]);
$recContributionId = $details[$result['custom_group_id']][$customGroupTableId]['fields'][$result['id']]['field_value'];
$recContributionId = $details[$result['custom_group_id']][$customGroupTableId]['fields'][$result['id']]['data'];

if (empty($recContributionId)) return;

Expand Down
52 changes: 2 additions & 50 deletions CRM/Contract/Upgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* Collection of upgrade steps.
*/
class CRM_Contract_Upgrader extends CRM_Contract_Upgrader_Base {
class CRM_Contract_Upgrader extends CRM_Extension_Upgrader_Base {

// By convention, functions that look like "function upgrade_NNNN()" are
// upgrade tasks. They are executed in order (like Drupal's hook_update_N).
Expand All @@ -22,24 +22,6 @@ public function install() {
}

public function enable() {
require_once 'CRM/Contract/CustomData.php';
$customData = new CRM_Contract_CustomData('de.systopia.contract');
$customData->syncOptionGroup(__DIR__ . '/../../resources/option_group_contact_channel.json');
$customData->syncOptionGroup(__DIR__ . '/../../resources/option_group_contract_cancel_reason.json');
$customData->syncOptionGroup(__DIR__ . '/../../resources/option_group_contract_cancel_reason.json');
$customData->syncOptionGroup(__DIR__ . '/../../resources/option_group_payment_frequency.json');
$customData->syncOptionGroup(__DIR__ . '/../../resources/option_group_activity_types.json');
$customData->syncOptionGroup(__DIR__ . '/../../resources/option_group_activity_status.json');
$customData->syncOptionGroup(__DIR__ . '/../../resources/option_group_shirt_type.json');
$customData->syncOptionGroup(__DIR__ . '/../../resources/option_group_shirt_size.json');
$customData->syncOptionGroup(__DIR__ . '/../../resources/option_group_contribution_recur_status.json');
$customData->syncCustomGroup(__DIR__ . '/../../resources/custom_group_contract_cancellation.json');
$customData->syncCustomGroup(__DIR__ . '/../../resources/custom_group_contract_updates.json');
$customData->syncCustomGroup(__DIR__ . '/../../resources/custom_group_membership_cancellation.json');
$customData->syncCustomGroup(__DIR__ . '/../../resources/custom_group_membership_payment.json');
$customData->syncCustomGroup(__DIR__ . '/../../resources/custom_group_membership_general.json');
$customData->syncEntities(__DIR__ . '/../../resources/entities_membership_status.json');

// create sub-type 'Dialoger'
$dialoger_exists = civicrm_api3('ContactType', 'getcount', ['name' => 'Dialoger']);
if (!$dialoger_exists) {
Expand All @@ -56,20 +38,6 @@ public function postInstall() {
public function uninstall() {
}

/**
* Add custom field "defer_payment_start"
*
* @return TRUE on success
* @throws Exception
*/
public function upgrade_1360() {
$this->ctx->log->info('Applying update 1360');
$customData = new CRM_Contract_CustomData('de.systopia.contract');
$customData->syncCustomGroup(__DIR__ . '/../../resources/custom_group_contract_updates.json');
$customData->syncCustomGroup(__DIR__ . '/../../resources/custom_group_membership_payment.json');
return TRUE;
}

public function upgrade_1370() {
$this->ctx->log->info('Applying update 1370');
$this->executeSqlFile('sql/contract.sql');
Expand All @@ -83,22 +51,13 @@ public function upgrade_1390() {
return TRUE;
}

public function upgrade_1402() {
$this->ctx->log->info('Applying updates for 14xx');
$customData = new CRM_Contract_CustomData('de.systopia.contract');
$customData->syncOptionGroup(__DIR__ . '/../../resources/option_group_contact_channel.json');
$customData->syncOptionGroup(__DIR__ . '/../../resources/option_group_order_type.json');
$customData->syncCustomGroup(__DIR__ . '/../../resources/custom_group_membership_general.json');
return TRUE;
}

/**
* Convert scheduled legacy update activities by adding ch_payment_changes
*
* @throws \CiviCRM_API3_Exception
*/
protected function convertLegacyUpdates() {
$paymentChangeField = \CRM_Contract_CustomData::getCustomFieldKey(
$paymentChangeField = CRM_Contract_CustomData::getCustomFieldKey(
'contract_updates',
'ch_payment_changes'
);
Expand Down Expand Up @@ -134,15 +93,8 @@ protected function convertLegacyUpdates() {
public function upgrade_1500() {
$this->ctx->log->info('Applying update 1500');
$customData = new CRM_Contract_CustomData('de.systopia.contract');
$customData->syncCustomGroup(__DIR__ . '/../../resources/custom_group_contract_updates.json');
$this->convertLegacyUpdates();
return TRUE;
}

public function upgrade_1510() {
$this->ctx->log->info('Applying update 1510');
$customData = new CRM_Contract_CustomData('de.systopia.contract');
$customData->syncOptionGroup(__DIR__ . '/../../resources/option_group_contribution_recur_status.json');
return TRUE;
}
}
Loading
Loading