| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
| 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 array('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(array("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 | } |