PageRenderTime 33ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/drupal/sites/all/modules/civicrm/api/v3/Relationship.php

https://github.com/michaelmcandrew/lms
PHP | 313 lines | 182 code | 44 blank | 87 comment | 38 complexity | 9ae7c13274b962867215f13314d48d32 MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 4.0 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2011 |
  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. * File for the CiviCRM APIv3 relationship functions
  29. *
  30. * @package CiviCRM_APIv3
  31. * @subpackage API_Relationship
  32. *
  33. * @copyright CiviCRM LLC (c) 2004-2011
  34. * @version $Id: Relationship.php 30486 2010-11-02 16:12:09Z shot $
  35. *
  36. */
  37. /**
  38. * Include utility functions
  39. */
  40. require_once 'api/v3/utils.php';
  41. require_once 'CRM/Contact/BAO/Relationship.php';
  42. require_once 'CRM/Contact/BAO/RelationshipType.php';
  43. /**
  44. * Add or update a relationship
  45. *
  46. * @param array $params (reference ) input parameters
  47. *
  48. * {@example RelationshipTypeCreate.php 0}
  49. * @return array (reference) id of created or updated record
  50. * @static void
  51. * @access public
  52. * @todo date handling is 'funky' check difference in tests between update & create. current create test won't work if end_date is same format as start_date
  53. * @todo create should handle update.
  54. */
  55. function civicrm_api3_relationship_create( $params ) {
  56. _civicrm_api3_initialize(true );
  57. try{
  58. // check params for required fields (add/update)
  59. static $required = array( 'contact_id_a',
  60. 'contact_id_b',
  61. array('relationship_type_id' , 'relationship_type'));
  62. civicrm_api3_verify_mandatory($params,'CRM_Contact_DAO_Relationship',$required);
  63. // check entities exist
  64. _civicrm_api3_relationship_check_params( $params );
  65. $values = array( );
  66. require_once 'CRM/Contact/BAO/Relationship.php';
  67. $error = _civicrm_api3_relationship_format_params( $params, $values );
  68. if ( civicrm_api3_error( $error ) ) {
  69. return $error;
  70. }
  71. $ids = array( );
  72. $action = CRM_Core_Action::ADD;
  73. require_once 'CRM/Utils/Array.php';
  74. if( CRM_Utils_Array::value( 'id', $params ) ) {
  75. $ids['relationship'] = $params['id'];
  76. $ids['contactTarget'] = $params['contact_id_b'];
  77. $action = CRM_Core_Action::UPDATE;
  78. }
  79. $values['relationship_type_id'] = $params['relationship_type_id'].'_a_b';
  80. $values['contact_check'] = array ( $params['contact_id_b'] => $params['contact_id_b'] );
  81. $ids ['contact' ] = $params['contact_id_a'];
  82. $relationshipBAO = CRM_Contact_BAO_Relationship::create( $values, $ids );
  83. if ( is_a( $relationshipBAO, 'CRM_Core_Error' ) ) {
  84. return civicrm_api3_create_error( 'Relationship can not be created' );
  85. } else if ( $relationshipBAO[1] ) {
  86. return civicrm_api3_create_error( 'Relationship is not valid' );
  87. } else if ( $relationshipBAO[2] ) {
  88. return civicrm_api3_create_error( 'Relationship already exists' );
  89. }
  90. CRM_Contact_BAO_Relationship::relatedMemberships( $params['contact_id_a'], $values, $ids, $action );
  91. $relationID = $relationshipBAO[4][0];
  92. return civicrm_api3_create_success( array( $relationID => array( 'id' => $relationID,
  93. 'moreIDs' => implode( ',', $relationshipBAO[4] ) ) ) );
  94. } catch (PEAR_Exception $e) {
  95. return civicrm_api3_create_error( $e->getMessage() );
  96. } catch (Exception $e) {
  97. return civicrm_api3_create_error( $e->getMessage() );
  98. }
  99. }
  100. /**
  101. * Delete a relationship
  102. *
  103. * @param array $params
  104. *
  105. * @return boolean true if success, else false
  106. * @static void
  107. * @access public
  108. */
  109. function civicrm_api3_relationship_delete( $params ) {
  110. _civicrm_api3_initialize(true );
  111. try{
  112. civicrm_api3_verify_mandatory($params,null,array('id'));
  113. require_once 'CRM/Utils/Rule.php';
  114. if( $params['id'] != null && ! CRM_Utils_Rule::integer( $params['id'] ) ) {
  115. return civicrm_api3_create_error( 'Invalid value for relationship ID' );
  116. }
  117. $relationBAO = new CRM_Contact_BAO_Relationship( );
  118. $relationBAO->id = $params['id'];
  119. if ( !$relationBAO->find( true ) ) {
  120. return civicrm_api3_create_error( 'Relationship id is not valid' );
  121. } else {
  122. $relationBAO->del( $params['id'] );
  123. return civicrm_api3_create_success( 'Deleted relationship successfully' );
  124. }
  125. } catch (PEAR_Exception $e) {
  126. return civicrm_api3_create_error( $e->getMessage() );
  127. } catch (Exception $e) {
  128. return civicrm_api3_create_error( $e->getMessage() );
  129. }
  130. }
  131. /**
  132. * Function to get the relationship
  133. *
  134. * @param array $params (reference ) input parameters.
  135. * @todo function in not a search - just returns all relationships for 'contact_id'
  136. *
  137. * @return Array of all relevant relationships.
  138. *
  139. * @access public
  140. */
  141. function civicrm_api3_relationship_get($params)
  142. {
  143. try{
  144. civicrm_api3_verify_mandatory($params, null,array('contact_id'));
  145. require_once 'CRM/Contact/BAO/Relationship.php';
  146. $relationships= array();
  147. $contactID = $params['contact_id'];
  148. $relationships = CRM_Contact_BAO_Relationship::getRelationship($contactID,
  149. CRM_Utils_Array::value('status_id',$params),
  150. 0,
  151. 0,
  152. CRM_Utils_Array::value('id',$params),null );
  153. //handle custom data.
  154. require_once 'CRM/Core/BAO/CustomGroup.php';
  155. foreach ( $relationships as $relationshipId => $values ) {
  156. $groupTree =& CRM_Core_BAO_CustomGroup::getTree( 'Relationship', CRM_Core_DAO::$_nullObject, $relationshipId, false,
  157. $values['civicrm_relationship_type_id'] );
  158. $formatTree = CRM_Core_BAO_CustomGroup::formatGroupTree( $groupTree, 1, CRM_Core_DAO::$_nullObject );
  159. $defaults = array( );
  160. CRM_Core_BAO_CustomGroup::setDefaults( $formatTree, $defaults );
  161. if ( !empty( $defaults ) ) {
  162. foreach ( $defaults as $key => $val ) {
  163. $relationships[$relationshipId][$key] = $val;
  164. }
  165. }
  166. }
  167. return civicrm_api3_create_success( $relationships ,$params);
  168. } catch (PEAR_Exception $e) {
  169. return civicrm_api3_create_error( $e->getMessage() );
  170. } catch (Exception $e) {
  171. return civicrm_api3_create_error( $e->getMessage() );
  172. }
  173. }
  174. /**
  175. * take the input parameter list as specified in the data model and
  176. * convert it into the same format that we use in QF and BAO object
  177. *
  178. * @param array $params Associative array of property name/value
  179. * pairs to insert in new contact.
  180. * @param array $values The reformatted properties that we can use internally
  181. * '
  182. * @return array|CRM_Error
  183. * @access public
  184. */
  185. function _civicrm_api3_relationship_format_params( $params, &$values ) {
  186. // copy all the relationship fields as is
  187. $fields =& CRM_Contact_DAO_Relationship::fields( );
  188. _civicrm_api3_store_values( $fields, $params, $values );
  189. $relationTypes = CRM_Core_PseudoConstant::relationshipType( 'name' );
  190. foreach ($params as $key => $value) {
  191. // ignore empty values or empty arrays etc
  192. require_once 'CRM/Utils/System.php';
  193. if ( CRM_Utils_System::isNull( $value ) ) {
  194. continue;
  195. }
  196. switch ($key) {
  197. case 'contact_id_a':
  198. case 'contact_id_b':
  199. require_once 'CRM/Utils/Rule.php';
  200. if (!CRM_Utils_Rule::integer($value)) {
  201. return civicrm_api3_create_error("contact_id not valid: $value");
  202. }
  203. $dao = new CRM_Core_DAO();
  204. $qParams = array();
  205. $svq = $dao->singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value",
  206. $qParams);
  207. if (!$svq) {
  208. return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value.");
  209. }
  210. break;
  211. case 'relationship_type':
  212. foreach ( $relationTypes as $relTypId => $relValue ) {
  213. if ( CRM_Utils_Array::key( ucfirst( $value ), $relValue ) ) {
  214. $relationshipTypeId = $relTypId;
  215. break;
  216. }
  217. }
  218. if ( $relationshipTypeId ) {
  219. if ( CRM_Utils_Array::value( 'relationship_type_id', $values ) &&
  220. $relationshipTypeId != $values['relationship_type_id'] ) {
  221. return civicrm_api3_create_error( 'Mismatched Relationship Type and Relationship Type Id' );
  222. }
  223. $values['relationship_type_id'] = $params['relationship_type_id'] = $relationshipTypeId ;
  224. } else {
  225. return civicrm_api3_create_error( 'Invalid Relationship Type' );
  226. }
  227. case 'relationship_type_id':
  228. if ( $key == 'relationship_type_id' && !array_key_exists($value, $relationTypes) ) {
  229. return civicrm_api3_create_error("$key not a valid: $value");
  230. }
  231. // execute for both relationship_type and relationship_type_id
  232. $relation = $relationTypes[$params['relationship_type_id']];
  233. require_once 'CRM/Contact/BAO/Contact.php';
  234. if ($relation['contact_type_a'] &&
  235. $relation['contact_type_a'] != CRM_Contact_BAO_Contact::getContactType($params['contact_id_a'])) {
  236. return civicrm_api3_create_error("Contact ID :{$params['contact_id_a']} is not of contact type {$relation['contact_type_a']}");
  237. }
  238. if ($relation['contact_type_b'] &&
  239. $relation['contact_type_b'] != CRM_Contact_BAO_Contact::getContactType($params['contact_id_b'])) {
  240. return civicrm_api3_create_error("Contact ID :{$params['contact_id_b']} is not of contact type {$relation['contact_type_b']}");
  241. }
  242. break;
  243. default:
  244. break;
  245. }
  246. }
  247. if ( array_key_exists( 'note', $params ) ) {
  248. $values['note'] = $params['note'];
  249. }
  250. _civicrm_api3_custom_format_params( $params, $values, 'Relationship' );
  251. return array();
  252. }
  253. function _civicrm_api3_relationship_check_params( &$params ) {
  254. // check params for validity of Relationship id
  255. if ( CRM_Utils_Array::value( 'id', $params ) ) {
  256. require_once 'CRM/Contact/BAO/Relationship.php';
  257. $relation = new CRM_Contact_BAO_Relationship();
  258. $relation->id = $params['id'];
  259. if ( !$relation->find( true ) ) {
  260. return civicrm_api3_create_error( 'Relationship id is not valid' );
  261. } else {
  262. if ( ( $params['contact_id_a'] != $relation->contact_id_a ) ||
  263. ( $params['contact_id_b'] != $relation->contact_id_b ) ) {
  264. return civicrm_api3_create_error( 'Cannot change the contacts once relationship has been created' );
  265. }
  266. }
  267. }
  268. return array();
  269. }