3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * File for the CiviCRM APIv3 user framework group functions
31 * @package CiviCRM_APIv3
34 * @copyright CiviCRM LLC (c) 2004-2014
35 * @version $Id: UFField.php 30171 2010-10-14 09:11:27Z mover $
39 * Defines 'uf field' within a group.
41 * @param array $params
42 * Array per getfields metadata.
44 * @throws API_Exception
47 * Newly created $ufFieldArray
49 function civicrm_api3_uf_field_create($params) {
50 civicrm_api3_verify_one_mandatory($params, NULL, array('field_name', 'uf_group_id'));
51 $groupId = CRM_Utils_Array
::value('uf_group_id', $params);
52 if ((int) $groupId < 1) {
53 throw new API_Exception('Params must be a field_name-carrying array and a positive integer.');
56 $field_type = CRM_Utils_Array
::value('field_type', $params);
57 $field_name = CRM_Utils_Array
::value('field_name', $params);
58 $location_type_id = CRM_Utils_Array
::value('location_type_id', $params);
59 $phone_type = CRM_Utils_Array
::value('phone_type_id', $params, CRM_Utils_Array
::value('phone_type', $params));
61 if (!CRM_Core_BAO_UFField
::isValidFieldName($field_name)) {
62 throw new API_Exception('The field_name is not valid');
64 $params['field_name'] = array($field_type, $field_name, $location_type_id, $phone_type);
66 if (!(CRM_Utils_Array
::value('group_id', $params))) {
67 $params['group_id'] = $groupId;
70 $ids = $ufFieldArray = array();
71 $ids['uf_group'] = $groupId;
73 $fieldId = CRM_Utils_Array
::value('id', $params);
74 if (!empty($fieldId)) {
75 $UFField = new CRM_Core_BAO_UFField();
76 $UFField->id
= $fieldId;
77 if ($UFField->find(TRUE)) {
78 $ids['uf_group'] = $UFField->uf_group_id
;
79 if (!(CRM_Utils_Array
::value('group_id', $params))) {
80 // this copied here from previous api function - not sure if required
81 $params['group_id'] = $UFField->uf_group_id
;
85 throw new API_Exception("there is no field for this fieldId");
87 $ids['uf_field'] = $fieldId;
90 if (CRM_Core_BAO_UFField
::duplicateField($params, $ids)) {
91 throw new API_Exception("The field was not added. It already exists in this profile.");
93 //@todo why is this even optional? Surely weight should just be 'managed' ??
94 if (CRM_Utils_Array
::value('option.autoweight', $params, TRUE)) {
95 $params['weight'] = CRM_Core_BAO_UFField
::autoWeight($params);
97 $ufField = CRM_Core_BAO_UFField
::add($params, $ids);
99 $fieldsType = CRM_Core_BAO_UFGroup
::calculateGroupType($groupId, TRUE);
100 CRM_Core_BAO_UFGroup
::updateGroupTypes($groupId, $fieldsType);
102 _civicrm_api3_object_to_array($ufField, $ufFieldArray[$ufField->id
]);
103 civicrm_api3('profile', 'getfields', array('cache_clear' => TRUE));
104 return civicrm_api3_create_success($ufFieldArray, $params);
108 * Adjust metadata for civicrm_uf_field create.
110 * @param array $params
112 function _civicrm_api3_uf_field_create_spec(&$params) {
113 $params['option.autoweight'] = array(
114 'title' => "Automatically adjust weights in UFGroup to align with UFField",
115 'type' => CRM_Utils_Type
::T_BOOLEAN
,
116 'api.default' => TRUE,
118 $params['created_id']['api.default'] = 'user_contact_id';
119 $params['created_id']['title'] = 'Created By';
120 $params['is_active']['api.default'] = TRUE;
124 * Returns array of uf groups (profiles) matching a set of one or more group properties.
126 * @param array $params
127 * Array per getfields metadata.
131 function civicrm_api3_uf_field_get($params) {
132 return _civicrm_api3_basic_get('CRM_Core_BAO_UFField', $params);
138 * @param array $params
140 * @throws API_Exception
144 function civicrm_api3_uf_field_delete($params) {
145 $fieldId = $params['id'];
147 $ufGroupId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFField', $fieldId, 'uf_group_id');
149 throw new API_Exception('Invalid value for field_id.');
152 $result = CRM_Core_BAO_UFField
::del($fieldId);
154 $fieldsType = CRM_Core_BAO_UFGroup
::calculateGroupType($ufGroupId, TRUE);
155 CRM_Core_BAO_UFGroup
::updateGroupTypes($ufGroupId, $fieldsType);
157 return civicrm_api3_create_success($result, $params);
161 * Field id accepted for backward compatibility - unset required on id.
163 * @param array $params
165 function _civicrm_api3_uf_field_delete_spec(&$params) {
166 // legacy support for field_id
167 $params['id']['api.aliases'] = array('field_id');