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