Api4 - Support wildcard * in select clause
[civicrm-core.git] / Civi / Api4 / Service / Schema / Joinable / CustomGroupJoinable.php
CommitLineData
19b53e5b
C
1<?php
2
380f3545
TO
3/*
4 +--------------------------------------------------------------------+
41498ac5 5 | Copyright CiviCRM LLC. All rights reserved. |
380f3545 6 | |
41498ac5
TO
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
380f3545
TO
10 +--------------------------------------------------------------------+
11 */
12
13/**
14 *
15 * @package CRM
ca5cec67 16 * @copyright CiviCRM LLC https://civicrm.org/licensing
380f3545
TO
17 * $Id$
18 *
19 */
20
21
19b53e5b
C
22namespace Civi\Api4\Service\Schema\Joinable;
23
24use Civi\Api4\CustomField;
25
26class CustomGroupJoinable extends Joinable {
27
28 /**
29 * @var string
30 */
31 protected $joinSide = self::JOIN_SIDE_LEFT;
32
33 /**
34 * @var string
35 *
36 * Name of the custom field column.
37 */
38 protected $columns;
39
40 /**
41 * @param $targetTable
42 * @param $alias
43 * @param bool $isMultiRecord
44 * @param string $entity
45 * @param string $columns
46 */
47 public function __construct($targetTable, $alias, $isMultiRecord, $entity, $columns) {
48 $this->entity = $entity;
49 $this->columns = $columns;
50 parent::__construct($targetTable, 'entity_id', $alias);
51 $this->joinType = $isMultiRecord ?
52 self::JOIN_TYPE_ONE_TO_MANY : self::JOIN_TYPE_ONE_TO_ONE;
53 }
54
55 /**
56 * @inheritDoc
57 */
58 public function getEntityFields() {
59 if (!$this->entityFields) {
60 $fields = CustomField::get()
61 ->setCheckPermissions(FALSE)
62 ->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', 'date_format', 'time_format', 'start_date_years', 'end_date_years'])
63 ->addWhere('custom_group.table_name', '=', $this->getTargetTable())
64 ->execute();
65 foreach ($fields as $field) {
66 $this->entityFields[] = \Civi\Api4\Service\Spec\SpecFormatter::arrayToField($field, $this->getEntity());
67 }
68 }
69 return $this->entityFields;
70 }
71
72 /**
73 * @inheritDoc
74 */
75 public function getField($fieldName) {
76 foreach ($this->getEntityFields() as $field) {
77 $name = $field->getName();
78 if ($name === $fieldName || strrpos($name, '.' . $fieldName) === strlen($name) - strlen($fieldName) - 1) {
79 return $field;
80 }
81 }
82 return NULL;
83 }
84
85 /**
86 * @return string
87 */
88 public function getSqlColumn($fieldName) {
39e0f675
CW
89 if (strpos($fieldName, '.') !== FALSE) {
90 $fieldName = substr($fieldName, 1 + strrpos($fieldName, '.'));
91 }
19b53e5b
C
92 return $this->columns[$fieldName];
93 }
94
95}