2 // $Id: UFGroup.php 45502 2013-02-08 13:32:55Z kurund $
6 +--------------------------------------------------------------------+
7 | CiviCRM version 4.3 |
8 +--------------------------------------------------------------------+
9 | Copyright CiviCRM LLC (c) 2004-2013 |
10 +--------------------------------------------------------------------+
11 | This file is a part of CiviCRM. |
13 | CiviCRM is free software; you can copy, modify, and distribute it |
14 | under the terms of the GNU Affero General Public License |
15 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
17 | CiviCRM is distributed in the hope that it will be useful, but |
18 | WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
20 | See the GNU Affero General Public License for more details. |
22 | You should have received a copy of the GNU Affero General Public |
23 | License and the CiviCRM Licensing Exception along |
24 | with this program; if not, contact CiviCRM LLC |
25 | at info[AT]civicrm[DOT]org. If you have questions about the |
26 | GNU Affero General Public License or the licensing of CiviCRM, |
27 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
28 +--------------------------------------------------------------------+
32 * File for the CiviCRM APIv2 user framework group functions
34 * @package CiviCRM_APIv2
37 * @copyright CiviCRM LLC (c) 2004-2013
38 * @version $Id: UFGroup.php 45502 2013-02-08 13:32:55Z kurund $
43 * Files required for this package
45 require_once 'api/v2/utils.php';
46 require_once 'CRM/Core/BAO/UFGroup.php';
49 * Most API functions take in associative arrays ( name => value pairs
50 * as parameters. Some of the most commonly used parameters are
53 * @param array $params an associative array used in construction
54 * / retrieval of the object
55 * @param array $returnProperties the limited set of object properties that
56 * need to be returned to the caller
61 * Get all the user framework groups
65 * @return array - array reference of all groups.
68 function &civicrm_uf_profile_groups_get() {
69 return CRM_Core_PseudoConstant
::ufGroup();
75 * @param int $id id of uf_form
77 * @return string title
83 function civicrm_uf_profile_title_get($id) {
85 return CRM_Core_BAO_UFGroup
::getTitle($id);
88 return civicrm_create_error('Param needs to be a positive integer.');
93 * get all the fields that belong to the group with the named title
95 * @param int $id the id of the UF group
96 * @param int $register are we interested in registration fields
97 * @param int $action what action are we doing
98 * @param string $visibility visibility of fields we are interested in
100 * @return array the fields that belong to this title
104 function civicrm_uf_profile_fields_get($id, $register = FALSE, $action = NULL, $visibility = NULL) {
106 return CRM_Core_BAO_UFGroup
::getFields($id, $register, $action, NULL, $visibility, FALSE, NULL, TRUE);
109 return civicrm_create_error('Param needs to be a positive integer.');
114 * get the html for the form that represents this particular group
116 * @param int $userID the user id that we are actually editing
117 * @param string $title the title of the group we are interested in
118 * @param int $action the action of the form
119 * @param boolean $register is this the registration form
120 * @param boolean $reset should we reset the form?
122 * @return string the html for the form
126 function civicrm_uf_profile_html_get($userID, $title, $action = NULL, $register = FALSE, $reset = FALSE) {
127 if ((int) $userID > 0 and is_string($title)) {
128 return CRM_Core_BAO_UFGroup
::getEditHTML($userID, $title, $action, $register, $reset);
131 return civicrm_create_error('Params need to be a positive integer and a string.');
136 * get the html for the form that represents this particular group
138 * @param int $userID the user id that we are actually editing
139 * @param int $profileID the id of the group we are interested in
140 * @param int $action the action of the form
141 * @param boolean $register is this the registration form
142 * @param boolean $reset should we reset the form?
144 * @return string the html for the form
148 function civicrm_uf_profile_html_by_id_get($userID, $profileID, $action = NULL, $register = FALSE, $reset = FALSE) {
149 if ((int) $userID > 0 and (int) $profileID > 0) {
150 return CRM_Core_BAO_UFGroup
::getEditHTML($userID, NULL, $action, $register, $reset, $profileID);
153 return civicrm_create_error('Params need to be positive integers.');
158 * get the html for the form for profile creation
160 * @param int $gid group id
161 * @param boolean $reset should we reset the form?
163 * @return string the html for the form
167 function civicrm_uf_create_html_get($gid, $reset = FALSE) {
168 if ((int) $gid < 1) {
169 return civicrm_create_error('Param needs to be a positive integer.');
172 require_once 'CRM/Core/Controller/Simple.php';
173 $session = CRM_Core_Session
::singleton();
174 $controller = new CRM_Core_Controller_Simple('CRM_Profile_Form_Edit', '', CRM_Core_Action
::ADD
);
176 unset($_POST['_qf_default']);
177 unset($_REQUEST['_qf_default']);
179 $controller->set('gid', $gid);
180 $controller->set('skipPermission', 1);
181 $controller->process();
182 $controller->setEmbedded(TRUE);
185 $template = CRM_Core_Smarty
::singleton();
186 return trim($template->fetch('CRM/Profile/Form/Dynamic.tpl'));
190 * get the contact_id given a uf_id
194 * @return int contact_id
198 function civicrm_uf_match_id_get($ufID) {
199 if ((int) $ufID > 0) {
200 require_once 'CRM/Core/BAO/UFMatch.php';
201 return CRM_Core_BAO_UFMatch
::getContactId($ufID);
204 return civicrm_create_error('Param needs to be a positive integer.');
209 * get the uf_id given a contact_id
211 * @param int $contactID
217 function civicrm_uf_id_get($contactID) {
218 if ((int) $contactID > 0) {
219 require_once 'CRM/Core/BAO/UFMatch.php';
220 return CRM_Core_BAO_UFMatch
::getUFId($contactID);
223 return civicrm_create_error('Param needs to be a positive integer.');
227 /*******************************************************************/
230 * Use this API to create a new group. See the CRM Data Model for uf_group property definitions
232 * @param $params array Associative array of property name/value pairs to insert in group.
234 * @return Newly create $ufGroupArray array
238 function civicrm_uf_group_create($params) {
239 if (!is_array($params) or empty($params) or !isset($params['title'])) {
240 return civicrm_create_error('Params must be an array and have a title field.');
243 _civicrm_initialize();
246 require_once 'CRM/Core/BAO/UFGroup.php';
248 $ufGroup = CRM_Core_BAO_UFGroup
::add($params, $ids);
249 _civicrm_object_to_array($ufGroup, $ufGroupArray);
251 return $ufGroupArray;
255 * Use this API to update group. See the CRM Data Model for uf_group property definitions
257 * @param $params array Associative array of property name/value pairs to insert in group.
259 * @param $groupId int A valid UF Group ID that to be updated.
261 * @return updated $ufGroupArray array
265 function civicrm_uf_group_update($params, $groupId) {
266 if (!is_array($params) or empty($params) or (int) $groupId < 1) {
267 return civicrm_create_error('Params must be a non-empty array and a positive integer.');
270 _civicrm_initialize();
273 $ids['ufgroup'] = $groupId;
275 require_once 'CRM/Core/BAO/UFGroup.php';
277 $ufGroup = CRM_Core_BAO_UFGroup
::add($params, $ids);
278 _civicrm_object_to_array($ufGroup, $ufGroupArray);
280 return $ufGroupArray;
284 * Defines 'uf field' within a group.
286 * @param $groupId int Valid uf_group id
288 * @param $params array Associative array of property name/value pairs to create new uf field.
290 * @return Newly created $ufFieldArray array
295 function civicrm_uf_field_create($groupId, $params) {
296 if (!is_array($params) or !isset($params['field_name']) or (int) $groupId < 1) {
297 return civicrm_create_error('Params must be a field_name-carrying array and a positive integer.');
300 _civicrm_initialize();
302 $field_type = CRM_Utils_Array
::value('field_type', $params);
303 $field_name = CRM_Utils_Array
::value('field_name', $params);
304 $location_type_id = CRM_Utils_Array
::value('location_type_id', $params);
305 $phone_type = CRM_Utils_Array
::value('phone_type', $params);
307 $params['field_name'] = array($field_type, $field_name, $location_type_id, $phone_type);
309 if (!(CRM_Utils_Array
::value('group_id', $params))) {
310 $params['group_id'] = $groupId;
314 $ids['uf_group'] = $groupId;
316 require_once 'CRM/Core/BAO/UFField.php';
317 if (CRM_Core_BAO_UFField
::duplicateField($params, $ids)) {
318 return civicrm_create_error("The field was not added. It already exists in this profile.");
321 $params['weight'] = CRM_Core_BAO_UFField
::autoWeight($params);
322 $ufField = CRM_Core_BAO_UFField
::add($params, $ids);
323 _civicrm_object_to_array($ufField, $ufFieldArray);
325 return $ufFieldArray;
329 * Use this API to update uf field . See the CRM Data Model for uf_field property definitions
331 * @param $params array Associative array of property name/value pairs to update in field.
333 * @param $fieldId int A valid uf field id that to be updated.
335 * @return updated $ufFieldArray array
339 function civicrm_uf_field_update($params, $fieldId) {
341 _civicrm_initialize();
343 if (!isset($fieldId)) {
344 return civicrm_create_error("parameter fieldId is not set");
347 if (!is_array($params)) {
348 return civicrm_create_error("params is not an array ");
351 $field_type = CRM_Utils_Array
::value('field_type', $params);
352 $field_name = CRM_Utils_Array
::value('field_name', $params);
353 $location_type_id = CRM_Utils_Array
::value('location_type_id', $params);
354 $phone_type = CRM_Utils_Array
::value('phone_type', $params);
356 $params['field_name'] = array($field_type, $field_name, $location_type_id, $phone_type);
358 require_once 'CRM/Core/BAO/UFField.php';
359 $UFField = new CRM_core_BAO_UFField();
360 $UFField->id
= $fieldId;
362 if (!(CRM_Utils_Array
::value('group_id', $params)) && $UFField->find(TRUE)) {
363 $params['group_id'] = $UFField->uf_group_id
;
368 if ($UFField->find(TRUE)) {
369 $ids['uf_group'] = $UFField->uf_group_id
;
372 return civicrm_create_error("there is no field for this fieldId");
374 $ids['uf_field'] = $fieldId;
376 if (CRM_Core_BAO_UFField
::duplicateField($params, $ids)) {
377 return civicrm_create_error("The field was not added. It already exists in this profile.");
380 $params['weight'] = CRM_Core_BAO_UFField
::autoWeight($params);
381 $ufField = CRM_Core_BAO_UFField
::add($params, $ids);
382 _civicrm_object_to_array($ufField, $ufFieldArray);
384 return $ufFieldArray;
390 * @param $groupId int Valid uf_group id that to be deleted
392 * @return true on successful delete or return error
397 function civicrm_uf_group_delete($groupId) {
398 _civicrm_initialize();
400 if (!isset($groupId)) {
401 return civicrm_create_error("provide a valid groupId.");
404 require_once 'CRM/Core/BAO/UFGroup.php';
405 return CRM_Core_BAO_UFGroup
::del($groupId);
411 * @param $fieldId int Valid uf_field id that to be deleted
413 * @return true on successful delete or return error
418 function civicrm_uf_field_delete($fieldId) {
419 _civicrm_initialize();
421 if (!isset($fieldId)) {
422 return civicrm_create_error("provide a valid fieldId.");
425 require_once 'CRM/Core/BAO/UFField.php';
426 return CRM_Core_BAO_UFField
::del($fieldId);
430 * check the data validity
432 * @param int $userID the user id
433 * @param string $title the title of the group we are interested in
434 * @param boolean $register is this the registrtion form
435 * @param int $action the action of the form
437 * @return error if data not valid
441 function civicrm_profile_html_validate($userID, $title, $action = NULL, $register = FALSE) {
442 return CRM_Core_BAO_UFGroup
::isValid($userID, $title, $register, $action);
446 * used to edit uf field
448 * @param array as key value pair
451 * @return error if updation fails else array of updated data
455 function civicrm_uf_group_weight($params) {
456 unset($params['fnName']);
457 require_once 'CRM/Core/DAO/UFField.php';
458 foreach ($params as $key => $value) {
459 $value['is_active'] = 1;
460 $result[] = civicrm_uf_field_update($value, $key);