3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
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';
162 * @param array $formValues
165 public static function fixCustomFieldValue(&$formValues) {
166 if (empty($formValues)) {
169 foreach (array_keys($formValues) as $key) {
170 if (substr($key, 0, 7) != 'custom_') {
173 elseif (empty($formValues[$key])) {
177 $htmlType = CRM_Core_DAO
::getFieldValue('CRM_Core_BAO_CustomField',
178 substr($key, 7), 'html_type'
180 $dataType = CRM_Core_DAO
::getFieldValue('CRM_Core_BAO_CustomField',
181 substr($key, 7), 'data_type'
184 if (is_array($formValues[$key])) {
185 if (!in_array(key($formValues[$key]), CRM_Core_DAO
::acceptedSQLOperators(), TRUE)) {
186 $formValues[$key] = array('IN' => $formValues[$key]);
189 elseif (($htmlType == 'TextArea' ||
190 ($htmlType == 'Text' && $dataType == 'String')
191 ) && strstr($formValues[$key], '%')
193 $formValues[$key] = array('LIKE' => $formValues[$key]);
199 * Delete option value give an option value and custom group id.
201 * @param int $customValueID
203 * @param int $customGroupID
206 public static function deleteCustomValue($customValueID, $customGroupID) {
207 // first we need to find custom value table, from custom group ID
208 $tableName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupID, 'table_name');
210 // Retrieve the $entityId so we can pass that to the hook.
211 $entityID = CRM_Core_DAO
::singleValueQuery("SELECT entity_id FROM {$tableName} WHERE id = %1", array(
212 1 => array($customValueID, 'Integer'),
215 // delete custom value from corresponding custom value table
216 $sql = "DELETE FROM {$tableName} WHERE id = {$customValueID}";
217 CRM_Core_DAO
::executeQuery($sql);
219 CRM_Utils_Hook
::custom('delete',