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