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

CRM-17789 - CRM_Custom_Form_Group - Simplify reltype list #9035

Merged
merged 2 commits into from
Sep 14, 2016
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
18 changes: 18 additions & 0 deletions CRM/Core/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,24 @@ public function fetchAll() {
return $result;
}

/**
* Get all the result records as mapping between columns.
*
* @param string $keyColumn
* Ex: "name"
* @param string $valueColumn
* Ex: "label"
* @return array
* Ex: ["foo" => "The Foo Bar", "baz" => "The Baz Qux"]
*/
public function fetchMap($keyColumn, $valueColumn) {
$result = array();
while ($this->fetch()) {
$result[$this->{$keyColumn}] = $this->{$valueColumn};
}
return $result;
}

/**
* Given a DAO name, a column name and a column value, find the record and GET the value of another column in that record
*
Expand Down
79 changes: 24 additions & 55 deletions CRM/Custom/Form/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,29 +201,13 @@ public function buildQuickForm() {
$campaignTypes = CRM_Campaign_PseudoConstant::campaignType();
$membershipType = CRM_Member_BAO_MembershipType::getMembershipTypes(FALSE);
$participantRole = CRM_Core_OptionGroup::values('participant_role');
$relTypeInd = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, 'Individual');
$relTypeOrg = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, 'Organization');
$relTypeHou = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, 'Household');

ksort($sel1);
asort($activityType);
asort($eventType);
asort($grantType);
asort($membershipType);
asort($participantRole);
$allRelationshipType = array();
$allRelationshipType = array_merge($relTypeInd, $relTypeOrg);
$allRelationshipType = array_merge($allRelationshipType, $relTypeHou);

//adding subtype specific relationships CRM-5256
$subTypes = CRM_Contact_BAO_ContactType::subTypeInfo();

foreach ($subTypes as $subType => $val) {
$subTypeRelationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, $val['parent'],
FALSE, 'label', TRUE, $subType
);
$allRelationshipType = array_merge($allRelationshipType, $subTypeRelationshipTypes);
}

$sel2['Event'] = $eventType;
$sel2['Grant'] = $grantType;
Expand All @@ -234,7 +218,7 @@ public function buildQuickForm() {
$sel2['ParticipantEventName'] = CRM_Event_PseudoConstant::event(NULL, FALSE, "( is_template IS NULL OR is_template != 1 )");
$sel2['ParticipantEventType'] = $eventType;
$sel2['Contribution'] = CRM_Contribute_PseudoConstant::financialType();
$sel2['Relationship'] = $allRelationshipType;
$sel2['Relationship'] = self::getRelationshipTypes();

$sel2['Individual'] = CRM_Contact_BAO_ContactType::subTypePairs('Individual', FALSE, NULL);
$sel2['Household'] = CRM_Contact_BAO_ContactType::subTypePairs('Household', FALSE, NULL);
Expand All @@ -244,17 +228,9 @@ public function buildQuickForm() {

foreach ($sel2 as $main => $sub) {
if (!empty($sel2[$main])) {
if ($main == 'Relationship') {
$relName = self::getFormattedList($sel2[$main]);
$sel2[$main] = array(
'' => ts("- Any -"),
) + $relName;
}
else {
$sel2[$main] = array(
'' => ts("- Any -"),
) + $sel2[$main];
}
$sel2[$main] = array(
'' => ts("- Any -"),
) + $sel2[$main];
}
}

Expand Down Expand Up @@ -519,36 +495,29 @@ public function postProcess() {
}

/**
* Return a formatted list of relationship name.
*
* @param array $list
* Array of relationship name.
* Return a formatted list of relationship labels.
*
* @return array
* Array of relationship name.
* Array (int $id => string $label).
*/
public static function getFormattedList(&$list) {
$relName = array();

foreach ($list as $listItemKey => $itemValue) {
// Extract the relationship ID.
$key = substr($listItemKey, 0, strpos($listItemKey, '_'));
if (isset($list["{$key}_b_a"])) {
$relName["$key"] = $list["{$key}_a_b"];
// Are the two labels different?
if ($list["{$key}_a_b"] != $list["{$key}_b_a"]) {
$relName["$key"] = $list["{$key}_a_b"] . ' / ' . $list["{$key}_b_a"];
}
unset($list["{$key}_b_a"]);
unset($list["{$key}_a_b"]);
}
else {
// If no '_b_a' label exists save the '_a_b' one and unset it from the list
$relName["{$key}"] = $list["{$key}_a_b"];
unset($list["{$key}_a_b"]);
}
}
return $relName;
public static function getRelationshipTypes() {
// Note: We include inactive reltypes because we don't want to break custom-data
// UI when a reltype is disabled.
return CRM_Core_DAO::executeQuery('
SELECT
id,
(CASE 1
WHEN label_a_b is not null AND label_b_a is not null AND label_a_b != label_b_a
THEN concat(label_a_b, \' / \', label_b_a)
WHEN label_a_b is not null
THEN label_a_b
WHEN label_b_a is not null
THEN label_b_a
ELSE concat("RelType #", id)
END) as label
FROM civicrm_relationship_type
'
)->fetchMap('id', 'label');
}

}