| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * This api exposes CiviCRM relationships. |
| 30 | * |
| 31 | * @package CiviCRM_APIv3 |
| 32 | */ |
| 33 | |
| 34 | /** |
| 35 | * Add or update a relationship. |
| 36 | * |
| 37 | * @param array $params |
| 38 | * Input parameters. |
| 39 | * |
| 40 | * @throws API_Exception |
| 41 | * |
| 42 | * @return array |
| 43 | * API Result Array |
| 44 | */ |
| 45 | function civicrm_api3_relationship_create($params) { |
| 46 | _civicrm_api3_handle_relationship_type($params); |
| 47 | return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Relationship'); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Adjust Metadata for Create action. |
| 52 | * |
| 53 | * @param array $params |
| 54 | * Array of parameters determined by getfields. |
| 55 | */ |
| 56 | function _civicrm_api3_relationship_create_spec(&$params) { |
| 57 | $params['contact_id_a']['api.required'] = 1; |
| 58 | $params['contact_id_b']['api.required'] = 1; |
| 59 | $params['relationship_type_id']['api.required'] = 1; |
| 60 | $params['is_active']['api.default'] = 1; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Delete a relationship. |
| 65 | * |
| 66 | * @param array $params |
| 67 | * |
| 68 | * @return array |
| 69 | * API Result Array |
| 70 | */ |
| 71 | function civicrm_api3_relationship_delete($params) { |
| 72 | |
| 73 | if (!CRM_Utils_Rule::integer($params['id'])) { |
| 74 | return civicrm_api3_create_error('Invalid value for relationship ID'); |
| 75 | } |
| 76 | |
| 77 | $relationBAO = new CRM_Contact_BAO_Relationship(); |
| 78 | $relationBAO->id = $params['id']; |
| 79 | if (!$relationBAO->find(TRUE)) { |
| 80 | return civicrm_api3_create_error('Relationship id is not valid'); |
| 81 | } |
| 82 | else { |
| 83 | $relationBAO->del($params['id']); |
| 84 | return civicrm_api3_create_success('Deleted relationship successfully'); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get one or more relationship/s. |
| 90 | * |
| 91 | * @param array $params |
| 92 | * Input parameters. |
| 93 | * |
| 94 | * @todo Result is inconsistent depending on whether contact_id is passed in : |
| 95 | * - if you pass in contact_id - it just returns all relationships for 'contact_id' |
| 96 | * - if you don't pass in contact_id then it does a filter on the relationship table (DAO based search) |
| 97 | * |
| 98 | * @return array |
| 99 | * API Result Array |
| 100 | */ |
| 101 | function civicrm_api3_relationship_get($params) { |
| 102 | $options = _civicrm_api3_get_options_from_params($params); |
| 103 | |
| 104 | if (empty($params['contact_id'])) { |
| 105 | if (!empty($params['membership_type_id']) && empty($params['relationship_type_id'])) { |
| 106 | CRM_Contact_BAO_Relationship::membershipTypeToRelationshipTypes($params); |
| 107 | } |
| 108 | $relationships = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE, 'Relationship'); |
| 109 | } |
| 110 | else { |
| 111 | $relationships = CRM_Contact_BAO_Relationship::getRelationship($params['contact_id'], |
| 112 | CRM_Utils_Array::value('status_id', $params), |
| 113 | 0, |
| 114 | CRM_Utils_Array::value('is_count', $options), |
| 115 | CRM_Utils_Array::value('id', $params), |
| 116 | NULL, |
| 117 | NULL, |
| 118 | FALSE, |
| 119 | $params |
| 120 | ); |
| 121 | } |
| 122 | //perhaps we should add a 'getcount' but at this stage lets just handle getcount output |
| 123 | if ($options['is_count']) { |
| 124 | return ['count' => $relationships]; |
| 125 | } |
| 126 | foreach ($relationships as $relationshipId => $values) { |
| 127 | _civicrm_api3_custom_data_get($relationships[$relationshipId], CRM_Utils_Array::value('check_permissions', $params), 'Relationship', $relationshipId, NULL, CRM_Utils_Array::value('relationship_type_id', $values)); |
| 128 | } |
| 129 | return civicrm_api3_create_success($relationships, $params); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Legacy handling for relationship_type parameter. |
| 134 | * |
| 135 | * @param array $params |
| 136 | * Associative array of property name/value. |
| 137 | * pairs to insert in new contact. |
| 138 | */ |
| 139 | function _civicrm_api3_handle_relationship_type(&$params) { |
| 140 | if (empty($params['relationship_type_id']) && !empty($params['relationship_type'])) { |
| 141 | $relationTypes = CRM_Core_PseudoConstant::relationshipType('name'); |
| 142 | foreach ($relationTypes as $relationshipTypeId => $relationshipValue) { |
| 143 | if (CRM_Utils_Array::key(ucfirst($params['relationship_type']), $relationshipValue)) { |
| 144 | $params['relationship_type_id'] = $relationshipTypeId; |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Hack to ensure inherited membership got created/deleted on |
| 152 | * relationship add/delete respectively. |
| 153 | * |
| 154 | * @param array $params |
| 155 | * Array per getfields metadata. |
| 156 | * |
| 157 | * @return array |
| 158 | */ |
| 159 | function civicrm_api3_relationship_setvalue($params) { |
| 160 | require_once 'api/v3/Generic/Setvalue.php'; |
| 161 | $result = civicrm_api3_generic_setValue(["entity" => 'Relationship', 'params' => $params]); |
| 162 | |
| 163 | if (empty($result['is_error']) && CRM_Utils_String::munge($params['field']) == 'is_active') { |
| 164 | $action = CRM_Core_Action::DISABLE; |
| 165 | if ($params['value'] == TRUE) { |
| 166 | $action = CRM_Core_Action::ENABLE; |
| 167 | } |
| 168 | CRM_Contact_BAO_Relationship::disableEnableRelationship($params['id'], $action); |
| 169 | } |
| 170 | return $result; |
| 171 | } |
| 172 | |
| 173 | function _civicrm_api3_relationship_getoptions_spec(&$params) { |
| 174 | $params['field']['options']['relationship_type_id'] = ts('Relationship Type ID'); |
| 175 | |
| 176 | // Add parameters for limiting relationship type ID |
| 177 | $relationshipTypePrefix = ts('(For relationship_type_id only) '); |
| 178 | $params['contact_id'] = [ |
| 179 | 'title' => ts('Contact ID'), |
| 180 | 'description' => $relationshipTypePrefix . ts('Limits options to those' |
| 181 | . ' available to give contact'), |
| 182 | 'type' => CRM_Utils_Type::T_INT, |
| 183 | 'FKClassName' => 'CRM_Contact_DAO_Contact', |
| 184 | 'FKApiName' => 'Contact', |
| 185 | ]; |
| 186 | $params['relationship_direction'] = [ |
| 187 | 'title' => ts('Relationship Direction'), |
| 188 | 'description' => $relationshipTypePrefix . ts('For relationships where the ' |
| 189 | . 'name is the same for both sides (i.e. "Spouse Of") show the option ' |
| 190 | . 'from "A" (origin) side or "B" (target) side of the relationship?'), |
| 191 | 'type' => CRM_Utils_Type::T_STRING, |
| 192 | 'options' => ['a_b' => 'a_b', 'b_a' => 'b_a'], |
| 193 | 'api.default' => 'a_b', |
| 194 | ]; |
| 195 | $params['relationship_id'] = [ |
| 196 | 'title' => ts('Reference Relationship ID'), |
| 197 | 'description' => $relationshipTypePrefix . ts('If provided alongside ' |
| 198 | . 'contact ID it will be used to establish the contact type of the "B" ' |
| 199 | . 'side of the relationship and limit options based on it. If the ' |
| 200 | . 'provided contact ID does not match the "A" side of this relationship ' |
| 201 | . 'then the "A" side of this relationship will be used to limit options'), |
| 202 | 'type' => CRM_Utils_Type::T_INT, |
| 203 | 'FKClassName' => 'CRM_Contact_DAO_Relationship', |
| 204 | 'FKApiName' => 'Relationship', |
| 205 | ]; |
| 206 | $contactTypes = CRM_Contact_BAO_ContactType::contactTypes(); |
| 207 | $params['contact_type'] = [ |
| 208 | 'title' => ts('Contact Type'), |
| 209 | 'description' => $relationshipTypePrefix . ts('Limits options to those ' |
| 210 | . 'available to this contact type. Overridden by the contact type of ' |
| 211 | . 'contact ID (if provided)'), |
| 212 | 'type' => CRM_Utils_Type::T_STRING, |
| 213 | 'options' => array_combine($contactTypes, $contactTypes), |
| 214 | ]; |
| 215 | $params['is_form'] = [ |
| 216 | 'title' => ts('Is Form?'), |
| 217 | 'description' => $relationshipTypePrefix . ts('Formats the options for use' |
| 218 | . ' in a form if true. The format is <id>_a_b => <label>'), |
| 219 | 'type' => CRM_Utils_Type::T_BOOLEAN |
| 220 | ]; |
| 221 | } |