Skip to content

Commit

Permalink
APIv4 - Use non-deprecated join syntax internally
Browse files Browse the repository at this point in the history
Updates APIv4 calls in CiviCRM core to use e.g. `event_id.title` instead of `event.title`
  • Loading branch information
colemanw committed Jun 29, 2021
1 parent a057a07 commit c1c648b
Show file tree
Hide file tree
Showing 25 changed files with 125 additions and 125 deletions.
6 changes: 3 additions & 3 deletions CRM/Contact/Form/Task/EmailTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -529,11 +529,11 @@ protected function getEmailString(array $emailIDs): string {
$emails = Email::get()
->addWhere('id', 'IN', $emailIDs)
->setCheckPermissions(FALSE)
->setSelect(['contact_id', 'email', 'contact.sort_name', 'contact.display_name'])->execute();
->setSelect(['contact_id', 'email', 'contact_id.sort_name', 'contact_id.display_name'])->execute();
$emailStrings = [];
foreach ($emails as $email) {
$this->contactEmails[$email['id']] = $email;
$emailStrings[] = '"' . $email['contact.sort_name'] . '" <' . $email['email'] . '>';
$emailStrings[] = '"' . $email['contact_id.sort_name'] . '" <' . $email['email'] . '>';
}
return implode(',', $emailStrings);
}
Expand All @@ -552,7 +552,7 @@ protected function getEmailUrlString(array $emailIDs): string {
$urls = [];
foreach ($emailIDs as $email) {
$contactURL = CRM_Utils_System::url('civicrm/contact/view', ['reset' => 1, 'cid' => $this->contactEmails[$email]['contact_id']], TRUE);
$urls[] = "<a href='{$contactURL}'>" . $this->contactEmails[$email]['contact.display_name'] . '</a>';
$urls[] = "<a href='{$contactURL}'>" . $this->contactEmails[$email]['contact_id.display_name'] . '</a>';
}
return implode(', ', $urls);
}
Expand Down
4 changes: 2 additions & 2 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -1318,13 +1318,13 @@ protected static function isEmailReceipt(array $input, int $contributionID, $rec
return (bool) ContributionRecur::get(FALSE)->addWhere('id', '=', $recurringContributionID)->addSelect('is_email_receipt')->execute()->first()['is_email_receipt'];
}
$contributionPage = Contribution::get(FALSE)
->addSelect('contribution_page.is_email_receipt')
->addSelect('contribution_page_id.is_email_receipt')
->addWhere('contribution_page_id', 'IS NOT NULL')
->addWhere('id', '=', $contributionID)
->execute()->first();

if (!empty($contributionPage)) {
return (bool) $contributionPage['contribution_page.is_email_receipt'];
return (bool) $contributionPage['contribution_page_id.is_email_receipt'];
}
// This would be the case for backoffice (where is_email_receipt is not passed in) or events, where Event::sendMail will filter
// again anyway.
Expand Down
26 changes: 13 additions & 13 deletions CRM/Contribute/BAO/ContributionPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,13 +522,13 @@ public static function recurringNotify($contributionID, $type, $recur): void {
'contribution_page_id',
'contact_id',
'contribution_recur_id',
'contribution_recur.is_email_receipt',
'contribution_page.title',
'contribution_page.is_email_receipt',
'contribution_page.receipt_from_name',
'contribution_page.receipt_from_email',
'contribution_page.cc_receipt',
'contribution_page.bcc_receipt',
'contribution_recur_id.is_email_receipt',
'contribution_page_id.title',
'contribution_page_id.is_email_receipt',
'contribution_page_id.receipt_from_name',
'contribution_page_id.receipt_from_email',
'contribution_page_id.cc_receipt',
'contribution_page_id.bcc_receipt',
])
->execute()->first();

Expand All @@ -537,10 +537,10 @@ public static function recurringNotify($contributionID, $type, $recur): void {
->addWhere('entity_table', '=', 'civicrm_membership')
->addSelect('id')->execute()->first());

if ($contribution['contribution_recur.is_email_receipt'] || $contribution['contribution_page.is_email_receipt']) {
if ($contribution['contribution_page.receipt_from_email']) {
$receiptFromName = $contribution['contribution_page.receipt_from_name'];
$receiptFromEmail = $contribution['contribution_page.receipt_from_email'];
if ($contribution['contribution_recur_id.is_email_receipt'] || $contribution['contribution_page_id.is_email_receipt']) {
if ($contribution['contribution_page_id.receipt_from_email']) {
$receiptFromName = $contribution['contribution_page_id.receipt_from_name'];
$receiptFromEmail = $contribution['contribution_page_id.receipt_from_email'];
}
else {
[$receiptFromName, $receiptFromEmail] = CRM_Core_BAO_Domain::getNameAndEmail();
Expand Down Expand Up @@ -570,8 +570,8 @@ public static function recurringNotify($contributionID, $type, $recur): void {
'toEmail' => $email,
];
//CRM-13811
$templatesParams['cc'] = $contribution['contribution_page.cc_receipt'];
$templatesParams['bcc'] = $contribution['contribution_page.cc_receipt'];
$templatesParams['cc'] = $contribution['contribution_page_id.cc_receipt'];
$templatesParams['bcc'] = $contribution['contribution_page_id.cc_receipt'];
if ($recur->id) {
// in some cases its just recurringNotify() thats called for the first time and these urls don't get set.
// like in PaypalPro, & therefore we set it here additionally.
Expand Down
6 changes: 3 additions & 3 deletions CRM/Contribute/Form/Task/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,13 +430,13 @@ public static function printPDF($contribIDs, &$params, $contactIds) {
$emails = Email::get()
->addWhere('id', 'IN', $emailIDs)
->setCheckPermissions(FALSE)
->setSelect(['contact_id', 'email', 'contact.sort_name', 'contact.display_name'])->execute();
->setSelect(['contact_id', 'email', 'contact_id.sort_name', 'contact_id.display_name'])->execute();
$emailStrings = $contactUrlStrings = [];
foreach ($emails as $email) {
$emailStrings[] = '"' . $email['contact.sort_name'] . '" <' . $email['email'] . '>';
$emailStrings[] = '"' . $email['contact_id.sort_name'] . '" <' . $email['email'] . '>';
// generate the contact url to put in Activity
$contactURL = CRM_Utils_System::url('civicrm/contact/view', ['reset' => 1, 'force' => 1, 'cid' => $email['contact_id']], TRUE);
$contactUrlStrings[] = "<a href='{$contactURL}'>" . $email['contact.display_name'] . '</a>';
$contactUrlStrings[] = "<a href='{$contactURL}'>" . $email['contact_id.display_name'] . '</a>';
}
$cc_emails = implode(',', $emailStrings);
$values['cc_receipt'] = $cc_emails;
Expand Down
4 changes: 2 additions & 2 deletions CRM/Dedupe/Merger.php
Original file line number Diff line number Diff line change
Expand Up @@ -655,14 +655,14 @@ protected static function filterRowBasedCustomDataFromCustomTables(array &$cidRe
*/
protected static function getMultiValueCidRefs() {
$fields = \Civi\Api4\CustomField::get(FALSE)
->addSelect('custom_group.table_name', 'column_name', 'serialize')
->addSelect('custom_group_id.table_name', 'column_name', 'serialize')
->addWhere('data_type', '=', 'ContactReference')
->addWhere('serialize', 'IS NOT EMPTY')
->execute();

$map = [];
foreach ($fields as $field) {
$map[$field['custom_group.table_name']][$field['column_name']] = $field['serialize'];
$map[$field['custom_group_id.table_name']][$field['column_name']] = $field['serialize'];
}
return $map;
}
Expand Down
6 changes: 3 additions & 3 deletions CRM/Event/Form/ManageEvent/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public function buildQuickForm() {
if ($this->_id) {
$this->locationBlock = Event::get()
->addWhere('id', '=', $this->_id)
->setSelect(['loc_block.*', 'loc_block_id'])
->setSelect(['loc_block_id.*', 'loc_block_id'])
->execute()->first();
$this->_oldLocBlockId = $this->locationBlock['loc_block_id'];
}
Expand Down Expand Up @@ -249,8 +249,8 @@ public function postProcess() {
}
$params[$block][$index]['location_type_id'] = $defaultLocationTypeID;
$fieldKey = (int) $index === 1 ? '_id' : '_2_id';
if ($isUpdateToExistingLocationBlock && !empty($this->locationBlock['loc_block.' . $block . $fieldKey])) {
$params[$block][$index]['id'] = $this->locationBlock['loc_block.' . $block . $fieldKey];
if ($isUpdateToExistingLocationBlock && !empty($this->locationBlock['loc_block_id.' . $block . $fieldKey])) {
$params[$block][$index]['id'] = $this->locationBlock['loc_block_id.' . $block . $fieldKey];
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions CRM/Event/Page/UserDashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public function listParticipations() {
$event_rows = [];

$participants = \Civi\Api4\Participant::get(FALSE)
->addSelect('id', 'contact_id', 'status_id:name', 'status_id:label', 'event.id', 'event.title', 'event.start_date', 'event.end_date')
->addSelect('id', 'contact_id', 'status_id:name', 'status_id:label', 'event_id', 'event_id.title', 'event_id.start_date', 'event_id.end_date')
->addWhere('contact_id', '=', $this->_contactId)
->addOrderBy('event.start_date', 'DESC')
->addOrderBy('event_id.start_date', 'DESC')
->execute()
->indexBy('id');

Expand All @@ -39,10 +39,10 @@ public function listParticipations() {
$p['participant_id'] = $p['id'];
$p['status'] = $p['status_id:name'];
$p['participant_status'] = $p['status_id:label'];
$p['event_id'] = $p['event.id'];
$p['event_title'] = $p['event.title'];
$p['event_start_date'] = $p['event.start_date'];
$p['event_end_date'] = $p['event.end_date'];
$p['event_id'] = $p['event_id'];
$p['event_title'] = $p['event_id.title'];
$p['event_start_date'] = $p['event_id.start_date'];
$p['event_end_date'] = $p['event_id.end_date'];

$event_rows[] = $p;
}
Expand Down
2 changes: 1 addition & 1 deletion CRM/Financial/BAO/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ public function setPriceSelectionFromUnfilteredInput(array $input): void {
public function getDefaultPriceField() {
return PriceField::get(FALSE)
->addWhere('name', '=', 'contribution_amount')
->addWhere('price_set.name', '=', 'default_contribution_amount')
->addWhere('price_set_id.name', '=', 'default_contribution_amount')
->execute()->first()['id'];
}

Expand Down
8 changes: 4 additions & 4 deletions Civi/Api4/Generic/Traits/DAOActionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ protected function formatCustomParams(&$params, $entityId) {
$field['id'],
$customParams,
$value,
$field['custom_group.extends'],
$field['custom_group_id.extends'],
// todo check when this is needed
NULL,
$entityId,
Expand Down Expand Up @@ -270,13 +270,13 @@ protected function getCustomFieldInfo(string $fieldExpr) {
if (!isset($info[$fieldName])) {
$info = [];
$fields = CustomField::get(FALSE)
->addSelect('id', 'name', 'html_type', 'data_type', 'custom_group.extends')
->addWhere('custom_group.name', '=', $groupName)
->addSelect('id', 'name', 'html_type', 'data_type', 'custom_group_id.extends')
->addWhere('custom_group_id.name', '=', $groupName)
->execute()->indexBy('name');
foreach ($fields as $name => $field) {
$field['custom_field_id'] = $field['id'];
$field['name'] = $groupName . '.' . $name;
$field['entity'] = CustomGroupJoinable::getEntityFromExtends($field['custom_group.extends']);
$field['entity'] = CustomGroupJoinable::getEntityFromExtends($field['custom_group_id.extends']);
$info[$name] = $field;
}
\Civi::cache('metadata')->set($cacheKey, $info);
Expand Down
6 changes: 3 additions & 3 deletions Civi/Api4/Service/Schema/Joinable/CustomGroupJoinable.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ public function getEntityFields() {
$entityFields = (array) \Civi::cache('metadata')->get($cacheKey);
if (!$entityFields) {
$fields = CustomField::get(FALSE)
->setSelect(['custom_group.name', 'custom_group.extends', 'custom_group.table_name', 'custom_group.title', '*'])
->addWhere('custom_group.table_name', '=', $this->getTargetTable())
->setSelect(['custom_group_id.name', 'custom_group_id.extends', 'custom_group_id.table_name', 'custom_group_id.title', '*'])
->addWhere('custom_group_id.table_name', '=', $this->getTargetTable())
->execute();
foreach ($fields as $field) {
$entityFields[] = \Civi\Api4\Service\Spec\SpecFormatter::arrayToField($field, self::getEntityFromExtends($field['custom_group.extends']));
$entityFields[] = \Civi\Api4\Service\Spec\SpecFormatter::arrayToField($field, self::getEntityFromExtends($field['custom_group_id.extends']));
}
\Civi::cache('metadata')->set($cacheKey, $entityFields);
}
Expand Down
8 changes: 4 additions & 4 deletions Civi/Api4/Service/Spec/SpecFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ public static function arrayToField(array $data, $entity) {
if (!empty($data['custom_group_id'])) {
$field = new CustomFieldSpec($data['name'], $entity, $dataTypeName);
if (strpos($entity, 'Custom_') !== 0) {
$field->setName($data['custom_group.name'] . '.' . $data['name']);
$field->setName($data['custom_group_id.name'] . '.' . $data['name']);
}
else {
$field->setTableName($data['custom_group.table_name']);
$field->setTableName($data['custom_group_id.table_name']);
}
$field->setColumnName($data['column_name']);
$field->setCustomFieldId($data['id'] ?? NULL);
$field->setCustomGroupName($data['custom_group.name']);
$field->setCustomGroupName($data['custom_group_id.name']);
$field->setTitle($data['label']);
$field->setLabel($data['custom_group.title'] . ': ' . $data['label']);
$field->setLabel($data['custom_group_id.title'] . ': ' . $data['label']);
$field->setHelpPre($data['help_pre'] ?? NULL);
$field->setHelpPost($data['help_post'] ?? NULL);
if (self::customFieldHasOptions($data)) {
Expand Down
10 changes: 5 additions & 5 deletions Civi/Api4/Service/Spec/SpecGatherer.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ private function addCustomFields($entity, RequestSpec $specification, $values =
$extends = $customInfo['extends'];
}
$customFields = CustomField::get(FALSE)
->addWhere('custom_group.extends', 'IN', $extends)
->addWhere('custom_group.is_multiple', '=', '0')
->setSelect(['custom_group.name', 'custom_group.title', '*'])
->addWhere('custom_group_id.extends', 'IN', $extends)
->addWhere('custom_group_id.is_multiple', '=', '0')
->setSelect(['custom_group_id.name', 'custom_group_id.title', '*'])
->execute();

foreach ($customFields as $fieldArray) {
Expand All @@ -145,8 +145,8 @@ private function addCustomFields($entity, RequestSpec $specification, $values =
*/
private function getCustomGroupFields($customGroup, RequestSpec $specification) {
$customFields = CustomField::get(FALSE)
->addWhere('custom_group.name', '=', $customGroup)
->setSelect(['custom_group.name', 'custom_group.table_name', 'custom_group.title', '*'])
->addWhere('custom_group_id.name', '=', $customGroup)
->setSelect(['custom_group_id.name', 'custom_group_id.table_name', 'custom_group_id.title', '*'])
->execute();

foreach ($customFields as $fieldArray) {
Expand Down
16 changes: 8 additions & 8 deletions tests/phpunit/CRM/Event/Form/ManageEvent/LocationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ public function testCreateWithLocBlock() {

$locationBlock = Event::get()
->addWhere('id', '=', $eventID)
->setSelect(['loc_block.*', 'loc_block_id'])
->setSelect(['loc_block_id.*', 'loc_block_id'])
->execute()->first();

$address = $this->callAPISuccessGetSingle('Address', ['id' => $locationBlock['loc_block.address_id']]);
$address = $this->callAPISuccessGetSingle('Address', ['id' => $locationBlock['loc_block_id.address_id']]);

$this->assertEquals([
'id' => $address['id'],
Expand All @@ -101,9 +101,9 @@ public function testCreateWithLocBlock() {
'manual_geo_code' => '0',
], $address);

$this->callAPISuccessGetSingle('Email', ['id' => $locationBlock['loc_block.email_id'], 'email' => 'john.smith@example.org']);
$this->callAPISuccessGetSingle('Phone', ['id' => $locationBlock['loc_block.phone_id'], 'phone' => '303443689']);
$this->callAPISuccessGetSingle('Phone', ['id' => $locationBlock['loc_block.phone_2_id'], 'phone' => '9833910234']);
$this->callAPISuccessGetSingle('Email', ['id' => $locationBlock['loc_block_id.email_id'], 'email' => 'john.smith@example.org']);
$this->callAPISuccessGetSingle('Phone', ['id' => $locationBlock['loc_block_id.phone_id'], 'phone' => '303443689']);
$this->callAPISuccessGetSingle('Phone', ['id' => $locationBlock['loc_block_id.phone_2_id'], 'phone' => '9833910234']);

// Cleanup.
CRM_Core_BAO_Location::deleteLocBlock($locationBlock['loc_block_id']);
Expand Down Expand Up @@ -255,12 +255,12 @@ protected function assertCorrectEmails($eventID) {
$firstEmail = $emails->first();
$locationBlock = Event::get()
->addWhere('id', '=', $eventID)
->setSelect(['loc_block.*', 'loc_block_id'])
->setSelect(['loc_block_id.*', 'loc_block_id'])
->execute()->first();
$this->ids['LocBlock'][0] = $locationBlock['loc_block_id'];
$this->assertEquals($firstEmail['id'], $locationBlock['loc_block.email_id']);
$this->assertEquals($firstEmail['id'], $locationBlock['loc_block_id.email_id']);
$secondEmail = $emails->last();
$this->assertEquals($secondEmail['id'], $locationBlock['loc_block.email_2_id']);
$this->assertEquals($secondEmail['id'], $locationBlock['loc_block_id.email_2_id']);
return $emails;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/api/v3/ACLPermissionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ public function testContactGetViaJoin($version) {
$tag2 = $this->tagCreate(['name' => uniqid('other'), 'created_id' => $other])['id'];
$this->setPermissions(['access CiviCRM']);
$this->hookClass->setHook('civicrm_aclWhereClause', [$this, 'aclWhereHookAllResults']);
$createdFirstName = $version === 4 ? 'created.first_name' : 'created_id.first_name';
$createdFirstName = 'created_id.first_name';
$result = $this->callAPISuccess('Tag', 'get', [
'check_permissions' => 1,
'return' => ['id', $createdFirstName],
Expand Down
14 changes: 7 additions & 7 deletions tests/phpunit/api/v4/Action/ContactApiKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ public function testGetApiKey() {

// Can also be fetched via join
$email = Email::get()
->addSelect('contact.api_key')
->addSelect('contact_id.api_key')
->addWhere('id', '=', $contact['email']['id'])
->execute()->first();
$this->assertEquals($key, $email['contact.api_key']);
$this->assertEquals($key, $email['contact_id.api_key']);
$this->assertFalse($isSafe($email), "Should reveal secret details ($key): " . var_export($email, 1));

// Remove permission and we should not see the key
Expand All @@ -76,12 +76,12 @@ public function testGetApiKey() {

// Also not available via join
$email = Email::get()
->addSelect('contact.api_key')
->addSelect('contact_id.api_key')
->addWhere('id', '=', $contact['email']['id'])
->setDebug(TRUE)
->execute();
$this->assertContains('contact.api_key', $email->debug['undefined_fields']);
$this->assertArrayNotHasKey('contact.api_key', $email[0]);
$this->assertContains('contact_id.api_key', $email->debug['undefined_fields']);
$this->assertArrayNotHasKey('contact_id.api_key', $email[0]);
$this->assertTrue($isSafe($email[0]), "Should NOT reveal secret details ($key): " . var_export($email[0], 1));

$result = Contact::get()
Expand Down Expand Up @@ -137,14 +137,14 @@ public function testGetApiKeyViaJoin() {
$result = Email::get(FALSE)
->addWhere('contact_id', '=', $contact['id'])
->addSelect('email')
->addSelect('contact.api_key')
->addSelect('contact_id.api_key')
->execute()
->first();
$this->assertFalse($isSafe($result), "Should reveal secret details ($key): " . var_export($result, 1));

$result = Email::get(TRUE)
->addWhere('contact_id', '=', $contact['id'])
->addSelect('contact.api_key')
->addSelect('contact_id.api_key')
->execute()
->first();
$this->assertTrue($isSafe($result), "Should NOT reveal secret details ($key): " . var_export($result, 1));
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/api/v4/Action/ContactGetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function testGetWithLimit() {
$msg = $e->getMessage();
}
$this->assertRegExp(';Expected to find one Contact record;', $msg);
$limit1 = Contact::get(FALSE)->setLimit(1)->execute();
$limit1 = Contact::get(FALSE)->addWhere('last_name', '=', $last_name)->setLimit(1)->execute();
$this->assertCount(1, (array) $limit1);
$this->assertCount(1, $limit1);
$this->assertTrue(!empty($limit1->single()['sort_name']));
Expand Down
Loading

0 comments on commit c1c648b

Please sign in to comment.