Merge pull request #7181 from eileenmcnaughton/CRM-17335-2
[civicrm-core.git] / api / v3 / UFField.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
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/**
c28e1768 29 * This api exposes CiviCRM profile field.
6a488035
TO
30 *
31 * @package CiviCRM_APIv3
6a488035
TO
32 */
33
6a488035
TO
34/**
35 * Defines 'uf field' within a group.
36 *
16b10e64 37 * @param array $params
2e66abf8 38 * Array per getfields metadata.
6a488035 39 *
7e42d294 40 * @throws API_Exception
7e42d294 41 *
a6c01b45 42 * @return array
72b3a70c 43 * Newly created $ufFieldArray
6a488035
TO
44 */
45function civicrm_api3_uf_field_create($params) {
8114c378 46 // CRM-14756: kind of a hack-ish fix. If the user gives the id, uf_group_id is retrieved and then set.
3cb163df 47 if (isset($params['id'])) {
8114c378
SB
48 $groupId = civicrm_api3('UFField', 'getvalue', array(
49 'return' => 'uf_group_id',
50 'id' => $params['id'],
51 ));
52 }
53 else {
54 $groupId = CRM_Utils_Array::value('uf_group_id', $params);
6a488035
TO
55 }
56
57 $field_type = CRM_Utils_Array::value('field_type', $params);
58 $field_name = CRM_Utils_Array::value('field_name', $params);
1a228b4c 59 $location_type_id = CRM_Utils_Array::value('location_type_id', $params, CRM_Utils_Array::value('website_type_id', $params));
6a488035
TO
60 $phone_type = CRM_Utils_Array::value('phone_type_id', $params, CRM_Utils_Array::value('phone_type', $params));
61
0103eaf0 62 if (strpos($field_name, 'formatting') !== 0 && !CRM_Core_BAO_UFField::isValidFieldName($field_name)) {
3f1a97cd 63 throw new API_Exception('The field_name is not valid');
6a488035
TO
64 }
65 $params['field_name'] = array($field_type, $field_name, $location_type_id, $phone_type);
66
67 if (!(CRM_Utils_Array::value('group_id', $params))) {
68 $params['group_id'] = $groupId;
69 }
70
7e42d294 71 $ids = $ufFieldArray = array();
6a488035
TO
72 $ids['uf_group'] = $groupId;
73
74 $fieldId = CRM_Utils_Array::value('id', $params);
75 if (!empty($fieldId)) {
575e044f 76 $UFField = new CRM_Core_BAO_UFField();
6a488035
TO
77 $UFField->id = $fieldId;
78 if ($UFField->find(TRUE)) {
79 $ids['uf_group'] = $UFField->uf_group_id;
80 if (!(CRM_Utils_Array::value('group_id', $params))) {
81 // this copied here from previous api function - not sure if required
82 $params['group_id'] = $UFField->uf_group_id;
83 }
84 }
85 else {
3f1a97cd 86 throw new API_Exception("there is no field for this fieldId");
6a488035
TO
87 }
88 $ids['uf_field'] = $fieldId;
89 }
90
91 if (CRM_Core_BAO_UFField::duplicateField($params, $ids)) {
3f1a97cd 92 throw new API_Exception("The field was not added. It already exists in this profile.");
6a488035 93 }
7e42d294 94 //@todo why is this even optional? Surely weight should just be 'managed' ??
6a488035
TO
95 if (CRM_Utils_Array::value('option.autoweight', $params, TRUE)) {
96 $params['weight'] = CRM_Core_BAO_UFField::autoWeight($params);
97 }
3cb163df 98 $ufField = CRM_Core_BAO_UFField::add($params);
6a488035
TO
99
100 $fieldsType = CRM_Core_BAO_UFGroup::calculateGroupType($groupId, TRUE);
101 CRM_Core_BAO_UFGroup::updateGroupTypes($groupId, $fieldsType);
102
103 _civicrm_api3_object_to_array($ufField, $ufFieldArray[$ufField->id]);
7e42d294 104 civicrm_api3('profile', 'getfields', array('cache_clear' => TRUE));
6a488035
TO
105 return civicrm_api3_create_success($ufFieldArray, $params);
106}
107
108/**
9d32e6f7 109 * Adjust metadata for civicrm_uf_field create.
6a488035 110 *
c490a46a 111 * @param array $params
6a488035
TO
112 */
113function _civicrm_api3_uf_field_create_spec(&$params) {
8114c378
SB
114 $params['field_name']['api.required'] = TRUE;
115 $params['uf_group_id']['api.required'] = TRUE;
116
6a488035 117 $params['option.autoweight'] = array(
b2ed8e73
CW
118 'title' => "Auto Weight",
119 'description' => "Automatically adjust weights in UFGroup to align with UFField",
7e42d294 120 'type' => CRM_Utils_Type::T_BOOLEAN,
121 'api.default' => TRUE,
6a488035 122 );
6616c349 123 $params['is_active']['api.default'] = TRUE;
6a488035
TO
124}
125
126/**
22242c87 127 * Returns array of uf groups (profiles) matching a set of one or more group properties.
6a488035 128 *
cf470720 129 * @param array $params
9d32e6f7 130 * Array per getfields metadata.
6a488035 131 *
a6c01b45 132 * @return array
6a488035
TO
133 */
134function civicrm_api3_uf_field_get($params) {
135 return _civicrm_api3_basic_get('CRM_Core_BAO_UFField', $params);
136}
137
138/**
9d32e6f7 139 * Delete uf field.
6a488035 140 *
c490a46a 141 * @param array $params
7e42d294 142 *
143 * @throws API_Exception
6a488035 144 *
72b3a70c 145 * @return array
6a488035
TO
146 */
147function civicrm_api3_uf_field_delete($params) {
148 $fieldId = $params['id'];
149
150 $ufGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFField', $fieldId, 'uf_group_id');
151 if (!$ufGroupId) {
3f1a97cd 152 throw new API_Exception('Invalid value for field_id.');
6a488035
TO
153 }
154
155 $result = CRM_Core_BAO_UFField::del($fieldId);
156
157 $fieldsType = CRM_Core_BAO_UFGroup::calculateGroupType($ufGroupId, TRUE);
158 CRM_Core_BAO_UFGroup::updateGroupTypes($ufGroupId, $fieldsType);
159
160 return civicrm_api3_create_success($result, $params);
161}
645ee340 162
7e42d294 163/**
22242c87
EM
164 * Field id accepted for backward compatibility - unset required on id.
165 *
d0997921 166 * @param array $params
6a488035
TO
167 */
168function _civicrm_api3_uf_field_delete_spec(&$params) {
169 // legacy support for field_id
170 $params['id']['api.aliases'] = array('field_id');
171}