Add contact type to schema to allow GetFields filtering
[civicrm-core.git] / Civi / Api4 / Service / Spec / SpecGatherer.php
index 94de36a5328b87aff19505c75df6b33ec98955ce..0edb4e8c6a5885ac25396e756b2aa7d02d672586 100644 (file)
@@ -2,34 +2,18 @@
 
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 5                                                  |
- +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2020                                |
- +--------------------------------------------------------------------+
- | This file is a part of CiviCRM.                                    |
- |                                                                    |
- | CiviCRM is free software; you can copy, modify, and distribute it  |
- | under the terms of the GNU Affero General Public License           |
- | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
- |                                                                    |
- | CiviCRM is distributed in the hope that it will be useful, but     |
- | WITHOUT ANY WARRANTY; without even the implied warranty of         |
- | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
- | See the GNU Affero General Public License for more details.        |
+ | Copyright CiviCRM LLC. All rights reserved.                        |
  |                                                                    |
- | You should have received a copy of the GNU Affero General Public   |
- | License and the CiviCRM Licensing Exception along                  |
- | with this program; if not, contact CiviCRM LLC                     |
- | at info[AT]civicrm[DOT]org. If you have questions about the        |
- | GNU Affero General Public License or the licensing of CiviCRM,     |
- | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
  +--------------------------------------------------------------------+
  */
 
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2020
+ * @copyright CiviCRM LLC https://civicrm.org/licensing
  * $Id$
  *
  */
@@ -62,18 +46,19 @@ class SpecGatherer {
    *
    * @param string $entity
    * @param string $action
-   * @param $includeCustom
+   * @param bool $includeCustom
+   * @param array $values
    *
    * @return \Civi\Api4\Service\Spec\RequestSpec
    */
-  public function getSpec($entity, $action, $includeCustom) {
+  public function getSpec($entity, $action, $includeCustom, $values = []) {
     $specification = new RequestSpec($entity, $action);
 
     // Real entities
     if (strpos($entity, 'Custom_') !== 0) {
-      $this->addDAOFields($entity, $action, $specification);
+      $this->addDAOFields($entity, $action, $specification, $values);
       if ($includeCustom && array_key_exists($entity, \CRM_Core_SelectValues::customGroupExtends())) {
-        $this->addCustomFields($entity, $specification);
+        $this->addCustomFields($entity, $specification, $values);
       }
     }
     // Custom pseudo-entities
@@ -108,14 +93,18 @@ class SpecGatherer {
    * @param string $entity
    * @param string $action
    * @param \Civi\Api4\Service\Spec\RequestSpec $specification
+   * @param array $values
    */
-  private function addDAOFields($entity, $action, RequestSpec $specification) {
+  private function addDAOFields($entity, $action, RequestSpec $specification, $values = []) {
     $DAOFields = $this->getDAOFields($entity);
 
     foreach ($DAOFields as $DAOField) {
       if ($DAOField['name'] == 'id' && $action == 'create') {
         continue;
       }
+      if (array_key_exists('contactType', $DAOField) && !empty($values['contact_type']) && $DAOField['contactType'] != $values['contact_type']) {
+        continue;
+      }
       if ($action !== 'create' || isset($DAOField['default'])) {
         $DAOField['required'] = FALSE;
       }
@@ -130,13 +119,19 @@ class SpecGatherer {
   /**
    * @param string $entity
    * @param \Civi\Api4\Service\Spec\RequestSpec $specification
+   * @param array $values
+   * @throws \API_Exception
    */
-  private function addCustomFields($entity, RequestSpec $specification) {
-    $extends = ($entity == 'Contact') ? ['Contact', 'Individual', 'Organization', 'Household'] : [$entity];
+  private function addCustomFields($entity, RequestSpec $specification, $values = []) {
+    $extends = [$entity];
+    if ($entity === 'Contact') {
+      $extends = !empty($values['contact_type']) ? [$values['contact_type'], 'Contact'] : ['Contact', 'Individual', 'Organization', 'Household'];
+    }
     $customFields = CustomField::get()
       ->setCheckPermissions(FALSE)
       ->addWhere('custom_group.extends', 'IN', $extends)
-      ->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'])
+      ->addWhere('custom_group.is_multiple', '=', '0')
+      ->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', 'help_pre', 'help_post'])
       ->execute();
 
     foreach ($customFields as $fieldArray) {
@@ -152,7 +147,7 @@ class SpecGatherer {
   private function getCustomGroupFields($customGroup, RequestSpec $specification) {
     $customFields = CustomField::get()
       ->addWhere('custom_group.name', '=', $customGroup)
-      ->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'])
+      ->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', 'help_pre', 'help_post'])
       ->execute();
 
     foreach ($customFields as $fieldArray) {