Skip to content

Commit

Permalink
Merge pull request #19010 from colemanw/bridges
Browse files Browse the repository at this point in the history
Use trait instead of class for Entity Bridges; add OptionList trait
  • Loading branch information
eileenmcnaughton authored Nov 22, 2020
2 parents f72d958 + 465bc32 commit ad07104
Show file tree
Hide file tree
Showing 23 changed files with 71 additions and 18 deletions.
3 changes: 2 additions & 1 deletion Civi/Api4/ActivityContact.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* @see \Civi\Api4\Activity
* @package Civi\Api4
*/
class ActivityContact extends Generic\BridgeEntity {
class ActivityContact extends Generic\DAOEntity {
use Generic\Traits\EntityBridge;

}
1 change: 1 addition & 0 deletions Civi/Api4/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
* @package Civi\Api4
*/
class Country extends Generic\DAOEntity {
use Generic\Traits\OptionList;

}
3 changes: 2 additions & 1 deletion Civi/Api4/DashboardContact.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* @see \Civi\Api4\Dashboard
* @package Civi\Api4
*/
class DashboardContact extends Generic\BridgeEntity {
class DashboardContact extends Generic\DAOEntity {
use Generic\Traits\EntityBridge;

}
9 changes: 8 additions & 1 deletion Civi/Api4/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,15 @@ public static function getFields($checkPermissions = TRUE) {
],
[
'name' => 'type',
'data_type' => 'Array',
'description' => 'Base class for this entity',
'options' => ['DAOEntity' => 'DAOEntity', 'BasicEntity' => 'BasicEntity', 'BridgeEntity' => 'BridgeEntity', 'AbstractEntity' => 'AbstractEntity'],
'options' => [
'AbstractEntity' => 'AbstractEntity',
'DAOEntity' => 'DAOEntity',
'BasicEntity' => 'BasicEntity',
'EntityBridge' => 'EntityBridge',
'OptionList' => 'OptionList',
],
],
[
'name' => 'description',
Expand Down
3 changes: 2 additions & 1 deletion Civi/Api4/EntityTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*
* @package Civi\Api4
*/
class EntityTag extends Generic\BridgeEntity {
class EntityTag extends Generic\DAOEntity {
use Generic\Traits\EntityBridge;

}
5 changes: 4 additions & 1 deletion Civi/Api4/Generic/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,12 @@ public static function getInfo() {
'name' => static::getEntityName(),
'title' => static::getEntityTitle(),
'title_plural' => static::getEntityTitle(TRUE),
'type' => self::stripNamespace(get_parent_class(static::class)),
'type' => [self::stripNamespace(get_parent_class(static::class))],
'paths' => static::getEntityPaths(),
];
foreach (ReflectionUtils::getTraits(static::class) as $trait) {
$info['type'][] = self::stripNamespace($trait);
}
$reflection = new \ReflectionClass(static::class);
$info += ReflectionUtils::getCodeDocs($reflection, NULL, ['entity' => $info['name']]);
unset($info['package'], $info['method']);
Expand Down
4 changes: 2 additions & 2 deletions Civi/Api4/Generic/DAOGetAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ class DAOGetAction extends AbstractGetAction {
*
* - `Entity`: the name of the api entity to join onto.
* - `Required`: `TRUE` for an `INNER JOIN`, `FALSE` for a `LEFT JOIN`.
* - `Bridge` (optional): Name of a BridgeEntity to incorporate into the join.
* - `Bridge` (optional): Name of a Bridge to incorporate into the join.
* - `[field, op, value]...`: zero or more conditions for the ON clause, using the same nested format as WHERE and HAVING
* but with the difference that "value" is interpreted as an expression (e.g. can be the name of a field).
* Enclose literal values with quotes.
*
* @var array
* @see \Civi\Api4\Generic\BridgeEntity
* @see \Civi\Api4\Generic\Traits\EntityBridge
*/
protected $join = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
+--------------------------------------------------------------------+
*/

namespace Civi\Api4\Generic;
namespace Civi\Api4\Generic\Traits;

/**
* A bridge is a small table that provides an intermediary link between two other tables.
*
* The API can automatically incorporate a bridgeEntity into a join expression.
* The API can automatically incorporate a Bridge into a join expression.
*/
class BridgeEntity extends DAOEntity {
trait EntityBridge {

}
21 changes: 21 additions & 0 deletions Civi/Api4/Generic/Traits/OptionList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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 |
+--------------------------------------------------------------------+
*/

namespace Civi\Api4\Generic\Traits;

/**
* An optionList is a small entity whose primary purpose is to supply a semi-static list of options to fields in other entities.
*
* The options appear in the field metadata for other entities that reference this one via pseudoconstant.
*/
trait OptionList {

}
3 changes: 2 additions & 1 deletion Civi/Api4/GroupContact.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
*
* @package Civi\Api4
*/
class GroupContact extends Generic\BridgeEntity {
class GroupContact extends Generic\DAOEntity {
use Generic\Traits\EntityBridge;

/**
* @param bool $checkPermissions
Expand Down
1 change: 1 addition & 0 deletions Civi/Api4/LocationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
* @package Civi\Api4
*/
class LocationType extends Generic\DAOEntity {
use Generic\Traits\OptionList;

}
1 change: 1 addition & 0 deletions Civi/Api4/MembershipType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
* @package Civi\Api4
*/
class MembershipType extends Generic\DAOEntity {
use Generic\Traits\OptionList;

}
1 change: 1 addition & 0 deletions Civi/Api4/OptionValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@
* @package Civi\Api4
*/
class OptionValue extends Generic\DAOEntity {
use Generic\Traits\OptionList;

}
1 change: 1 addition & 0 deletions Civi/Api4/PaymentProcessorType.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
* @package Civi\Api4
*/
class PaymentProcessorType extends Generic\DAOEntity {
use Generic\Traits\OptionList;

}
8 changes: 5 additions & 3 deletions Civi/Api4/Query/Api4SelectQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ private function getJoinConditions($joinTree, $joinEntity, $alias) {
}

/**
* Join onto a BridgeEntity table
* Join onto a Bridge table
*
* @param array $joinTree
* @param string $joinEntity
Expand All @@ -604,13 +604,15 @@ private function getJoinConditions($joinTree, $joinEntity, $alias) {
*/
protected function getBridgeJoin(&$joinTree, $joinEntity, $alias) {
$bridgeEntity = array_shift($joinTree);
if (!is_a('\Civi\Api4\\' . $bridgeEntity, '\Civi\Api4\Generic\BridgeEntity', TRUE)) {
/* @var \Civi\Api4\Generic\DAOEntity $bridgeEntityClass */
$bridgeEntityClass = '\Civi\Api4\\' . $bridgeEntity;
if (!in_array('EntityBridge', $bridgeEntityClass::getInfo()['type'], TRUE)) {
throw new \API_Exception("Illegal bridge entity specified: " . $bridgeEntity);
}
$bridgeAlias = $alias . '_via_' . strtolower($bridgeEntity);
$bridgeTable = CoreUtil::getTableName($bridgeEntity);
$joinTable = CoreUtil::getTableName($joinEntity);
$bridgeEntityGet = \Civi\API\Request::create($bridgeEntity, 'get', ['version' => 4, 'checkPermissions' => $this->getCheckPermissions()]);
$bridgeEntityGet = $bridgeEntityClass::get($this->getCheckPermissions());
$fkToJoinField = $fkToBaseField = NULL;
// Find the bridge field that links to the joinEntity (either an explicit FK or an entity_id/entity_table combo)
foreach ($bridgeEntityGet->entityFields() as $name => $field) {
Expand Down
1 change: 1 addition & 0 deletions Civi/Api4/RelationshipCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* @package Civi\Api4
*/
class RelationshipCache extends Generic\AbstractEntity {
use Generic\Traits\EntityBridge;

/**
* @param bool $checkPermissions
Expand Down
1 change: 1 addition & 0 deletions Civi/Api4/RelationshipType.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@
* @package Civi\Api4
*/
class RelationshipType extends Generic\DAOEntity {
use Generic\Traits\OptionList;

}
1 change: 1 addition & 0 deletions Civi/Api4/StateProvince.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
* @package Civi\Api4
*/
class StateProvince extends Generic\DAOEntity {
use Generic\Traits\OptionList;

}
1 change: 1 addition & 0 deletions Civi/Api4/UFJoin.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@
* @package Civi\Api4
*/
class UFJoin extends Generic\DAOEntity {
use Generic\Traits\EntityBridge;

}
3 changes: 2 additions & 1 deletion Civi/Api4/UFMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*
* @package Civi\Api4
*/
class UFMatch extends Generic\BridgeEntity {
class UFMatch extends Generic\DAOEntity {
use Generic\Traits\EntityBridge;

}
3 changes: 2 additions & 1 deletion ang/api4Explorer/Explorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
$scope.controls = {};
$scope.langs = ['php', 'js', 'ang', 'cli'];
$scope.joinTypes = [{k: false, v: 'FALSE (LEFT JOIN)'}, {k: true, v: 'TRUE (INNER JOIN)'}];
$scope.bridgeEntities = _.filter(schema, {type: 'BridgeEntity'});
$scope.bridgeEntities = _.filter(schema, function(entity) {return _.includes(entity.type, 'EntityBridge');});
$scope.code = {
php: [
{name: 'oop', label: ts('OOP Style'), code: ''},
Expand Down Expand Up @@ -873,6 +873,7 @@
setHelp($scope.entity, {
description: entityInfo.description,
comment: entityInfo.comment,
type: entityInfo.type,
see: entityInfo.see
});
}
Expand Down
2 changes: 1 addition & 1 deletion ext/search/Civi/Search/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static function getOperators():array {
public static function getSchema() {
$schema = [];
$entities = \Civi\Api4\Entity::get()
->addSelect('name', 'title', 'title_plural', 'description', 'icon', 'paths')
->addSelect('name', 'title', 'type', 'title_plural', 'description', 'icon', 'paths')
->addWhere('name', '!=', 'Entity')
->addOrderBy('title_plural')
->setChain([
Expand Down
7 changes: 6 additions & 1 deletion ext/search/ang/crmSearchAdmin/crmSearchAdmin.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@
$scope.controls = {tab: 'compose'};
$scope.joinTypes = [{k: false, v: ts('Optional')}, {k: true, v: ts('Required')}];
$scope.groupOptions = CRM.crmSearchActions.groupOptions;
$scope.entities = formatForSelect2(CRM.vars.search.schema, 'name', 'title_plural', ['description', 'icon']);
// Try to create a sensible list of entities one might want to search for,
// excluding those whos primary purpose is to provide joins or option lists to other entities
var primaryEntities = _.filter(CRM.vars.search.schema, function(entity) {
return !_.includes(entity.type, 'EntityBridge') && !_.includes(entity.type, 'OptionList');
});
$scope.entities = formatForSelect2(primaryEntities, 'name', 'title_plural', ['description', 'icon']);
this.perm = {
editGroups: CRM.checkPerm('edit groups')
};
Expand Down

0 comments on commit ad07104

Please sign in to comment.