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