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

Add APIv4 and pseudoconstants for RelationshipCache #17879

Merged
merged 1 commit into from
Jul 20, 2020
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
14 changes: 13 additions & 1 deletion CRM/Contact/DAO/RelationshipCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* Generated from xml/schema/CRM/Contact/RelationshipCache.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:6a586a5a5550a64db4c9907387c1411d)
* (GenCodeChecksum:d511533f30d5d2d8deac82664a6288d1)
*/

/**
Expand Down Expand Up @@ -207,6 +207,9 @@ public static function &fields() {
'entity' => 'RelationshipCache',
'bao' => 'CRM_Contact_BAO_RelationshipCache',
'localizable' => 0,
'pseudoconstant' => [
'callback' => 'CRM_Core_SelectValues::relationshipOrientation',
],
'add' => '5.29',
],
'near_contact_id' => [
Expand All @@ -221,6 +224,9 @@ public static function &fields() {
'bao' => 'CRM_Contact_BAO_RelationshipCache',
'localizable' => 0,
'FKClassName' => 'CRM_Contact_DAO_Contact',
'html' => [
'type' => 'EntityRef',
],
'add' => '5.29',
],
'near_relation' => [
Expand All @@ -235,6 +241,9 @@ public static function &fields() {
'entity' => 'RelationshipCache',
'bao' => 'CRM_Contact_BAO_RelationshipCache',
'localizable' => 0,
'pseudoconstant' => [
'callback' => 'CRM_Core_PseudoConstant::relationshipTypeOptions',
],
'add' => '5.29',
],
'far_contact_id' => [
Expand Down Expand Up @@ -266,6 +275,9 @@ public static function &fields() {
'entity' => 'RelationshipCache',
'bao' => 'CRM_Contact_BAO_RelationshipCache',
'localizable' => 0,
'pseudoconstant' => [
'callback' => 'CRM_Core_PseudoConstant::relationshipTypeOptions',
],
'add' => '5.29',
],
'is_active' => [
Expand Down
17 changes: 17 additions & 0 deletions CRM/Core/PseudoConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,23 @@ public static function relationshipType($valueColumnName = 'label', $reset = FAL
return self::$relationshipType[$cacheKey];
}

/**
* Name => Label pairs for all relationship types
*
* @return array
*/
public static function relationshipTypeOptions() {
$relationshipTypes = [];
$relationshipLabels = self::relationshipType();
foreach (self::relationshipType('name') as $id => $type) {
$relationshipTypes[$type['name_a_b']] = $relationshipLabels[$id]['label_a_b'];
if ($type['name_b_a'] && $type['name_b_a'] != $type['name_a_b']) {
$relationshipTypes[$type['name_b_a']] = $relationshipLabels[$id]['label_b_a'];
}
}
return $relationshipTypes;
}

/**
* Get all the ISO 4217 currency codes
*
Expand Down
10 changes: 10 additions & 0 deletions CRM/Core/SelectValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -1196,4 +1196,14 @@ public static function navigationMenuSeparator() {
];
}

/**
* @return array
*/
public static function relationshipOrientation() {
return [
'a_b' => ts('A to B'),
'b_a' => ts('B to A'),
];
}

}
49 changes: 49 additions & 0 deletions Civi/Api4/RelationshipCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/


namespace Civi\Api4;

/**
* RelationshipCache - readonly table to facilitate joining and finding contacts by relationship.
*
* @see \Civi\Api4\Relationship
*
* @package Civi\Api4
*/
class RelationshipCache extends Generic\AbstractEntity {

/**
* @param bool $checkPermissions
* @return Generic\DAOGetAction
*/
public static function get($checkPermissions = TRUE) {
return (new Generic\DAOGetAction(static::class, __FUNCTION__))
->setCheckPermissions($checkPermissions);
}

/**
* @param bool $checkPermissions
* @return Generic\DAOGetFieldsAction
*/
public static function getFields($checkPermissions = TRUE) {
return (new Generic\DAOGetFieldsAction(static::class, __FUNCTION__))
->setCheckPermissions($checkPermissions);
}

}
50 changes: 50 additions & 0 deletions tests/phpunit/api/v4/Entity/RelationshipTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/

namespace api\v4\Entity;

use Civi\Api4\Contact;
use api\v4\UnitTestCase;
use Civi\Api4\Relationship;
use Civi\Api4\RelationshipCache;
use Civi\Test\TransactionalInterface;

/**
* Assert that interchanging data between APIv3 and APIv4 yields consistent
* encodings.
*
* @group headless
*/
class RelationshipTest extends UnitTestCase implements TransactionalInterface {

public function testRelCache() {
$c1 = Contact::create(FALSE)->addValue('first_name', '1')->execute()->first()['id'];
$c2 = Contact::create(FALSE)->addValue('first_name', '2')->execute()->first()['id'];
Relationship::create(FALSE)
->setValues([
'contact_id_a' => $c1,
'contact_id_b' => $c2,
'relationship_type_id' => 1,
])->execute();
$cacheRecords = RelationshipCache::get(FALSE)
->addClause('OR', ['near_contact_id', '=', $c1], ['far_contact_id', '=', $c1])
->execute();
$this->assertCount(2, $cacheRecords);
}

}
13 changes: 12 additions & 1 deletion xml/schema/Contact/RelationshipCache.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@
<length>3</length>
<title>Orientation (a_b or b_a)</title>
<required>true</required>
<default></default>
<pseudoconstant>
<callback>CRM_Core_SelectValues::relationshipOrientation</callback>
</pseudoconstant>
<comment>The cache record is a permutation of the original relationship record. The orientation indicates whether it is forward (a_b) or reverse (b_a) relationship.</comment>
<add>5.29</add>
</field>
Expand All @@ -72,6 +74,9 @@
<required>true</required>
<comment>id of the first contact</comment>
<add>5.29</add>
<html>
<type>EntityRef</type>
</html>
</field>
<foreignKey>
<name>near_contact_id</name>
Expand All @@ -88,6 +93,9 @@
<length>64</length>
<comment>name for relationship of near_contact to far_contact.</comment>
<add>5.29</add>
<pseudoconstant>
<callback>CRM_Core_PseudoConstant::relationshipTypeOptions</callback>
</pseudoconstant>
</field>

<field>
Expand Down Expand Up @@ -116,6 +124,9 @@
<length>64</length>
<comment>name for relationship of far_contact to near_contact.</comment>
<add>5.29</add>
<pseudoconstant>
<callback>CRM_Core_PseudoConstant::relationshipTypeOptions</callback>
</pseudoconstant>
</field>

<index>
Expand Down