PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/michaelmcandrew/cic
PHP | 421 lines | 143 code | 69 blank | 209 comment | 23 complexity | 9843da0bdd4e5b2ff3c46f49a4157573 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 User Profile Group 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/Core/BAO/UFGroup.php';
  44. /**
  45. * Most API functions take in associative arrays ( name => value pairs
  46. * as parameters. Some of the most commonly used parameters are
  47. * described below
  48. *
  49. * @param array $params an associative array used in construction
  50. * / retrieval of the object
  51. * @param array $returnProperties the limited set of object properties that
  52. * need to be returned to the caller
  53. *
  54. */
  55. /**
  56. * Get all the user framework groups
  57. *
  58. * @access public
  59. * @return array - array reference of all groups.
  60. * @static
  61. */
  62. function &crm_uf_get_profile_groups( ) {
  63. return CRM_Core_PseudoConstant::ufGroup( );
  64. }
  65. /**
  66. * Get the form title.
  67. *
  68. * @param int $id id of uf_form
  69. * @return string title
  70. *
  71. * @access public
  72. * @static
  73. *
  74. */
  75. function crm_uf_get_profile_title ( $id ) {
  76. return CRM_Core_BAO_UFGroup::getTitle( $id );
  77. }
  78. /**
  79. * get all the fields that belong to the group with the named title
  80. *
  81. * @param int $id the id of the UF group
  82. * @param int $register are we interested in registration fields
  83. * @param int $action what action are we doing
  84. * @param string $visibility visibility of fields we are interested in
  85. *
  86. * @return array the fields that belong to this title
  87. * @static
  88. * @access public
  89. */
  90. function crm_uf_get_profile_fields ( $id, $register = false, $action = null, $visibility = null ) {
  91. return CRM_Core_BAO_UFGroup::getFields( $id, $register, $action, null, $visibility );
  92. }
  93. /**
  94. * get the html for the form that represents this particular group
  95. *
  96. * @param int $userID the user id that we are actually editing
  97. * @param string $title the title of the group we are interested in
  98. * @param int $action the action of the form
  99. * @param boolean $register is this the registration form
  100. * @param boolean $reset should we reset the form?
  101. *
  102. * @return string the html for the form
  103. * @static
  104. * @access public
  105. */
  106. function crm_uf_get_profile_html ( $userID, $title, $action = null, $register = false, $reset = false ) {
  107. return CRM_Core_BAO_UFGroup::getEditHTML( $userID, $title, $action, $register, $reset );
  108. }
  109. /**
  110. * get the html for the form that represents this particular group
  111. *
  112. * @param int $userID the user id that we are actually editing
  113. * @param int $profileID the id of the group we are interested in
  114. * @param int $action the action of the form
  115. * @param boolean $register is this the registration form
  116. * @param boolean $reset should we reset the form?
  117. *
  118. * @return string the html for the form
  119. * @static
  120. * @access public
  121. */
  122. function crm_uf_get_profile_html_by_id ( $userID,
  123. $profileID,
  124. $action = null,
  125. $register = false,
  126. $reset = false ) {
  127. return CRM_Core_BAO_UFGroup::getEditHTML( $userID, null, $action, $register, $reset, $profileID );
  128. }
  129. /**
  130. * get the html for the form for profile creation
  131. *
  132. * @param boolean $reset should we reset the form?
  133. *
  134. * @return string the html for the form
  135. * @static
  136. * @access public
  137. */
  138. function crm_uf_get_create_html ( $reset = false ) {
  139. $session =& CRM_Core_Session::singleton( );
  140. $controller =& new CRM_Core_Controller_Simple( 'CRM_Profile_Form_Edit', '', CRM_Core_Action::ADD );
  141. if ( $reset ) {
  142. unset( $_POST['_qf_default'] );
  143. unset( $_REQUEST['_qf_default'] );
  144. }
  145. $controller->process( );
  146. $controller->setEmbedded( true );
  147. $controller->run( );
  148. $template =& CRM_Core_Smarty::singleton( );
  149. return trim( $template->fetch( 'CRM/Profile/Form/Dynamic.tpl' ) );
  150. }
  151. /**
  152. * get the contact_id given a uf_id
  153. *
  154. * @param int $ufID
  155. *
  156. * @return int contact_id
  157. * @access public
  158. * @static
  159. */
  160. function crm_uf_get_match_id ( $ufID ) {
  161. require_once 'CRM/Core/BAO/UFMatch.php';
  162. return CRM_Core_BAO_UFMatch::getContactId( $ufID );
  163. }
  164. /**
  165. * get the uf_id given a contact_id
  166. *
  167. * @param int $contactID
  168. *
  169. * @return int ufID
  170. * @access public
  171. * @static
  172. */
  173. function crm_uf_get_uf_id ( $contactID ) {
  174. require_once 'CRM/Core/BAO/UFMatch.php';
  175. return CRM_Core_BAO_UFMatch::getUFId( $contactID );
  176. }
  177. /*******************************************************************/
  178. /**
  179. * Use this API to create a new group. See the CRM Data Model for uf_group property definitions
  180. *
  181. * @param $params array Associative array of property name/value pairs to insert in group.
  182. *
  183. * @return Newly create uf_group object
  184. *
  185. * @access public
  186. */
  187. function crm_create_uf_group( $params ) {
  188. _crm_initialize( );
  189. if(! is_array($params) || ! isset($params['title']) ) {
  190. return _crm_error("params is not an array or may be empty array ");
  191. }
  192. $ids = array();
  193. require_once 'CRM/Core/BAO/UFGroup.php';
  194. return CRM_Core_BAO_UFGroup::add( $params,$ids );
  195. }
  196. /**
  197. * Use this API to update group. See the CRM Data Model for uf_group property definitions
  198. *
  199. * @param $params array Associative array of property name/value pairs to insert in group.
  200. *
  201. * @param $ufGroup Object A valid UF Group object that to be updated.
  202. *
  203. * @return updated uf_group object
  204. *
  205. * @access public
  206. */
  207. function crm_update_uf_group( $params ,$ufGroup) {
  208. _crm_initialize( );
  209. $groupId = $ufGroup->id;
  210. if(! is_array( $params ) ) {
  211. return _crm_error("params is not an array ");
  212. }
  213. if(! isset( $groupId ) ) {
  214. return _crm_error("parameter $groupId is not set ");
  215. }
  216. $ids = array();
  217. $ids['ufgroup'] = $groupId;
  218. require_once 'CRM/Core/BAO/UFGroup.php';
  219. return CRM_Core_BAO_UFGroup::add( $params ,$ids );
  220. }
  221. /**
  222. * Defines 'uf field' within a group.
  223. *
  224. * @param $UFGroup object Valid uf_group object
  225. *
  226. * @param $params array Associative array of property name/value pairs to create new uf field.
  227. *
  228. * @return Newly created custom_field object
  229. *
  230. * @access public
  231. *
  232. */
  233. function crm_create_uf_field( $UFGroup , $params ) {
  234. _crm_initialize( );
  235. if(! isset($UFGroup->id) ) {
  236. return _crm_error("id is not set in uf_group object");
  237. }
  238. $field_type = $params['field_type'];
  239. $field_name = $params['field_name'];
  240. $location_type_id = $params['location_type_id'];
  241. $phone_type = $params['phone_type'];
  242. $params['field_name'] = array( $field_type, $field_name, $location_type_id, $phone_type);
  243. if(! is_array( $params ) || $params['field_name'][1] == null || $params['weight'] == null ) {
  244. return _crm_error("missing required fields ");
  245. }
  246. if ( !( CRM_Utils_Array::value('group_id', $params) ) ) {
  247. $params['group_id'] = $UFGroup->id;
  248. }
  249. $ids = array();
  250. $ids['uf_group'] = $UFGroup->id;
  251. require_once 'CRM/Core/BAO/UFField.php';
  252. if (CRM_Core_BAO_UFField::duplicateField($params, $ids) ) {
  253. return _crm_error("The field was not added. It already exists in this profile.");
  254. }
  255. return CRM_Core_BAO_UFField::add( $params , $ids );
  256. }
  257. /**
  258. * Use this API to update uf field . See the CRM Data Model for uf_field property definitions
  259. *
  260. * @param $params array Associative array of property name/value pairs to update in field.
  261. *
  262. * @param $ufField Object A valid uf field object that to be updated.
  263. *
  264. * @return updated uf_field object
  265. *
  266. * @access public
  267. */
  268. function crm_update_uf_field( $params , $ufField) {
  269. _crm_initialize( );
  270. $fieldId = $ufField->id;
  271. if(! isset( $fieldId ) ) {
  272. return _crm_error("parameter fieldId is not set");
  273. }
  274. if(! is_array( $params ) ) {
  275. return _crm_error("params is not an array ");
  276. }
  277. $field_type = $params['field_type'];
  278. $field_name = $params['field_name'];
  279. $location_type_id = $params['location_type_id'];
  280. $phone_type = $params['phone_type'];
  281. $params['field_name'] = array( $field_type, $field_name, $location_type_id, $phone_type);
  282. require_once 'CRM/Core/BAO/UFField.php';
  283. $UFField = &new CRM_core_BAO_UFField();
  284. $UFField->id = $fieldId;
  285. if ( !( CRM_Utils_Array::value('group_id', $params) ) && $UFField->find(true) ) {
  286. $params['group_id'] = $UFField->uf_group_id;
  287. }
  288. $ids = array();
  289. if ( $UFField->find(true) ) {
  290. $ids['uf_group'] = $UFField->uf_group_id;
  291. } else {
  292. return _crm_error("there is no field for this fieldId");
  293. }
  294. $ids['uf_field'] = $fieldId;
  295. if (CRM_Core_BAO_UFField::duplicateField($params, $ids) ) {
  296. return _crm_error("The field was not added. It already exists in this profile.");
  297. }
  298. return CRM_Core_BAO_UFField::add( $params , $ids );
  299. }
  300. /**
  301. * Delete uf group
  302. *
  303. * @param $ufGroup Object Valid uf_group object that to be deleted
  304. *
  305. * @return true on successful delete or return error
  306. *
  307. * @access public
  308. *
  309. */
  310. function crm_delete_uf_group( $ufGroup ) {
  311. _crm_initialize( );
  312. $groupId = $ufGroup->id;
  313. if(! isset( $groupId ) ) {
  314. return _crm_error("parameter $groupId is not set ");
  315. }
  316. require_once 'CRM/Core/BAO/UFGroup.php';
  317. return CRM_Core_BAO_UFGroup::del($groupId);
  318. }
  319. /**
  320. * Delete uf field
  321. *
  322. * @param $ufField Object Valid uf_field object that to be deleted
  323. *
  324. * @return true on successful delete or return error
  325. *
  326. * @access public
  327. *
  328. */
  329. function crm_delete_uf_field( $ufField ) {
  330. _crm_initialize( );
  331. $fieldId = $ufField->id;
  332. if(! isset( $fieldId ) ) {
  333. return _crm_error("parameter $fieldId is not set ");
  334. }
  335. require_once 'CRM/Core/BAO/UFField.php';
  336. return CRM_Core_BAO_UFField::del($fieldId);
  337. }
  338. /**
  339. * check the data validity
  340. *
  341. * @param int $userID the user id
  342. * @param string $title the title of the group we are interested in
  343. * @param boolean $register is this the registrtion form
  344. * @param int $action the action of the form
  345. *
  346. * @return error if data not valid
  347. *
  348. * @access public
  349. */
  350. function crm_validate_profile_html($userID, $title, $action = null, $register = false) {
  351. return CRM_Core_BAO_UFGroup::isValid( $userID, $title, $register, $action );
  352. }