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