From 8cefa956f96c72b82cecbeba75b260a0eb151286 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 22 Apr 2016 17:19:07 -0700 Subject: [PATCH] CRM-17984 - CRM_Core_BAO_CustomGroup - Extract whereListHas() This seems to have been a rather fiddly bit of SQL that was duplicated in the past. --- CRM/Core/BAO/CustomGroup.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/CRM/Core/BAO/CustomGroup.php b/CRM/Core/BAO/CustomGroup.php index fe42c04bf8..d56c7ed18b 100644 --- a/CRM/Core/BAO/CustomGroup.php +++ b/CRM/Core/BAO/CustomGroup.php @@ -441,7 +441,7 @@ LEFT JOIN civicrm_custom_field ON (civicrm_custom_field.custom_group_id = civicr if (!empty($subTypes)) { foreach ($subTypes as $key => $subType) { - $subTypeClauses[] = "civicrm_custom_group.extends_entity_column_value LIKE '%" . self::validateSubTypeByEntity($entityType, $subType) . "%'"; + $subTypeClauses[] = self::whereListHas("civicrm_custom_group.extends_entity_column_value", self::validateSubTypeByEntity($entityType, $subType)); } $subTypeClause = '(' . implode(' OR ', $subTypeClauses) . ')'; if (!$onlySubType) { @@ -660,6 +660,22 @@ ORDER BY civicrm_custom_group.weight, return $subType; } + /** + * Suppose you have a SQL column, $column, which includes a delimited list, and you want + * a WHERE condition for rows that include $value. Use whereListHas(). + * + * @param string $column + * @param string $value + * @param string $delimiter + * @return string + * SQL condition. + */ + static private function whereListHas($column, $value, $delimiter = CRM_Core_DAO::VALUE_SEPARATOR) { + $bareValue = trim($value, $delimiter); // ? + $escapedValue = CRM_Utils_Type::escape("%{$delimiter}{$bareValue}{$delimiter}%", 'String', FALSE); + return "($column LIKE \"$escapedValue\")"; + } + /** * Check whether the custom group has any data for the given entity. * -- 2.25.1