3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2020
35 * Business objects for managing custom data values.
37 class CRM_Core_BAO_CustomValue
extends CRM_Core_DAO
{
40 * Validate a value against a CustomField type.
43 * The type of the data.
44 * @param string $value
45 * The data to be validated.
48 * True if the value is of the specified type
50 public static function typecheck($type, $value) {
56 return CRM_Utils_Rule
::string($value);
59 return CRM_Utils_Rule
::integer($value);
63 return CRM_Utils_Rule
::numeric($value);
66 if (is_numeric($value)) {
67 return CRM_Utils_Rule
::dateTime($value);
70 return CRM_Utils_Rule
::date($value);
73 return CRM_Utils_Rule
::boolean($value);
75 case 'ContactReference':
76 return CRM_Utils_Rule
::validContact($value);
80 //fix for multi select state, CRM-3437
82 $mulValues = explode(',', $value);
83 foreach ($mulValues as $key => $state) {
84 $valid = array_key_exists(strtolower(trim($state)),
85 array_change_key_case(array_flip(CRM_Core_PseudoConstant
::stateProvinceAbbreviation()), CASE_LOWER
)
86 ) ||
array_key_exists(strtolower(trim($state)),
87 array_change_key_case(array_flip(CRM_Core_PseudoConstant
::stateProvince()), CASE_LOWER
)
97 //fix multi select country, CRM-3437
99 $mulValues = explode(',', $value);
100 foreach ($mulValues as $key => $country) {
101 $valid = array_key_exists(strtolower(trim($country)),
102 array_change_key_case(array_flip(CRM_Core_PseudoConstant
::countryIsoCode()), CASE_LOWER
)
103 ) ||
array_key_exists(strtolower(trim($country)),
104 array_change_key_case(array_flip(CRM_Core_PseudoConstant
::country()), CASE_LOWER
)
113 return CRM_Utils_Rule
::url($value);
119 * Given a 'civicrm' type string, return the mysql data store area
121 * @param string $type
122 * The civicrm type string.
124 * @return string|null
125 * the mysql data store placeholder
127 public static function typeToField($type) {
135 case 'StateProvince':
137 case 'Auto-complete':
144 return 'decimal_data';
161 * @param array $formValues
164 public static function fixCustomFieldValue(&$formValues) {
165 if (empty($formValues)) {
168 foreach (array_keys($formValues) as $key) {
169 if (substr($key, 0, 7) != 'custom_') {
172 elseif (empty($formValues[$key])) {
176 $htmlType = CRM_Core_DAO
::getFieldValue('CRM_Core_BAO_CustomField',
177 substr($key, 7), 'html_type'
179 $dataType = CRM_Core_DAO
::getFieldValue('CRM_Core_BAO_CustomField',
180 substr($key, 7), 'data_type'
183 if (is_array($formValues[$key])) {
184 if (!in_array(key($formValues[$key]), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
185 $formValues[$key] = ['IN' => $formValues[$key]];
188 elseif (($htmlType == 'TextArea' ||
189 ($htmlType == 'Text' && $dataType == 'String')
190 ) && strstr($formValues[$key], '%')
192 $formValues[$key] = ['LIKE' => $formValues[$key]];
198 * Delete option value give an option value and custom group id.
200 * @param int $customValueID
202 * @param int $customGroupID
205 public static function deleteCustomValue($customValueID, $customGroupID) {
206 // first we need to find custom value table, from custom group ID
207 $tableName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupID, 'table_name');
209 // Retrieve the $entityId so we can pass that to the hook.
210 $entityID = CRM_Core_DAO
::singleValueQuery("SELECT entity_id FROM {$tableName} WHERE id = %1", [
211 1 => [$customValueID, 'Integer'],
214 // delete custom value from corresponding custom value table
215 $sql = "DELETE FROM {$tableName} WHERE id = {$customValueID}";
216 CRM_Core_DAO
::executeQuery($sql);
218 CRM_Utils_Hook
::custom('delete',