| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | +--------------------------------------------------------------------+ |
| 5 | | CiviCRM version 4.5 | |
| 6 | +--------------------------------------------------------------------+ |
| 7 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 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-2014 |
| 36 | * @version $Id: UFField.php 30171 2010-10-14 09:11:27Z mover $ |
| 37 | * |
| 38 | */ |
| 39 | |
| 40 | /** |
| 41 | * Defines 'uf field' within a group. |
| 42 | * |
| 43 | * @param $params array Associative array of property name/value pairs to create new uf field. |
| 44 | * |
| 45 | * @throws API_Exception |
| 46 | * @internal param int $groupId Valid uf_group id |
| 47 | * |
| 48 | * @return Newly created $ufFieldArray array |
| 49 | * |
| 50 | * @access public |
| 51 | * {@getfields UFField_create} |
| 52 | * @example UFFieldCreate.php |
| 53 | */ |
| 54 | function 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) { |
| 58 | throw new API_Exception('Params must be a field_name-carrying array and a positive integer.'); |
| 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)) { |
| 67 | throw new API_Exception('The field_name is not valid'); |
| 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 | |
| 75 | $ids = $ufFieldArray = array(); |
| 76 | $ids['uf_group'] = $groupId; |
| 77 | |
| 78 | $fieldId = CRM_Utils_Array::value('id', $params); |
| 79 | if (!empty($fieldId)) { |
| 80 | $UFField = new CRM_Core_BAO_UFField(); |
| 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 { |
| 90 | throw new API_Exception("there is no field for this fieldId"); |
| 91 | } |
| 92 | $ids['uf_field'] = $fieldId; |
| 93 | } |
| 94 | |
| 95 | if (CRM_Core_BAO_UFField::duplicateField($params, $ids)) { |
| 96 | throw new API_Exception("The field was not added. It already exists in this profile."); |
| 97 | } |
| 98 | //@todo why is this even optional? Surely weight should just be 'managed' ?? |
| 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]); |
| 108 | civicrm_api3('profile', 'getfields', array('cache_clear' => TRUE)); |
| 109 | return civicrm_api3_create_success($ufFieldArray, $params); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Gets field for civicrm_uf_field create |
| 114 | * |
| 115 | * @param $params |
| 116 | * |
| 117 | * @return array fields valid for other functions |
| 118 | */ |
| 119 | function _civicrm_api3_uf_field_create_spec(&$params) { |
| 120 | $params['option.autoweight'] = array( |
| 121 | 'title' => "Automatically adjust weights in UFGroup to align with UFField", |
| 122 | 'type' => CRM_Utils_Type::T_BOOLEAN, |
| 123 | 'api.default' => TRUE, |
| 124 | ); |
| 125 | $params['created_id']['api.default'] = 'user_contact_id'; |
| 126 | $params['is_active']['api.default'] = TRUE; |
| 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 | */ |
| 141 | function 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 $params |
| 149 | * |
| 150 | * @throws API_Exception |
| 151 | * @internal param int $fieldId Valid uf_field id that to be deleted |
| 152 | * |
| 153 | * @return true on successful delete or return error |
| 154 | * |
| 155 | * @access public |
| 156 | * {@getfields UFField_delete} |
| 157 | * @example UFFieldDelete.php |
| 158 | */ |
| 159 | function civicrm_api3_uf_field_delete($params) { |
| 160 | $fieldId = $params['id']; |
| 161 | |
| 162 | $ufGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFField', $fieldId, 'uf_group_id'); |
| 163 | if (!$ufGroupId) { |
| 164 | throw new API_Exception('Invalid value for field_id.'); |
| 165 | } |
| 166 | |
| 167 | $result = CRM_Core_BAO_UFField::del($fieldId); |
| 168 | |
| 169 | $fieldsType = CRM_Core_BAO_UFGroup::calculateGroupType($ufGroupId, TRUE); |
| 170 | CRM_Core_BAO_UFGroup::updateGroupTypes($ufGroupId, $fieldsType); |
| 171 | |
| 172 | return civicrm_api3_create_success($result, $params); |
| 173 | } |
| 174 | /** |
| 175 | * field id accepted for backward compatibility - unset required on id |
| 176 | */ |
| 177 | function _civicrm_api3_uf_field_delete_spec(&$params) { |
| 178 | // legacy support for field_id |
| 179 | $params['id']['api.aliases'] = array('field_id'); |
| 180 | } |
| 181 | |