phpdoc 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
TO
35 * @version $Id: UFField.php 30171 2010-10-14 09:11:27Z mover $
36 *
37 */
38
6a488035
TO
39/**
40 * Defines 'uf field' within a group.
41 *
16b10e64 42 * @param array $params
cf470720 43 * Array Associative array of property name/value pairs to create new uf field.
6a488035 44 *
7e42d294 45 * @throws API_Exception
7e42d294 46 *
a6c01b45 47 * @return array
72b3a70c 48 * Newly created $ufFieldArray
6a488035
TO
49 *
50 * @access public
51 * {@getfields UFField_create}
52 * @example UFFieldCreate.php
53 */
54function civicrm_api3_uf_field_create($params) {
55 civicrm_api3_verify_one_mandatory($params, NULL, array('field_name', 'uf_group_id'));
56 $groupId = CRM_Utils_Array::value('uf_group_id', $params);
57 if ((int) $groupId < 1) {
3f1a97cd 58 throw new API_Exception('Params must be a field_name-carrying array and a positive integer.');
6a488035
TO
59 }
60
61 $field_type = CRM_Utils_Array::value('field_type', $params);
62 $field_name = CRM_Utils_Array::value('field_name', $params);
63 $location_type_id = CRM_Utils_Array::value('location_type_id', $params);
64 $phone_type = CRM_Utils_Array::value('phone_type_id', $params, CRM_Utils_Array::value('phone_type', $params));
65
66 if (! CRM_Core_BAO_UFField::isValidFieldName($field_name)) {
3f1a97cd 67 throw new API_Exception('The field_name is not valid');
6a488035
TO
68 }
69 $params['field_name'] = array($field_type, $field_name, $location_type_id, $phone_type);
70
71 if (!(CRM_Utils_Array::value('group_id', $params))) {
72 $params['group_id'] = $groupId;
73 }
74
7e42d294 75 $ids = $ufFieldArray = array();
6a488035
TO
76 $ids['uf_group'] = $groupId;
77
78 $fieldId = CRM_Utils_Array::value('id', $params);
79 if (!empty($fieldId)) {
575e044f 80 $UFField = new CRM_Core_BAO_UFField();
6a488035
TO
81 $UFField->id = $fieldId;
82 if ($UFField->find(TRUE)) {
83 $ids['uf_group'] = $UFField->uf_group_id;
84 if (!(CRM_Utils_Array::value('group_id', $params))) {
85 // this copied here from previous api function - not sure if required
86 $params['group_id'] = $UFField->uf_group_id;
87 }
88 }
89 else {
3f1a97cd 90 throw new API_Exception("there is no field for this fieldId");
6a488035
TO
91 }
92 $ids['uf_field'] = $fieldId;
93 }
94
95 if (CRM_Core_BAO_UFField::duplicateField($params, $ids)) {
3f1a97cd 96 throw new API_Exception("The field was not added. It already exists in this profile.");
6a488035 97 }
7e42d294 98 //@todo why is this even optional? Surely weight should just be 'managed' ??
6a488035
TO
99 if (CRM_Utils_Array::value('option.autoweight', $params, TRUE)) {
100 $params['weight'] = CRM_Core_BAO_UFField::autoWeight($params);
101 }
102 $ufField = CRM_Core_BAO_UFField::add($params, $ids);
103
104 $fieldsType = CRM_Core_BAO_UFGroup::calculateGroupType($groupId, TRUE);
105 CRM_Core_BAO_UFGroup::updateGroupTypes($groupId, $fieldsType);
106
107 _civicrm_api3_object_to_array($ufField, $ufFieldArray[$ufField->id]);
7e42d294 108 civicrm_api3('profile', 'getfields', array('cache_clear' => TRUE));
6a488035
TO
109 return civicrm_api3_create_success($ufFieldArray, $params);
110}
111
112/**
113 * Gets field for civicrm_uf_field create
114 *
c490a46a 115 * @param array $params
7e42d294 116 *
a6c01b45 117 * @return array
72b3a70c 118 * fields valid for other functions
6a488035
TO
119 */
120function _civicrm_api3_uf_field_create_spec(&$params) {
121 $params['option.autoweight'] = array(
122 'title' => "Automatically adjust weights in UFGroup to align with UFField",
7e42d294 123 'type' => CRM_Utils_Type::T_BOOLEAN,
124 'api.default' => TRUE,
6a488035 125 );
7e42d294 126 $params['created_id']['api.default'] = 'user_contact_id';
a200bc65 127 $params['created_id']['title'] = 'Created By';
6616c349 128 $params['is_active']['api.default'] = TRUE;
6a488035
TO
129}
130
131/**
132 * Returns array of uf groups (profiles) matching a set of one or more group properties
133 *
cf470720
TO
134 * @param array $params
135 * (reference) Array of one or more valid.
6a488035
TO
136 * property_name=>value pairs. If $params is set
137 * as null, all surveys will be returned
138 *
a6c01b45 139 * @return array
72b3a70c 140 * (reference) Array
6a488035
TO
141 * {@getfields UFField_get
142 * @example UFFieldGet.php
143 * @access public
144 */
145function civicrm_api3_uf_field_get($params) {
146 return _civicrm_api3_basic_get('CRM_Core_BAO_UFField', $params);
147}
148
149/**
150 * Delete uf field
151 *
c490a46a 152 * @param array $params
7e42d294 153 *
154 * @throws API_Exception
6a488035 155 *
72b3a70c 156 * @return array
6a488035
TO
157 *
158 * @access public
159 * {@getfields UFField_delete}
160 * @example UFFieldDelete.php
161 */
162function civicrm_api3_uf_field_delete($params) {
163 $fieldId = $params['id'];
164
165 $ufGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFField', $fieldId, 'uf_group_id');
166 if (!$ufGroupId) {
3f1a97cd 167 throw new API_Exception('Invalid value for field_id.');
6a488035
TO
168 }
169
170 $result = CRM_Core_BAO_UFField::del($fieldId);
171
172 $fieldsType = CRM_Core_BAO_UFGroup::calculateGroupType($ufGroupId, TRUE);
173 CRM_Core_BAO_UFGroup::updateGroupTypes($ufGroupId, $fieldsType);
174
175 return civicrm_api3_create_success($result, $params);
176}
7e42d294 177/**
178 * field id accepted for backward compatibility - unset required on id
6a488035
TO
179 */
180function _civicrm_api3_uf_field_delete_spec(&$params) {
181 // legacy support for field_id
182 $params['id']['api.aliases'] = array('field_id');
183}