Added extension code

This commit is contained in:
civiwaresolution
2019-09-16 00:01:08 +01:00
committed by Pradeep Nayak
commit 5596e9b90b
17 changed files with 2435 additions and 0 deletions

View File

@ -0,0 +1,8 @@
<?php
/**
* CRM_RelateEntities_BAO_RelateEntities constructor.
*/
class CRM_RelateEntities_BAO_RelateEntities extends CRM_RelateEntities_DAO_RelateEntities {
}

View File

@ -0,0 +1,275 @@
<?php
/**
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2019
*
* Generated from xml/schema/CRM/Contact/RelateEntities.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:ebfcea88ae4bd09a0821a942ecdfa4db)
*/
/**
* Database access object for the RelateEntities entity.
*/
class CRM_RelateEntities_DAO_RelateEntities extends CRM_Core_DAO {
/**
* Static instance to hold the table name.
*
* @var string
*/
public static $_tableName = 'civicrm_relate_entities';
/**
* Should CiviCRM log any modifications to this table in the civicrm_log table.
*
* @var bool
*/
public static $_log = TRUE;
/**
* RelateEntities ID
*
* @var int
*/
public $id;
public $entity_table_a;
public $entity_table_b;
public $entity_id_a;
public $entity_id_b;
public $relationship_type_id;
public $created_date;
public $modified_date;
public $is_active;
/**
* Class constructor.
*/
public function __construct() {
$this->__table = 'civicrm_relate_entities';
parent::__construct();
}
/**
* Returns foreign keys and entity references.
*
* @return array
* [CRM_Core_Reference_Interface]
*/
public static function getReferenceColumns() {
if (!isset(Civi::$statics[__CLASS__]['links'])) {
Civi::$statics[__CLASS__]['links'] = static::createReferenceColumns(__CLASS__);
Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), 'relationship_type_id', 'civicrm_relationship_type', 'id');
CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'links_callback', Civi::$statics[__CLASS__]['links']);
}
return Civi::$statics[__CLASS__]['links'];
}
/**
* Returns all the column names of this table
*
* @return array
*/
public static function &fields() {
if (!isset(Civi::$statics[__CLASS__]['fields'])) {
Civi::$statics[__CLASS__]['fields'] = [
'id' => [
'name' => 'id',
'type' => CRM_Utils_Type::T_INT,
'title' => ts('RelateEntities ID'),
'description' => ts('RelateEntities ID'),
'required' => TRUE,
'where' => 'civicrm_relate_entities.id',
'table_name' => 'civicrm_relate_entities',
'entity' => 'RelateEntities',
'bao' => 'CRM_RelateEntities_BAO_RelateEntities',
'localizable' => 0,
],
'entity_table_a' => [
'name' => 'entity_table_a',
'type' => CRM_Utils_Type::T_STRING,
'title' => ts('Entity Table A'),
'description' => ts('May contain civicrm_financial_item, civicrm_contribution, civicrm_financial_trxn, civicrm_grant, etc'),
'required' => TRUE,
'maxlength' => 64,
'size' => CRM_Utils_Type::BIG,
'import' => TRUE,
'export' => TRUE,
'table_name' => 'civicrm_relate_entities',
'entity' => 'RelateEntities',
'bao' => 'CRM_RelateEntities_DAO_RelateEntities',
'localizable' => 0,
],
'entity_table_b' => [
'name' => 'entity_table_b',
'type' => CRM_Utils_Type::T_STRING,
'title' => ts('Entity Table B'),
'description' => ts('May contain civicrm_financial_item, civicrm_contribution, civicrm_financial_trxn, civicrm_grant, etc'),
'required' => TRUE,
'maxlength' => 64,
'size' => CRM_Utils_Type::BIG,
'import' => TRUE,
'export' => TRUE,
'table_name' => 'civicrm_relate_entities',
'entity' => 'RelateEntities',
'bao' => 'CRM_RelateEntities_DAO_RelateEntities',
'localizable' => 0,
],
'entity_id_a' => [
'name' => 'entity_id_a',
'type' => CRM_Utils_Type::T_INT,
'title' => ts('Entity Id A'),
'description' => '',
'required' => TRUE,
'table_name' => 'civicrm_relate_entities',
'entity' => 'RelateEntities',
'bao' => 'CRM_RelateEntities_BAO_RelateEntities',
'localizable' => 0,
],
'entity_id_b' => [
'name' => 'entity_id_b',
'type' => CRM_Utils_Type::T_INT,
'title' => ts('Entity Id B'),
'description' => '',
'required' => TRUE,
'table_name' => 'civicrm_relate_entities',
'entity' => 'RelateEntities',
'bao' => 'CRM_RelateEntities_BAO_RelateEntities',
'localizable' => 0,
],
'relationship_type_id' => [
'name' => 'relationship_type_id',
'type' => CRM_Utils_Type::T_INT,
'title' => ts('Relationship Type'),
'description' => ts('id of the relationship'),
'required' => TRUE,
'table_name' => 'civicrm_relate_entities',
'entity' => 'RelateEntities',
'bao' => 'CRM_RelateEntities_BAO_RelateEntities',
'localizable' => 0,
'FKClassName' => 'CRM_Relationship_DAO_RelationshipType',
'html' => [
'type' => 'Select',
],
],
'created_date' => [
'name' => 'created_date',
'type' => CRM_Utils_Type::T_DATE,
'title' => ts('RelateEntities Created Date'),
'description' => ts('date when the relationship created'),
'table_name' => 'civicrm_relate_entities',
'entity' => 'RelateEntities',
'bao' => 'CRM_RelateEntities_BAO_RelateEntities',
'localizable' => 0,
'html' => [
'type' => 'Select Date',
'formatType' => 'activityDate',
],
],
'modified_date' => [
'name' => 'modified_date',
'type' => CRM_Utils_Type::T_DATE,
'title' => ts('RelateEntities Modified Date'),
'description' => ts('date when the relationship modified'),
'table_name' => 'civicrm_relate_entities',
'entity' => 'RelateEntities',
'bao' => 'CRM_RelateEntities_BAO_RelateEntities',
'localizable' => 0,
'html' => [
'type' => 'Select Date',
'formatType' => 'activityDate',
],
],
'is_active' => [
'name' => 'is_active',
'type' => CRM_Utils_Type::T_BOOLEAN,
'title' => ts('Is Active?'),
'description' => ts('is active ?'),
'default' => '1',
'table_name' => 'civicrm_relate_entities',
'entity' => 'RelateEntities',
'bao' => 'CRM_RelateEntities_BAO_RelateEntities',
'localizable' => 0,
'html' => [
'type' => 'Radio',
],
'options' => [
'1' => ts('Yes'),
'0' => ts('No'),
],
],
];
CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']);
}
return Civi::$statics[__CLASS__]['fields'];
}
/**
* Return a mapping from field-name to the corresponding key (as used in fields()).
*
* @return array
* Array(string $name => string $uniqueName).
*/
public static function &fieldKeys() {
if (!isset(Civi::$statics[__CLASS__]['fieldKeys'])) {
Civi::$statics[__CLASS__]['fieldKeys'] = array_flip(CRM_Utils_Array::collect('name', self::fields()));
}
return Civi::$statics[__CLASS__]['fieldKeys'];
}
/**
* Returns the names of this table
*
* @return string
*/
public static function getTableName() {
return self::$_tableName;
}
/**
* Returns if this table needs to be logged
*
* @return bool
*/
public function getLog() {
return self::$_log;
}
/**
* Returns the list of fields that can be imported
*
* @param bool $prefix
*
* @return array
*/
public static function &import($prefix = FALSE) {
$r = CRM_Core_DAO_AllCoreTables::getImports(__CLASS__, 'relate_entities', $prefix, []);
return $r;
}
/**
* Returns the list of fields that can be exported
*
* @param bool $prefix
*
* @return array
*/
public static function &export($prefix = FALSE) {
$r = CRM_Core_DAO_AllCoreTables::getExports(__CLASS__, 'relate_entities', $prefix, []);
return $r;
}
/**
* Returns the list of indices
*
* @param bool $localize
*
* @return array
*/
public static function indices($localize = TRUE) {
$indices = [];
return ($localize && !empty($indices)) ? CRM_Core_DAO_AllCoreTables::multilingualize(__CLASS__, $indices) : $indices;
}
}

View File

@ -0,0 +1,283 @@
<?php
/**
* This class generates form components for Bank Account
*/
class CRM_RelateEntities_Form_RelatedEntity extends CRM_Core_Form {
use CRM_Core_Form_EntityFormTrait;
public $_id;
/**
* Set variables up before form is built.
*/
public function preProcess() {
parent::preProcess();
$this->entity_table = CRM_Utils_Request::retrieve('entityTable', 'String', $this, TRUE);
$this->_id = CRM_Utils_Request::retrieve('id', 'String', $this);
if (!$this->_id) {
$this->entity_id = CRM_Utils_Request::retrieve('entityId', 'Positive', $this, TRUE);
}
$this->entityA = ($this->entity_table == 'Contact') ? 'Contact' : 'FinancialType';
$this->entityB = ($this->entity_table == 'FinancialType') ? 'Contact' : 'FinancialType';
}
/**
* Set entity fields to be assigned to the form.
*/
protected function setEntityFields() {
$this->entityFields = [
'is_active' => [
'name' => 'is_active',
'required' => TRUE,
],
];
return $this->entityFields;
}
/**
* Classes extending CRM_Core_Form should implement this method.
*
*/
public function getDefaultContext() {
return 'create';
}
/**
* Classes extending CRM_Core_Form should implement this method.
*/
public function getDefaultEntity() {
return 'RelateEntities';
}
/**
* Set the delete message.
*
* We do this from the constructor in order to do a translation.
*/
public function setDeleteMessage() {
$this->deleteMessage = ts('Deleting a related entity cannot be undone.') . ts('Do you want to continue?');
}
/**
* Add defined entity field to template.
*/
protected function addEntityFieldsToTemplates() {
foreach ($this->setEntityFields() as $fieldSpec) {
if (empty($fieldSpec['not-auto-addable'])) {
$element = $this->addField($fieldSpec['name'], CRM_Utils_Array::value('props', $fieldSpec, []), CRM_Utils_Array::value('required', $fieldSpec));
if (!empty($fieldSpec['is_freeze'])) {
$element->freeze();
}
}
}
}
/**
* Build the form object.
*/
public function buildQuickForm() {
self::buildQuickEntityForm();
if ($this->_action & CRM_Core_Action::DELETE) {
$this->addButtons([
[
'type' => 'next',
'name' => ts('Delete'),
'isDefault' => TRUE,
],
[
'type' => 'cancel',
'name' => ts('Cancel'),
],
]);
return;
}
$this->addEntityFieldsToTemplates();
$this->addEntityRef(
'entity_a',
ts('Entity A'),
[
'entity' => $this->entityA,
],
TRUE
)->freeze();
$this->addEntityRef(
'entity_b',
ts('Entity B'),
[
'entity' => $this->entityB,
'select' => ['minimumInputLength' => 0],
],
TRUE
);
$this->add(
'select',
'relationship_type_id',
ts('Relationship Type'),
CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE),
FALSE,
['class' => 'crm-select2', 'placeholder' => ts('Select Relationship Types')]
);
$fields = [
'entity_a' => [
'name' => 'entity_a',
],
'relationship_type_id' => [
'name' => 'relationship_type_id',
],
'entity_b' => [
'name' => 'entity_b',
]
] + $this->entityFields;
$this->assign('entityFields', $fields);
$this->addButtons([
[
'type' => 'next',
'name' => ts('Save'),
'isDefault' => TRUE,
],
[
'type' => 'next',
'name' => ts('Save and New'),
'subName' => 'new',
],
[
'type' => 'cancel',
'name' => ts('Cancel'),
],
]);
$this->addFormRule(array('CRM_RelateEntities_Form_RelatedEntity', 'formRule'), $this);
}
/**
* Global form rule.
*
* @param array $fields
* The input form values.
* @param array $files
* The uploaded files if any.
* @param $self
*
* @return bool|array
* true if no errors, else array of errors
*/
public static function formRule($fields, $files, $self) {
$error = [];
return $error;
}
/**
* Set the default values for the form.
*/
public function setDefaultValues() {
$defaults = [];
if (!$this->_id) {
$defaults['is_active'] = 1;
$defaults['entity_a'] = $this->entity_id;
}
else {
$defaults = civicrm_api3('RelateEntities', 'getsingle', [
'id' => $this->_id,
]);
if ($defaults['entity_table_b'] == CRM_RelateEntities_Utils::getEntityTable($this->entityB)) {
$defaults['entity_b'] = $defaults['entity_id_b'];
$defaults['relationship_type_id'] .= '_a_b';
}
else if ($defaults['entity_table_a'] == CRM_RelateEntities_Utils::getEntityTable($this->entityB)) {
$defaults['entity_b'] = $defaults['entity_id_a'];
$defaults['relationship_type_id'] .= '_b_a';
}
if ($defaults['entity_table_b'] == CRM_RelateEntities_Utils::getEntityTable($this->entityA)) {
$defaults['entity_a'] = $defaults['entity_id_b'];
}
else if ($defaults['entity_table_a'] == CRM_RelateEntities_Utils::getEntityTable($this->entityA)) {
$defaults['entity_a'] = $defaults['entity_id_a'];
}
}
return $defaults;
}
/**
* Process the form submission.
*/
public function postProcess() {
if ($this->_action & CRM_Core_Action::DELETE) {
$result = civicrm_api3('RelateEntities', 'delete', [
'id' => $this->_id,
]);
if (!empty($result['is_error'])) {
CRM_Core_Error::statusBounce($result['error_message'], CRM_Utils_System::url('civicrm/', "reset=1&action=browse"), ts('Cannot Delete'));
}
CRM_Core_Session::setStatus(ts('Selected Bank Account has been deleted.'), ts('Record Deleted'), 'success');
}
else {
// store the submitted values in an array
$params = $this->_submitValues;
try {
$apiParams = $this->buildParams($params);
if ($this->_id) {
$apiParams['id'] = $this->_id;
}
civicrm_api3('RelateEntities', 'create', $apiParams);
CRM_Core_Session::setStatus(ts('The Related entity has been saved.'), ts('Saved'), 'success');
}
catch (CRM_Core_Exception $e) {
CRM_Core_Error::statusBounce($e->getMessage());
}
}
$buttonName = $this->controller->getButtonName();
$session = CRM_Core_Session::singleton();
if ($this->entity_table == 'Contact') {
$this->ajaxResponse['updateTabs']['#tab_relateentities'] = CRM_RelateEntities_Utils::getRelatedEntities(
'Contact', $apiParams['entity_id_a'], FALSE, TRUE
);
}
if ($buttonName == $this->getButtonName('next', 'new')) {
CRM_Core_Session::setStatus(ts(' You can add another Bank Account.'));
$session->replaceUserContext(CRM_Utils_System::url('civicrm/relatedentities/add',
"action=add&reset=1&entityTable={$this->entity_table}&entityId={$apiParams['entity_id_a']}")
);
}
else {
if ($this->entity_table == 'Contact') {
$url = CRM_Utils_System::url('civicrm/contact/view',
"reset=1&force=1&cid={$apiParams['entity_id_a']}&selectedChild=relateentities");
}
if ($this->entity_table == 'FinancialType') {
$url = CRM_Utils_System::url('civicrm/admin/financial/financialType', 'reset=1');
}
$session->replaceUserContext($url);
}
}
public function buildParams($formValues) {
$rType = explode('_', $formValues['relationship_type_id'], 2);
if ($rType[1] == 'a_b') {
$params = [
'entity_id_a' => $formValues['entity_a'],
'entity_id_b' => $formValues['entity_b'],
'entity_table_a' => CRM_RelateEntities_Utils::getEntityTable($this->entityA),
'entity_table_b' => CRM_RelateEntities_Utils::getEntityTable($this->entityB),
];
}
else {
$params = [
'entity_id_a' => $formValues['entity_b'],
'entity_id_b' => $formValues['entity_a'],
'entity_table_a' => CRM_RelateEntities_Utils::getEntityTable($this->entityB),
'entity_table_b' => CRM_RelateEntities_Utils::getEntityTable($this->entityA),
];
}
$params['relationship_type_id'] = $rType[0];
$params['is_active'] = $formValues['is_active'];
return $params;
}
}

View File

@ -0,0 +1,111 @@
<?php
class CRM_RelateEntities_Page_Tab extends CRM_Core_Page_Basic {
/**
* The action links that we need to display for the browse screen.
*
* @var array
*/
public static $_links = NULL;
/**
* Get BAO Name.
*
* @return string
* Classname of BAO.
*/
public function getBAOName() {
return 'CRM_RelateEntities_DAO_RelateEntities';
}
/**
* Get action Links.
*
* @return array
* (reference) of action links
*/
public function &links() {
if (!(self::$_links)) {
self::$_links = [
CRM_Core_Action::UPDATE => [
'name' => ts('Edit'),
'url' => 'civicrm/relateentities/list',
'qs' => 'reset=1&action=update&id=%%id%%&entityTable=%%entityTable%%',
'title' => ts('Edit'),
],
CRM_Core_Action::DISABLE => [
'name' => ts('Disable'),
'ref' => 'crm-enable-disable',
'title' => ts('Disable'),
],
CRM_Core_Action::ENABLE => [
'name' => ts('Enable'),
'ref' => 'crm-enable-disable',
'title' => ts('Enable'),
],
CRM_Core_Action::DELETE => [
'name' => ts('Delete'),
'url' => 'civicrm/relateentities/list',
'qs' => 'reset=1&action=delete&id=%%id%%',
'title' => ts('Delete'),
],
];
}
return self::$_links;
}
/**
* Browse all address formats for country.
*/
public function browse() {
$this->_id = CRM_Utils_Request::retrieve('entityId', 'Positive', $this, TRUE);
$this->_entityTable = CRM_Utils_Request::retrieve('entityTable', 'String', $this, TRUE);
$this->assign('contactId', $this->_id);
$this->assign('contactID', $this->_id);
// check logged in url permission
if ($this->_entityTable == 'Contact') {
$this->_contactId = $this->_id;
CRM_Contact_Page_View::checkUserPermission($this);
}
CRM_RelateEntities_Utils::buildEntitiesList($this, $this->_entityTable, $this->_id);
$this->ajaxResponse['tabCount'] = CRM_RelateEntities_Utils::getRelatedEntities(
$this->_entityTable, $this->_id, FALSE, TRUE
);
}
/**
* Get name of edit form.
*
* @return string
* Classname of edit form.
*/
public function editForm() {
return 'CRM_RelateEntities_Form_RelatedEntity';
}
/**
* Get edit form name.
*
* @return string
* name of this page.
*/
public function editName() {
return ts('Relate Entity');
}
/**
* Get user context.
*
* @param null $mode
*
* @return string
* user context.
*/
public function userContext($mode = NULL) {
return 'civicrm/relateentities/list';
}
}

View File

@ -0,0 +1,37 @@
<?php
class CRM_RelateEntities_Settings {
public static function install() {
CRM_Core_DAO::executeQuery("
CREATE TABLE IF NOT EXISTS `civicrm_relate_entities` (
`id` int(10) UNSIGNED NOT NULL,
`entity_table_a` varchar(255) NOT NULL,
`entity_table_b` varchar(255) NOT NULL,
`entity_id_a` int(10) UNSIGNED NOT NULL,
`entity_id_b` int(10) UNSIGNED NOT NULL,
`relationship_type_id` int(10) UNSIGNED NOT NULL,
`is_active` tinyint(4) NOT NULL DEFAULT '1',
`created_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`modified_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `UI_civicrm_relate_entities_entity_table_a` (`entity_table_a`),
KEY `UI_civicrm_relate_entities_entity_table_b` (`entity_table_b`),
KEY `UI_civicrm_relate_entities_entity_id_a` (`entity_id_a`),
KEY `UI_civicrm_relate_entities_entity_id_b` (`entity_id_b`),
KEY `FK_civicrm_relate_entities_relationship_type_id` (`relationship_type_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
");
CRM_Core_DAO::executeQuery("
ALTER TABLE `civicrm_relate_entities`
ADD CONSTRAINT `FK_civicrm_relate_entities_relationship_type_id` FOREIGN KEY (`relationship_type_id`) REFERENCES `civicrm_relationship_type` (`id`) ON DELETE CASCADE;
");
}
public static function uninstall() {
CRM_Core_DAO::executeQuery("
DROP TABLE IF EXISTS civicrm_relate_entities;
");
}
}

View File

@ -0,0 +1,220 @@
<?php
class CRM_RelateEntities_Utils {
public static $_entityRefs = [];
public static function getList() {
$requiredParameters = [
'entityId' => 'Integer',
'entityTable' => 'String',
'entityTableB' => 'String',
];
$optionalParameters = [];
$params = CRM_Core_Page_AJAX::defaultSortAndPagerParams();
$params += CRM_Core_Page_AJAX::validateParams($requiredParameters, $optionalParameters);
$params['offset'] = ($params['page'] - 1) * $params['rp'];
$params['rowCount'] = $params['rp'];
$params['sort'] = CRM_Utils_Array::value('sortBy', $params);
$relatedEntities = self::getrelatedEntitiesSelector($params);
CRM_Utils_JSON::output($relatedEntities);
}
public static function getrelatedEntitiesSelector($params) {
$total = self::getRelatedEntities(
$params['entityTable'], $params['entityId'], TRUE, TRUE,
$params['entityTableB']
);
$results = self::getRelatedEntities(
$params['entityTable'], $params['entityId'], TRUE, FALSE,
$params['entityTableB']
);
$relatedEntities = [];
$links = new CRM_RelateEntities_Page_Tab();
$links = $links->links();
while ($results->fetch()) {
$action = CRM_Core_Action::UPDATE + CRM_Core_Action::DELETE + CRM_Core_Action::ADD;
$class = ' crm-entity ';
if ($results->is_active) {
$action += CRM_Core_Action::DISABLE;
}
else {
$class .= ' disabled';
$action += CRM_Core_Action::ENABLE;
}
$relatedEntity = [
'DT_RowId' => $results->id,
'DT_RowClass' => $class,
'DT_RowAttr' => [
'data-entity' => 'RelateEntities',
'data-id' => $results->id,
],
'name' => self::buildName($results->entity_id, $params['entityTableB']),
'is_active' => $results->is_active ? ts('Yes') : ts('No'),
'relationship_name' => $results->relationship_name,
];
$relatedEntity['links'] = CRM_Core_Action::formLink(
$links,
$action,
['id' => $results->id, 'entityTable' => $params['entityTable']],
ts('more'),
FALSE,
'relatedentity.manage.action',
'RelatedEntity',
$results->id
);
array_push($relatedEntities, $relatedEntity);
}
$relatedEntitiesDT = [];
$relatedEntitiesDT['data'] = $relatedEntities;
$relatedEntitiesDT['recordsTotal'] = $total;
$relatedEntitiesDT['recordsFiltered'] = $total;
return $relatedEntitiesDT;
}
public static function buildName($entityId, $entityTable) {
$name = '';
$createLink = TRUE;
try {
switch ($entityTable) {
case 'Contact':
$return = 'sort_name';
$url = 'civicrm/contact/view';
$q = "reset=1&cid=$entityId";
break;
case 'FinancialType':
$return = 'name';
$url = 'civicrm/admin/financial/financialType';
$q = "action=update&id={$entityId}&reset=1";
break;
case 'MembershipType':
$return = 'name';
$url = 'civicrm/admin/member/membershipType';
$q = "action=update&id={$entityId}&reset=1";
break;
}
$name = civicrm_api3($entityTable, 'getvalue', ['id' => $entityId, 'return' => $return]);
if ($createLink) {
$name = CRM_Utils_System::href($name, $url, $q);
}
}
catch (Exception $e) {
}
return $name;
}
public static function buildEntitiesList(&$form, $entityTable, $entityId) {
$results = self::getRelatedEntities($entityTable, $entityId, TRUE, FALSE, NULL, TRUE);
$relatedEntities = [];
while ($results->fetch()) {
$entityRef = self::getEntityTable($results->entity_table, TRUE);
$relatedEntities[$entityRef] = [
'label' => preg_replace('/(?<!\ )[A-Z]/', ' $0', $entityRef),
'columns' => self::getColumnHeaders($entityTable, $entityRef),
];
}
$form->assign('entityTable', $entityTable);
$form->assign('entityId', $entityId);
$form->assign('relatedEntities', $relatedEntities);
}
public function getColumnHeaders($entityTable, $entityRef) {
return [
'relationship_name' => ts('Relationship Type'),
'name' => ts('Title'),
'is_active' => ts('Is Active?'),
];
}
public static function getEntityTable($entityTable, $returnEntityRef = FALSE) {
if (empty($entityTable)) {
return;
}
if (empty(self::$_entityRefs)) {
$entityTypesClasses = array_flip(CRM_Core_DAO_AllCoreTables::daoToClass());
$entityTablesClasses = array_flip(CRM_Core_DAO_AllCoreTables::tables());
self::$_entityRefs = array_combine($entityTablesClasses, $entityTypesClasses);
}
if ($returnEntityRef) {
return CRM_Utils_Array::value($entityTable, self::$_entityRefs);
}
return array_search($entityTable, self::$_entityRefs);
}
public static function getRelatedEntities(
$entityTableA, $entityIdA, $all = TRUE, $getCount = FALSE, $entityTableB = NULL,
$groupBy = FALSE
) {
$entityTableA = self::getEntityTable($entityTableA);
if (empty($entityTableA) || empty($entityIdA)) {
return;
}
$isActiveClause = '';
if (!$all) {
$isActiveClause = ' AND cre.is_active = 1 ';
}
$selectClause = '*';
if ($getCount) {
$selectClause = ' count(*) ';
}
$queryParams = [
1 => [$entityTableA, 'String'],
2 => [$entityIdA, 'Integer'],
];
$entityTable1Clause = $entityTable2Clause = '';
$entityTableB = self::getEntityTable($entityTableB);
if ($entityTableB) {
$entityTable1Clause = ' AND `entity_table_a`= %3 ';
$entityTable2Clause = ' AND `entity_table_b`= %3 ';
$queryParams[3] = [$entityTableB, 'String'];
}
$groupByClause = '';
if ($groupBy) {
$groupByClause = ' GROUP BY entity_table ';
}
$query = "
SELECT {$selectClause} FROM (
SELECT
cre.`id`, cre.`entity_table_a` entity_table, cre.`entity_id_a` entity_id,
crt.`label_b_a` relationship_name, cre.`is_active`
FROM `civicrm_relate_entities` cre
INNER JOIN civicrm_relationship_type crt
ON crt.id = cre.relationship_type_id
WHERE `entity_table_b`= %1 AND `entity_id_b` = %2
{$isActiveClause} $entityTable1Clause
UNION
SELECT
cre.`id`, cre.`entity_table_b` entity_table, cre.`entity_id_b`,
crt.`label_a_b` relationship_name, cre.`is_active`
FROM `civicrm_relate_entities` cre
INNER JOIN civicrm_relationship_type crt
ON crt.id = cre.relationship_type_id
WHERE `entity_table_a`= %1 AND `entity_id_a` = %2
{$isActiveClause} $entityTable2Clause
) AS temp
$groupByClause
";
if ($getCount) {
return CRM_Core_DAO::singleValueQuery($query, $queryParams);
}
else {
return CRM_Core_DAO::executeQuery($query, $queryParams);
}
}
}