Skip to content

Commit

Permalink
Improve APIv4 metadata for RelationshipCache and Bridge entities in g…
Browse files Browse the repository at this point in the history
…eneral

These are the core changes needed toward geting joins supported by Search Kit
  • Loading branch information
colemanw committed Dec 3, 2020
1 parent ddaa3ff commit c5368b3
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 37 deletions.
8 changes: 4 additions & 4 deletions 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:3bee16c8388728d3e391e9dd7c17abb8)
* (GenCodeChecksum:ba039fcadc13e48749f965343301ec1d)
*/

/**
Expand Down Expand Up @@ -129,7 +129,7 @@ public function __construct() {
* Whether to return the plural version of the title.
*/
public static function getEntityTitle($plural = FALSE) {
return $plural ? ts('Relationship Caches') : ts('Relationship Cache');
return $plural ? ts('Related Contacts') : ts('Related Contact');
}

/**
Expand Down Expand Up @@ -237,7 +237,7 @@ public static function &fields() {
'near_relation' => [
'name' => 'near_relation',
'type' => CRM_Utils_Type::T_STRING,
'title' => ts('Relationship Name (Near side)'),
'title' => ts('Relationship Name (to related contact)'),
'description' => ts('name for relationship of near_contact to far_contact.'),
'maxlength' => 64,
'size' => CRM_Utils_Type::BIG,
Expand Down Expand Up @@ -271,7 +271,7 @@ public static function &fields() {
'far_relation' => [
'name' => 'far_relation',
'type' => CRM_Utils_Type::T_STRING,
'title' => ts('Relationship Name (Near side)'),
'title' => ts('Relationship Name (from related contact)'),
'description' => ts('name for relationship of far_contact to near_contact.'),
'maxlength' => 64,
'size' => CRM_Utils_Type::BIG,
Expand Down
3 changes: 1 addition & 2 deletions CRM/Core/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,8 +530,7 @@ public function sequenceKey() {
* Returns list of FK relationships.
*
*
* @return array
* Array of CRM_Core_Reference_Interface
* @return CRM_Core_Reference_Basic[]
*/
public static function getReferenceColumns() {
return [];
Expand Down
5 changes: 5 additions & 0 deletions Civi/Api4/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ public static function getFields($checkPermissions = TRUE) {
'data_type' => 'Array',
'description' => 'Any @see annotations from docblock',
],
[
'name' => 'bridge',
'data_type' => 'Array',
'description' => 'Connecting fields for EntityBridge types',
],
];
}))->setCheckPermissions($checkPermissions);
}
Expand Down
11 changes: 10 additions & 1 deletion Civi/Api4/Generic/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ protected static function getEntityName() {
* @return string
*/
protected static function getEntityTitle($plural = FALSE) {
return static::getEntityName();
$name = static::getEntityName();
$dao = \CRM_Core_DAO_AllCoreTables::getFullName($name);
return $dao ? $dao::getEntityTitle($plural) : ($plural ? \CRM_Utils_String::pluralize($name) : $name);
}

/**
Expand Down Expand Up @@ -136,6 +138,13 @@ public static function getInfo() {
'type' => [self::stripNamespace(get_parent_class(static::class))],
'paths' => static::getEntityPaths(),
];
// Add info for entities with a corresponding DAO
$dao = \CRM_Core_DAO_AllCoreTables::getFullName($info['name']);
if ($dao) {
$info['paths'] = $dao::getEntityPaths();
$info['icon'] = $dao::$_icon;
$info['dao'] = $dao;
}
foreach (ReflectionUtils::getTraits(static::class) as $trait) {
$info['type'][] = self::stripNamespace($trait);
}
Expand Down
25 changes: 0 additions & 25 deletions Civi/Api4/Generic/DAOEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,29 +90,4 @@ public static function replace($checkPermissions = TRUE) {
->setCheckPermissions($checkPermissions);
}

/**
* @param bool $plural
* Whether to return a plural title.
* @return string
*/
protected static function getEntityTitle($plural = FALSE) {
$name = static::getEntityName();
$dao = \CRM_Core_DAO_AllCoreTables::getFullName($name);
return $dao ? $dao::getEntityTitle($plural) : $name;
}

/**
* @return array
*/
public static function getInfo() {
$info = parent::getInfo();
$dao = \CRM_Core_DAO_AllCoreTables::getFullName($info['name']);
if ($dao) {
$info['paths'] = $dao::getEntityPaths();
$info['icon'] = $dao::$_icon;
$info['dao'] = $dao;
}
return $info;
}

}
20 changes: 20 additions & 0 deletions Civi/Api4/Generic/Traits/EntityBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,24 @@
*/
trait EntityBridge {

/**
* Adds "bridge" info, which should specify an array of two field names from this entity
*
* This automatic function can be overridden by annotating the APIv4 entity like
* `@bridge contact_id group_id`
*
* @return array
*/
public static function getInfo() {
$info = parent::getInfo();
if (!empty($info['dao']) && empty($info['bridge'])) {
foreach (($info['dao'])::fields() as $field) {
if (!empty($field['FKClassName']) || $field['name'] === 'entity_id') {
$info['bridge'][] = $field['name'];
}
}
}
return $info;
}

}
3 changes: 2 additions & 1 deletion Civi/Api4/GroupContact.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
* A contact can either be "Added" "Removed" or "Pending" in a group.
* CiviCRM only considers them to be "in" a group if their status is "Added".
*
* @bridge group_id contact_id
* @see \Civi\Api4\Group
*
* @searchable false
* @package Civi\Api4
*/
class GroupContact extends Generic\DAOEntity {
Expand Down
2 changes: 1 addition & 1 deletion Civi/Api4/RelationshipCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* RelationshipCache - readonly table to facilitate joining and finding contacts by relationship.
*
* @see \Civi\Api4\Relationship
*
* @bridge near_contact_id far_contact_id
* @package Civi\Api4
*/
class RelationshipCache extends Generic\AbstractEntity {
Expand Down
3 changes: 3 additions & 0 deletions Civi/Api4/Utils/ReflectionUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ public static function parseDocBlock($comment) {
elseif ($key == 'searchable') {
$info[$key] = strtolower($words[0]) !== 'false';
}
elseif ($key == 'bridge') {
$info[$key] = $words;
}
elseif ($key == 'param' && $words) {
$type = $words[0][0] !== '$' ? explode('|', array_shift($words)) : NULL;
$param = rtrim(array_shift($words), '-:()/');
Expand Down
6 changes: 3 additions & 3 deletions xml/schema/Contact/RelationshipCache.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<add>5.29</add>
<log>false</log>
<icon>fa-handshake-o</icon>

<title>Related Contact</title>
<field>
<name>id</name>
<type>int unsigned</type>
Expand Down Expand Up @@ -89,7 +89,7 @@
<field>
<name>near_relation</name>
<type>varchar</type>
<title>Relationship Name (Near side)</title>
<title>Relationship Name (to related contact)</title>
<length>64</length>
<comment>name for relationship of near_contact to far_contact.</comment>
<add>5.29</add>
Expand Down Expand Up @@ -120,7 +120,7 @@
<field>
<name>far_relation</name>
<type>varchar</type>
<title>Relationship Name (Near side)</title>
<title>Relationship Name (from related contact)</title>
<length>64</length>
<comment>name for relationship of far_contact to near_contact.</comment>
<add>5.29</add>
Expand Down

0 comments on commit c5368b3

Please sign in to comment.