Skip to content

Commit

Permalink
Merge pull request #27666 from mattwire/api4underscore
Browse files Browse the repository at this point in the history
Searchdisplay issue with contact join activity and _ on the end of custom field
  • Loading branch information
colemanw authored Oct 14, 2023
2 parents bd9d31c + e17746a commit 78c3bd8
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
22 changes: 21 additions & 1 deletion Civi/Api4/Query/SqlExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,26 @@ public function __construct(string $expr, $alias = NULL) {

abstract protected function initialize();

private static function munge($name, $char = '_', $len = 63) {
// Replace all white space and non-alpha numeric with $char
// we only use the ascii character set since mysql does not create table names / field names otherwise
// CRM-11744
$name = preg_replace('/[^a-zA-Z0-9_]+/', $char, trim($name));

// If there are no ascii characters present.
if (!strlen(trim($name, $char))) {
$name = \CRM_Utils_String::createRandom($len, \CRM_Utils_String::ALPHANUMERIC);
}

if ($len) {
// lets keep variable names short
return substr($name, 0, $len);
}
else {
return $name;
}
}

/**
* Converts a string to a SqlExpression object.
*
Expand All @@ -82,7 +102,7 @@ abstract protected function initialize();
public static function convert(string $expression, $parseAlias = FALSE, $mustBe = []) {
$as = $parseAlias ? strrpos($expression, ' AS ') : FALSE;
$expr = $as ? substr($expression, 0, $as) : $expression;
$alias = $as ? \CRM_Utils_String::munge(substr($expression, $as + 4), '_', 256) : NULL;
$alias = $as ? self::munge(substr($expression, $as + 4), '_', 256) : NULL;
$bracketPos = strpos($expr, '(');
$firstChar = substr($expr, 0, 1);
$lastChar = substr($expr, -1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,4 +502,90 @@ public function testEntityReferenceJoins() {
$this->assertEquals('Dewey', $result[0]['columns'][0]['val']);
}

public function testJoinWithCustomFieldEndingIn_() {
$subject = uniqid(__FUNCTION__);

$contact = Contact::create(FALSE)
->execute()->single();

// CustomGroup based on Activity Type
CustomGroup::create(FALSE)
->addValue('extends', 'Activity')
->addValue('title', 'testactivity2')
->addChain('field', CustomField::create()
->addValue('custom_group_id', '$id')
->addValue('label', 'testactivity_')
->addValue('data_type', 'Boolean')
->addValue('html_type', 'Radio')
)
->execute();

$sampleData = [
['activity_type_id:name' => 'Meeting', 'testactivity2.testactivity_' => TRUE],
];
$this->saveTestRecords('Activity', [
'defaults' => ['subject' => $subject, 'source_contact_id', $contact['id']],
'records' => $sampleData,
]);

$params = [
'checkPermissions' => FALSE,
'return' => 'page:1',
'savedSearch' => [
'api_entity' => 'Contact',
'api_params' => [
'version' => 4,
'select' => [
'id',
'GROUP_CONCAT(DISTINCT Contact_ActivityContact_Activity_01.testactivity2.testactivity_:label) AS GROUP_CONCAT_Contact_ActivityContact_Activity_01_testactivity2_testactivity__label',
],
'orderBy' => [],
'where' => [['contact_type:name', '=', 'Individual']],
'groupBy' => ['id'],
'join' => [
['Activity AS Contact_ActivityContact_Activity_01', 'INNER', 'ActivityContact',
['id', '=', 'Contact_ActivityContact_Activity_01.contact_id'],
['Contact_ActivityContact_Activity_01.record_type_id:name', '=', '"Activity Source"'],
['Contact_ActivityContact_Activity_01.activity_type_id:name', '=', '"Meeting"'],
],
],
'having' => [],
],
],
'display' => [
'type' => 'table',
'label' => '',
'settings' => [
'actions' => TRUE,
'pager' => [],
'columns' => [
[
'type' => 'field',
'key' => 'id',
'dataType' => 'Integer',
'label' => 'Contact ID',
'sortable' => TRUE,
],
[
'type' => 'field',
'key' => 'GROUP_CONCAT_Contact_ActivityContact_Activity_01_testactivity2_testactivity__label',
'dataType' => 'Boolean',
'label' => '(List) Contact Activities: testactivity2: testactivity_',
'sortable' => TRUE,
],
],
'sort' => [
['id', 'ASC'],
],
],
],
'afform' => NULL,
];

$result = civicrm_api4('SearchDisplay', 'run', $params);

$this->assertArrayHasKey('GROUP_CONCAT_Contact_ActivityContact_Activity_01_testactivity2_testactivity__label', $result[0]['data']);
$this->assertEquals('Yes', $result[0]['data']['GROUP_CONCAT_Contact_ActivityContact_Activity_01_testactivity2_testactivity__label'][0]);
}

}

0 comments on commit 78c3bd8

Please sign in to comment.