CRM-13234 define type for 'Country' custom fields (integer) as e-Notice was picked...
[civicrm-core.git] / api / v3 / UFField.php
CommitLineData
6a488035 1<?php
6a488035
TO
2
3/*
4 +--------------------------------------------------------------------+
232624b1 5 | CiviCRM version 4.4 |
6a488035
TO
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29/**
30 * File for the CiviCRM APIv3 user framework group functions
31 *
32 * @package CiviCRM_APIv3
33 * @subpackage API_UF
34 *
35 * @copyright CiviCRM LLC (c) 2004-2013
36 * @version $Id: UFField.php 30171 2010-10-14 09:11:27Z mover $
37 *
38 */
39
6a488035
TO
40/**
41 * Defines 'uf field' within a group.
42 *
43 * @param $groupId int Valid uf_group id
44 *
45 * @param $params array Associative array of property name/value pairs to create new uf field.
46 *
47 * @return Newly created $ufFieldArray array
48 *
49 * @access public
50 * {@getfields UFField_create}
51 * @example UFFieldCreate.php
52 */
53function civicrm_api3_uf_field_create($params) {
54 civicrm_api3_verify_one_mandatory($params, NULL, array('field_name', 'uf_group_id'));
55 $groupId = CRM_Utils_Array::value('uf_group_id', $params);
56 if ((int) $groupId < 1) {
57 return civicrm_api3_create_error('Params must be a field_name-carrying array and a positive integer.');
58 }
59
60 $field_type = CRM_Utils_Array::value('field_type', $params);
61 $field_name = CRM_Utils_Array::value('field_name', $params);
62 $location_type_id = CRM_Utils_Array::value('location_type_id', $params);
63 $phone_type = CRM_Utils_Array::value('phone_type_id', $params, CRM_Utils_Array::value('phone_type', $params));
64
65 if (! CRM_Core_BAO_UFField::isValidFieldName($field_name)) {
66 return civicrm_api3_create_error('The field_name is not valid');
67 }
68 $params['field_name'] = array($field_type, $field_name, $location_type_id, $phone_type);
69
70 if (!(CRM_Utils_Array::value('group_id', $params))) {
71 $params['group_id'] = $groupId;
72 }
73
74 $ids = array();
75 $ids['uf_group'] = $groupId;
76
77 $fieldId = CRM_Utils_Array::value('id', $params);
78 if (!empty($fieldId)) {
79 $UFField = new CRM_core_BAO_UFField();
80 $UFField->id = $fieldId;
81 if ($UFField->find(TRUE)) {
82 $ids['uf_group'] = $UFField->uf_group_id;
83 if (!(CRM_Utils_Array::value('group_id', $params))) {
84 // this copied here from previous api function - not sure if required
85 $params['group_id'] = $UFField->uf_group_id;
86 }
87 }
88 else {
89 return civicrm_api3_create_error("there is no field for this fieldId");
90 }
91 $ids['uf_field'] = $fieldId;
92 }
93
94 if (CRM_Core_BAO_UFField::duplicateField($params, $ids)) {
95 return civicrm_api3_create_error("The field was not added. It already exists in this profile.");
96 }
97 if (CRM_Utils_Array::value('option.autoweight', $params, TRUE)) {
98 $params['weight'] = CRM_Core_BAO_UFField::autoWeight($params);
99 }
100 $ufField = CRM_Core_BAO_UFField::add($params, $ids);
101
102 $fieldsType = CRM_Core_BAO_UFGroup::calculateGroupType($groupId, TRUE);
103 CRM_Core_BAO_UFGroup::updateGroupTypes($groupId, $fieldsType);
104
105 _civicrm_api3_object_to_array($ufField, $ufFieldArray[$ufField->id]);
106 return civicrm_api3_create_success($ufFieldArray, $params);
107}
108
109/**
110 * Gets field for civicrm_uf_field create
111 *
112 * @return array fields valid for other functions
113 */
114function _civicrm_api3_uf_field_create_spec(&$params) {
115 $params['option.autoweight'] = array(
116 'title' => "Automatically adjust weights in UFGroup to align with UFField",
b29e2e8b 117 'type' => CRM_Utils_Type::T_BOOLEAN
6a488035
TO
118 );
119}
120
121/**
122 * Returns array of uf groups (profiles) matching a set of one or more group properties
123 *
124 * @param array $params (reference) Array of one or more valid
125 * property_name=>value pairs. If $params is set
126 * as null, all surveys will be returned
127 *
128 * @return array (reference) Array
129 * {@getfields UFField_get
130 * @example UFFieldGet.php
131 * @access public
132 */
133function civicrm_api3_uf_field_get($params) {
134 return _civicrm_api3_basic_get('CRM_Core_BAO_UFField', $params);
135}
136
137/**
138 * Delete uf field
139 *
140 * @param $fieldId int Valid uf_field id that to be deleted
141 *
142 * @return true on successful delete or return error
143 *
144 * @access public
145 * {@getfields UFField_delete}
146 * @example UFFieldDelete.php
147 */
148function civicrm_api3_uf_field_delete($params) {
149 $fieldId = $params['id'];
150
151 $ufGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFField', $fieldId, 'uf_group_id');
152 if (!$ufGroupId) {
153 return civicrm_api3_create_error('Invalid value for field_id.');
154 }
155
156 $result = CRM_Core_BAO_UFField::del($fieldId);
157
158 $fieldsType = CRM_Core_BAO_UFGroup::calculateGroupType($ufGroupId, TRUE);
159 CRM_Core_BAO_UFGroup::updateGroupTypes($ufGroupId, $fieldsType);
160
161 return civicrm_api3_create_success($result, $params);
162}
1c88e578 163/*
6a488035
TO
164 * field id accepted for backward compat - unset required on id
165 */
166function _civicrm_api3_uf_field_delete_spec(&$params) {
167 // legacy support for field_id
168 $params['id']['api.aliases'] = array('field_id');
169}
170