more comment fixes
[civicrm-core.git] / api / v3 / UFField.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
731a0992 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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/**
29 * File for the CiviCRM APIv3 user framework group functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_UF
33 *
731a0992 34 * @copyright CiviCRM LLC (c) 2004-2014
6a488035 35 * @version $Id: UFField.php 30171 2010-10-14 09:11:27Z mover $
6a488035
TO
36 */
37
6a488035
TO
38/**
39 * Defines 'uf field' within a group.
40 *
16b10e64 41 * @param array $params
cf470720 42 * Array Associative array of property name/value pairs to create new uf field.
6a488035 43 *
7e42d294 44 * @throws API_Exception
7e42d294 45 *
a6c01b45 46 * @return array
72b3a70c 47 * Newly created $ufFieldArray
6a488035
TO
48 */
49function 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) {
3f1a97cd 53 throw new API_Exception('Params must be a field_name-carrying array and a positive integer.');
6a488035
TO
54 }
55
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));
60
28a04ea9 61 if (!CRM_Core_BAO_UFField::isValidFieldName($field_name)) {
3f1a97cd 62 throw new API_Exception('The field_name is not valid');
6a488035
TO
63 }
64 $params['field_name'] = array($field_type, $field_name, $location_type_id, $phone_type);
65
66 if (!(CRM_Utils_Array::value('group_id', $params))) {
67 $params['group_id'] = $groupId;
68 }
69
7e42d294 70 $ids = $ufFieldArray = array();
6a488035
TO
71 $ids['uf_group'] = $groupId;
72
73 $fieldId = CRM_Utils_Array::value('id', $params);
74 if (!empty($fieldId)) {
575e044f 75 $UFField = new CRM_Core_BAO_UFField();
6a488035
TO
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;
82 }
83 }
84 else {
3f1a97cd 85 throw new API_Exception("there is no field for this fieldId");
6a488035
TO
86 }
87 $ids['uf_field'] = $fieldId;
88 }
89
90 if (CRM_Core_BAO_UFField::duplicateField($params, $ids)) {
3f1a97cd 91 throw new API_Exception("The field was not added. It already exists in this profile.");
6a488035 92 }
7e42d294 93 //@todo why is this even optional? Surely weight should just be 'managed' ??
6a488035
TO
94 if (CRM_Utils_Array::value('option.autoweight', $params, TRUE)) {
95 $params['weight'] = CRM_Core_BAO_UFField::autoWeight($params);
96 }
97 $ufField = CRM_Core_BAO_UFField::add($params, $ids);
98
99 $fieldsType = CRM_Core_BAO_UFGroup::calculateGroupType($groupId, TRUE);
100 CRM_Core_BAO_UFGroup::updateGroupTypes($groupId, $fieldsType);
101
102 _civicrm_api3_object_to_array($ufField, $ufFieldArray[$ufField->id]);
7e42d294 103 civicrm_api3('profile', 'getfields', array('cache_clear' => TRUE));
6a488035
TO
104 return civicrm_api3_create_success($ufFieldArray, $params);
105}
106
107/**
9d32e6f7 108 * Adjust metadata for civicrm_uf_field create.
6a488035 109 *
c490a46a 110 * @param array $params
6a488035
TO
111 */
112function _civicrm_api3_uf_field_create_spec(&$params) {
113 $params['option.autoweight'] = array(
114 'title' => "Automatically adjust weights in UFGroup to align with UFField",
7e42d294 115 'type' => CRM_Utils_Type::T_BOOLEAN,
116 'api.default' => TRUE,
6a488035 117 );
7e42d294 118 $params['created_id']['api.default'] = 'user_contact_id';
a200bc65 119 $params['created_id']['title'] = 'Created By';
6616c349 120 $params['is_active']['api.default'] = TRUE;
6a488035
TO
121}
122
123/**
9d32e6f7 124 * Returns array of uf groups (profiles) matching a set of one or more group properties.
6a488035 125 *
cf470720 126 * @param array $params
9d32e6f7 127 * Array per getfields metadata.
6a488035 128 *
a6c01b45 129 * @return array
6a488035
TO
130 */
131function civicrm_api3_uf_field_get($params) {
132 return _civicrm_api3_basic_get('CRM_Core_BAO_UFField', $params);
133}
134
135/**
9d32e6f7 136 * Delete uf field.
6a488035 137 *
c490a46a 138 * @param array $params
7e42d294 139 *
140 * @throws API_Exception
6a488035 141 *
72b3a70c 142 * @return array
6a488035
TO
143 */
144function civicrm_api3_uf_field_delete($params) {
145 $fieldId = $params['id'];
146
147 $ufGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFField', $fieldId, 'uf_group_id');
148 if (!$ufGroupId) {
3f1a97cd 149 throw new API_Exception('Invalid value for field_id.');
6a488035
TO
150 }
151
152 $result = CRM_Core_BAO_UFField::del($fieldId);
153
154 $fieldsType = CRM_Core_BAO_UFGroup::calculateGroupType($ufGroupId, TRUE);
155 CRM_Core_BAO_UFGroup::updateGroupTypes($ufGroupId, $fieldsType);
156
157 return civicrm_api3_create_success($result, $params);
158}
645ee340 159
7e42d294 160/**
161 * field id accepted for backward compatibility - unset required on id
d0997921 162 * @param array $params
6a488035
TO
163 */
164function _civicrm_api3_uf_field_delete_spec(&$params) {
165 // legacy support for field_id
166 $params['id']['api.aliases'] = array('field_id');
167}