diff options
Diffstat (limited to 'www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/Civi/Api4/Service/Schema/Joinable')
5 files changed, 472 insertions, 0 deletions
diff --git a/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/Civi/Api4/Service/Schema/Joinable/ActivityToActivityContactAssigneesJoinable.php b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/Civi/Api4/Service/Schema/Joinable/ActivityToActivityContactAssigneesJoinable.php new file mode 100644 index 00000000..191f4389 --- /dev/null +++ b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/Civi/Api4/Service/Schema/Joinable/ActivityToActivityContactAssigneesJoinable.php @@ -0,0 +1,40 @@ +<?php + +namespace Civi\Api4\Service\Schema\Joinable; + +class ActivityToActivityContactAssigneesJoinable extends Joinable { + /** + * @var string + */ + protected $baseTable = 'civicrm_activity'; + + /** + * @var string + */ + protected $baseColumn = 'id'; + + /** + * @param $alias + */ + public function __construct($alias) { + $optionValueTable = 'civicrm_option_value'; + $optionGroupTable = 'civicrm_option_group'; + + $subSubSelect = sprintf( + 'SELECT id FROM %s WHERE name = "%s"', + $optionGroupTable, + 'activity_contacts' + ); + + $subSelect = sprintf( + 'SELECT value FROM %s WHERE name = "%s" AND option_group_id = (%s)', + $optionValueTable, + 'Activity Assignees', + $subSubSelect + ); + + $this->addCondition(sprintf('%s.record_type_id = (%s)', $alias, $subSelect)); + parent::__construct('civicrm_activity_contact', 'activity_id', $alias); + } + +} diff --git a/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/Civi/Api4/Service/Schema/Joinable/BridgeJoinable.php b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/Civi/Api4/Service/Schema/Joinable/BridgeJoinable.php new file mode 100644 index 00000000..370c5898 --- /dev/null +++ b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/Civi/Api4/Service/Schema/Joinable/BridgeJoinable.php @@ -0,0 +1,23 @@ +<?php + +namespace Civi\Api4\Service\Schema\Joinable; + +class BridgeJoinable extends Joinable { + /** + * @var Joinable + */ + protected $middleLink; + + public function __construct($targetTable, $targetColumn, $alias, Joinable $middleLink) { + parent::__construct($targetTable, $targetColumn, $alias); + $this->middleLink = $middleLink; + } + + /** + * @return Joinable + */ + public function getMiddleLink() { + return $this->middleLink; + } + +} diff --git a/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/Civi/Api4/Service/Schema/Joinable/CustomGroupJoinable.php b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/Civi/Api4/Service/Schema/Joinable/CustomGroupJoinable.php new file mode 100644 index 00000000..a1dd1a1d --- /dev/null +++ b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/Civi/Api4/Service/Schema/Joinable/CustomGroupJoinable.php @@ -0,0 +1,71 @@ +<?php + +namespace Civi\Api4\Service\Schema\Joinable; + +use Civi\Api4\CustomField; + +class CustomGroupJoinable extends Joinable { + + /** + * @var string + */ + protected $joinSide = self::JOIN_SIDE_LEFT; + + /** + * @var string + * + * Name of the custom field column. + */ + protected $columns; + + /** + * @param $targetTable + * @param $alias + * @param bool $isMultiRecord + * @param null $entity + */ + public function __construct($targetTable, $alias, $isMultiRecord, $entity, $columns) { + $this->entity = $entity; + $this->columns = $columns; + parent::__construct($targetTable, 'entity_id', $alias); + $this->joinType = $isMultiRecord ? + self::JOIN_TYPE_ONE_TO_MANY : self::JOIN_TYPE_ONE_TO_ONE; + } + + /** + * @inheritDoc + */ + public function getEntityFields() { + if (!$this->entityFields) { + $fields = CustomField::get() + ->setSelect(['custom_group.name', 'custom_group_id', 'name', 'label', 'data_type', 'html_type', 'is_required', 'is_searchable', 'is_search_range', 'weight', 'is_active', 'is_view', 'option_group_id', 'default_value']) + ->addWhere('custom_group.table_name', '=', $this->getTargetTable()) + ->execute(); + foreach ($fields as $field) { + $this->entityFields[] = \Civi\Api4\Service\Spec\SpecFormatter::arrayToField($field, $this->getEntity()); + } + } + return $this->entityFields; + } + + /** + * @inheritDoc + */ + public function getField($fieldName) { + foreach ($this->getEntityFields() as $field) { + $name = $field->getName(); + if ($name === $fieldName || strrpos($name, '.' . $fieldName) === strlen($name) - strlen($fieldName) - 1) { + return $field; + } + } + return NULL; + } + + /** + * @return string + */ + public function getSqlColumn($fieldName) { + return $this->columns[$fieldName]; + } + +} diff --git a/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/Civi/Api4/Service/Schema/Joinable/Joinable.php b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/Civi/Api4/Service/Schema/Joinable/Joinable.php new file mode 100644 index 00000000..0e92e3ab --- /dev/null +++ b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/Civi/Api4/Service/Schema/Joinable/Joinable.php @@ -0,0 +1,277 @@ +<?php + +namespace Civi\Api4\Service\Schema\Joinable; + +use Civi\Api4\Service\Spec\FieldSpec; +use CRM_Core_DAO_AllCoreTables as Tables; + +class Joinable { + + const JOIN_SIDE_LEFT = 'LEFT'; + const JOIN_SIDE_INNER = 'INNER'; + + const JOIN_TYPE_ONE_TO_ONE = '1_to_1'; + const JOIN_TYPE_MANY_TO_ONE = 'n_to_1'; + const JOIN_TYPE_ONE_TO_MANY = '1_to_n'; + + /** + * @var string + */ + protected $baseTable; + + /** + * @var string + */ + protected $baseColumn; + + /** + * @var string + */ + protected $targetTable; + + /** + * @var string + * + * Name (or alias) of the target column) + */ + protected $targetColumn; + + /** + * @var string + */ + protected $alias; + + /** + * @var array + */ + protected $conditions = []; + + /** + * @var string + */ + protected $joinSide = self::JOIN_SIDE_LEFT; + + /** + * @var int + */ + protected $joinType = self::JOIN_TYPE_ONE_TO_ONE; + + /** + * @var string + */ + protected $entity; + + /** + * @var array + */ + protected $entityFields; + + /** + * @param $targetTable + * @param $targetColumn + * @param string|null $alias + */ + public function __construct($targetTable, $targetColumn, $alias = NULL) { + $this->targetTable = $targetTable; + $this->targetColumn = $targetColumn; + if (!$this->entity) { + $this->entity = Tables::getBriefName(Tables::getClassForTable($targetTable)); + } + $this->alias = $alias ?: str_replace('civicrm_', '', $targetTable); + } + + /** + * Gets conditions required when joining to a base table + * + * @param string|null $baseTableAlias + * Name of the base table, if aliased. + * + * @return array + */ + public function getConditionsForJoin($baseTableAlias = NULL) { + $baseCondition = sprintf( + '%s.%s = %s.%s', + $baseTableAlias ?: $this->baseTable, + $this->baseColumn, + $this->getAlias(), + $this->targetColumn + ); + + return array_merge([$baseCondition], $this->conditions); + } + + /** + * @return string + */ + public function getBaseTable() { + return $this->baseTable; + } + + /** + * @param string $baseTable + * + * @return $this + */ + public function setBaseTable($baseTable) { + $this->baseTable = $baseTable; + + return $this; + } + + /** + * @return string + */ + public function getBaseColumn() { + return $this->baseColumn; + } + + /** + * @param string $baseColumn + * + * @return $this + */ + public function setBaseColumn($baseColumn) { + $this->baseColumn = $baseColumn; + + return $this; + } + + /** + * @return string + */ + public function getAlias() { + return $this->alias; + } + + /** + * @param string $alias + * + * @return $this + */ + public function setAlias($alias) { + $this->alias = $alias; + + return $this; + } + + /** + * @return string + */ + public function getTargetTable() { + return $this->targetTable; + } + + /** + * @return string + */ + public function getTargetColumn() { + return $this->targetColumn; + } + + /** + * @return string + */ + public function getEntity() { + return $this->entity; + } + + /** + * @param $condition + * + * @return $this + */ + public function addCondition($condition) { + $this->conditions[] = $condition; + + return $this; + } + + /** + * @return array + */ + public function getExtraJoinConditions() { + return $this->conditions; + } + + /** + * @param array $conditions + * + * @return $this + */ + public function setConditions($conditions) { + $this->conditions = $conditions; + + return $this; + } + + /** + * @return string + */ + public function getJoinSide() { + return $this->joinSide; + } + + /** + * @param string $joinSide + * + * @return $this + */ + public function setJoinSide($joinSide) { + $this->joinSide = $joinSide; + + return $this; + } + + /** + * @return int + */ + public function getJoinType() { + return $this->joinType; + } + + /** + * @param int $joinType + * + * @return $this + */ + public function setJoinType($joinType) { + $this->joinType = $joinType; + + return $this; + } + + /** + * @return array + */ + public function toArray() { + return get_object_vars($this); + } + + /** + * @return \Civi\Api4\Service\Spec\FieldSpec[] + */ + public function getEntityFields() { + if (!$this->entityFields) { + $bao = Tables::getClassForTable($this->getTargetTable()); + if ($bao) { + foreach ($bao::fields() as $field) { + $this->entityFields[] = \Civi\Api4\Service\Spec\SpecFormatter::arrayToField($field, $this->getEntity()); + } + } + } + return $this->entityFields; + } + + /** + * @return \Civi\Api4\Service\Spec\FieldSpec|NULL + */ + public function getField($fieldName) { + foreach ($this->getEntityFields() as $field) { + if ($field->getName() === $fieldName) { + return $field; + } + } + return NULL; + } + +} diff --git a/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/Civi/Api4/Service/Schema/Joinable/OptionValueJoinable.php b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/Civi/Api4/Service/Schema/Joinable/OptionValueJoinable.php new file mode 100644 index 00000000..96f65488 --- /dev/null +++ b/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/Civi/Api4/Service/Schema/Joinable/OptionValueJoinable.php @@ -0,0 +1,61 @@ +<?php + +namespace Civi\Api4\Service\Schema\Joinable; + +class OptionValueJoinable extends Joinable { + /** + * @var string + */ + protected $optionGroupName; + + /** + * @param string $optionGroup + * Can be either the option group name or ID + * @param string|null $alias + * The join alias + * @param string $keyColumn + * Which column to use to join, defaults to "value" + */ + public function __construct($optionGroup, $alias = NULL, $keyColumn = 'value') { + $this->optionGroupName = $optionGroup; + $optionValueTable = 'civicrm_option_value'; + + // default join alias to option group name, e.g. activity_type + if (!$alias && !is_numeric($optionGroup)) { + $alias = $optionGroup; + } + + parent::__construct($optionValueTable, $keyColumn, $alias); + + if (!is_numeric($optionGroup)) { + $subSelect = 'SELECT id FROM civicrm_option_group WHERE name = "%s"'; + $subQuery = sprintf($subSelect, $optionGroup); + $condition = sprintf('%s.option_group_id = (%s)', $alias, $subQuery); + } + else { + $condition = sprintf('%s.option_group_id = %d', $alias, $optionGroup); + } + + $this->addCondition($condition); + } + + /** + * The existing condition must also be re-aliased + * + * @param string $alias + * + * @return $this + */ + public function setAlias($alias) { + foreach ($this->conditions as $index => $condition) { + $search = $this->alias . '.'; + $replace = $alias . '.'; + $this->conditions[$index] = str_replace($search, $replace, $condition); + } + + parent::setAlias($alias); + + return $this; + } + +} |