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

APIv4 - Emit notices about deprecated join aliases #20716

Merged
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
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
3 changes: 3 additions & 0 deletions Civi/Api4/Service/Schema/Joiner.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public function autoJoin(Api4SelectQuery $query, array $joinPath, $side = 'LEFT'
if ($joinEntity && !$query->checkEntityAccess($joinEntity)) {
throw new UnauthorizedException('Cannot join to ' . $joinEntity);
}
if ($link->isDeprecated()) {
\CRM_Core_Error::deprecatedWarning("Deprecated join alias '$alias' used in APIv4 get. Should be changed to '{$alias}_id'");
}

$bao = $joinEntity ? CoreUtil::getBAOFromApiName($joinEntity) : NULL;
$conditions = $link->getConditionsForJoin($baseTableAlias);
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
14 changes: 2 additions & 12 deletions Civi/Test/Api3TestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public function runApi4Legacy($v3Entity, $v3Action, $v3Params = []) {
$v4Params['language'] = $language;
}
$toRemove = ['option.', 'return', 'api.', 'format.'];
$chains = $joins = $custom = [];
$chains = $custom = [];
foreach ($v3Params as $key => $val) {
foreach ($toRemove as $remove) {
if (strpos($key, $remove) === 0) {
Expand Down Expand Up @@ -403,7 +403,7 @@ public function runApi4Legacy($v3Entity, $v3Action, $v3Params = []) {
// This is a per field hack (bad) but we can't solve everything at once
// & a cleverer way turned out to be too much for this round.
// Being in the test class it's tested....
$v3Params['option_group.name'] = $v3Params['option_group_id'];
$v3Params['option_group_id.name'] = $v3Params['option_group_id'];
unset($v3Params['option_group_id']);
}
if (isset($field['pseudoconstant'], $v3Params[$name]) && $field['type'] === \CRM_Utils_Type::T_INT && !is_numeric($v3Params[$name]) && is_string($v3Params[$name])) {
Expand All @@ -426,12 +426,6 @@ public function runApi4Legacy($v3Entity, $v3Action, $v3Params = []) {
if ($v4Entity != 'Setting' && !in_array('id', $v4Params['select'])) {
$v4Params['select'][] = 'id';
}
// Convert join syntax
foreach ($v4Params['select'] as $idx => $select) {
if (strstr($select, '_id.')) {
$joins[$select] = $v4Params['select'][$idx] = str_replace('_id.', '.', $select);
}
}
}
if ($options['limit'] && $v4Entity != 'Setting') {
$v4Params['limit'] = $options['limit'];
Expand Down Expand Up @@ -598,10 +592,6 @@ public function runApi4Legacy($v3Entity, $v3Action, $v3Params = []) {
foreach ($chains as $key => $params) {
$result[$index][$key] = $this->runApi4LegacyChain($key, $params, $v4Entity, $row, $sequential);
}
// Convert join format
foreach ($joins as $api3Key => $api4Key) {
$result[$index][$api3Key] = $result[$index][$api4Key] ?? NULL;
}
// Resolve custom field names
foreach ($custom as $group => $fields) {
foreach ($fields as $field => $v3FieldName) {
Expand Down
2 changes: 1 addition & 1 deletion ext/afform/core/Civi/Afform/AfformMetadataInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function preprocess($e) {
->alterHtml(';\\.aff\\.html$;', function($doc, $path) {
try {
$module = \Civi::service('angular')->getModule(basename($path, '.aff.html'));
$meta = \Civi\Api4\Afform::get()->addWhere('name', '=', $module['_afform'])->setSelect(['join', 'block'])->setCheckPermissions(FALSE)->execute()->first();
$meta = \Civi\Api4\Afform::get(FALSE)->addWhere('name', '=', $module['_afform'])->setSelect(['join', 'block'])->execute()->first();
}
catch (\Exception $e) {
}
Expand Down
12 changes: 6 additions & 6 deletions ext/oauth-client/tests/phpunit/api/v4/OAuthContactTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public function testUpdate() {

$this->usePerms(['manage my OAuth contact tokens', 'view my contact']);
$updateTokensWithLimitedAccess = Civi\Api4\OAuthContactToken::update()
->addWhere('client.guid', '=', $client['guid'])
->addWhere('client_id.guid', '=', $client['guid'])
->setValues(['access_token' => 'own-token-revised'])
->execute();
$this->assertCount(1, $updateTokensWithLimitedAccess);
Expand Down Expand Up @@ -257,7 +257,7 @@ public function testUpdate() {

$this->usePerms(['manage my OAuth contact tokens', 'view my contact']);
$updateTokensForWrongContact = Civi\Api4\OAuthContactToken::update()
->addWhere('contact.id', '=', $notLoggedInContactID)
->addWhere('contact_id.id', '=', $notLoggedInContactID)
// ^ sneaky way to update a different contact?
->setValues(['access_token' => "stranger-token-revised"])
->execute();
Expand All @@ -271,7 +271,7 @@ public function testDelete() {

$this->usePerms(['manage my OAuth contact tokens', 'view all contacts']);
$deleteTokensWithLimitedAccess = Civi\Api4\OAuthContactToken::delete()
->setWhere([['client.guid', '=', $client['guid']]])
->setWhere([['client_id.guid', '=', $client['guid']]])
->execute();

$this->usePerms(['manage my OAuth contact tokens', 'view all contacts']);
Expand Down Expand Up @@ -309,22 +309,22 @@ public function testGetByScope() {

$this->usePerms(['manage all OAuth contact tokens', 'view all contacts']);
$getTokens = Civi\Api4\OAuthContactToken::get()
->addWhere('client.provider', '=', $client['provider'])
->addWhere('client_id.provider', '=', $client['provider'])
->addWhere('scopes', 'CONTAINS', 'foo')
->execute();
$this->assertCount(1, $getTokens);
$this->assertEquals($createToken->first()['id'], $getTokens->first()['id']);

$this->usePerms(['manage all OAuth contact tokens', 'view all contacts']);
$getTokens = Civi\Api4\OAuthContactToken::get()
->addWhere('client.provider', '=', $client['provider'])
->addWhere('client_id.provider', '=', $client['provider'])
->addWhere('scopes', 'CONTAINS', 'nada')
->execute();
$this->assertCount(0, $getTokens);

$this->usePerms(['manage all OAuth contact tokens', 'view all contacts']);
$getTokens = Civi\Api4\OAuthContactToken::get()
->addWhere('client.provider', '=', 'some-other-provider')
->addWhere('client_id.provider', '=', 'some-other-provider')
->addWhere('scopes', 'CONTAINS', 'foo')
->execute();
$this->assertCount(0, $getTokens);
Expand Down
Loading