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