Merge pull request #15595 from eileenmcnaughton/dedupe3
[civicrm-core.git] / Civi / Api4 / Service / Spec / SpecGatherer.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2020 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
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. |
14 | |
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. |
19 | |
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 +--------------------------------------------------------------------+
27 */
28
29 /**
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2020
33 * $Id$
34 *
35 */
36
37
38 namespace Civi\Api4\Service\Spec;
39
40 use Civi\Api4\CustomField;
41 use Civi\Api4\Service\Spec\Provider\Generic\SpecProviderInterface;
42 use Civi\Api4\Utils\CoreUtil;
43
44 class SpecGatherer {
45
46 /**
47 * @var \Civi\Api4\Service\Spec\Provider\Generic\SpecProviderInterface[]
48 */
49 protected $specProviders = [];
50
51 /**
52 * A cache of DAOs based on entity
53 *
54 * @var \CRM_Core_DAO[]
55 */
56 protected $DAONames;
57
58 /**
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.
62 *
63 * @param string $entity
64 * @param string $action
65 * @param $includeCustom
66 *
67 * @return \Civi\Api4\Service\Spec\RequestSpec
68 */
69 public function getSpec($entity, $action, $includeCustom) {
70 $specification = new RequestSpec($entity, $action);
71
72 // Real entities
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);
77 }
78 }
79 // Custom pseudo-entities
80 else {
81 $this->getCustomGroupFields(substr($entity, 7), $specification);
82 }
83
84 // Default value only makes sense for create actions
85 if ($action != 'create') {
86 foreach ($specification->getFields() as $field) {
87 $field->setDefaultValue(NULL);
88 }
89 }
90
91 foreach ($this->specProviders as $provider) {
92 if ($provider->applies($entity, $action)) {
93 $provider->modifySpec($specification);
94 }
95 }
96
97 return $specification;
98 }
99
100 /**
101 * @param \Civi\Api4\Service\Spec\Provider\Generic\SpecProviderInterface $provider
102 */
103 public function addSpecProvider(SpecProviderInterface $provider) {
104 $this->specProviders[] = $provider;
105 }
106
107 /**
108 * @param string $entity
109 * @param string $action
110 * @param \Civi\Api4\Service\Spec\RequestSpec $specification
111 */
112 private function addDAOFields($entity, $action, RequestSpec $specification) {
113 $DAOFields = $this->getDAOFields($entity);
114
115 foreach ($DAOFields as $DAOField) {
116 if ($DAOField['name'] == 'id' && $action == 'create') {
117 continue;
118 }
119 if ($action !== 'create' || isset($DAOField['default'])) {
120 $DAOField['required'] = FALSE;
121 }
122 if ($DAOField['name'] == 'is_active' && empty($DAOField['default'])) {
123 $DAOField['default'] = '1';
124 }
125 $field = SpecFormatter::arrayToField($DAOField, $entity);
126 $specification->addFieldSpec($field);
127 }
128 }
129
130 /**
131 * @param string $entity
132 * @param \Civi\Api4\Service\Spec\RequestSpec $specification
133 */
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'])
140 ->execute();
141
142 foreach ($customFields as $fieldArray) {
143 $field = SpecFormatter::arrayToField($fieldArray, $entity);
144 $specification->addFieldSpec($field);
145 }
146 }
147
148 /**
149 * @param string $customGroup
150 * @param \Civi\Api4\Service\Spec\RequestSpec $specification
151 */
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'])
156 ->execute();
157
158 foreach ($customFields as $fieldArray) {
159 $field = SpecFormatter::arrayToField($fieldArray, 'Custom_' . $customGroup);
160 $specification->addFieldSpec($field);
161 }
162 }
163
164 /**
165 * @param string $entityName
166 *
167 * @return array
168 */
169 private function getDAOFields($entityName) {
170 $bao = CoreUtil::getBAOFromApiName($entityName);
171
172 return $bao::fields();
173 }
174
175 }