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