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