Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
e70a7fc0 | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * Business objects for managing custom data fields. | |
38 | * | |
39 | */ | |
40 | class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { | |
41 | ||
42 | /** | |
43 | * Array for valid combinations of data_type & descriptions | |
44 | * | |
45 | * @var array | |
6a488035 TO |
46 | */ |
47 | public static $_dataType = NULL; | |
48 | ||
49 | /** | |
50 | * Array for valid combinations of data_type & html_type | |
51 | * | |
52 | * @var array | |
6a488035 TO |
53 | */ |
54 | public static $_dataToHtml = NULL; | |
55 | ||
56 | /** | |
57 | * Array to hold (formatted) fields for import | |
58 | * | |
59 | * @var array | |
6a488035 TO |
60 | */ |
61 | public static $_importFields = NULL; | |
62 | ||
63 | /** | |
64 | * Build and retrieve the list of data types and descriptions | |
65 | * | |
a6c01b45 CW |
66 | * @return array |
67 | * Data type => Description | |
6a488035 | 68 | */ |
00be9182 | 69 | public static function &dataType() { |
6a488035 TO |
70 | if (!(self::$_dataType)) { |
71 | self::$_dataType = array( | |
72 | 'String' => ts('Alphanumeric'), | |
73 | 'Int' => ts('Integer'), | |
74 | 'Float' => ts('Number'), | |
75 | 'Money' => ts('Money'), | |
76 | 'Memo' => ts('Note'), | |
77 | 'Date' => ts('Date'), | |
78 | 'Boolean' => ts('Yes or No'), | |
79 | 'StateProvince' => ts('State/Province'), | |
80 | 'Country' => ts('Country'), | |
81 | 'File' => ts('File'), | |
82 | 'Link' => ts('Link'), | |
83 | 'ContactReference' => ts('Contact Reference'), | |
84 | ); | |
85 | } | |
86 | return self::$_dataType; | |
87 | } | |
88 | ||
b5c2afd0 EM |
89 | /** |
90 | * @return array | |
91 | */ | |
00be9182 | 92 | public static function dataToHtml() { |
6a488035 TO |
93 | if (!self::$_dataToHtml) { |
94 | self::$_dataToHtml = array( | |
95 | array( | |
f9f40af3 TO |
96 | 'Text' => 'Text', |
97 | 'Select' => 'Select', | |
98 | 'Radio' => 'Radio', | |
99 | 'CheckBox' => 'CheckBox', | |
6a488035 TO |
100 | 'Multi-Select' => 'Multi-Select', |
101 | 'AdvMulti-Select' => 'AdvMulti-Select', | |
102 | 'Autocomplete-Select' => 'Autocomplete-Select', | |
103 | ), | |
104 | array('Text' => 'Text', 'Select' => 'Select', 'Radio' => 'Radio'), | |
105 | array('Text' => 'Text', 'Select' => 'Select', 'Radio' => 'Radio'), | |
106 | array('Text' => 'Text', 'Select' => 'Select', 'Radio' => 'Radio'), | |
107 | array('TextArea' => 'TextArea', 'RichTextEditor' => 'RichTextEditor'), | |
108 | array('Date' => 'Select Date'), | |
109 | array('Radio' => 'Radio'), | |
110 | array('StateProvince' => 'Select State/Province', 'Multi-Select' => 'Multi-Select State/Province'), | |
111 | array('Country' => 'Select Country', 'Multi-Select' => 'Multi-Select Country'), | |
112 | array('File' => 'File'), | |
113 | array('Link' => 'Link'), | |
114 | array('ContactReference' => 'Autocomplete-Select'), | |
115 | ); | |
116 | } | |
117 | return self::$_dataToHtml; | |
118 | } | |
119 | ||
120 | /** | |
100fef9d | 121 | * Takes an associative array and creates a custom field object |
6a488035 TO |
122 | * |
123 | * This function is invoked from within the web form layer and also from the api layer | |
124 | * | |
6a0b768e TO |
125 | * @param array $params |
126 | * (reference) an assoc array of name/value pairs. | |
6a488035 | 127 | * |
16b10e64 | 128 | * @return CRM_Core_DAO_CustomField |
6a488035 | 129 | */ |
00be9182 | 130 | public static function create(&$params) { |
79f68e44 TO |
131 | $origParams = array_merge(array(), $params); |
132 | ||
f3f0709d | 133 | if (!isset($params['id'])) { |
134 | if (!isset($params['column_name'])) { | |
135 | // if add mode & column_name not present, calculate it. | |
136 | $params['column_name'] = strtolower(CRM_Utils_String::munge($params['label'], '_', 32)); | |
137 | } | |
138 | if (!isset($params['name'])) { | |
139 | $params['name'] = CRM_Utils_String::munge($params['label'], '_', 64); | |
140 | } | |
6a488035 | 141 | } |
f3f0709d | 142 | else { |
143 | $params['column_name'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', | |
6a488035 TO |
144 | $params['id'], |
145 | 'column_name' | |
146 | ); | |
147 | } | |
8001040d | 148 | $columnName = $params['column_name']; |
6a488035 TO |
149 | |
150 | $indexExist = FALSE; | |
151 | //as during create if field is_searchable we had created index. | |
a7488080 | 152 | if (!empty($params['id'])) { |
6a488035 TO |
153 | $indexExist = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $params['id'], 'is_searchable'); |
154 | } | |
155 | ||
f9f40af3 | 156 | switch (CRM_Utils_Array::value('html_type', $params)) { |
0d973850 | 157 | case 'Select Date': |
f9f40af3 | 158 | if (empty($params['date_format'])) { |
0d973850 | 159 | $config = CRM_Core_Config::singleton(); |
160 | $params['date_format'] = $config->dateInputFormat; | |
6a488035 | 161 | } |
0d973850 | 162 | break; |
f9f40af3 | 163 | |
0d973850 | 164 | case 'CheckBox': |
165 | case 'AdvMulti-Select': | |
166 | case 'Multi-Select': | |
167 | if (isset($params['default_checkbox_option'])) { | |
168 | $tempArray = array_keys($params['default_checkbox_option']); | |
169 | $defaultArray = array(); | |
170 | foreach ($tempArray as $k => $v) { | |
171 | if ($params['option_value'][$v]) { | |
172 | $defaultArray[] = $params['option_value'][$v]; | |
173 | } | |
174 | } | |
6a488035 | 175 | |
0d973850 | 176 | if (!empty($defaultArray)) { |
177 | // also add the seperator before and after the value per new conventio (CRM-1604) | |
178 | $params['default_value'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $defaultArray) . CRM_Core_DAO::VALUE_SEPARATOR; | |
179 | } | |
180 | } | |
181 | else { | |
a7488080 | 182 | if (!empty($params['default_option']) && isset($params['option_value'][$params['default_option']]) |
0d973850 | 183 | ) { |
184 | $params['default_value'] = $params['option_value'][$params['default_option']]; | |
185 | } | |
186 | } | |
f9f40af3 | 187 | break; |
6a488035 | 188 | } |
0d973850 | 189 | |
6a488035 TO |
190 | $transaction = new CRM_Core_Transaction(); |
191 | // create any option group & values if required | |
192 | if ($params['html_type'] != 'Text' && | |
193 | in_array($params['data_type'], array( | |
f9f40af3 TO |
194 | 'String', |
195 | 'Int', | |
196 | 'Float', | |
21dfd5f5 | 197 | 'Money', |
f9f40af3 | 198 | )) |
6a488035 TO |
199 | ) { |
200 | ||
201 | $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', | |
202 | $params['custom_group_id'], | |
203 | 'table_name' | |
204 | ); | |
205 | ||
45f37ea0 | 206 | if ($params['option_type'] == 1 && empty($params['option_group_id'])) { |
6a488035 TO |
207 | // first create an option group for this custom group |
208 | $optionGroup = new CRM_Core_DAO_OptionGroup(); | |
8b3b9a2e | 209 | $optionGroup->name = "{$columnName}_" . date('YmdHis'); |
6a488035 TO |
210 | $optionGroup->title = $params['label']; |
211 | $optionGroup->is_active = 1; | |
212 | $optionGroup->save(); | |
213 | $params['option_group_id'] = $optionGroup->id; | |
f9f40af3 | 214 | if (!empty($params['option_value']) && is_array($params['option_value'])) { |
b958933f | 215 | foreach ($params['option_value'] as $k => $v) { |
216 | if (strlen(trim($v))) { | |
217 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
218 | $optionValue->option_group_id = $optionGroup->id; | |
219 | $optionValue->label = $params['option_label'][$k]; | |
220 | $optionValue->name = CRM_Utils_String::titleToVar($params['option_label'][$k]); | |
221 | switch ($params['data_type']) { | |
222 | case 'Money': | |
223 | $optionValue->value = CRM_Utils_Rule::cleanMoney($v); | |
224 | break; | |
225 | ||
226 | case 'Int': | |
227 | $optionValue->value = intval($v); | |
228 | break; | |
229 | ||
230 | case 'Float': | |
231 | $optionValue->value = floatval($v); | |
232 | break; | |
233 | ||
234 | default: | |
235 | $optionValue->value = trim($v); | |
236 | } | |
6a488035 | 237 | |
b958933f | 238 | $optionValue->weight = $params['option_weight'][$k]; |
239 | $optionValue->is_active = CRM_Utils_Array::value($k, $params['option_status'], FALSE); | |
240 | $optionValue->save(); | |
6a488035 | 241 | } |
6a488035 TO |
242 | } |
243 | } | |
244 | } | |
245 | } | |
246 | ||
247 | // check for orphan option groups | |
a7488080 CW |
248 | if (!empty($params['option_group_id'])) { |
249 | if (!empty($params['id'])) { | |
6a488035 TO |
250 | self::fixOptionGroups($params['id'], $params['option_group_id']); |
251 | } | |
252 | ||
253 | // if we dont have a default value | |
254 | // retrive it from one of the other custom fields which use this option group | |
a7488080 | 255 | if (empty($params['default_value'])) { |
6a488035 TO |
256 | //don't insert only value separator as default value, CRM-4579 |
257 | $defaultValue = self::getOptionGroupDefault($params['option_group_id'], | |
258 | $params['html_type'] | |
259 | ); | |
260 | ||
261 | if (!CRM_Utils_System::isNull(explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
f9f40af3 TO |
262 | $defaultValue |
263 | )) | |
264 | ) { | |
6a488035 TO |
265 | $params['default_value'] = $defaultValue; |
266 | } | |
267 | } | |
268 | } | |
269 | ||
270 | // since we need to save option group id :) | |
271 | if (!isset($params['attributes']) && strtolower($params['html_type']) == 'textarea') { | |
272 | $params['attributes'] = 'rows=4, cols=60'; | |
273 | } | |
274 | ||
275 | $customField = new CRM_Core_DAO_CustomField(); | |
276 | $customField->copyValues($params); | |
277 | $customField->is_required = CRM_Utils_Array::value('is_required', $params, FALSE); | |
278 | $customField->is_searchable = CRM_Utils_Array::value('is_searchable', $params, FALSE); | |
e41f4660 | 279 | $customField->in_selector = CRM_Utils_Array::value('in_selector', $params, FALSE); |
6a488035 TO |
280 | $customField->is_search_range = CRM_Utils_Array::value('is_search_range', $params, FALSE); |
281 | $customField->is_active = CRM_Utils_Array::value('is_active', $params, FALSE); | |
282 | $customField->is_view = CRM_Utils_Array::value('is_view', $params, FALSE); | |
283 | $customField->save(); | |
284 | ||
285 | // make sure all values are present in the object for further processing | |
286 | $customField->find(TRUE); | |
287 | ||
a81b736d | 288 | $triggerRebuild = CRM_Utils_Array::value('triggerRebuild', $params, TRUE); |
6a488035 | 289 | //create/drop the index when we toggle the is_searchable flag |
a7488080 | 290 | if (!empty($params['id'])) { |
a81b736d | 291 | self::createField($customField, 'modify', $indexExist, $triggerRebuild); |
6a488035 TO |
292 | } |
293 | else { | |
79f68e44 TO |
294 | if (!isset($origParams['column_name'])) { |
295 | $columnName .= "_{$customField->id}"; | |
296 | $params['column_name'] = $columnName; | |
297 | } | |
8b3b9a2e | 298 | $customField->column_name = $columnName; |
6a488035 TO |
299 | $customField->save(); |
300 | // make sure all values are present in the object | |
301 | $customField->find(TRUE); | |
302 | ||
a81b736d JM |
303 | $indexExist = FALSE; |
304 | self::createField($customField, 'add', $indexExist, $triggerRebuild); | |
6a488035 TO |
305 | } |
306 | ||
307 | // complete transaction | |
308 | $transaction->commit(); | |
309 | ||
310 | CRM_Utils_System::flushCache(); | |
311 | ||
312 | return $customField; | |
313 | } | |
314 | ||
315 | /** | |
c490a46a | 316 | * Fetch object based on array of properties |
6a488035 | 317 | * |
6a0b768e TO |
318 | * @param array $params |
319 | * (reference ) an assoc array of name/value pairs. | |
320 | * @param array $defaults | |
321 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 322 | * |
16b10e64 | 323 | * @return CRM_Core_DAO_CustomField |
6a488035 | 324 | */ |
00be9182 | 325 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
326 | return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $params, $defaults); |
327 | } | |
328 | ||
329 | /** | |
100fef9d | 330 | * Update the is_active flag in the db |
6a488035 | 331 | * |
6a0b768e TO |
332 | * @param int $id |
333 | * Id of the database record. | |
334 | * @param bool $is_active | |
335 | * Value we want to set the is_active field. | |
6a488035 | 336 | * |
a6c01b45 CW |
337 | * @return Object |
338 | * DAO object on sucess, null otherwise | |
6a488035 | 339 | * |
6a488035 | 340 | */ |
00be9182 | 341 | public static function setIsActive($id, $is_active) { |
6a488035 TO |
342 | |
343 | CRM_Utils_System::flushCache(); | |
344 | ||
345 | //enable-disable CustomField | |
346 | CRM_Core_BAO_UFField::setUFField($id, $is_active); | |
347 | return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_CustomField', $id, 'is_active', $is_active); | |
348 | } | |
349 | ||
350 | /** | |
351 | * Get the field title. | |
352 | * | |
6a0b768e TO |
353 | * @param int $id |
354 | * Id of field. | |
6a488035 | 355 | * |
a6c01b45 CW |
356 | * @return string |
357 | * name | |
6a488035 | 358 | * |
6a488035 TO |
359 | */ |
360 | public static function getTitle($id) { | |
361 | return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $id, 'label'); | |
362 | } | |
363 | ||
364 | /** | |
365 | * Store and return an array of all active custom fields. | |
366 | * | |
6a0b768e TO |
367 | * @param string $customDataType |
368 | * Type of Custom Data; empty is a synonym for "all contact data types". | |
369 | * @param bool $showAll | |
370 | * If true returns all fields (includes disabled fields). | |
371 | * @param bool $inline | |
372 | * If true returns all inline fields (includes disabled fields). | |
373 | * @param int $customDataSubType | |
374 | * Custom Data sub type value. | |
375 | * @param int $customDataSubName | |
376 | * Custom Data sub name value. | |
377 | * @param bool $onlyParent | |
378 | * Return only top level custom data, for eg, only Participant and ignore subname and subtype. | |
379 | * @param bool $onlySubType | |
380 | * Return only custom data for subtype. | |
381 | * @param bool $checkPermission | |
382 | * If false, do not include permissioning clause. | |
6a488035 | 383 | * |
a6c01b45 CW |
384 | * @return array |
385 | * an array of active custom fields. | |
6a488035 | 386 | * |
6a488035 | 387 | */ |
f9f40af3 TO |
388 | public static function &getFields( |
389 | $customDataType = 'Individual', | |
390 | $showAll = FALSE, | |
391 | $inline = FALSE, | |
392 | $customDataSubType = NULL, | |
393 | $customDataSubName = NULL, | |
394 | $onlyParent = FALSE, | |
395 | $onlySubType = FALSE, | |
396 | $checkPermission = TRUE | |
6a488035 | 397 | ) { |
0400dfac TO |
398 | if (empty($customDataType)) { |
399 | $customDataType = array('Contact', 'Individual', 'Organization', 'Household'); | |
400 | } | |
645a0241 | 401 | if ($customDataType && !is_array($customDataType)) { |
6a488035 | 402 | |
645a0241 | 403 | if (in_array($customDataType, CRM_Contact_BAO_ContactType::subTypes())) { |
6a488035 TO |
404 | // This is the case when getFieldsForImport() requires fields |
405 | // limited strictly to a subtype. | |
406 | $customDataSubType = $customDataType; | |
645a0241 TO |
407 | $customDataType = CRM_Contact_BAO_ContactType::getBasicType($customDataType); |
408 | $onlySubType = TRUE; | |
6a488035 TO |
409 | } |
410 | ||
645a0241 | 411 | if (in_array($customDataType, array_keys(CRM_Core_SelectValues::customGroupExtends()))) { |
6a488035 TO |
412 | // this makes the method flexible to support retrieving fields |
413 | // for multiple extends value. | |
414 | $customDataType = array($customDataType); | |
415 | } | |
416 | } | |
417 | ||
5439e6fb | 418 | $customDataSubType = CRM_Utils_Array::explodePadded($customDataSubType); |
6a488035 TO |
419 | |
420 | if (is_array($customDataType)) { | |
421 | $cacheKey = implode('_', $customDataType); | |
422 | } | |
423 | else { | |
424 | $cacheKey = $customDataType; | |
425 | } | |
426 | ||
427 | $cacheKey .= !empty($customDataSubType) ? ('_' . implode('_', $customDataSubType)) : '_0'; | |
428 | $cacheKey .= $customDataSubName ? "{$customDataSubName}_" : '_0'; | |
429 | $cacheKey .= $showAll ? '_1' : '_0'; | |
430 | $cacheKey .= $inline ? '_1_' : '_0_'; | |
431 | $cacheKey .= $onlyParent ? '_1_' : '_0_'; | |
432 | $cacheKey .= $onlySubType ? '_1_' : '_0_'; | |
433 | $cacheKey .= $checkPermission ? '_1_' : '_0_'; | |
434 | ||
435 | $cgTable = CRM_Core_DAO_CustomGroup::getTableName(); | |
436 | ||
437 | // also get the permission stuff here | |
438 | if ($checkPermission) { | |
439 | $permissionClause = CRM_Core_Permission::customGroupClause(CRM_Core_Permission::VIEW, | |
440 | "{$cgTable}." | |
441 | ); | |
442 | } | |
443 | else { | |
444 | $permissionClause = '(1)'; | |
445 | } | |
446 | ||
447 | // lets md5 permission clause and take first 8 characters | |
448 | $cacheKey .= substr(md5($permissionClause), 0, 8); | |
449 | ||
450 | if (strlen($cacheKey) > 40) { | |
451 | $cacheKey = md5($cacheKey); | |
452 | } | |
453 | ||
454 | if (!self::$_importFields || | |
455 | CRM_Utils_Array::value($cacheKey, self::$_importFields) === NULL | |
456 | ) { | |
457 | if (!self::$_importFields) { | |
458 | self::$_importFields = array(); | |
459 | } | |
460 | ||
461 | // check if we can retrieve from database cache | |
462 | $fields = CRM_Core_BAO_Cache::getItem('contact fields', "custom importableFields $cacheKey"); | |
463 | ||
464 | if ($fields === NULL) { | |
465 | $cfTable = self::getTableName(); | |
466 | ||
467 | $extends = ''; | |
468 | if (is_array($customDataType)) { | |
469 | $value = NULL; | |
470 | foreach ($customDataType as $dataType) { | |
645a0241 TO |
471 | if (in_array($dataType, array_keys(CRM_Core_SelectValues::customGroupExtends()))) { |
472 | if (in_array($dataType, array('Individual', 'Household', 'Organization'))) { | |
6a488035 TO |
473 | $val = "'" . CRM_Utils_Type::escape($dataType, 'String') . "', 'Contact' "; |
474 | } | |
475 | else { | |
476 | $val = "'" . CRM_Utils_Type::escape($dataType, 'String') . "'"; | |
477 | } | |
478 | $value = $value ? $value . ", {$val}" : $val; | |
479 | } | |
480 | } | |
481 | if ($value) { | |
482 | $extends = "AND $cgTable.extends IN ( $value ) "; | |
483 | } | |
484 | } | |
485 | ||
4f57b83f | 486 | if (!empty($customDataType) && empty($extends)) { |
0400dfac | 487 | // $customDataType specified a filter, but there is no corresponding SQL ($extends) |
837afcfe TO |
488 | self::$_importFields[$cacheKey] = array(); |
489 | return self::$_importFields[$cacheKey]; | |
490 | } | |
491 | ||
6a488035 TO |
492 | if ($onlyParent) { |
493 | $extends .= " AND $cgTable.extends_entity_column_value IS NULL AND $cgTable.extends_entity_column_id IS NULL "; | |
494 | } | |
495 | ||
496 | $query = "SELECT $cfTable.id, $cfTable.label, | |
497 | $cgTable.title, | |
a389ed51 | 498 | $cfTable.data_type, |
499 | $cfTable.html_type, | |
500 | $cfTable.default_value, | |
6a488035 TO |
501 | $cfTable.options_per_line, $cfTable.text_length, |
502 | $cfTable.custom_group_id, | |
817cf27c | 503 | $cfTable.is_required, |
6a488035 TO |
504 | $cgTable.extends, $cfTable.is_search_range, |
505 | $cgTable.extends_entity_column_value, | |
506 | $cgTable.extends_entity_column_id, | |
507 | $cfTable.is_view, | |
508 | $cfTable.option_group_id, | |
509 | $cfTable.date_format, | |
510 | $cfTable.time_format, | |
511 | $cgTable.is_multiple | |
512 | FROM $cfTable | |
513 | INNER JOIN $cgTable | |
514 | ON $cfTable.custom_group_id = $cgTable.id | |
515 | WHERE ( 1 ) "; | |
516 | ||
517 | if (!$showAll) { | |
518 | $query .= " AND $cfTable.is_active = 1 AND $cgTable.is_active = 1 "; | |
519 | } | |
520 | ||
521 | if ($inline) { | |
522 | $query .= " AND $cgTable.style = 'Inline' "; | |
523 | } | |
524 | ||
525 | //get the custom fields for specific type in | |
526 | //combination with fields those support any type. | |
527 | if (!empty($customDataSubType)) { | |
528 | $subtypeClause = array(); | |
529 | foreach ($customDataSubType as $subtype) { | |
530 | $subtype = CRM_Core_DAO::VALUE_SEPARATOR . $subtype . CRM_Core_DAO::VALUE_SEPARATOR; | |
531 | $subtypeClause[] = "$cgTable.extends_entity_column_value LIKE '%{$subtype}%'"; | |
532 | } | |
533 | if (!$onlySubType) { | |
534 | $subtypeClause[] = "$cgTable.extends_entity_column_value IS NULL"; | |
535 | } | |
536 | $query .= " AND ( " . implode(' OR ', $subtypeClause) . " )"; | |
537 | } | |
538 | ||
539 | if ($customDataSubName) { | |
540 | $query .= " AND ( $cgTable.extends_entity_column_id = $customDataSubName ) "; | |
541 | } | |
542 | ||
543 | // also get the permission stuff here | |
544 | if ($checkPermission) { | |
545 | $permissionClause = CRM_Core_Permission::customGroupClause(CRM_Core_Permission::VIEW, | |
546 | "{$cgTable}.", TRUE | |
547 | ); | |
548 | } | |
549 | else { | |
550 | $permissionClause = '(1)'; | |
551 | } | |
552 | ||
553 | $query .= " $extends AND $permissionClause | |
554 | ORDER BY $cgTable.weight, $cgTable.title, | |
555 | $cfTable.weight, $cfTable.label"; | |
556 | ||
557 | $dao = CRM_Core_DAO::executeQuery($query); | |
558 | ||
559 | $fields = array(); | |
560 | while (($dao->fetch()) != NULL) { | |
561 | $fields[$dao->id]['label'] = $dao->label; | |
562 | $fields[$dao->id]['groupTitle'] = $dao->title; | |
563 | $fields[$dao->id]['data_type'] = $dao->data_type; | |
564 | $fields[$dao->id]['html_type'] = $dao->html_type; | |
a389ed51 | 565 | $fields[$dao->id]['default_value'] = $dao->default_value; |
6a488035 TO |
566 | $fields[$dao->id]['text_length'] = $dao->text_length; |
567 | $fields[$dao->id]['options_per_line'] = $dao->options_per_line; | |
568 | $fields[$dao->id]['custom_group_id'] = $dao->custom_group_id; | |
569 | $fields[$dao->id]['extends'] = $dao->extends; | |
570 | $fields[$dao->id]['is_search_range'] = $dao->is_search_range; | |
571 | $fields[$dao->id]['extends_entity_column_value'] = $dao->extends_entity_column_value; | |
572 | $fields[$dao->id]['extends_entity_column_id'] = $dao->extends_entity_column_id; | |
573 | $fields[$dao->id]['is_view'] = $dao->is_view; | |
574 | $fields[$dao->id]['is_multiple'] = $dao->is_multiple; | |
575 | $fields[$dao->id]['option_group_id'] = $dao->option_group_id; | |
576 | $fields[$dao->id]['date_format'] = $dao->date_format; | |
577 | $fields[$dao->id]['time_format'] = $dao->time_format; | |
817cf27c | 578 | $fields[$dao->id]['is_required'] = $dao->is_required; |
6a488035 TO |
579 | } |
580 | ||
581 | CRM_Core_BAO_Cache::setItem($fields, | |
582 | 'contact fields', | |
583 | "custom importableFields $cacheKey" | |
584 | ); | |
585 | } | |
586 | self::$_importFields[$cacheKey] = $fields; | |
587 | } | |
588 | ||
589 | return self::$_importFields[$cacheKey]; | |
590 | } | |
591 | ||
592 | /** | |
593 | * Return the field ids and names (with groups) for import purpose. | |
594 | * | |
2a6da8d7 | 595 | * @param int|string $contactType Contact type |
6a0b768e TO |
596 | * @param bool $showAll |
597 | * If true returns all fields (includes disabled fields). | |
598 | * @param bool $onlyParent | |
599 | * Return fields ONLY related to basic types. | |
600 | * @param bool $search | |
601 | * When called from search and multiple records need to be returned. | |
602 | * @param bool $checkPermission | |
603 | * If false, do not include permissioning clause. | |
2a6da8d7 EM |
604 | * |
605 | * @param bool $withMultiple | |
6a488035 | 606 | * |
a6c01b45 CW |
607 | * @return array |
608 | * | |
6a488035 | 609 | * |
6a488035 | 610 | */ |
f9f40af3 TO |
611 | public static function &getFieldsForImport( |
612 | $contactType = 'Individual', | |
613 | $showAll = FALSE, | |
614 | $onlyParent = FALSE, | |
615 | $search = FALSE, | |
6a488035 | 616 | $checkPermission = TRUE, |
f9f40af3 | 617 | $withMultiple = FALSE |
6a488035 TO |
618 | ) { |
619 | // Note: there are situations when we want getFieldsForImport() return fields related | |
620 | // ONLY to basic contact types, but NOT subtypes. And thats where $onlyParent is helpful | |
621 | $fields = &self::getFields($contactType, | |
622 | $showAll, | |
623 | FALSE, | |
624 | NULL, | |
625 | NULL, | |
626 | $onlyParent, | |
627 | FALSE, | |
628 | $checkPermission | |
629 | ); | |
630 | ||
631 | $importableFields = array(); | |
632 | foreach ($fields as $id => $values) { | |
633 | // for now we should not allow multiple fields in profile / export etc, hence unsetting | |
634 | if (!$search && | |
8cc574cf | 635 | (!empty($values['is_multiple']) && !$withMultiple) |
6a488035 TO |
636 | ) { |
637 | continue; | |
638 | } | |
639 | ||
640 | /* generate the key for the fields array */ | |
641 | ||
642 | $key = "custom_$id"; | |
643 | ||
644 | $regexp = preg_replace('/[.,;:!?]/', '', CRM_Utils_Array::value(0, $values)); | |
645 | $importableFields[$key] = array( | |
646 | 'name' => $key, | |
647 | 'title' => CRM_Utils_Array::value('label', $values), | |
648 | 'headerPattern' => '/' . preg_quote($regexp, '/') . '/', | |
649 | 'import' => 1, | |
650 | 'custom_field_id' => $id, | |
651 | 'options_per_line' => CRM_Utils_Array::value('options_per_line', $values), | |
26509325 | 652 | 'text_length' => CRM_Utils_Array::value('text_length', $values, 255), |
6a488035 TO |
653 | 'data_type' => CRM_Utils_Array::value('data_type', $values), |
654 | 'html_type' => CRM_Utils_Array::value('html_type', $values), | |
655 | 'is_search_range' => CRM_Utils_Array::value('is_search_range', $values), | |
656 | ); | |
657 | ||
658 | // CRM-6681, pass date and time format when html_type = Select Date | |
659 | if (CRM_Utils_Array::value('html_type', $values) == 'Select Date') { | |
660 | $importableFields[$key]['date_format'] = CRM_Utils_Array::value('date_format', $values); | |
661 | $importableFields[$key]['time_format'] = CRM_Utils_Array::value('time_format', $values); | |
662 | } | |
663 | } | |
664 | ||
665 | return $importableFields; | |
666 | } | |
667 | ||
668 | /** | |
669 | * Get the field id from an import key | |
670 | * | |
6a0b768e TO |
671 | * @param string $key |
672 | * The key to parse. | |
6a488035 | 673 | * |
2a6da8d7 | 674 | * @param bool $all |
72b3a70c CW |
675 | * @return int|null |
676 | * The id (if exists) | |
6a488035 TO |
677 | */ |
678 | public static function getKeyID($key, $all = FALSE) { | |
679 | $match = array(); | |
680 | if (preg_match('/^custom_(\d+)_?(-?\d+)?$/', $key, $match)) { | |
681 | if (!$all) { | |
682 | return $match[1]; | |
683 | } | |
684 | else { | |
685 | return array( | |
686 | $match[1], | |
687 | CRM_Utils_Array::value(2, $match), | |
688 | ); | |
689 | } | |
690 | } | |
5439e6fb | 691 | return $all ? array(NULL, NULL) : NULL; |
6a488035 TO |
692 | } |
693 | ||
694 | /** | |
695 | * Use the cache to get all values of a specific custom field | |
696 | * | |
6a0b768e TO |
697 | * @param int $fieldID |
698 | * The custom field ID. | |
6a488035 | 699 | * |
16b10e64 | 700 | * @return CRM_Core_DAO_CustomField |
5c766a0b | 701 | * The field object. |
6a488035 | 702 | */ |
00be9182 | 703 | public static function getFieldObject($fieldID) { |
6a488035 TO |
704 | $field = new CRM_Core_DAO_CustomField(); |
705 | ||
706 | // check if we can get the field values from the system cache | |
f9f40af3 TO |
707 | $cacheKey = "CRM_Core_DAO_CustomField_{$fieldID}"; |
708 | $cache = CRM_Utils_Cache::singleton(); | |
6a488035 TO |
709 | $fieldValues = $cache->get($cacheKey); |
710 | if (empty($fieldValues)) { | |
711 | $field->id = $fieldID; | |
712 | if (!$field->find(TRUE)) { | |
713 | CRM_Core_Error::fatal(); | |
714 | } | |
715 | ||
716 | $fieldValues = array(); | |
717 | CRM_Core_DAO::storeValues($field, $fieldValues); | |
718 | ||
719 | $cache->set($cacheKey, $fieldValues); | |
720 | } | |
721 | else { | |
722 | $field->copyValues($fieldValues); | |
723 | } | |
724 | ||
725 | return $field; | |
726 | } | |
727 | ||
728 | /** | |
729 | * This function for building custom fields | |
730 | * | |
6a0b768e TO |
731 | * @param CRM_Core_Form $qf |
732 | * Form object (reference). | |
733 | * @param string $elementName | |
734 | * Name of the custom field. | |
100fef9d | 735 | * @param int $fieldId |
6a0b768e TO |
736 | * @param bool $inactiveNeeded |
737 | * -deprecated. | |
738 | * @param bool $useRequired | |
739 | * True if required else false. | |
740 | * @param bool $search | |
741 | * True if used for search else false. | |
742 | * @param string $label | |
743 | * Label for custom field. | |
6a488035 | 744 | * |
6a488035 | 745 | */ |
f9f40af3 TO |
746 | public static function addQuickFormElement( |
747 | &$qf, | |
6a488035 TO |
748 | $elementName, |
749 | $fieldId, | |
750 | $inactiveNeeded = FALSE, | |
f9f40af3 TO |
751 | $useRequired = TRUE, |
752 | $search = FALSE, | |
753 | $label = NULL | |
6a488035 | 754 | ) { |
6a488035 | 755 | $field = self::getFieldObject($fieldId); |
3130209f | 756 | $widget = $field->html_type; |
2dd1b730 | 757 | |
a7d0519b | 758 | // Custom field HTML should indicate group+field name |
be09038f | 759 | $groupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $field->custom_group_id); |
f9f40af3 TO |
760 | $dataCrmCustomVal = $groupName . ':' . $field->name; |
761 | $dataCrmCustomAttr = 'data-crm-custom="' . $dataCrmCustomVal . '"'; | |
a7d0519b | 762 | $field->attributes .= $dataCrmCustomAttr; |
2dd1b730 | 763 | |
6a488035 | 764 | // Fixed for Issue CRM-2183 |
3130209f CW |
765 | if ($widget == 'TextArea' && $search) { |
766 | $widget = 'Text'; | |
767 | } | |
768 | ||
1b4d9e39 | 769 | $placeholder = $search ? ts('- any -') : ($useRequired ? ts('- select -') : ts('- none -')); |
6a488035 | 770 | |
8a6cfaa9 | 771 | // FIXME: Why are select state/country separate widget types? |
f9f40af3 TO |
772 | $isSelect = (in_array($widget, array( |
773 | 'Select', | |
774 | 'Multi-Select', | |
775 | 'Select State/Province', | |
776 | 'Multi-Select State/Province', | |
777 | 'Select Country', | |
778 | 'Multi-Select Country', | |
779 | 'AdvMulti-Select', | |
780 | 'CheckBox', | |
21dfd5f5 | 781 | 'Radio', |
f9f40af3 | 782 | ))); |
3130209f CW |
783 | |
784 | if ($isSelect) { | |
f9f40af3 TO |
785 | $options = CRM_Utils_Array::value('values', civicrm_api3('contact', 'getoptions', array( |
786 | 'field' => "custom_$fieldId", | |
21dfd5f5 | 787 | 'context' => $search ? 'search' : 'create', |
f9f40af3 | 788 | ), array())); |
3130209f CW |
789 | |
790 | // Consolidate widget types to simplify the below switch statement | |
791 | if ($search || ($widget !== 'AdvMulti-Select' && strpos($widget, 'Select') !== FALSE)) { | |
792 | $widget = 'Select'; | |
793 | } | |
8a6cfaa9 CW |
794 | $selectAttributes = array( |
795 | 'data-crm-custom' => $dataCrmCustomVal, | |
796 | 'class' => 'crm-select2', | |
797 | ); | |
3130209f CW |
798 | // Search field is always multi-select |
799 | if ($search || strpos($field->html_type, 'Multi') !== FALSE) { | |
800 | $selectAttributes['class'] .= ' huge'; | |
8a6cfaa9 | 801 | $selectAttributes['multiple'] = 'multiple'; |
3130209f CW |
802 | $selectAttributes['placeholder'] = $placeholder; |
803 | } | |
804 | // Add data for popup link. Normally this is handled by CRM_Core_Form->addSelect | |
805 | if ($field->option_group_id && !$search && $widget == 'Select' && CRM_Core_Permission::check('administer CiviCRM')) { | |
806 | $selectAttributes += array( | |
f9f40af3 TO |
807 | 'data-api-entity' => 'contact', |
808 | // FIXME: This works because the getoptions api isn't picky about custom fields, but it's WRONG | |
3130209f CW |
809 | 'data-api-field' => 'custom_' . $field->id, |
810 | 'data-option-edit-path' => 'civicrm/admin/options/' . CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $field->option_group_id), | |
811 | ); | |
8a6cfaa9 | 812 | } |
8a6cfaa9 CW |
813 | } |
814 | ||
6a488035 TO |
815 | if (!isset($label)) { |
816 | $label = $field->label; | |
817 | } | |
818 | ||
819 | /** | |
820 | * at some point in time we might want to split the below into small functions | |
821 | **/ | |
822 | ||
3130209f | 823 | switch ($widget) { |
6a488035 | 824 | case 'Text': |
3130209f | 825 | case 'Link': |
6a488035 TO |
826 | if ($field->is_search_range && $search) { |
827 | $qf->add('text', $elementName . '_from', $label . ' ' . ts('From'), $field->attributes); | |
828 | $qf->add('text', $elementName . '_to', ts('To'), $field->attributes); | |
829 | } | |
830 | else { | |
3130209f | 831 | $element = &$qf->add('text', $elementName, $label, |
6a488035 TO |
832 | $field->attributes, |
833 | $useRequired && !$search | |
834 | ); | |
835 | } | |
836 | break; | |
837 | ||
838 | case 'TextArea': | |
a7d0519b | 839 | $attributes = $dataCrmCustomAttr; |
6a488035 TO |
840 | if ($field->note_rows) { |
841 | $attributes .= 'rows=' . $field->note_rows; | |
842 | } | |
843 | else { | |
844 | $attributes .= 'rows=4'; | |
845 | } | |
6a488035 TO |
846 | if ($field->note_columns) { |
847 | $attributes .= ' cols=' . $field->note_columns; | |
848 | } | |
849 | else { | |
850 | $attributes .= ' cols=60'; | |
851 | } | |
2f940a36 NG |
852 | if ($field->text_length) { |
853 | $attributes .= ' maxlength=' . $field->text_length; | |
854 | } | |
3130209f | 855 | $element = &$qf->add('textarea', |
6a488035 TO |
856 | $elementName, |
857 | $label, | |
858 | $attributes, | |
859 | $useRequired && !$search | |
860 | ); | |
861 | break; | |
862 | ||
863 | case 'Select Date': | |
864 | if ($field->is_search_range && $search) { | |
865 | $qf->addDate($elementName . '_from', $label . ' - ' . ts('From'), FALSE, | |
866 | array( | |
867 | 'format' => $field->date_format, | |
868 | 'timeFormat' => $field->time_format, | |
869 | 'startOffset' => $field->start_date_years, | |
870 | 'endOffset' => $field->end_date_years, | |
be09038f | 871 | 'data-crm-custom' => $dataCrmCustomVal, |
6a488035 TO |
872 | ) |
873 | ); | |
874 | ||
875 | $qf->addDate($elementName . '_to', ts('To'), FALSE, | |
876 | array( | |
877 | 'format' => $field->date_format, | |
878 | 'timeFormat' => $field->time_format, | |
879 | 'startOffset' => $field->start_date_years, | |
880 | 'endOffset' => $field->end_date_years, | |
be09038f | 881 | 'data-crm-custom' => $dataCrmCustomVal, |
6a488035 TO |
882 | ) |
883 | ); | |
884 | } | |
885 | else { | |
886 | $required = $useRequired && !$search; | |
887 | ||
888 | $qf->addDate($elementName, $label, $required, array( | |
889 | 'format' => $field->date_format, | |
f9f40af3 TO |
890 | 'timeFormat' => $field->time_format, |
891 | 'startOffset' => $field->start_date_years, | |
892 | 'endOffset' => $field->end_date_years, | |
893 | 'data-crm-custom' => $dataCrmCustomVal, | |
894 | )); | |
6a488035 TO |
895 | } |
896 | break; | |
897 | ||
898 | case 'Radio': | |
899 | $choice = array(); | |
3130209f | 900 | foreach ($options as $v => $l) { |
f9f40af3 | 901 | $choice[] = $qf->createElement('radio', NULL, '', $l, (string) $v, $field->attributes); |
6a488035 | 902 | } |
3130209f | 903 | $group = $qf->addGroup($choice, $elementName, $label); |
6a488035 TO |
904 | if ($useRequired && !$search) { |
905 | $qf->addRule($elementName, ts('%1 is a required field.', array(1 => $label)), 'required'); | |
906 | } | |
8a4f27dc | 907 | else { |
b847e6e7 | 908 | $group->setAttribute('allowClear', TRUE); |
8a4f27dc | 909 | } |
6a488035 TO |
910 | break; |
911 | ||
3130209f | 912 | // For all select elements |
6a488035 | 913 | case 'Select': |
3130209f CW |
914 | if (empty($selectAttributes['multiple'])) { |
915 | $options = array('' => $placeholder) + $options; | |
916 | } | |
917 | $qf->add('select', $elementName, $label, $options, $useRequired && !$search, $selectAttributes); | |
6a488035 | 918 | |
3130209f CW |
919 | // Add and/or option for fields that store multiple values |
920 | if ($search && self::isSerialized($field)) { | |
6a488035 | 921 | |
3130209f CW |
922 | $operators = array( |
923 | $qf->createElement('radio', NULL, '', ts('Any'), 'or', array('title' => ts('Results may contain any of the selected options'))), | |
924 | $qf->createElement('radio', NULL, '', ts('All'), 'and', array('title' => ts('Results must have all of the selected options'))), | |
925 | ); | |
926 | $qf->addGroup($operators, $elementName . '_operator'); | |
927 | $qf->setDefaults(array($elementName . '_operator' => 'or')); | |
6a488035 | 928 | } |
3130209f | 929 | break; |
6a488035 | 930 | |
3130209f | 931 | case 'AdvMulti-Select': |
6a488035 TO |
932 | $include =& $qf->addElement( |
933 | 'advmultiselect', | |
934 | $elementName, | |
3130209f | 935 | $label, $options, |
6a488035 TO |
936 | array( |
937 | 'size' => 5, | |
938 | 'style' => '', | |
939 | 'class' => 'advmultiselect', | |
2dd1b730 | 940 | 'data-crm-custom' => $dataCrmCustomVal, |
6a488035 TO |
941 | ) |
942 | ); | |
943 | ||
944 | $include->setButtonAttributes('add', array('value' => ts('Add >>'))); | |
945 | $include->setButtonAttributes('remove', array('value' => ts('<< Remove'))); | |
946 | ||
947 | if ($useRequired && !$search) { | |
948 | $qf->addRule($elementName, ts('%1 is a required field.', array(1 => $label)), 'required'); | |
949 | } | |
950 | break; | |
951 | ||
6a488035 | 952 | case 'CheckBox': |
6a488035 | 953 | $check = array(); |
3130209f | 954 | foreach ($options as $v => $l) { |
be09038f | 955 | $check[] = &$qf->addElement('advcheckbox', $v, NULL, $l, array('data-crm-custom' => $dataCrmCustomVal)); |
6a488035 | 956 | } |
6a488035 TO |
957 | $qf->addGroup($check, $elementName, $label); |
958 | if ($useRequired && !$search) { | |
959 | $qf->addRule($elementName, ts('%1 is a required field.', array(1 => $label)), 'required'); | |
960 | } | |
961 | break; | |
962 | ||
963 | case 'File': | |
964 | // we should not build upload file in search mode | |
965 | if ($search) { | |
966 | return; | |
967 | } | |
968 | $qf->add( | |
969 | strtolower($field->html_type), | |
970 | $elementName, | |
971 | $label, | |
972 | $field->attributes, | |
973 | $useRequired && !$search | |
974 | ); | |
975 | $qf->addUploadElement($elementName); | |
976 | break; | |
977 | ||
6a488035 | 978 | case 'RichTextEditor': |
f9f40af3 TO |
979 | $attributes = array( |
980 | 'rows' => $field->note_rows, | |
981 | 'cols' => $field->note_columns, | |
21dfd5f5 | 982 | 'data-crm-custom' => $dataCrmCustomVal, |
f9f40af3 | 983 | ); |
2f940a36 | 984 | if ($field->text_length) { |
97d5a31f | 985 | $attributes['maxlength'] = $field->text_length; |
2f940a36 NG |
986 | } |
987 | $qf->addWysiwyg($elementName, $label, $attributes, $search); | |
6a488035 TO |
988 | break; |
989 | ||
990 | case 'Autocomplete-Select': | |
6a488035 | 991 | static $customUrls = array(); |
06508628 CW |
992 | // Fixme: why is this a string in the first place?? |
993 | $attributes = array(); | |
994 | if ($field->attributes) { | |
f9f40af3 | 995 | foreach (explode(' ', $field->attributes) as $at) { |
06508628 CW |
996 | if (strpos($at, '=')) { |
997 | list($k, $v) = explode('=', $at); | |
998 | $attributes[$k] = trim($v, ' "'); | |
999 | } | |
1000 | } | |
1001 | } | |
6a488035 | 1002 | if ($field->data_type == 'ContactReference') { |
06508628 CW |
1003 | $attributes['class'] = (isset($attributes['class']) ? $attributes['class'] . ' ' : '') . 'crm-form-contact-reference huge'; |
1004 | $attributes['data-api-entity'] = 'contact'; | |
1005 | $qf->add('text', $elementName, $label, $attributes, | |
1b4d9e39 CW |
1006 | $useRequired && !$search |
1007 | ); | |
1008 | ||
6a488035 TO |
1009 | $urlParams = "context=customfield&id={$field->id}"; |
1010 | ||
1011 | $customUrls[$elementName] = CRM_Utils_System::url('civicrm/ajax/contactref', | |
1012 | $urlParams, | |
1013 | FALSE, NULL, FALSE | |
1014 | ); | |
1015 | ||
6a488035 TO |
1016 | } |
1017 | else { | |
2fea9ed9 | 1018 | // FIXME: This won't work with customFieldOptions hook |
1b4d9e39 CW |
1019 | $attributes += array( |
1020 | 'entity' => 'option_value', | |
1021 | 'placeholder' => $placeholder, | |
9f388466 | 1022 | 'multiple' => $search, |
1b4d9e39 CW |
1023 | 'api' => array( |
1024 | 'params' => array('option_group_id' => $field->option_group_id), | |
1025 | ), | |
6a488035 | 1026 | ); |
1b4d9e39 | 1027 | $qf->addEntityRef($elementName, $label, $attributes, $useRequired && !$search); |
6a488035 TO |
1028 | } |
1029 | ||
1030 | $qf->assign('customUrls', $customUrls); | |
1031 | break; | |
1032 | } | |
1033 | ||
1034 | switch ($field->data_type) { | |
1035 | case 'Int': | |
1036 | // integers will have numeric rule applied to them. | |
1037 | if ($field->is_search_range && $search) { | |
1038 | $qf->addRule($elementName . '_from', ts('%1 From must be an integer (whole number).', array(1 => $label)), 'integer'); | |
1039 | $qf->addRule($elementName . '_to', ts('%1 To must be an integer (whole number).', array(1 => $label)), 'integer'); | |
1040 | } | |
3130209f | 1041 | elseif ($widget == 'Text') { |
6a488035 TO |
1042 | $qf->addRule($elementName, ts('%1 must be an integer (whole number).', array(1 => $label)), 'integer'); |
1043 | } | |
1044 | break; | |
1045 | ||
1046 | case 'Float': | |
1047 | if ($field->is_search_range && $search) { | |
1048 | $qf->addRule($elementName . '_from', ts('%1 From must be a number (with or without decimal point).', array(1 => $label)), 'numeric'); | |
1049 | $qf->addRule($elementName . '_to', ts('%1 To must be a number (with or without decimal point).', array(1 => $label)), 'numeric'); | |
1050 | } | |
3130209f | 1051 | elseif ($widget == 'Text') { |
6a488035 TO |
1052 | $qf->addRule($elementName, ts('%1 must be a number (with or without decimal point).', array(1 => $label)), 'numeric'); |
1053 | } | |
1054 | break; | |
1055 | ||
1056 | case 'Money': | |
1057 | if ($field->is_search_range && $search) { | |
1058 | $qf->addRule($elementName . '_from', ts('%1 From must in proper money format. (decimal point/comma/space is allowed).', array(1 => $label)), 'money'); | |
1059 | $qf->addRule($elementName . '_to', ts('%1 To must in proper money format. (decimal point/comma/space is allowed).', array(1 => $label)), 'money'); | |
1060 | } | |
3130209f | 1061 | elseif ($widget == 'Text') { |
6a488035 TO |
1062 | $qf->addRule($elementName, ts('%1 must be in proper money format. (decimal point/comma/space is allowed).', array(1 => $label)), 'money'); |
1063 | } | |
1064 | break; | |
1065 | ||
1066 | case 'Link': | |
3130209f CW |
1067 | $element->setAttribute('onfocus', "if (!this.value) {this.value='http://';}"); |
1068 | $element->setAttribute('onblur', "if (this.value == 'http://') {this.value='';}"); | |
1069 | $element->setAttribute('class', "url"); | |
6a488035 TO |
1070 | $qf->addRule($elementName, ts('Enter a valid Website.'), 'wikiURL'); |
1071 | break; | |
1072 | } | |
1073 | if ($field->is_view && !$search) { | |
1074 | $qf->freeze($elementName); | |
1075 | } | |
1076 | } | |
1077 | ||
1078 | /** | |
1079 | * Delete the Custom Field. | |
1080 | * | |
6a0b768e TO |
1081 | * @param object $field |
1082 | * The field object. | |
6a488035 TO |
1083 | */ |
1084 | public static function deleteField($field) { | |
1085 | CRM_Utils_System::flushCache(); | |
1086 | ||
1087 | // first delete the custom option group and values associated with this field | |
1088 | if ($field->option_group_id) { | |
1089 | //check if option group is related to any other field, if | |
1090 | //not delete the option group and related option values | |
1091 | self::checkOptionGroup($field->option_group_id); | |
1092 | } | |
1093 | ||
1094 | // next drop the column from the custom value table | |
1095 | self::createField($field, 'delete'); | |
1096 | ||
1097 | $field->delete(); | |
1098 | CRM_Core_BAO_UFField::delUFField($field->id); | |
1099 | CRM_Utils_Weight::correctDuplicateWeights('CRM_Core_DAO_CustomField'); | |
6a488035 TO |
1100 | } |
1101 | ||
1102 | /** | |
1103 | * Given a custom field value, its id and the set of options | |
1104 | * find the display value for this field | |
1105 | * | |
6a0b768e TO |
1106 | * @param mixed $value |
1107 | * The custom field value. | |
1108 | * @param int $id | |
1109 | * The custom field id. | |
1110 | * @param int $options | |
1111 | * The assoc array of option name/value pairs. | |
fd31fa4c | 1112 | * |
100fef9d CW |
1113 | * @param int $contactID |
1114 | * @param int $fieldID | |
6a488035 | 1115 | * |
a6c01b45 CW |
1116 | * @return string |
1117 | * the display value | |
6a488035 | 1118 | * |
6a488035 | 1119 | */ |
00be9182 | 1120 | public static function getDisplayValue($value, $id, &$options, $contactID = NULL, $fieldID = NULL) { |
f9f40af3 | 1121 | $option = &$options[$id]; |
6a488035 | 1122 | $attributes = &$option['attributes']; |
f9f40af3 TO |
1123 | $html_type = $attributes['html_type']; |
1124 | $data_type = $attributes['data_type']; | |
1125 | $format = CRM_Utils_Array::value('format', $attributes); | |
6a488035 TO |
1126 | |
1127 | return self::getDisplayValueCommon($value, | |
1128 | $option, | |
1129 | $html_type, | |
1130 | $data_type, | |
1131 | $format, | |
1132 | $contactID, | |
1133 | $fieldID | |
1134 | ); | |
1135 | } | |
1136 | ||
b5c2afd0 EM |
1137 | /** |
1138 | * @param $value | |
1139 | * @param $option | |
1140 | * @param $html_type | |
1141 | * @param $data_type | |
1142 | * @param null $format | |
100fef9d CW |
1143 | * @param int $contactID |
1144 | * @param int $fieldID | |
b5c2afd0 EM |
1145 | * |
1146 | * @return array|mixed|null|string | |
1147 | */ | |
2da40d21 | 1148 | public static function getDisplayValueCommon( |
f9f40af3 | 1149 | $value, |
6a488035 TO |
1150 | &$option, |
1151 | $html_type, | |
1152 | $data_type, | |
f9f40af3 | 1153 | $format = NULL, |
6a488035 | 1154 | $contactID = NULL, |
f9f40af3 | 1155 | $fieldID = NULL |
6a488035 TO |
1156 | ) { |
1157 | $display = $value; | |
1158 | ||
1159 | if ($fieldID && | |
1160 | (($html_type == 'Radio' && $data_type != 'Boolean') || | |
1161 | ($html_type == 'Autocomplete-Select' && $data_type != 'ContactReference') || | |
1162 | $html_type == 'Select' || | |
1163 | $html_type == 'CheckBox' || | |
1164 | $html_type == 'AdvMulti-Select' || | |
1165 | $html_type == 'Multi-Select' | |
1166 | ) | |
1167 | ) { | |
1168 | CRM_Utils_Hook::customFieldOptions($fieldID, $option); | |
1169 | } | |
1170 | ||
1171 | switch ($html_type) { | |
1172 | case 'Radio': | |
1173 | if ($data_type == 'Boolean') { | |
cbc718fc ML |
1174 | // Do not assume that if not yes means no. |
1175 | $display = ''; | |
1176 | if ($value) { | |
1177 | $display = ts('Yes'); | |
1178 | } | |
f9f40af3 | 1179 | elseif ((string) $value === '0') { |
cbc718fc ML |
1180 | $display = ts('No'); |
1181 | } | |
6a488035 TO |
1182 | } |
1183 | else { | |
1184 | $display = CRM_Utils_Array::value($value, $option); | |
1185 | } | |
1186 | break; | |
1187 | ||
1188 | case 'Autocomplete-Select': | |
1189 | if ($data_type == 'ContactReference' && | |
1190 | $value | |
1191 | ) { | |
1192 | $display = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $value, 'display_name'); | |
1193 | } | |
1194 | else { | |
1195 | $display = CRM_Utils_Array::value($value, $option); | |
1196 | } | |
1197 | break; | |
1198 | ||
1199 | case 'Select': | |
1200 | $display = CRM_Utils_Array::value($value, $option); | |
1201 | break; | |
1202 | ||
1203 | case 'CheckBox': | |
1204 | case 'AdvMulti-Select': | |
1205 | case 'Multi-Select': | |
1206 | if (is_array($value)) { | |
1207 | $checkedData = $value; | |
1208 | } | |
1209 | else { | |
1210 | $checkedData = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
1211 | substr($value, 1, -1) | |
1212 | ); | |
1213 | if ($html_type == 'CheckBox') { | |
1214 | $newData = array(); | |
1215 | foreach ($checkedData as $v) { | |
1216 | $newData[$v] = 1; | |
1217 | } | |
1218 | $checkedData = $newData; | |
1219 | } | |
1220 | } | |
1221 | ||
1222 | $v = array(); | |
1223 | $p = array(); | |
1224 | foreach ($checkedData as $key => $val) { | |
6a488035 TO |
1225 | if ($html_type == 'CheckBox') { |
1226 | if ($val) { | |
1227 | $p[] = $key; | |
1228 | $v[] = CRM_Utils_Array::value($key, $option); | |
1229 | } | |
1230 | } | |
1231 | else { | |
1232 | $p[] = $val; | |
1233 | $v[] = CRM_Utils_Array::value($val, $option); | |
1234 | } | |
1235 | } | |
1236 | if (!empty($v)) { | |
1237 | $display = implode(', ', $v); | |
1238 | } | |
1239 | break; | |
1240 | ||
1241 | case 'Select Date': | |
1242 | if (is_array($value)) { | |
1243 | foreach ($value as $key => $val) { | |
1244 | $display[$key] = CRM_Utils_Date::customFormat($val); | |
1245 | } | |
1246 | } | |
1247 | else { | |
1248 | // remove time element display if time is not set | |
1249 | if (empty($option['attributes']['time_format'])) { | |
1250 | $value = substr($value, 0, 10); | |
1251 | } | |
1252 | $display = CRM_Utils_Date::customFormat($value); | |
1253 | } | |
1254 | break; | |
1255 | ||
1256 | case 'Select State/Province': | |
1257 | if (empty($value)) { | |
1258 | $display = ''; | |
1259 | } | |
1260 | else { | |
1261 | $display = CRM_Core_PseudoConstant::stateProvince($value); | |
1262 | } | |
1263 | break; | |
1264 | ||
1265 | case 'Multi-Select State/Province': | |
1266 | if (is_array($value)) { | |
1267 | $checkedData = $value; | |
1268 | } | |
1269 | else { | |
1270 | $checkedData = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
1271 | substr($value, 1, -1) | |
1272 | ); | |
1273 | } | |
1274 | ||
1275 | $states = CRM_Core_PseudoConstant::stateProvince(); | |
1276 | $display = NULL; | |
1277 | foreach ($checkedData as $stateID) { | |
1278 | if ($display) { | |
1279 | $display .= ', '; | |
1280 | } | |
1281 | $display .= $states[$stateID]; | |
1282 | } | |
1283 | break; | |
1284 | ||
1285 | case 'Select Country': | |
1286 | if (empty($value)) { | |
1287 | $display = ''; | |
1288 | } | |
1289 | else { | |
1290 | $display = CRM_Core_PseudoConstant::country($value); | |
1291 | } | |
1292 | break; | |
1293 | ||
1294 | case 'Multi-Select Country': | |
1295 | if (is_array($value)) { | |
1296 | $checkedData = $value; | |
1297 | } | |
1298 | else { | |
1299 | $checkedData = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
1300 | substr($value, 1, -1) | |
1301 | ); | |
1302 | } | |
1303 | ||
1304 | $countries = CRM_Core_PseudoConstant::country(); | |
1305 | $display = NULL; | |
1306 | foreach ($checkedData as $countryID) { | |
1307 | if ($display) { | |
1308 | $display .= ', '; | |
1309 | } | |
1310 | $display .= $countries[$countryID]; | |
1311 | } | |
1312 | break; | |
1313 | ||
1314 | case 'File': | |
d8f34a6e CW |
1315 | if ($value) { |
1316 | $icons = CRM_Core_BAO_File::paperIconAttachment('*', $value); | |
1317 | $display = $icons[$value]; | |
6a488035 TO |
1318 | } |
1319 | break; | |
1320 | ||
1321 | case 'TextArea': | |
1322 | if (empty($value)) { | |
1323 | $display = ''; | |
1324 | } | |
1325 | else { | |
1326 | $display = nl2br($value); | |
1327 | } | |
1328 | break; | |
1329 | ||
1330 | case 'Link': | |
1331 | if (empty($value)) { | |
1332 | $display = ''; | |
1333 | } | |
1334 | else { | |
1335 | $display = $value; | |
1336 | } | |
1337 | } | |
6a488035 TO |
1338 | return $display ? $display : $value; |
1339 | } | |
1340 | ||
1341 | /** | |
100fef9d | 1342 | * Set default values for custom data used in profile |
6a488035 | 1343 | * |
6a0b768e TO |
1344 | * @param int $customFieldId |
1345 | * Custom field id. | |
1346 | * @param string $elementName | |
1347 | * Custom field name. | |
1348 | * @param array $defaults | |
1349 | * Associated array of fields. | |
1350 | * @param int $contactId | |
1351 | * Contact id. | |
1352 | * @param int $mode | |
1353 | * Profile mode. | |
1354 | * @param mixed $value | |
1355 | * If passed - dont fetch value from db,. | |
6a488035 | 1356 | * just format the given value |
77b97be7 | 1357 | * |
6a488035 | 1358 | */ |
2da40d21 | 1359 | public static function setProfileDefaults( |
f9f40af3 | 1360 | $customFieldId, |
6a488035 TO |
1361 | $elementName, |
1362 | &$defaults, | |
1363 | $contactId = NULL, | |
1364 | $mode = NULL, | |
1365 | $value = NULL | |
1366 | ) { | |
1367 | //get the type of custom field | |
1368 | $customField = new CRM_Core_BAO_CustomField(); | |
1369 | $customField->id = $customFieldId; | |
1370 | $customField->find(TRUE); | |
1371 | ||
1372 | if (!$contactId) { | |
1373 | if ($mode == CRM_Profile_Form::MODE_CREATE) { | |
1374 | $value = $customField->default_value; | |
1375 | } | |
1376 | } | |
1377 | else { | |
1378 | if (!isset($value)) { | |
f9f40af3 TO |
1379 | $info = self::getTableColumnGroup($customFieldId); |
1380 | $query = "SELECT {$info[0]}.{$info[1]} as value FROM {$info[0]} WHERE {$info[0]}.entity_id = {$contactId}"; | |
6a488035 TO |
1381 | $result = CRM_Core_DAO::executeQuery($query); |
1382 | if ($result->fetch()) { | |
1383 | $value = $result->value; | |
1384 | } | |
1385 | } | |
1386 | ||
1387 | if ($customField->data_type == 'Country') { | |
1388 | if (!$value) { | |
1389 | $config = CRM_Core_Config::singleton(); | |
1390 | if ($config->defaultContactCountry) { | |
1391 | $value = $config->defaultContactCountry(); | |
1392 | } | |
1393 | } | |
1394 | } | |
1395 | } | |
1396 | ||
1397 | //set defaults if mode is registration | |
1398 | if (!trim($value) && | |
1399 | ($value !== 0) && | |
1400 | (!in_array($mode, array(CRM_Profile_Form::MODE_EDIT, CRM_Profile_Form::MODE_SEARCH))) | |
1401 | ) { | |
1402 | $value = $customField->default_value; | |
1403 | } | |
1404 | ||
1405 | if ($customField->data_type == 'Money' && isset($value)) { | |
1406 | $value = number_format($value, 2); | |
1407 | } | |
1408 | switch ($customField->html_type) { | |
1409 | case 'CheckBox': | |
1410 | case 'AdvMulti-Select': | |
1411 | case 'Multi-Select': | |
1412 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldId, FALSE); | |
1413 | $defaults[$elementName] = array(); | |
1414 | $checkedValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
1415 | substr($value, 1, -1) | |
1416 | ); | |
1417 | foreach ($customOption as $val) { | |
1418 | if (in_array($val['value'], $checkedValue)) { | |
1419 | if ($customField->html_type == 'CheckBox') { | |
1420 | $defaults[$elementName][$val['value']] = 1; | |
1421 | } | |
1422 | elseif ($customField->html_type == 'Multi-Select' || | |
1423 | $customField->html_type == 'AdvMulti-Select' | |
1424 | ) { | |
1425 | $defaults[$elementName][$val['value']] = $val['value']; | |
1426 | } | |
1427 | } | |
1428 | } | |
1429 | break; | |
1430 | ||
1431 | case 'Select Date': | |
1432 | if ($value) { | |
1433 | list($defaults[$elementName], $defaults[$elementName . '_time']) = CRM_Utils_Date::setDateDefaults( | |
1434 | $value, | |
1435 | NULL, | |
1436 | $customField->date_format, | |
1437 | $customField->time_format | |
1438 | ); | |
1439 | } | |
1440 | break; | |
1441 | ||
1442 | case 'Autocomplete-Select': | |
1443 | if ($customField->data_type == 'ContactReference') { | |
1444 | if (is_numeric($value)) { | |
1445 | $defaults[$elementName . '_id'] = $value; | |
1446 | $defaults[$elementName] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $value, 'sort_name'); | |
1447 | } | |
1448 | } | |
1449 | else { | |
1b4d9e39 | 1450 | $defaults[$elementName] = $value; |
6a488035 TO |
1451 | } |
1452 | break; | |
1453 | ||
1454 | default: | |
1455 | $defaults[$elementName] = $value; | |
1456 | } | |
1457 | } | |
1458 | ||
b5c2afd0 | 1459 | /** |
100fef9d CW |
1460 | * @param int $contactID |
1461 | * @param int $cfID | |
1462 | * @param int $fileID | |
b5c2afd0 EM |
1463 | * @param bool $absolute |
1464 | * | |
1465 | * @return array | |
1466 | */ | |
1467 | /** | |
100fef9d CW |
1468 | * @param int $contactID |
1469 | * @param int $cfID | |
1470 | * @param int $fileID | |
b5c2afd0 EM |
1471 | * @param bool $absolute |
1472 | * | |
1473 | * @return array | |
1474 | */ | |
00be9182 | 1475 | public static function getFileURL($contactID, $cfID, $fileID = NULL, $absolute = FALSE, $multiRecordWhereClause = NULL) { |
6a488035 TO |
1476 | if ($contactID) { |
1477 | if (!$fileID) { | |
1478 | $params = array('id' => $cfID); | |
1479 | $defaults = array(); | |
1480 | CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $params, $defaults); | |
1481 | $columnName = $defaults['column_name']; | |
1482 | ||
1483 | //table name of custom data | |
1484 | $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', | |
1485 | $defaults['custom_group_id'], | |
1486 | 'table_name', 'id' | |
1487 | ); | |
1488 | ||
1489 | //query to fetch id from civicrm_file | |
b42d84aa | 1490 | if ($multiRecordWhereClause) { |
1491 | $query = "SELECT {$columnName} FROM {$tableName} where entity_id = {$contactID} AND {$multiRecordWhereClause}"; | |
1492 | } | |
1493 | else { | |
1494 | $query = "SELECT {$columnName} FROM {$tableName} where entity_id = {$contactID}"; | |
1495 | } | |
6a488035 TO |
1496 | $fileID = CRM_Core_DAO::singleValueQuery($query); |
1497 | } | |
1498 | ||
1499 | $result = array(); | |
1500 | if ($fileID) { | |
1501 | $fileType = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_File', | |
1502 | $fileID, | |
1503 | 'mime_type', | |
1504 | 'id' | |
1505 | ); | |
1506 | $result['file_id'] = $fileID; | |
1507 | ||
1508 | if ($fileType == 'image/jpeg' || | |
1509 | $fileType == 'image/pjpeg' || | |
1510 | $fileType == 'image/gif' || | |
1511 | $fileType == 'image/x-png' || | |
1512 | $fileType == 'image/png' | |
1513 | ) { | |
1514 | $entityId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_EntityFile', | |
1515 | $fileID, | |
1516 | 'entity_id', | |
1517 | 'id' | |
1518 | ); | |
1519 | list($path) = CRM_Core_BAO_File::path($fileID, $entityId, NULL, NULL); | |
1520 | list($imageWidth, $imageHeight) = getimagesize($path); | |
1521 | list($imageThumbWidth, $imageThumbHeight) = CRM_Contact_BAO_Contact::getThumbSize($imageWidth, $imageHeight); | |
1522 | $url = CRM_Utils_System::url('civicrm/file', | |
1523 | "reset=1&id=$fileID&eid=$contactID", | |
1524 | $absolute, NULL, TRUE, TRUE | |
1525 | ); | |
ebb9197b C |
1526 | $result['file_url'] = " |
1527 | <a href=\"$url\" class='crm-image-popup'> | |
1528 | <img src=\"$url\" width=$imageThumbWidth height=$imageThumbHeight/> | |
1529 | </a>"; | |
6a488035 TO |
1530 | // for non image files |
1531 | } | |
1532 | else { | |
1533 | $uri = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_File', | |
1534 | $fileID, | |
1535 | 'uri' | |
1536 | ); | |
1537 | $url = CRM_Utils_System::url('civicrm/file', | |
1538 | "reset=1&id=$fileID&eid=$contactID", | |
1539 | $absolute, NULL, TRUE, TRUE | |
1540 | ); | |
1541 | $result['file_url'] = "<a href=\"$url\">{$uri}</a>"; | |
1542 | } | |
1543 | } | |
1544 | return $result; | |
1545 | } | |
1546 | } | |
1547 | ||
1548 | /** | |
1549 | * Format custom fields before inserting | |
1550 | * | |
6a0b768e TO |
1551 | * @param int $customFieldId |
1552 | * Custom field id. | |
1553 | * @param array $customFormatted | |
1554 | * Formatted array. | |
1555 | * @param mix $value | |
1556 | * Value of custom field. | |
1557 | * @param string $customFieldExtend | |
1558 | * Custom field extends. | |
1559 | * @param int $customValueId | |
1560 | * Custom option value id. | |
1561 | * @param int $entityId | |
1562 | * Entity id (contribution, membership...). | |
1563 | * @param bool $inline | |
1564 | * Consider inline custom groups only. | |
1565 | * @param bool $checkPermission | |
1566 | * If false, do not include permissioning clause. | |
1567 | * @param bool $includeViewOnly | |
1568 | * If true, fields marked 'View Only' are included. Required for APIv3. | |
6a488035 | 1569 | * |
a1a2a83d | 1570 | * @return array|NULL |
a6c01b45 | 1571 | * formatted custom field array |
6a488035 | 1572 | */ |
2da40d21 | 1573 | public static function formatCustomField( |
f9f40af3 | 1574 | $customFieldId, &$customFormatted, $value, |
6a488035 TO |
1575 | $customFieldExtend, $customValueId = NULL, |
1576 | $entityId = NULL, | |
1577 | $inline = FALSE, | |
211353a8 | 1578 | $checkPermission = TRUE, |
f9f40af3 | 1579 | $includeViewOnly = FALSE |
6a488035 TO |
1580 | ) { |
1581 | //get the custom fields for the entity | |
1582 | //subtype and basic type | |
1583 | $customDataSubType = NULL; | |
f71d8e61 | 1584 | if (is_array($customFieldExtend)) { |
1585 | $customFieldExtend = $customFieldExtend[0]; | |
1586 | } | |
2dd1b730 | 1587 | |
6a488035 | 1588 | if (in_array($customFieldExtend, |
f9f40af3 TO |
1589 | CRM_Contact_BAO_ContactType::subTypes() |
1590 | )) { | |
6a488035 TO |
1591 | // This is the case when getFieldsForImport() requires fields |
1592 | // of subtype and its parent.CRM-5143 | |
1593 | $customDataSubType = $customFieldExtend; | |
1594 | $customFieldExtend = CRM_Contact_BAO_ContactType::getBasicType($customDataSubType); | |
1595 | } | |
1596 | ||
1597 | $customFields = CRM_Core_BAO_CustomField::getFields($customFieldExtend, | |
1598 | FALSE, | |
1599 | $inline, | |
1600 | $customDataSubType, | |
1601 | NULL, | |
1602 | FALSE, | |
1603 | FALSE, | |
1604 | $checkPermission | |
1605 | ); | |
1606 | ||
1607 | if (!array_key_exists($customFieldId, $customFields)) { | |
6c552737 | 1608 | return NULL; |
6a488035 TO |
1609 | } |
1610 | ||
1611 | // return if field is a 'code' field | |
211353a8 | 1612 | if (!$includeViewOnly && !empty($customFields[$customFieldId]['is_view'])) { |
a1a2a83d | 1613 | return NULL; |
6a488035 TO |
1614 | } |
1615 | ||
1616 | list($tableName, $columnName, $groupID) = self::getTableColumnGroup($customFieldId); | |
1617 | ||
6a488035 TO |
1618 | if (!$customValueId && |
1619 | // we always create new entites for is_multiple unless specified | |
1620 | !$customFields[$customFieldId]['is_multiple'] && | |
1621 | $entityId | |
1622 | ) { | |
1623 | $query = " | |
1624 | SELECT id | |
1625 | FROM $tableName | |
1626 | WHERE entity_id={$entityId}"; | |
1627 | ||
1628 | $customValueId = CRM_Core_DAO::singleValueQuery($query); | |
1629 | } | |
1630 | ||
1631 | //fix checkbox, now check box always submits values | |
1632 | if ($customFields[$customFieldId]['html_type'] == 'CheckBox') { | |
1633 | if ($value) { | |
1634 | // Note that only during merge this is not an array, and you can directly use value | |
1635 | if (is_array($value)) { | |
1636 | $selectedValues = array(); | |
1637 | foreach ($value as $selId => $val) { | |
1638 | if ($val) { | |
1639 | $selectedValues[] = $selId; | |
1640 | } | |
1641 | } | |
1642 | if (!empty($selectedValues)) { | |
1643 | $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
f9f40af3 TO |
1644 | $selectedValues |
1645 | ) . CRM_Core_DAO::VALUE_SEPARATOR; | |
6a488035 TO |
1646 | } |
1647 | else { | |
1648 | $value = ''; | |
1649 | } | |
1650 | } | |
1651 | } | |
1652 | } | |
1653 | ||
1654 | if ($customFields[$customFieldId]['html_type'] == 'Multi-Select' || | |
1655 | $customFields[$customFieldId]['html_type'] == 'AdvMulti-Select' | |
1656 | ) { | |
1657 | if ($value) { | |
1658 | // Note that only during merge this is not an array, | |
1659 | // and you can directly use value, CRM-4385 | |
1660 | if (is_array($value)) { | |
1661 | $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
f9f40af3 TO |
1662 | array_values($value) |
1663 | ) . CRM_Core_DAO::VALUE_SEPARATOR; | |
6a488035 TO |
1664 | } |
1665 | } | |
1666 | else { | |
1667 | $value = ''; | |
1668 | } | |
1669 | } | |
1670 | ||
1671 | if (($customFields[$customFieldId]['html_type'] == 'Multi-Select' || | |
1672 | $customFields[$customFieldId]['html_type'] == 'AdvMulti-Select' || | |
1673 | $customFields[$customFieldId]['html_type'] == 'CheckBox' | |
1674 | ) && | |
1675 | $customFields[$customFieldId]['data_type'] == 'String' && | |
1676 | !empty($customFields[$customFieldId]['text_length']) && | |
1677 | !empty($value) | |
1678 | ) { | |
1679 | // lets make sure that value is less than the length, else we'll | |
1680 | // be losing some data, CRM-7481 | |
1681 | if (strlen($value) >= $customFields[$customFieldId]['text_length']) { | |
1682 | // need to do a few things here | |
1683 | ||
1684 | // 1. lets find a new length | |
1685 | $newLength = $customFields[$customFieldId]['text_length']; | |
1686 | $minLength = strlen($value); | |
1687 | while ($newLength < $minLength) { | |
1688 | $newLength = $newLength * 2; | |
1689 | } | |
1690 | ||
1691 | // set the custom field meta data to have a length larger than value | |
1692 | // alter the custom value table column to match this length | |
1693 | CRM_Core_BAO_SchemaHandler::alterFieldLength($customFieldId, $tableName, $columnName, $newLength); | |
1694 | } | |
1695 | } | |
1696 | ||
1697 | $date = NULL; | |
1698 | if ($customFields[$customFieldId]['data_type'] == 'Date') { | |
1699 | if (!CRM_Utils_System::isNull($value)) { | |
1700 | $format = $customFields[$customFieldId]['date_format']; | |
1701 | $date = CRM_Utils_Date::processDate($value, NULL, FALSE, 'YmdHis', $format); | |
1702 | } | |
1703 | $value = $date; | |
1704 | } | |
1705 | ||
1706 | if ($customFields[$customFieldId]['data_type'] == 'Float' || | |
1707 | $customFields[$customFieldId]['data_type'] == 'Money' | |
1708 | ) { | |
1709 | if (!$value) { | |
1710 | $value = 0; | |
1711 | } | |
1712 | ||
1713 | if ($customFields[$customFieldId]['data_type'] == 'Money') { | |
1714 | $value = CRM_Utils_Rule::cleanMoney($value); | |
1715 | } | |
1716 | } | |
1717 | ||
1718 | if (($customFields[$customFieldId]['data_type'] == 'StateProvince' || | |
1719 | $customFields[$customFieldId]['data_type'] == 'Country' | |
1720 | ) && | |
1721 | empty($value) | |
1722 | ) { | |
1723 | // CRM-3415 | |
1724 | $value = 0; | |
1725 | } | |
1726 | ||
1727 | $fileId = NULL; | |
1728 | ||
1729 | if ($customFields[$customFieldId]['data_type'] == 'File') { | |
1730 | if (empty($value)) { | |
1731 | return; | |
1732 | } | |
1733 | ||
1734 | $config = CRM_Core_Config::singleton(); | |
1735 | ||
1736 | $fName = $value['name']; | |
1737 | $mimeType = $value['type']; | |
1738 | ||
1739 | $filename = pathinfo($fName, PATHINFO_BASENAME); | |
1740 | ||
1741 | // rename this file to go into the secure directory | |
1742 | if (!rename($fName, $config->customFileUploadDir . $filename)) { | |
1743 | CRM_Core_Error::statusBounce(ts('Could not move custom file to custom upload directory')); | |
6a488035 TO |
1744 | } |
1745 | ||
1746 | if ($customValueId) { | |
1747 | $query = " | |
1748 | SELECT $columnName | |
1749 | FROM $tableName | |
1750 | WHERE id = %1"; | |
1751 | $params = array(1 => array($customValueId, 'Integer')); | |
1752 | $fileId = CRM_Core_DAO::singleValueQuery($query, $params); | |
1753 | } | |
1754 | ||
1755 | $fileDAO = new CRM_Core_DAO_File(); | |
1756 | ||
1757 | if ($fileId) { | |
1758 | $fileDAO->id = $fileId; | |
1759 | } | |
1760 | ||
1761 | $fileDAO->uri = $filename; | |
1762 | $fileDAO->mime_type = $mimeType; | |
1763 | $fileDAO->upload_date = date('Ymdhis'); | |
1764 | $fileDAO->save(); | |
1765 | $fileId = $fileDAO->id; | |
1766 | $value = $filename; | |
1767 | } | |
1768 | ||
1769 | if (!is_array($customFormatted)) { | |
1770 | $customFormatted = array(); | |
1771 | } | |
1772 | ||
1773 | if (!array_key_exists($customFieldId, $customFormatted)) { | |
1774 | $customFormatted[$customFieldId] = array(); | |
1775 | } | |
1776 | ||
1777 | $index = -1; | |
1778 | if ($customValueId) { | |
1779 | $index = $customValueId; | |
1780 | } | |
1781 | ||
1782 | if (!array_key_exists($index, $customFormatted[$customFieldId])) { | |
1783 | $customFormatted[$customFieldId][$index] = array(); | |
1784 | } | |
1785 | $customFormatted[$customFieldId][$index] = array( | |
1786 | 'id' => $customValueId > 0 ? $customValueId : NULL, | |
1787 | 'value' => $value, | |
1788 | 'type' => $customFields[$customFieldId]['data_type'], | |
1789 | 'custom_field_id' => $customFieldId, | |
1790 | 'custom_group_id' => $groupID, | |
1791 | 'table_name' => $tableName, | |
1792 | 'column_name' => $columnName, | |
1793 | 'file_id' => $fileId, | |
1794 | 'is_multiple' => $customFields[$customFieldId]['is_multiple'], | |
1795 | ); | |
1796 | ||
1797 | //we need to sort so that custom fields are created in the order of entry | |
1798 | krsort($customFormatted[$customFieldId]); | |
1799 | return $customFormatted; | |
1800 | } | |
1801 | ||
b5c2afd0 | 1802 | /** |
c490a46a | 1803 | * @param array $params |
b5c2afd0 EM |
1804 | * |
1805 | * @return array | |
1806 | */ | |
00be9182 | 1807 | public static function &defaultCustomTableSchema(&$params) { |
6a488035 TO |
1808 | // add the id and extends_id |
1809 | $table = array( | |
1810 | 'name' => $params['name'], | |
1811 | 'is_multiple' => $params['is_multiple'], | |
1812 | 'attributes' => "ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci", | |
1813 | 'fields' => array( | |
1814 | array( | |
1815 | 'name' => 'id', | |
1816 | 'type' => 'int unsigned', | |
1817 | 'primary' => TRUE, | |
1818 | 'required' => TRUE, | |
1819 | 'attributes' => 'AUTO_INCREMENT', | |
1820 | 'comment' => 'Default MySQL primary key', | |
1821 | ), | |
1822 | array( | |
1823 | 'name' => 'entity_id', | |
1824 | 'type' => 'int unsigned', | |
1825 | 'required' => TRUE, | |
1826 | 'comment' => 'Table that this extends', | |
1827 | 'fk_table_name' => $params['extends_name'], | |
1828 | 'fk_field_name' => 'id', | |
1829 | 'fk_attributes' => 'ON DELETE CASCADE', | |
1830 | ), | |
1831 | ), | |
1832 | ); | |
1833 | ||
1834 | if (!$params['is_multiple']) { | |
1835 | $table['indexes'] = array( | |
1836 | array( | |
1837 | 'unique' => TRUE, | |
1838 | 'field_name_1' => 'entity_id', | |
1839 | ), | |
1840 | ); | |
1841 | } | |
1842 | return $table; | |
1843 | } | |
1844 | ||
b5c2afd0 EM |
1845 | /** |
1846 | * @param $field | |
1847 | * @param $operation | |
1848 | * @param bool $indexExist | |
1849 | * @param bool $triggerRebuild | |
1850 | */ | |
00be9182 | 1851 | public static function createField($field, $operation, $indexExist = FALSE, $triggerRebuild = TRUE) { |
6a488035 TO |
1852 | $tableName = CRM_Core_DAO::getFieldValue( |
1853 | 'CRM_Core_DAO_CustomGroup', | |
1854 | $field->custom_group_id, | |
1855 | 'table_name' | |
1856 | ); | |
1857 | ||
1858 | $params = array( | |
1859 | 'table_name' => $tableName, | |
1860 | 'operation' => $operation, | |
1861 | 'name' => $field->column_name, | |
1862 | 'type' => CRM_Core_BAO_CustomValueTable::fieldToSQLType( | |
1863 | $field->data_type, | |
1864 | $field->text_length | |
1865 | ), | |
1866 | 'required' => $field->is_required, | |
1867 | 'searchable' => $field->is_searchable, | |
1868 | ); | |
1869 | ||
1870 | if ($operation == 'delete') { | |
1871 | $fkName = "{$tableName}_{$field->column_name}"; | |
1872 | if (strlen($fkName) >= 48) { | |
1873 | $fkName = substr($fkName, 0, 32) . '_' . substr(md5($fkName), 0, 16); | |
1874 | } | |
1875 | $params['fkName'] = $fkName; | |
1876 | } | |
1877 | if ($field->data_type == 'Country' && $field->html_type == 'Select Country') { | |
1878 | $params['fk_table_name'] = 'civicrm_country'; | |
1879 | $params['fk_field_name'] = 'id'; | |
1880 | $params['fk_attributes'] = 'ON DELETE SET NULL'; | |
1881 | } | |
1882 | elseif ($field->data_type == 'Country' && $field->html_type == 'Multi-Select Country') { | |
1883 | $params['type'] = 'varchar(255)'; | |
1884 | } | |
1885 | elseif ($field->data_type == 'StateProvince' && $field->html_type == 'Select State/Province') { | |
1886 | $params['fk_table_name'] = 'civicrm_state_province'; | |
1887 | $params['fk_field_name'] = 'id'; | |
1888 | $params['fk_attributes'] = 'ON DELETE SET NULL'; | |
1889 | } | |
1890 | elseif ($field->data_type == 'StateProvince' && $field->html_type == 'Multi-Select State/Province') { | |
1891 | $params['type'] = 'varchar(255)'; | |
1892 | } | |
1893 | elseif ($field->data_type == 'File') { | |
1894 | $params['fk_table_name'] = 'civicrm_file'; | |
1895 | $params['fk_field_name'] = 'id'; | |
1896 | $params['fk_attributes'] = 'ON DELETE SET NULL'; | |
1897 | } | |
1898 | elseif ($field->data_type == 'ContactReference') { | |
1899 | $params['fk_table_name'] = 'civicrm_contact'; | |
1900 | $params['fk_field_name'] = 'id'; | |
1901 | $params['fk_attributes'] = 'ON DELETE SET NULL'; | |
1902 | } | |
1c96981b | 1903 | if (isset($field->default_value)) { |
6a488035 TO |
1904 | $params['default'] = "'{$field->default_value}'"; |
1905 | } | |
1906 | ||
1907 | CRM_Core_BAO_SchemaHandler::alterFieldSQL($params, $indexExist, $triggerRebuild); | |
1908 | } | |
1909 | ||
1910 | /** | |
1911 | * Determine whether it would be safe to move a field | |
1912 | * | |
6a0b768e TO |
1913 | * @param int $fieldID |
1914 | * FK to civicrm_custom_field. | |
1915 | * @param int $newGroupID | |
1916 | * FK to civicrm_custom_group. | |
6a488035 | 1917 | * |
5c766a0b TO |
1918 | * @return array |
1919 | * array(string) or TRUE | |
6a488035 | 1920 | */ |
00be9182 | 1921 | public static function _moveFieldValidate($fieldID, $newGroupID) { |
6a488035 TO |
1922 | $errors = array(); |
1923 | ||
1924 | $field = new CRM_Core_DAO_CustomField(); | |
1925 | $field->id = $fieldID; | |
1926 | if (!$field->find(TRUE)) { | |
1927 | $errors['fieldID'] = 'Invalid ID for custom field'; | |
1928 | return $errors; | |
1929 | } | |
1930 | ||
1931 | $oldGroup = new CRM_Core_DAO_CustomGroup(); | |
1932 | $oldGroup->id = $field->custom_group_id; | |
1933 | if (!$oldGroup->find(TRUE)) { | |
1934 | $errors['fieldID'] = 'Invalid ID for old custom group'; | |
1935 | return $errors; | |
1936 | } | |
1937 | ||
1938 | $newGroup = new CRM_Core_DAO_CustomGroup(); | |
1939 | $newGroup->id = $newGroupID; | |
1940 | if (!$newGroup->find(TRUE)) { | |
1941 | $errors['newGroupID'] = 'Invalid ID for new custom group'; | |
1942 | return $errors; | |
1943 | } | |
1944 | ||
1945 | $query = " | |
1946 | SELECT b.id | |
1947 | FROM civicrm_custom_field a | |
1948 | INNER JOIN civicrm_custom_field b | |
1949 | WHERE a.id = %1 | |
1950 | AND a.label = b.label | |
1951 | AND b.custom_group_id = %2 | |
1952 | "; | |
1953 | $params = array( | |
1954 | 1 => array($field->id, 'Integer'), | |
1955 | 2 => array($newGroup->id, 'Integer'), | |
1956 | ); | |
1957 | $count = CRM_Core_DAO::singleValueQuery($query, $params); | |
1958 | if ($count > 0) { | |
1959 | $errors['newGroupID'] = ts('A field of the same label exists in the destination group'); | |
1960 | } | |
1961 | ||
1962 | $tableName = $oldGroup->table_name; | |
1963 | $columnName = $field->column_name; | |
1964 | ||
1965 | $query = " | |
1966 | SELECT count(*) | |
1967 | FROM $tableName | |
1968 | WHERE $columnName is not null | |
1969 | "; | |
1970 | $count = CRM_Core_DAO::singleValueQuery($query, | |
1971 | CRM_Core_DAO::$_nullArray | |
1972 | ); | |
1973 | if ($count > 0) { | |
1974 | $query = " | |
1975 | SELECT extends | |
1976 | FROM civicrm_custom_group | |
1977 | WHERE id IN ( %1, %2 ) | |
1978 | "; | |
f9f40af3 TO |
1979 | $params = array( |
1980 | 1 => array($oldGroup->id, 'Integer'), | |
6a488035 TO |
1981 | 2 => array($newGroup->id, 'Integer'), |
1982 | ); | |
1983 | ||
1984 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
1985 | $extends = array(); | |
1986 | while ($dao->fetch()) { | |
1987 | $extends[] = $dao->extends; | |
1988 | } | |
1989 | if ($extends[0] != $extends[1]) { | |
1990 | $errors['newGroupID'] = ts('The destination group extends a different entity type.'); | |
1991 | } | |
1992 | } | |
1993 | ||
1994 | return empty($errors) ? TRUE : $errors; | |
1995 | } | |
1996 | ||
1997 | /** | |
1998 | * Move a custom data field from one group (table) to another | |
1999 | * | |
6a0b768e TO |
2000 | * @param int $fieldID |
2001 | * FK to civicrm_custom_field. | |
2002 | * @param int $newGroupID | |
2003 | * FK to civicrm_custom_group. | |
6a488035 TO |
2004 | * |
2005 | * @return void | |
2006 | */ | |
00be9182 | 2007 | public static function moveField($fieldID, $newGroupID) { |
6a488035 TO |
2008 | $validation = self::_moveFieldValidate($fieldID, $newGroupID); |
2009 | if (TRUE !== $validation) { | |
2010 | CRM_Core_Error::fatal(implode(' ', $validation)); | |
2011 | } | |
2012 | $field = new CRM_Core_DAO_CustomField(); | |
2013 | $field->id = $fieldID; | |
2014 | $field->find(TRUE); | |
2015 | ||
2016 | $newGroup = new CRM_Core_DAO_CustomGroup(); | |
2017 | $newGroup->id = $newGroupID; | |
2018 | $newGroup->find(TRUE); | |
2019 | ||
2020 | $oldGroup = new CRM_Core_DAO_CustomGroup(); | |
2021 | $oldGroup->id = $field->custom_group_id; | |
2022 | $oldGroup->find(TRUE); | |
2023 | ||
2024 | $add = clone$field; | |
2025 | $add->custom_group_id = $newGroup->id; | |
2026 | self::createField($add, 'add'); | |
2027 | ||
2028 | $sql = "INSERT INTO {$newGroup->table_name} (entity_id, {$field->column_name}) | |
2029 | SELECT entity_id, {$field->column_name} FROM {$oldGroup->table_name} | |
2030 | ON DUPLICATE KEY UPDATE {$field->column_name} = {$oldGroup->table_name}.{$field->column_name} | |
2031 | "; | |
2032 | CRM_Core_DAO::executeQuery($sql); | |
2033 | ||
2034 | $del = clone$field; | |
2035 | $del->custom_group_id = $oldGroup->id; | |
2036 | self::createField($del, 'delete'); | |
2037 | ||
2038 | $add->save(); | |
2039 | ||
2040 | CRM_Utils_System::flushCache(); | |
2041 | } | |
2042 | ||
2043 | /** | |
2044 | * Get the database table name and column name for a custom field | |
2045 | * | |
6a0b768e TO |
2046 | * @param int $fieldID |
2047 | * The fieldID of the custom field. | |
2048 | * @param bool $force | |
2049 | * Force the sql to be run again (primarily used for tests). | |
6a488035 | 2050 | * |
a6c01b45 CW |
2051 | * @return array |
2052 | * fatal is fieldID does not exists, else array of tableName, columnName | |
6a488035 | 2053 | */ |
00be9182 | 2054 | public static function getTableColumnGroup($fieldID, $force = FALSE) { |
f9f40af3 TO |
2055 | $cacheKey = "CRM_Core_DAO_CustomField_CustomGroup_TableColumn_{$fieldID}"; |
2056 | $cache = CRM_Utils_Cache::singleton(); | |
6a488035 TO |
2057 | $fieldValues = $cache->get($cacheKey); |
2058 | if (empty($fieldValues) || $force) { | |
2059 | $query = " | |
2060 | SELECT cg.table_name, cf.column_name, cg.id | |
2061 | FROM civicrm_custom_group cg, | |
2062 | civicrm_custom_field cf | |
2063 | WHERE cf.custom_group_id = cg.id | |
2064 | AND cf.id = %1"; | |
2065 | $params = array(1 => array($fieldID, 'Integer')); | |
2066 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
2067 | ||
2068 | if (!$dao->fetch()) { | |
2069 | CRM_Core_Error::fatal(); | |
2070 | } | |
2071 | $dao->free(); | |
2072 | $fieldValues = array($dao->table_name, $dao->column_name, $dao->id); | |
2073 | $cache->set($cacheKey, $fieldValues); | |
2074 | } | |
2075 | return $fieldValues; | |
2076 | } | |
2077 | ||
2078 | /** | |
100fef9d | 2079 | * Get custom option groups |
6a488035 | 2080 | * |
6a0b768e TO |
2081 | * @param array $includeFieldIds |
2082 | * Ids of custom fields for which. | |
16b10e64 | 2083 | * option groups must be included. |
6a488035 TO |
2084 | * |
2085 | * Currently this is required in the cases where option groups are to be included | |
2086 | * for inactive fields : CRM-5369 | |
2087 | * | |
6a488035 | 2088 | * |
72b3a70c | 2089 | * @return mixed |
6a488035 TO |
2090 | */ |
2091 | public static function &customOptionGroup($includeFieldIds = NULL) { | |
2092 | static $customOptionGroup = NULL; | |
2093 | ||
2094 | $cacheKey = (empty($includeFieldIds)) ? 'onlyActive' : 'force'; | |
2095 | if ($cacheKey == 'force') { | |
2096 | $customOptionGroup[$cacheKey] = NULL; | |
2097 | } | |
2098 | ||
a7488080 | 2099 | if (empty($customOptionGroup[$cacheKey])) { |
6a488035 TO |
2100 | $whereClause = '( g.is_active = 1 AND f.is_active = 1 )'; |
2101 | ||
2102 | //support for single as well as array format. | |
2103 | if (!empty($includeFieldIds)) { | |
2104 | if (is_array($includeFieldIds)) { | |
2105 | $includeFieldIds = implode(',', $includeFieldIds); | |
2106 | } | |
2107 | $whereClause .= "OR f.id IN ( $includeFieldIds )"; | |
2108 | } | |
2109 | ||
2110 | $query = " | |
2111 | SELECT g.id, g.title | |
2112 | FROM civicrm_option_group g | |
2113 | INNER JOIN civicrm_custom_field f ON ( g.id = f.option_group_id ) | |
2114 | WHERE {$whereClause}"; | |
2115 | ||
2116 | $dao = CRM_Core_DAO::executeQuery($query); | |
2117 | while ($dao->fetch()) { | |
2118 | $customOptionGroup[$cacheKey][$dao->id] = $dao->title; | |
2119 | } | |
2120 | } | |
2121 | ||
2122 | return $customOptionGroup[$cacheKey]; | |
2123 | } | |
2124 | ||
2125 | /** | |
100fef9d | 2126 | * Fix orphan groups |
6a488035 | 2127 | * |
6a0b768e TO |
2128 | * @param int $customFieldId |
2129 | * Custom field id. | |
2130 | * @param int $optionGroupId | |
2131 | * Option group id. | |
6a488035 | 2132 | * |
6a488035 | 2133 | * @return void |
6a488035 | 2134 | */ |
00be9182 | 2135 | public static function fixOptionGroups($customFieldId, $optionGroupId) { |
6a488035 TO |
2136 | // check if option group belongs to any custom Field else delete |
2137 | // get the current option group | |
2138 | $currentOptionGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', | |
2139 | $customFieldId, | |
2140 | 'option_group_id' | |
2141 | ); | |
2142 | // get the updated option group | |
2143 | // if both are same return | |
2144 | if ($currentOptionGroupId == $optionGroupId) { | |
2145 | return; | |
2146 | } | |
2147 | ||
2148 | // check if option group is related to any other field | |
2149 | self::checkOptionGroup($currentOptionGroupId); | |
2150 | } | |
2151 | ||
2152 | /** | |
100fef9d | 2153 | * Check if option group is related to more than one |
6a488035 TO |
2154 | * custom field |
2155 | * | |
6a0b768e TO |
2156 | * @param int $optionGroupId |
2157 | * Option group id. | |
77b97be7 EM |
2158 | * |
2159 | * @return void | |
6a488035 | 2160 | */ |
00be9182 | 2161 | public static function checkOptionGroup($optionGroupId) { |
6a488035 TO |
2162 | $query = " |
2163 | SELECT count(*) | |
2164 | FROM civicrm_custom_field | |
2165 | WHERE option_group_id = {$optionGroupId}"; | |
2166 | ||
2167 | $count = CRM_Core_DAO::singleValueQuery($query); | |
2168 | ||
2169 | if ($count < 2) { | |
2170 | //delete the option group | |
2171 | CRM_Core_BAO_OptionGroup::del($optionGroupId); | |
2172 | } | |
2173 | } | |
2174 | ||
b5c2afd0 | 2175 | /** |
100fef9d | 2176 | * @param int $optionGroupId |
b5c2afd0 EM |
2177 | * @param $htmlType |
2178 | * | |
2179 | * @return null|string | |
2180 | */ | |
00be9182 | 2181 | public static function getOptionGroupDefault($optionGroupId, $htmlType) { |
6a488035 TO |
2182 | $query = " |
2183 | SELECT default_value, html_type | |
2184 | FROM civicrm_custom_field | |
2185 | WHERE option_group_id = {$optionGroupId} | |
2186 | AND default_value IS NOT NULL | |
2187 | ORDER BY html_type"; | |
2188 | ||
f9f40af3 TO |
2189 | $dao = CRM_Core_DAO::executeQuery($query); |
2190 | $defaultValue = NULL; | |
6a488035 TO |
2191 | $defaultHTMLType = NULL; |
2192 | while ($dao->fetch()) { | |
2193 | if ($dao->html_type == $htmlType) { | |
2194 | return $dao->default_value; | |
2195 | } | |
2196 | if ($defaultValue == NULL) { | |
2197 | $defaultValue = $dao->default_value; | |
2198 | $defaultHTMLType = $dao->html_type; | |
2199 | } | |
2200 | } | |
2201 | ||
2202 | // some conversions are needed if either the old or new has a html type which has potential | |
2203 | // multiple default values. | |
2204 | if (($htmlType == 'CheckBox' || $htmlType == 'Multi-Select') && | |
2205 | ($defaultHTMLType != 'CheckBox' && $defaultHTMLType != 'Multi-Select') | |
2206 | ) { | |
2207 | $defaultValue = CRM_Core_DAO::VALUE_SEPARATOR . $defaultValue . CRM_Core_DAO::VALUE_SEPARATOR; | |
2208 | } | |
2209 | elseif (($defaultHTMLType == 'CheckBox' || $defaultHTMLType == 'Multi-Select') && | |
2210 | ($htmlType != 'CheckBox' && $htmlType != 'Multi-Select') | |
2211 | ) { | |
2212 | $defaultValue = substr($defaultValue, 1, -1); | |
2213 | $values = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
2214 | substr($defaultValue, 1, -1) | |
2215 | ); | |
2216 | $defaultValue = $values[0]; | |
2217 | } | |
2218 | ||
2219 | return $defaultValue; | |
2220 | } | |
2221 | ||
b5c2afd0 | 2222 | /** |
c490a46a | 2223 | * @param array $params |
b5c2afd0 | 2224 | * @param $customFields |
100fef9d | 2225 | * @param int $entityID |
b5c2afd0 EM |
2226 | * @param $customFieldExtends |
2227 | * @param bool $inline | |
2228 | * | |
2229 | * @return array | |
2230 | */ | |
2da40d21 | 2231 | public static function postProcess( |
f9f40af3 | 2232 | &$params, |
6a488035 TO |
2233 | &$customFields, |
2234 | $entityID, | |
2235 | $customFieldExtends, | |
2236 | $inline = FALSE | |
2237 | ) { | |
2238 | $customData = array(); | |
2239 | ||
2240 | foreach ($params as $key => $value) { | |
2241 | if ($customFieldInfo = CRM_Core_BAO_CustomField::getKeyID($key, TRUE)) { | |
2242 | ||
2243 | // for autocomplete transfer hidden value instead of label | |
2244 | if ($params[$key] && isset($params[$key . '_id'])) { | |
2245 | $value = $params[$key . '_id']; | |
2246 | } | |
2247 | ||
2248 | // we need to append time with date | |
2249 | if ($params[$key] && isset($params[$key . '_time'])) { | |
2250 | $value .= ' ' . $params[$key . '_time']; | |
2251 | } | |
2252 | ||
2253 | CRM_Core_BAO_CustomField::formatCustomField($customFieldInfo[0], | |
2254 | $customData, | |
2255 | $value, | |
2256 | $customFieldExtends, | |
2257 | $customFieldInfo[1], | |
2258 | $entityID, | |
2259 | $inline | |
2260 | ); | |
2261 | } | |
2262 | } | |
2263 | return $customData; | |
2264 | } | |
2265 | ||
b5c2afd0 EM |
2266 | /** |
2267 | * @param $field | |
2268 | * @param $options | |
2269 | * | |
2270 | * @throws Exception | |
2271 | */ | |
00be9182 | 2272 | public static function buildOption($field, &$options) { |
deceed83 CW |
2273 | // Fixme - adding anything but options to the $options array is a bad idea |
2274 | // What if an option had the key 'attributes'? | |
6a488035 TO |
2275 | $options['attributes'] = array( |
2276 | 'label' => $field['label'], | |
2277 | 'data_type' => $field['data_type'], | |
2278 | 'html_type' => $field['html_type'], | |
2279 | ); | |
2280 | ||
2281 | $optionGroupID = NULL; | |
2282 | if (($field['html_type'] == 'CheckBox' || | |
f9f40af3 TO |
2283 | $field['html_type'] == 'Radio' || |
2284 | $field['html_type'] == 'Select' || | |
2285 | $field['html_type'] == 'AdvMulti-Select' || | |
2286 | $field['html_type'] == 'Multi-Select' || | |
2287 | ($field['html_type'] == 'Autocomplete-Select' && $field['data_type'] != 'ContactReference') | |
2288 | ) | |
2289 | ) { | |
6a488035 TO |
2290 | if ($field['option_group_id']) { |
2291 | $optionGroupID = $field['option_group_id']; | |
2292 | } | |
2293 | elseif ($field['data_type'] != 'Boolean') { | |
2294 | CRM_Core_Error::fatal(); | |
2295 | } | |
2296 | } | |
2297 | ||
2298 | // build the cache for custom values with options (label => value) | |
2299 | if ($optionGroupID != NULL) { | |
2300 | $query = " | |
2301 | SELECT label, value | |
2302 | FROM civicrm_option_value | |
2303 | WHERE option_group_id = $optionGroupID | |
2304 | "; | |
2305 | ||
2306 | $dao = CRM_Core_DAO::executeQuery($query); | |
2307 | while ($dao->fetch()) { | |
2308 | if ($field['data_type'] == 'Int' || $field['data_type'] == 'Float') { | |
2309 | $num = round($dao->value, 2); | |
2310 | $options["$num"] = $dao->label; | |
2311 | } | |
2312 | else { | |
2313 | $options[$dao->value] = $dao->label; | |
2314 | } | |
2315 | } | |
2316 | ||
2317 | CRM_Utils_Hook::customFieldOptions($field['id'], $options); | |
2318 | } | |
2319 | } | |
2320 | ||
b5c2afd0 EM |
2321 | /** |
2322 | * @param $fieldLabel | |
2323 | * @param null $groupTitle | |
2324 | * | |
2325 | * @return null | |
2326 | */ | |
00be9182 | 2327 | public static function getCustomFieldID($fieldLabel, $groupTitle = NULL) { |
6a488035 TO |
2328 | $params = array(1 => array($fieldLabel, 'String')); |
2329 | if ($groupTitle) { | |
2330 | $params[2] = array($groupTitle, 'String'); | |
2331 | $sql = " | |
2332 | SELECT f.id | |
2333 | FROM civicrm_custom_field f | |
2334 | INNER JOIN civicrm_custom_group g ON f.custom_group_id = g.id | |
2335 | WHERE ( f.label = %1 OR f.name = %1 ) | |
2336 | AND ( g.title = %2 OR g.name = %2 ) | |
2337 | "; | |
2338 | } | |
2339 | else { | |
2340 | $sql = " | |
2341 | SELECT f.id | |
2342 | FROM civicrm_custom_field f | |
2343 | WHERE ( f.label = %1 OR f.name = %1 ) | |
2344 | "; | |
2345 | } | |
2346 | ||
2347 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
2348 | if ($dao->fetch() && | |
2349 | $dao->N == 1 | |
2350 | ) { | |
2351 | return $dao->id; | |
2352 | } | |
2353 | else { | |
2354 | return NULL; | |
2355 | } | |
2356 | } | |
2357 | ||
2358 | /** | |
2359 | * Given ID of a custom field, return its name as well as the name of the custom group it belongs to. | |
6a488035 | 2360 | */ |
00be9182 | 2361 | public static function getNameFromID($ids) { |
6a488035 TO |
2362 | if (is_array($ids)) { |
2363 | $ids = implode(',', $ids); | |
2364 | } | |
2365 | $sql = " | |
2366 | SELECT f.id, f.name AS field_name, f.label AS field_label, g.name AS group_name, g.title AS group_title | |
2367 | FROM civicrm_custom_field f | |
2368 | INNER JOIN civicrm_custom_group g ON f.custom_group_id = g.id | |
2369 | WHERE f.id IN ($ids)"; | |
2370 | ||
6a488035 TO |
2371 | $dao = CRM_Core_DAO::executeQuery($sql); |
2372 | $result = array(); | |
2373 | while ($dao->fetch()) { | |
2374 | $result[$dao->id] = array( | |
2375 | 'field_name' => $dao->field_name, | |
2376 | 'field_label' => $dao->field_label, | |
2377 | 'group_name' => $dao->group_name, | |
2378 | 'group_title' => $dao->group_title, | |
2379 | ); | |
2380 | } | |
2381 | return $result; | |
2382 | } | |
2383 | ||
2384 | /** | |
2385 | * Validate custom data. | |
2386 | * | |
6a0b768e TO |
2387 | * @param array $params |
2388 | * Custom data submitted. | |
16b10e64 | 2389 | * ie array( 'custom_1' => 'validate me' ); |
6a488035 | 2390 | * |
a6c01b45 CW |
2391 | * @return array |
2392 | * validation errors. | |
6a488035 | 2393 | */ |
00be9182 | 2394 | public static function validateCustomData($params) { |
6a488035 TO |
2395 | $errors = array(); |
2396 | if (!is_array($params) || empty($params)) { | |
2397 | return $errors; | |
2398 | } | |
2399 | ||
6a488035 TO |
2400 | //pick up profile fields. |
2401 | $profileFields = array(); | |
2402 | $ufGroupId = CRM_Utils_Array::value('ufGroupId', $params); | |
2403 | if ($ufGroupId) { | |
2404 | $profileFields = CRM_Core_BAO_UFGroup::getFields($ufGroupId, | |
2405 | FALSE, | |
2406 | CRM_Core_Action::VIEW | |
2407 | ); | |
2408 | } | |
2409 | ||
2410 | //lets start w/ params. | |
2411 | foreach ($params as $key => $value) { | |
2412 | $customFieldID = self::getKeyID($key); | |
2413 | if (!$customFieldID) { | |
2414 | continue; | |
2415 | } | |
2416 | ||
2417 | //load the structural info for given field. | |
2418 | $field = new CRM_Core_DAO_CustomField(); | |
2419 | $field->id = $customFieldID; | |
2420 | if (!$field->find(TRUE)) { | |
2421 | continue; | |
2422 | } | |
2423 | $dataType = $field->data_type; | |
2424 | ||
2425 | $profileField = CRM_Utils_Array::value($key, $profileFields, array()); | |
f9f40af3 TO |
2426 | $fieldTitle = CRM_Utils_Array::value('title', $profileField); |
2427 | $isRequired = CRM_Utils_Array::value('is_required', $profileField); | |
6a488035 TO |
2428 | if (!$fieldTitle) { |
2429 | $fieldTitle = $field->label; | |
2430 | } | |
2431 | ||
2432 | //no need to validate. | |
2433 | if (CRM_Utils_System::isNull($value) && !$isRequired) { | |
2434 | continue; | |
2435 | } | |
2436 | ||
2437 | //lets validate first for required field. | |
2438 | if ($isRequired && CRM_Utils_System::isNull($value)) { | |
2439 | $errors[$key] = ts('%1 is a required field.', array(1 => $fieldTitle)); | |
2440 | continue; | |
2441 | } | |
2442 | ||
2443 | //now time to take care of custom field form rules. | |
2444 | $ruleName = $errorMsg = NULL; | |
2445 | switch ($dataType) { | |
2446 | case 'Int': | |
2447 | $ruleName = 'integer'; | |
2448 | $errorMsg = ts('%1 must be an integer (whole number).', | |
2449 | array(1 => $fieldTitle) | |
2450 | ); | |
2451 | break; | |
2452 | ||
2453 | case 'Money': | |
2454 | $ruleName = 'money'; | |
2455 | $errorMsg = ts('%1 must in proper money format. (decimal point/comma/space is allowed).', | |
2456 | array(1 => $fieldTitle) | |
2457 | ); | |
2458 | break; | |
2459 | ||
2460 | case 'Float': | |
2461 | $ruleName = 'numeric'; | |
2462 | $errorMsg = ts('%1 must be a number (with or without decimal point).', | |
2463 | array(1 => $fieldTitle) | |
2464 | ); | |
2465 | break; | |
2466 | ||
2467 | case 'Link': | |
2468 | $ruleName = 'wikiURL'; | |
2469 | $errorMsg = ts('%1 must be valid Website.', | |
2470 | array(1 => $fieldTitle) | |
2471 | ); | |
2472 | break; | |
2473 | } | |
2474 | ||
2475 | if ($ruleName && !CRM_Utils_System::isNull($value)) { | |
2476 | $valid = FALSE; | |
2477 | $funName = "CRM_Utils_Rule::{$ruleName}"; | |
2478 | if (is_callable($funName)) { | |
2479 | $valid = call_user_func($funName, $value); | |
2480 | } | |
2481 | if (!$valid) { | |
2482 | $errors[$key] = $errorMsg; | |
2483 | } | |
2484 | } | |
2485 | } | |
2486 | ||
2487 | return $errors; | |
2488 | } | |
2489 | ||
ffd93213 | 2490 | /** |
100fef9d | 2491 | * @param int $customId |
ffd93213 EM |
2492 | * |
2493 | * @return bool | |
2494 | */ | |
00be9182 | 2495 | public static function isMultiRecordField($customId) { |
6a488035 TO |
2496 | $isMultipleWithGid = FALSE; |
2497 | if (!is_numeric($customId)) { | |
2498 | $customId = self::getKeyID($customId); | |
2499 | } | |
2500 | if (is_numeric($customId)) { | |
2501 | $sql = "SELECT cg.id cgId | |
2502 | FROM civicrm_custom_group cg | |
2503 | INNER JOIN civicrm_custom_field cf | |
2504 | ON cg.id = cf.custom_group_id | |
2505 | WHERE cf.id = %1 AND cg.is_multiple = 1"; | |
2506 | $params[1] = array($customId, 'Integer'); | |
2507 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
2508 | if ($dao->fetch()) { | |
2509 | if ($dao->cgId) { | |
2510 | $isMultipleWithGid = $dao->cgId; | |
2511 | } | |
2512 | } | |
2513 | } | |
2514 | ||
2515 | return $isMultipleWithGid; | |
2516 | } | |
3130209f CW |
2517 | |
2518 | /** | |
2519 | * Does this field store a serialized string? | |
2520 | * @param CRM_Core_DAO_CustomField|array $field | |
2521 | * @return bool | |
2522 | */ | |
00be9182 | 2523 | public static function isSerialized($field) { |
3130209f CW |
2524 | // Fields retrieved via api are an array, or from the dao are an object. We'll accept either. |
2525 | $field = (array) $field; | |
2526 | // FIXME: Currently the only way to know if data is serialized is by looking at the html_type. It would be cleaner to decouple this. | |
2527 | return ($field['html_type'] == 'CheckBox' || strpos($field['html_type'], 'Multi') !== FALSE); | |
2528 | } | |
96025800 | 2529 | |
6a488035 | 2530 | } |