PageRenderTime 55ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/public_html/sites/all/modules/civicrm/api/v3/Profile.php

https://github.com/timstephenson/NatureBridge
PHP | 308 lines | 180 code | 53 blank | 75 comment | 38 complexity | 2b423e38db235b0a16ed356b4bcbbb29 MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 3.4 |
  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 activity profile functions
  29. *
  30. * @package CiviCRM_APIv3
  31. * @subpackage API_ActivityProfile
  32. * @copyright CiviCRM LLC (c) 2004-2011
  33. * @version $Id: ActivityProfile.php 30486 2011-05-20 16:12:09Z rajan $
  34. *
  35. */
  36. /**
  37. * Include common API util functions
  38. */
  39. require_once 'api/v3/utils.php';
  40. require_once 'CRM/Core/BAO/UFGroup.php';
  41. require_once 'CRM/Core/BAO/UFField.php';
  42. require_once 'CRM/Core/Permission.php';
  43. /**
  44. * Retrieve Profile field values.
  45. *
  46. * @param array $params Associative array of property name/value
  47. * pairs to get profile field values
  48. *
  49. * @return Profile field values|CRM_Error
  50. *
  51. * @todo add example
  52. * @todo add test cases
  53. *
  54. */
  55. function civicrm_api3_profile_get( $params ) {
  56. civicrm_api3_verify_mandatory($params, null, array('profile_id', 'contact_id'));
  57. if ( !CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $params['profile_id'], 'is_active' ) ) {
  58. return civicrm_api3_create_error('Invalid value for profile_id');
  59. }
  60. $isContactActivityProfile = CRM_Core_BAO_UFField::checkContactActivityProfileType( $params['profile_id'] );
  61. if ( CRM_Core_BAO_UFField::checkProfileType($params['profile_id']) && !$isContactActivityProfile ) {
  62. return civicrm_api3_create_error('Can not retrieve values for profiles include fields for more than one record type.' );
  63. }
  64. $profileFields = CRM_Core_BAO_UFGroup::getFields( $params['profile_id'],
  65. false,
  66. null,
  67. null,
  68. null,
  69. false,
  70. null,
  71. true,
  72. null,
  73. CRM_Core_Permission::EDIT );
  74. $values = array( );
  75. if ( $isContactActivityProfile ) {
  76. civicrm_api3_verify_mandatory($params, null, array('activity_id'));
  77. require_once 'CRM/Profile/Form.php';
  78. $errors = CRM_Profile_Form::validateContactActivityProfile( $params['activity_id'],
  79. $params['contact_id'],
  80. $params['profile_id'] );
  81. if ( !empty($errors) ) {
  82. return civicrm_api3_create_error(array_pop($errors));
  83. }
  84. $contactFields = $activityFields = array( );
  85. foreach ( $profileFields as $fieldName => $field ) {
  86. if ( CRM_Utils_Array::value('field_type', $field) == 'Activity' ) {
  87. $activityFields[$fieldName] = $field;
  88. } else {
  89. $contactFields[$fieldName] = $field;
  90. }
  91. }
  92. CRM_Core_BAO_UFGroup::setProfileDefaults($params['contact_id'], $contactFields, $values, true );
  93. if ( $params['activity_id'] ) {
  94. CRM_Core_BAO_UFGroup::setComponentDefaults( $activityFields, $params['activity_id'], 'Activity', $values, true );
  95. }
  96. } else {
  97. CRM_Core_BAO_UFGroup::setProfileDefaults( $params['contact_id'], $profileFields, $values, true );
  98. }
  99. $result = civicrm_api3_create_success( );
  100. $result['values'] = $values;
  101. return $result;
  102. }
  103. /**
  104. * Update Profile field values.
  105. *
  106. * @param array $params Associative array of property name/value
  107. * pairs to update profile field values
  108. *
  109. * @return Updated Contact/ Activity object|CRM_Error
  110. *
  111. * @todo add example
  112. * @todo add test cases
  113. *
  114. */
  115. function civicrm_api3_profile_set( $params ) {
  116. civicrm_api3_verify_mandatory($params, null, array('profile_id', 'contact_id'));
  117. if ( !CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $params['profile_id'], 'is_active' ) ) {
  118. return civicrm_api3_create_error('Invalid value for profile_id');
  119. }
  120. $isContactActivityProfile = CRM_Core_BAO_UFField::checkContactActivityProfileType( $params['profile_id'] );
  121. if ( CRM_Core_BAO_UFField::checkProfileType($params['profile_id']) && !$isContactActivityProfile ) {
  122. return civicrm_api3_create_error('Can not retrieve values for profiles include fields for more than one record type.' );
  123. }
  124. $contactParams = $activityParams = $missingParams = array( );
  125. $profileFields = CRM_Core_BAO_UFGroup::getFields( $params['profile_id'],
  126. false,
  127. null,
  128. null,
  129. null,
  130. false,
  131. null,
  132. true,
  133. null,
  134. CRM_Core_Permission::EDIT );
  135. if ( $isContactActivityProfile ) {
  136. civicrm_api3_verify_mandatory($params, null, array('activity_id'));
  137. require_once 'CRM/Profile/Form.php';
  138. $errors = CRM_Profile_Form::validateContactActivityProfile( $params['activity_id'],
  139. $params['contact_id'],
  140. $params['profile_id'] );
  141. if ( !empty($errors) ) {
  142. return civicrm_api3_create_error(array_pop($errors));
  143. }
  144. }
  145. foreach ( $profileFields as $fieldName => $field ) {
  146. if ( CRM_Utils_Array::value('is_required', $field) ) {
  147. if ( !CRM_Utils_Array::value($fieldName, $params) || empty($params[$fieldName]) ) {
  148. $missingParams[] = $fieldName;
  149. }
  150. }
  151. if ( !isset($params[$fieldName]) ) {
  152. continue;
  153. }
  154. $value = $params[$fieldName];
  155. if ( $params[$fieldName] && isset($params[$fieldName.'_id']) ) {
  156. $value = $params[$fieldName.'_id'];
  157. }
  158. if ( $isContactActivityProfile && CRM_Utils_Array::value('field_type', $field) == 'Activity' ) {
  159. $activityParams[$fieldName] = $value;
  160. } else {
  161. $contactParams[$fieldName] = $value;
  162. }
  163. }
  164. if ( !empty($missingParams) ) {
  165. return civicrm_api3_create_error("Missing required parameters for profile id {$params['profile_id']}: ". implode(', ', $missingParams) );
  166. }
  167. $contactParams['version'] = 3;
  168. $contactParams['contact_id'] = $params['contact_id'];
  169. $contactParams['profile_id'] = $params['profile_id'];
  170. $contactParams['skip_custom'] = 1;
  171. $contactProfileParams = civicrm_api3_profile_apply( $contactParams );
  172. if ( CRM_Utils_Array::value('is_error', $contactProfileParams) ) {
  173. return $contactProfileParams;
  174. }
  175. // Contact profile fields
  176. $profileParams = $contactProfileParams['values'];
  177. // If profile having activity fields
  178. if ( $isContactActivityProfile && !empty($activityParams) ) {
  179. $activityParams['id'] = $params['activity_id'];
  180. $profileParams['api.activity.create'] = $activityParams;
  181. }
  182. $groups = $tags = array( );
  183. if ( isset($profileParams['group']) ) {
  184. $groups = $profileParams['group'];
  185. unset($profileParams['group']);
  186. }
  187. if ( isset($profileParams['tag']) ) {
  188. $tags = $profileParams['tag'];
  189. unset($profileParams['tag']);
  190. }
  191. $result = civicrm_api('contact', 'create', $profileParams);
  192. if ( CRM_Utils_Array::value('is_error', $result) ) {
  193. return $result;
  194. }
  195. $ufGroupDetails = array( );
  196. $ufGroupParams = array( 'id' => $params['profile_id'] );
  197. CRM_Core_BAO_UFGroup::retrieve( $ufGroupParams, $ufGroupDetails );
  198. if ( isset($profileFields['group']) ) {
  199. CRM_Contact_BAO_GroupContact::create( $groups,
  200. $params['contact_id'],
  201. false,
  202. 'Admin' );
  203. }
  204. if ( isset($profileFields['tag']) ) {
  205. require_once 'CRM/Core/BAO/EntityTag.php';
  206. CRM_Core_BAO_EntityTag::create( $tags,
  207. 'civicrm_contact',
  208. $params['contact_id'] );
  209. }
  210. if ( CRM_Utils_Array::value('add_to_group_id', $ufGroupDetails) ) {
  211. $contactIds = array( $params['contact_id'] );
  212. CRM_Contact_BAO_GroupContact::addContactsToGroup( $contactIds,
  213. $ufGroupDetails['add_to_group_id'] );
  214. }
  215. return $result;
  216. }
  217. /**
  218. * Provide formatted values for profile fields.
  219. *
  220. * @param array $params Associative array of property name/value
  221. * pairs to profile field values
  222. *
  223. * @return formatted profile field values|CRM_Error
  224. *
  225. * @todo add example
  226. * @todo add test cases
  227. *
  228. */
  229. function civicrm_api3_profile_apply( $params ) {
  230. civicrm_api3_verify_mandatory($params, null, array('profile_id', 'contact_id'));
  231. require_once 'CRM/Contact/BAO/Contact.php';
  232. if ( !CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $params['profile_id'], 'is_active' ) ) {
  233. return civicrm_api3_create_error('Invalid value for profile_id');
  234. }
  235. $profileFields = CRM_Core_BAO_UFGroup::getFields( $params['profile_id'],
  236. false,
  237. null,
  238. null,
  239. null,
  240. false,
  241. null,
  242. true,
  243. null,
  244. CRM_Core_Permission::EDIT );
  245. list($data, $contactDetails) = CRM_Contact_BAO_Contact::formatProfileContactParams( $params,
  246. $profileFields,
  247. $params['contact_id'],
  248. $params['profile_id'],
  249. CRM_Utils_Array::value('contact_type', $params),
  250. CRM_Utils_Array::value('skip_custom', $params, false) );
  251. if ( empty($data) ) {
  252. return civicrm_api3_create_error('Enable to format profile parameters.');
  253. }
  254. return civicrm_api3_create_success( $data );
  255. }