4 +--------------------------------------------------------------------+
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2020 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2020
38 namespace Civi\Api4\Service\Spec
;
40 use Civi\Api4\CustomField
;
41 use Civi\Api4\Service\Spec\Provider\Generic\SpecProviderInterface
;
42 use Civi\Api4\Utils\CoreUtil
;
47 * @var \Civi\Api4\Service\Spec\Provider\Generic\SpecProviderInterface[]
49 protected $specProviders = [];
52 * A cache of DAOs based on entity
54 * @var \CRM_Core_DAO[]
59 * Returns a RequestSpec with all the fields available. Uses spec providers
60 * to add or modify field specifications.
61 * For an example @see CustomFieldSpecProvider.
63 * @param string $entity
64 * @param string $action
65 * @param $includeCustom
67 * @return \Civi\Api4\Service\Spec\RequestSpec
69 public function getSpec($entity, $action, $includeCustom) {
70 $specification = new RequestSpec($entity, $action);
73 if (strpos($entity, 'Custom_') !== 0) {
74 $this->addDAOFields($entity, $action, $specification);
75 if ($includeCustom && array_key_exists($entity, \CRM_Core_SelectValues
::customGroupExtends())) {
76 $this->addCustomFields($entity, $specification);
79 // Custom pseudo-entities
81 $this->getCustomGroupFields(substr($entity, 7), $specification);
84 // Default value only makes sense for create actions
85 if ($action != 'create') {
86 foreach ($specification->getFields() as $field) {
87 $field->setDefaultValue(NULL);
91 foreach ($this->specProviders
as $provider) {
92 if ($provider->applies($entity, $action)) {
93 $provider->modifySpec($specification);
97 return $specification;
101 * @param \Civi\Api4\Service\Spec\Provider\Generic\SpecProviderInterface $provider
103 public function addSpecProvider(SpecProviderInterface
$provider) {
104 $this->specProviders
[] = $provider;
108 * @param string $entity
109 * @param string $action
110 * @param \Civi\Api4\Service\Spec\RequestSpec $specification
112 private function addDAOFields($entity, $action, RequestSpec
$specification) {
113 $DAOFields = $this->getDAOFields($entity);
115 foreach ($DAOFields as $DAOField) {
116 if ($DAOField['name'] == 'id' && $action == 'create') {
119 if ($action !== 'create' ||
isset($DAOField['default'])) {
120 $DAOField['required'] = FALSE;
122 if ($DAOField['name'] == 'is_active' && empty($DAOField['default'])) {
123 $DAOField['default'] = '1';
125 $field = SpecFormatter
::arrayToField($DAOField, $entity);
126 $specification->addFieldSpec($field);
131 * @param string $entity
132 * @param \Civi\Api4\Service\Spec\RequestSpec $specification
134 private function addCustomFields($entity, RequestSpec
$specification) {
135 $extends = ($entity == 'Contact') ?
['Contact', 'Individual', 'Organization', 'Household'] : [$entity];
136 $customFields = CustomField
::get()
137 ->setCheckPermissions(FALSE)
138 ->addWhere('custom_group.extends', 'IN', $extends)
139 ->setSelect(['custom_group.name', 'custom_group_id', 'name', 'label', 'data_type', 'html_type', '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'])
142 foreach ($customFields as $fieldArray) {
143 $field = SpecFormatter
::arrayToField($fieldArray, $entity);
144 $specification->addFieldSpec($field);
149 * @param string $customGroup
150 * @param \Civi\Api4\Service\Spec\RequestSpec $specification
152 private function getCustomGroupFields($customGroup, RequestSpec
$specification) {
153 $customFields = CustomField
::get()
154 ->addWhere('custom_group.name', '=', $customGroup)
155 ->setSelect(['custom_group.name', 'custom_group_id', 'name', 'label', 'data_type', 'html_type', 'is_searchable', 'is_search_range', 'weight', 'is_active', 'is_view', 'option_group_id', 'default_value', 'custom_group.table_name', 'column_name', 'date_format', 'time_format', 'start_date_years', 'end_date_years'])
158 foreach ($customFields as $fieldArray) {
159 $field = SpecFormatter
::arrayToField($fieldArray, 'Custom_' . $customGroup);
160 $specification->addFieldSpec($field);
165 * @param string $entityName
169 private function getDAOFields($entityName) {
170 $bao = CoreUtil
::getBAOFromApiName($entityName);
172 return $bao::fields();