| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | +--------------------------------------------------------------------+ |
| 5 | | CiviCRM version 4.3 | |
| 6 | +--------------------------------------------------------------------+ |
| 7 | | Copyright CiviCRM LLC (c) 2004-2013 | |
| 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 | * new version of civicrm apis. See blog post at |
| 31 | * http://civicrm.org/node/131 |
| 32 | * |
| 33 | * @package CiviCRM_APIv3 |
| 34 | * @subpackage API_Contact |
| 35 | * @copyright CiviCRM LLC (c) 2004-2013 |
| 36 | * $Id: Contact.php 30415 2010-10-29 12:02:47Z shot $ |
| 37 | * |
| 38 | */ |
| 39 | |
| 40 | /** |
| 41 | * Function to create relationship type |
| 42 | * |
| 43 | * @param array $params Associative array of property name/value pairs to insert in new relationship type. |
| 44 | * |
| 45 | * @return Newly created Relationship_type object |
| 46 | * {@getfields RelationshipType_create} |
| 47 | * @access public |
| 48 | * {@schema Contact/RelationshipType.xml} |
| 49 | */ |
| 50 | function civicrm_api3_relationship_type_create($params) { |
| 51 | |
| 52 | if (!isset($params['label_a_b'])) { |
| 53 | |
| 54 | $params['label_a_b'] = $params['name_a_b']; |
| 55 | } |
| 56 | |
| 57 | if (!isset($params['label_b_a'])) { |
| 58 | |
| 59 | $params['label_b_a'] = $params['name_b_a']; |
| 60 | } |
| 61 | |
| 62 | $ids = array(); |
| 63 | if (isset($params['id']) && !CRM_Utils_Rule::integer($params['id'])) { |
| 64 | return civicrm_api3_create_error('Invalid value for relationship type ID'); |
| 65 | } |
| 66 | else { |
| 67 | $ids['relationshipType'] = CRM_Utils_Array::value('id', $params); |
| 68 | } |
| 69 | |
| 70 | $relationType = new CRM_Contact_BAO_RelationshipType(); |
| 71 | $relationType = CRM_Contact_BAO_RelationshipType::add($params, $ids); |
| 72 | |
| 73 | $relType = array(); |
| 74 | |
| 75 | _civicrm_api3_object_to_array($relationType, $relType[$relationType->id]); |
| 76 | |
| 77 | return civicrm_api3_create_success($relType, $params, 'relationship_type', 'create', $relationType); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Adjust Metadata for Create action |
| 82 | * |
| 83 | * The metadata is used for setting defaults, documentation & validation |
| 84 | * @param array $params array or parameters determined by getfields |
| 85 | */ |
| 86 | function _civicrm_api3_relationship_type_create_spec(&$params) { |
| 87 | $params['contact_type_a']['api.required'] = 1; |
| 88 | $params['contact_type_b']['api.required'] = 1; |
| 89 | $params['name_a_b']['api.required'] = 1; |
| 90 | $params['name_b_a']['api.required'] = 1; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Function to get all relationship type |
| 95 | * retruns An array of Relationship_type |
| 96 | * @access public |
| 97 | * {@getfields RelationshipType_get} |
| 98 | * @example RelationshipTypeGet.php |
| 99 | */ |
| 100 | function civicrm_api3_relationship_type_get($params) { |
| 101 | |
| 102 | return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Delete a relationship type delete |
| 107 | * |
| 108 | * @param id of relationship type $id |
| 109 | * |
| 110 | * @return array API Result Array |
| 111 | * {@getfields RelationshipType_delete} |
| 112 | * @static void |
| 113 | * @access public |
| 114 | */ |
| 115 | function civicrm_api3_relationship_type_delete($params) { |
| 116 | |
| 117 | if ($params['id'] != NULL && !CRM_Utils_Rule::integer($params['id'])) { |
| 118 | return civicrm_api3_create_error('Invalid value for relationship type ID'); |
| 119 | } |
| 120 | |
| 121 | $relationTypeBAO = new CRM_Contact_BAO_RelationshipType(); |
| 122 | $result = $relationTypeBAO->del($params['id']); |
| 123 | if (!$result) { |
| 124 | return civicrm_api3_create_error('Could not delete relationship type'); |
| 125 | } |
| 126 | return civicrm_api3_create_success($result, $params, 'relationship_type', 'delete', $relationTypeBAO); |
| 127 | } |
| 128 | |