Skip to content
This repository has been archived by the owner on Feb 21, 2020. It is now read-only.

Handle extends_entity_column_value for contributions. #13

Open
wants to merge 2 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
123 changes: 42 additions & 81 deletions CRM/Civiconfig/Entity/CustomGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* This class creates the custom fields as well.
*
* @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
* @date 3 Feb 2016
* Johan Vervloet (Chirojeugd-Vlaanderen vzw) <helpdesk@chiro.be>
* @date 16 Feb 2017
* @license AGPL-3.0
*/
class CRM_Civiconfig_Entity_CustomGroup extends CRM_Civiconfig_Entity {
Expand Down Expand Up @@ -41,8 +42,6 @@ public function create(array $params) {
/**
* Manipulate $params before entity creation.
*
* TODO: This function is too complex and can probably be refactored.
*
* @param array $params params that will be used for entity creation
* @param array $existing existing entity (if available)
*/
Expand All @@ -54,87 +53,49 @@ protected function prepareParams(array &$params, array $existing = []) {

unset($params['fields']);

switch ($params['extends']) {
case "Activity":
if (!empty($params['extends_entity_column_value'])) {
if (is_array($params['extends_entity_column_value'])) {
foreach ($params['extends_entity_column_value'] as $extendsValue) {
$activityType = new CRM_Civiconfig_Entity_ActivityType();
$found = $activityType->getExisting(['name' => $extendsValue]);
if (isset($found['value'])) {
$params['extends_entity_column_value'][] = $found['value'];
}
unset ($activityType);
}
} else {
$activityType = new CRM_Civiconfig_Entity_ActivityType();
$found = $activityType->getExisting(['name' => $params['extends_entity_column_value']]);
if (isset($found['value'])) {
$params['extends_entity_column_value'] = $found['value'];
}
}
}
if (empty($params['extends_entity_column_value'])) {
return;
}

// FIXME: This is rather hacky.
$entityType = $params['extends'];
switch ($entityType) {
case 'Organization':
case 'Individual':
case 'Household':
// Contact sub types can be identified by their names. No need to
// look up ID's.
return;
break;
case "Membership":
if (!empty($params['extends_entity_column_value'])) {
if (is_array($params['extends_entity_column_value'])) {
foreach ($params['extends_entity_column_value'] as $extendsValue) {
$membershipType = new CRM_Civiconfig_Entity_MembershipType();
$found = $membershipType->getExisting(['name' => $extendsValue]);
if (isset($found['id'])) {
$params['extends_entity_column_value'][] = $found['id'];
}
unset ($membershipType);
}
} else {
$membershipType = new CRM_Civiconfig_Entity_MembershipType();
$found = $membershipType->getExisting(['name' => $params['extends_entity_column_value']]);
if (isset($found['id'])) {
$params['extends_entity_column_value'] = $found['id'];
}
}
}
case 'Contribution':
$entitySubType = 'FinancialType';
break;
case "Relationship":
if (!empty($params['extends_entity_column_value'])) {
if (is_array($params['extends_entity_column_value'])) {
foreach ($params['extends_entity_column_value'] as $extendsValue) {
$relationshipType = new CRM_Civiconfig_Entity_RelationshipType();
$found = $relationshipType->getExisting(['name_a_b' => $extendsValue]);
if (isset($found['id'])) {
$params['extends_entity_column_value'][] = $found['id'];
}
unset ($relationshipType);
}
} else {
$relationshipType = new CRM_Civiconfig_Entity_RelationshipType();
$found = $relationshipType->getExisting(['name_a_b' => $params['extends_entity_column_value']]);
if (isset($found['id'])) {
$params['extends_entity_column_value'] = $found['id'];
}
}
}
case 'ParticipantEventType':
$entitySubType = 'EventType';
break;
case "ParticipantEventType":
if (!empty($params['extends_entity_column_value'])) {
if (is_array($params['extends_entity_column_value'])) {
foreach ($params['extends_entity_column_value'] as $extendsValue) {
$eventType = new CRM_Civiconfig_Entity_EventType();
$found = $eventType->getExisting(['name' => $extendsValue]);
if (isset($found['value'])) {
$params['extends_entity_column_value'][] = $found['value'];
}
unset ($eventType);
}
} else {
$eventType = new CRM_Civiconfig_Entity_EventType();
$found = $eventType->getExisting(['name' => $params['extends_entity_column_value']]);
if (isset($found['value'])) {
$params['extends_entity_column_value'] = $found['value'];
}
}
}
default:
$entitySubType = $entityType . "Type";
break;
}
$entitySubTypeKey = in_array($entityType, [
'Activity',
'Event'
]) ? 'value' : 'id';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ParticipantEventType also works based on the value, not the id. 'ParticipantEventType' should also be included in the in_array statement


$values = $params['extends_entity_column_value'];
if (!is_array($values)) {
$values = [$values];
}

$className = "CRM_Civiconfig_Entity_$entitySubType";
$entityConfig = new $className();

$params['extends_entity_column_value'] = [];
foreach ($values as $extendsValue) {
$found = $entityConfig->getExisting(['name' => $extendsValue]);
if (isset($found[$entitySubTypeKey])) {
$params['extends_entity_column_value'][] = $found[$entitySubTypeKey];
}
}
}
}
}
23 changes: 23 additions & 0 deletions resources_examples/custom_groups.json
Original file line number Diff line number Diff line change
Expand Up @@ -389,5 +389,28 @@
"is_active": 1
}
}
},
"fee21_extra": {
"name": "fee21_extra",
"title": "Some extra field for event fee 21",
"extends": "Contribution",
"extends_entity_column_value":"Event Fee (BTW 21%)",
"style": "Inline",
"is_active": 1,
"is_reserved": 0,
"collapse_display": 0,
"table_name": "civicrm_value_fee21_extra",
"fields": {
"foobar": {
"name": "foobar",
"label": "Foo/Bar",
"data_type": "Int",
"html_type": "Text",
"is_searchable": 1,
"is_view": 0,
"column_name": "foobar",
"is_active": 1
}
}
}
}