PageRenderTime 27ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/michaelmcandrew/cic
PHP | 311 lines | 150 code | 51 blank | 110 comment | 32 complexity | 74beebceaf1fab6fc6e4e7b810ca9a11 MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 3.1 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2010 |
  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. * Definition of the Group part of the CRM API.
  30. * More detailed documentation can be found
  31. * {@link http://objectledge.org/confluence/display/CRM/CRM+v1.0+Public+APIs
  32. * here}
  33. *
  34. * @package CRM
  35. * @copyright CiviCRM LLC (c) 2004-2010
  36. * $Id$
  37. *
  38. */
  39. /**
  40. * Files required for this package
  41. */
  42. require_once 'api/utils.php';
  43. require_once 'CRM/Contact/BAO/Relationship.php';
  44. require_once 'CRM/Contact/BAO/RelationshipType.php';
  45. /**
  46. * Function to create new retaionship
  47. *
  48. * @param object $contact A valid Contact object.
  49. *
  50. * @param object $target_contact A valid Contact object
  51. * @param String $relationship_type_name A valid Relationship_type eg. Parent of etc.
  52. * @param array $ params Associative array of property name/value pairs to be inserted. See Data Model for available properties.
  53. *
  54. * @return newly created 'relationship object' object
  55. *
  56. * @access public
  57. *
  58. */
  59. function crm_create_relationship($contact =null, $target_contact= null, $relationship_type_name, $params) {
  60. $relationTypeID = null;
  61. if( ! isset( $contact->id ) and ! isset( $target_contact->id )) {
  62. return _crm_error('source or target contact object does not have contact ID');
  63. }
  64. $sourceContact = $contact->id;
  65. $targetContact = $target_contact->id;
  66. require_once 'CRM/Contact/DAO/RelationshipType.php';
  67. $reletionType = & new CRM_Contact_DAO_RelationshipType();
  68. $reletionType->name_a_b = $relationship_type_name;
  69. $reletionType->find();
  70. if($reletionType->fetch()) {
  71. $relationTypeID = $reletionType->id;
  72. $relationTypeID .='_a_b';
  73. }
  74. if (!$relationTypeID) {
  75. $reletionType = & new CRM_Contact_DAO_RelationshipType();
  76. $reletionType->name_b_a = $relationship_type_name;
  77. $reletionType->find();
  78. if($reletionType->fetch()) {
  79. $relationTypeID = $reletionType->id;
  80. $relationTypeID .='_b_a';
  81. }
  82. }
  83. if (!$relationTypeID) {
  84. return _crm_error('$relationship_type_ is not valid relationship type ');
  85. }
  86. $params['relationship_type_id' ] = $relationTypeID;
  87. $ids ['contact' ] = $sourceContact;
  88. $params['contact_check'] = array ( $targetContact => $targetContact) ;
  89. require_once 'CRM/Contact/BAO/Relationship.php';
  90. $errors = CRM_Contact_BAO_Relationship::checkValidRelationship( $params, $ids, $targetContact );
  91. if ( $errors ) {
  92. return _crm_error($errors);
  93. }
  94. if ( CRM_Contact_BAO_Relationship::checkDuplicateRelationship( $params ,$sourceContact,$targetContact )) {
  95. return _crm_error('Duplicate relationship');
  96. }
  97. $relationship = CRM_Contact_BAO_Relationship::add($params, $ids, $targetContact);
  98. if ( CRM_Core_Permission::access( 'CiviMember' ) ) {
  99. CRM_Contact_BAO_Relationship::relatedMemberships( $contact->contact_id,
  100. $params, $ids,
  101. CRM_Core_Action::ADD );
  102. }
  103. return $relationship;
  104. }
  105. /**
  106. * Function to get the relationship
  107. *
  108. * @param object $contact_a A valid Contact object
  109. * @param object $contact_b A valid Contact object
  110. * @param array $relationship_type_name An array of Relationship Type Name.
  111. * @param array $returnProperties Which properties should be included in the related Contact object(s). If NULL, the default set of contact properties will be included.
  112. * @param array $sort Associative array of one or more "property_name"=>"sort direction" pairs which will control order of Contact objects returned
  113. * @param int $offset Starting row index.
  114. *
  115. * @return Array of all relationship.
  116. *
  117. * @access public
  118. *
  119. */
  120. function crm_get_relationships($contact_a,
  121. $contact_b=null,
  122. $relationship_type_name = null,
  123. $returnProperties = null,
  124. $sort = null,
  125. $offset = 0,
  126. $row_count = 25 ) {
  127. if( ! isset( $contact_a->id ) ) {
  128. return _crm_error('$contact_a is not valid contact datatype');
  129. }
  130. require_once 'CRM/Contact/BAO/Relationship.php';
  131. $contactID = $contact_a->id;
  132. $relationships = CRM_Contact_BAO_Relationship::getRelationship($contactID);
  133. if ( isset( $relationship_type_name ) && is_array( $relationship_type_name ) ){
  134. $result =array();
  135. foreach ( $relationship_type_name as $relationshipType ) {
  136. foreach( $relationships as $key => $relationship ) {
  137. if ( $relationship['relation'] == $relationshipType ) {
  138. $result[$key] = $relationship;
  139. }
  140. }
  141. }
  142. $relationships = $result;
  143. }
  144. if( isset( $contact_b->id ) ) {
  145. $cid = $contact_b->id;
  146. $result =array();
  147. foreach($relationships as $key => $relationship) {
  148. if ($relationship['cid'] == $cid ) {
  149. $result[$key] = $relationship;
  150. }
  151. }
  152. $relationships = $result;
  153. }
  154. return $relationships;
  155. }
  156. /**
  157. * Function to delete relationship
  158. *
  159. * @param object $contact A valid Contact object (passed by reference).
  160. * @param object $target_contact A valid Contact object (passed by reference).
  161. * @param object $relationship_type An array of Relationship_type objects.
  162. *
  163. *
  164. * @return null if successful
  165. *
  166. * @access public
  167. *
  168. */
  169. function crm_delete_relationship(&$contact, &$target_contact, $relationship_type) {
  170. require_once 'CRM/Contact/BAO/Relationship.php';
  171. $relationTypeID = null;
  172. if( ! isset( $contact->id ) && ! isset( $target_contact->id )) {
  173. return _crm_error('source or target contact object does not have contact ID');
  174. }
  175. $sourceContact = $contact->id;
  176. $targetContact = $target_contact->id;
  177. if (!is_array($relationship_type)) {
  178. return _crm_error('$relationship_type is not array of relationship type objects');
  179. }
  180. foreach ($relationship_type as $rel ) {
  181. $relationShip = & new CRM_Contact_DAO_Relationship();
  182. $relationShip->relationship_type_id = $rel->id ;
  183. $relationShip->find();
  184. while($relationShip->fetch()) {
  185. if($relationShip->contact_id_a == $sourceContact || $relationShip->contact_id_b == $sourceContact ){
  186. if($relationShip->contact_id_a == $targetContact || $relationShip->contact_id_b == $targetContact) {
  187. CRM_Contact_BAO_Relationship::del($relationShip->id);
  188. }
  189. }
  190. }
  191. }
  192. return null;
  193. }
  194. /**
  195. * Function to create relationship type
  196. *
  197. * @param array $params Associative array of property name/value pairs to insert in new relationship type.
  198. *
  199. * @return Newly created Relationship_type object
  200. *
  201. * @access public
  202. *
  203. */
  204. function crm_create_relationship_type($params) {
  205. if(! isset($params['name_a_b']) and ! isset($params['name_b_a']) and ! isset($params['contact_type_a']) and ! isset($params['contact_type_b'] )) {
  206. return _crm_error('Return array is not properly set');
  207. }
  208. require_once 'CRM/Contact/BAO/RelationshipType.php';
  209. $relationType = CRM_Contact_BAO_RelationshipType::add( $params, $ids);
  210. return $relationType;
  211. }
  212. /**
  213. * Function to get all relationship type
  214. *
  215. * retruns An array of Relationship_type objects
  216. * @access public
  217. *
  218. */
  219. function crm_get_relationship_types() {
  220. require_once 'CRM/Contact/DAO/RelationshipType.php';
  221. $relationshipTypes = array();
  222. $relationType = & new CRM_Contact_DAO_RelationshipType();
  223. $relationType->find();
  224. while($relationType->fetch())
  225. {
  226. $relationshipTypes[] = clone($relationType);
  227. }
  228. return $relationshipTypes;
  229. }
  230. /**
  231. * Function to update relationship
  232. *
  233. * @param object $relationship A valid Relationship object.
  234. * @param array $params Associative array of property name/value pairs to be updated. See Data Model for available properties.
  235. *
  236. * @return updated relationship object
  237. *
  238. * @access public
  239. *
  240. */
  241. function crm_update_relationship(&$relationship, $params )
  242. {
  243. $ids = array();
  244. if( ! isset($relationship->id) && ! isset($relationship->contact_id_a) && ! isset($relationship->contact_id_b)) {
  245. return _crm_error('$relationship is not valid relationship type object');
  246. }
  247. $conactId = $relationship->contact_id_b;
  248. $params['relationship_type_id' ] = $relationship->relationship_type_id.'_a_b';
  249. $ids['contact'] = $relationship->contact_id_a;
  250. $ids['relationship'] = $relationship->id;
  251. $ids['contactTarget'] = $relationship->contact_id_b;
  252. $relationship = CRM_Contact_BAO_Relationship::add($params, $ids,$conactId);
  253. if ( CRM_Core_Permission::access( 'CiviMember' ) ) {
  254. $params['contact_check'] = array( $relationship->contact_id_b => 1 );
  255. CRM_Contact_BAO_Relationship::relatedMemberships( $relationship->contact_id_a,
  256. $params, $ids,
  257. CRM_Core_Action::ADD );
  258. }
  259. return $relationship;
  260. }