Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
8eedd10a | 19 | * This class contains function for UFField. |
6a488035 TO |
20 | */ |
21 | class CRM_Core_BAO_UFField extends CRM_Core_DAO_UFField { | |
22 | ||
23 | /** | |
fe482240 | 24 | * Batch entry fields. |
518fa0ee | 25 | * @var array |
6a488035 TO |
26 | */ |
27 | private static $_contriBatchEntryFields = NULL; | |
28 | private static $_memberBatchEntryFields = NULL; | |
29 | ||
c8ec6bbf SB |
30 | /** |
31 | * Create UFField object. | |
32 | * | |
33 | * @param array $params | |
34 | * Array per getfields metadata. | |
35 | * | |
36 | * @return \CRM_Core_BAO_UFField | |
37 | * @throws \API_Exception | |
38 | */ | |
aa9732b9 CW |
39 | public static function create($params) { |
40 | $id = CRM_Utils_Array::value('id', $params); | |
c8ec6bbf | 41 | |
aa9732b9 CW |
42 | // Merge in data from existing field |
43 | if (!empty($id)) { | |
44 | $UFField = new CRM_Core_BAO_UFField(); | |
45 | $UFField->id = $params['id']; | |
46 | if ($UFField->find(TRUE)) { | |
47 | $defaults = $UFField->toArray(); | |
48 | // This will be calculated based on field name | |
49 | unset($defaults['field_type']); | |
50 | $params += $defaults; | |
51 | } | |
52 | else { | |
53 | throw new API_Exception("UFFIeld id {$params['id']} not found."); | |
54 | } | |
55 | } | |
c8ec6bbf | 56 | |
aa9732b9 CW |
57 | // Validate field_name |
58 | if (strpos($params['field_name'], 'formatting') !== 0 && !CRM_Core_BAO_UFField::isValidFieldName($params['field_name'])) { | |
c8ec6bbf SB |
59 | throw new API_Exception('The field_name is not valid'); |
60 | } | |
61 | ||
aa9732b9 CW |
62 | // Supply default label if not set |
63 | if (empty($id) && !isset($params['label'])) { | |
64 | $params['label'] = self::getAvailableFieldTitles()[$params['field_name']]; | |
c8ec6bbf SB |
65 | } |
66 | ||
aa9732b9 CW |
67 | // Supply field_type if not set |
68 | if (empty($params['field_type']) && strpos($params['field_name'], 'formatting') !== 0) { | |
69 | $params['field_type'] = CRM_Utils_Array::pathGet(self::getAvailableFieldsFlat(), [$params['field_name'], 'field_type']); | |
70 | } | |
71 | elseif (empty($params['field_type'])) { | |
72 | $params['field_type'] = 'Formatting'; | |
73 | } | |
74 | ||
75 | // Generate unique name for formatting fields | |
76 | if ($params['field_name'] === 'formatting') { | |
77 | $params['field_name'] = 'formatting_' . substr(uniqid(), -4); | |
c8ec6bbf | 78 | } |
c8ec6bbf | 79 | |
aa9732b9 | 80 | if (self::duplicateField($params)) { |
c8ec6bbf SB |
81 | throw new API_Exception("The field was not added. It already exists in this profile."); |
82 | } | |
83 | ||
c8ec6bbf SB |
84 | //@todo why is this even optional? Surely weight should just be 'managed' ?? |
85 | if (CRM_Utils_Array::value('option.autoweight', $params, TRUE)) { | |
86 | $params['weight'] = CRM_Core_BAO_UFField::autoWeight($params); | |
87 | } | |
aa9732b9 CW |
88 | |
89 | // Set values for uf field properties and save | |
78580003 SB |
90 | $ufField = new CRM_Core_DAO_UFField(); |
91 | $ufField->copyValues($params); | |
78580003 | 92 | |
aa9732b9 CW |
93 | if ($params['field_name'] == 'url') { |
94 | $ufField->location_type_id = 'null'; | |
78580003 SB |
95 | } |
96 | else { | |
aa9732b9 | 97 | $ufField->website_type_id = 'null'; |
78580003 | 98 | } |
aa9732b9 CW |
99 | if (!strstr($params['field_name'], 'phone')) { |
100 | $ufField->phone_type_id = 'null'; | |
78580003 SB |
101 | } |
102 | ||
78580003 | 103 | $ufField->save(); |
c8ec6bbf | 104 | |
aa9732b9 CW |
105 | $fieldsType = CRM_Core_BAO_UFGroup::calculateGroupType($ufField->uf_group_id, TRUE); |
106 | CRM_Core_BAO_UFGroup::updateGroupTypes($ufField->uf_group_id, $fieldsType); | |
c8ec6bbf | 107 | |
be2fb01f | 108 | civicrm_api3('profile', 'getfields', ['cache_clear' => TRUE]); |
c8ec6bbf SB |
109 | return $ufField; |
110 | } | |
111 | ||
6a488035 | 112 | /** |
fe482240 | 113 | * Fetch object based on array of properties. |
6a488035 | 114 | * |
6a0b768e TO |
115 | * @param array $params |
116 | * (reference ) an assoc array of name/value pairs. | |
117 | * @param array $defaults | |
118 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 119 | * |
16b10e64 | 120 | * @return CRM_Core_BAO_UFField |
6a488035 | 121 | */ |
00be9182 | 122 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
123 | return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_UFField', $params, $defaults); |
124 | } | |
125 | ||
6a488035 | 126 | /** |
fe482240 | 127 | * Update the is_active flag in the db. |
6a488035 | 128 | * |
6a0b768e TO |
129 | * @param int $id |
130 | * Id of the database record. | |
131 | * @param bool $is_active | |
132 | * Value we want to set the is_active field. | |
6a488035 | 133 | * |
8a4fede3 | 134 | * @return bool |
135 | * true if we found and updated the object, else false | |
6a488035 | 136 | */ |
00be9182 | 137 | public static function setIsActive($id, $is_active) { |
6a488035 TO |
138 | //check if custom data profile field is disabled |
139 | if ($is_active) { | |
140 | if (CRM_Core_BAO_UFField::checkUFStatus($id)) { | |
141 | return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_UFField', $id, 'is_active', $is_active); | |
142 | } | |
143 | else { | |
144 | CRM_Core_Session::setStatus(ts('Cannot enable this UF field since the used custom field is disabled.'), ts('Check Custom Field'), 'error'); | |
145 | } | |
146 | } | |
147 | else { | |
148 | return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_UFField', $id, 'is_active', $is_active); | |
149 | } | |
150 | } | |
151 | ||
152 | /** | |
153 | * Delete the profile Field. | |
154 | * | |
6a0b768e TO |
155 | * @param int $id |
156 | * Field Id. | |
6a488035 | 157 | * |
608e6658 | 158 | * @return bool |
6a488035 | 159 | * |
6a488035 TO |
160 | */ |
161 | public static function del($id) { | |
162 | //delete field field | |
163 | $field = new CRM_Core_DAO_UFField(); | |
164 | $field->id = $id; | |
165 | $field->delete(); | |
166 | return TRUE; | |
167 | } | |
168 | ||
169 | /** | |
fe482240 | 170 | * Check duplicate for duplicate field in a group. |
6a488035 | 171 | * |
6a0b768e TO |
172 | * @param array $params |
173 | * An associative array with field and values. | |
6a488035 | 174 | * |
b99bd217 | 175 | * @return bool |
6a488035 | 176 | */ |
b99bd217 | 177 | public static function duplicateField($params) { |
6a488035 | 178 | $ufField = new CRM_Core_DAO_UFField(); |
b99bd217 | 179 | $ufField->uf_group_id = CRM_Utils_Array::value('uf_group_id', $params); |
aa9732b9 CW |
180 | $ufField->field_type = CRM_Utils_Array::value('field_type', $params); |
181 | $ufField->field_name = CRM_Utils_Array::value('field_name', $params); | |
b99bd217 | 182 | $ufField->website_type_id = CRM_Utils_Array::value('website_type_id', $params); |
2e74ff55 SP |
183 | if (is_null(CRM_Utils_Array::value('location_type_id', $params, ''))) { |
184 | // primary location type have NULL value in DB | |
185 | $ufField->whereAdd("location_type_id IS NULL"); | |
186 | } | |
187 | else { | |
188 | $ufField->location_type_id = CRM_Utils_Array::value('location_type_id', $params); | |
189 | } | |
b99bd217 | 190 | $ufField->phone_type_id = CRM_Utils_Array::value('phone_type_id', $params);; |
6a488035 | 191 | |
b99bd217 ASB |
192 | if (!empty($params['id'])) { |
193 | $ufField->whereAdd("id <> " . $params['id']); | |
6a488035 TO |
194 | } |
195 | ||
aa9732b9 | 196 | return (bool) $ufField->find(TRUE); |
6a488035 TO |
197 | } |
198 | ||
f3c39657 | 199 | /** |
ad37ac8e | 200 | * Does profile consists of a multi-record custom field. |
201 | * | |
202 | * @param int $gId | |
203 | * | |
204 | * @return bool | |
6a488035 TO |
205 | */ |
206 | public static function checkMultiRecordFieldExists($gId) { | |
207 | $queryString = "SELECT f.field_name | |
208 | FROM civicrm_uf_field f, civicrm_uf_group g | |
209 | WHERE f.uf_group_id = g.id | |
210 | AND g.id = %1 AND f.field_name LIKE 'custom%'"; | |
be2fb01f | 211 | $p = [1 => [$gId, 'Integer']]; |
6a488035 | 212 | $dao = CRM_Core_DAO::executeQuery($queryString, $p); |
be2fb01f | 213 | $customFieldIds = []; |
6a488035 TO |
214 | $isMultiRecordFieldPresent = FALSE; |
215 | while ($dao->fetch()) { | |
216 | if ($customId = CRM_Core_BAO_CustomField::getKeyID($dao->field_name)) { | |
217 | if (is_numeric($customId)) { | |
218 | $customFieldIds[] = $customId; | |
219 | } | |
220 | } | |
221 | } | |
222 | ||
223 | if (!empty($customFieldIds) && count($customFieldIds) == 1) { | |
224 | $customFieldId = array_pop($customFieldIds); | |
225 | $isMultiRecordFieldPresent = CRM_Core_BAO_CustomField::isMultiRecordField($customFieldId); | |
226 | } | |
227 | elseif (count($customFieldIds) > 1) { | |
228 | $customFieldIds = implode(", ", $customFieldIds); | |
229 | $queryString = " | |
230 | SELECT cg.id as cgId | |
231 | FROM civicrm_custom_group cg | |
cbb7c7e0 | 232 | INNER JOIN civicrm_custom_field cf |
6a488035 TO |
233 | ON cg.id = cf.custom_group_id |
234 | WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1"; | |
235 | ||
236 | $dao = CRM_Core_DAO::executeQuery($queryString); | |
237 | if ($dao->fetch()) { | |
238 | $isMultiRecordFieldPresent = ($dao->cgId) ? $dao->cgId : FALSE; | |
239 | } | |
240 | } | |
241 | ||
242 | return $isMultiRecordFieldPresent; | |
243 | } | |
244 | ||
6a488035 | 245 | /** |
fe482240 | 246 | * Automatically determine one weight and modify others. |
6a488035 | 247 | * |
6a0b768e TO |
248 | * @param array $params |
249 | * UFField record, e.g. with 'weight', 'uf_group_id', and 'field_id'. | |
6a488035 TO |
250 | * @return int |
251 | */ | |
252 | public static function autoWeight($params) { | |
253 | // fix for CRM-316 | |
254 | $oldWeight = NULL; | |
255 | ||
aa9732b9 CW |
256 | if (!empty($params['field_id']) || !empty($params['id'])) { |
257 | $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFField', !empty($params['id']) ? $params['id'] : $params['field_id'], 'weight', 'id'); | |
6a488035 | 258 | } |
aa9732b9 | 259 | $fieldValues = ['uf_group_id' => !empty($params['uf_group_id']) ? $params['uf_group_id'] : $params['group_id']]; |
44cc86bd | 260 | return CRM_Utils_Weight::updateOtherWeights('CRM_Core_DAO_UFField', $oldWeight, CRM_Utils_Array::value('weight', $params, 0), $fieldValues); |
6a488035 TO |
261 | } |
262 | ||
263 | /** | |
100fef9d | 264 | * Enable/disable profile field given a custom field id |
6a488035 | 265 | * |
6a0b768e TO |
266 | * @param int $customFieldId |
267 | * Custom field id. | |
268 | * @param bool $is_active | |
269 | * Set the is_active field. | |
6a488035 | 270 | */ |
00be9182 | 271 | public static function setUFField($customFieldId, $is_active) { |
a43adbad | 272 | // Find the profile id given custom field. |
6a488035 TO |
273 | $ufField = new CRM_Core_DAO_UFField(); |
274 | $ufField->field_name = "custom_" . $customFieldId; | |
275 | ||
276 | $ufField->find(); | |
277 | while ($ufField->fetch()) { | |
a43adbad | 278 | // Enable/ disable profile. |
6a488035 TO |
279 | CRM_Core_BAO_UFField::setIsActive($ufField->id, $is_active); |
280 | } | |
281 | } | |
282 | ||
283 | /** | |
100fef9d | 284 | * Copy existing profile fields to |
6a488035 TO |
285 | * new profile from the already built profile |
286 | * | |
6a0b768e TO |
287 | * @param int $old_id |
288 | * From which we need to copy. | |
289 | * @param bool $new_id | |
290 | * In which to copy. | |
6a488035 | 291 | */ |
00be9182 | 292 | public static function copy($old_id, $new_id) { |
6a488035 TO |
293 | $ufField = new CRM_Core_DAO_UFField(); |
294 | $ufField->uf_group_id = $old_id; | |
295 | $ufField->find(); | |
296 | while ($ufField->fetch()) { | |
297 | //copy the field records as it is on new ufgroup id | |
298 | $ufField->uf_group_id = $new_id; | |
299 | $ufField->id = NULL; | |
300 | $ufField->save(); | |
301 | } | |
302 | } | |
303 | ||
304 | /** | |
fe482240 | 305 | * Delete profile field given a custom field. |
6a488035 | 306 | * |
6a0b768e TO |
307 | * @param int $customFieldId |
308 | * ID of the custom field to be deleted. | |
6a488035 | 309 | */ |
00be9182 | 310 | public static function delUFField($customFieldId) { |
6a488035 TO |
311 | //find the profile id given custom field id |
312 | $ufField = new CRM_Core_DAO_UFField(); | |
313 | $ufField->field_name = "custom_" . $customFieldId; | |
314 | ||
315 | $ufField->find(); | |
316 | while ($ufField->fetch()) { | |
317 | //enable/ disable profile | |
318 | CRM_Core_BAO_UFField::del($ufField->id); | |
319 | } | |
320 | } | |
321 | ||
322 | /** | |
100fef9d | 323 | * Enable/disable profile field given a custom group id |
6a488035 | 324 | * |
6a0b768e TO |
325 | * @param int $customGroupId |
326 | * Custom group id. | |
327 | * @param bool $is_active | |
328 | * Value we want to set the is_active field. | |
6a488035 | 329 | */ |
00be9182 | 330 | public static function setUFFieldStatus($customGroupId, $is_active) { |
6a488035 TO |
331 | //find the profile id given custom group id |
332 | $queryString = "SELECT civicrm_custom_field.id as custom_field_id | |
333 | FROM civicrm_custom_field, civicrm_custom_group | |
334 | WHERE civicrm_custom_field.custom_group_id = civicrm_custom_group.id | |
335 | AND civicrm_custom_group.id = %1"; | |
be2fb01f | 336 | $p = [1 => [$customGroupId, 'Integer']]; |
6a488035 TO |
337 | $dao = CRM_Core_DAO::executeQuery($queryString, $p); |
338 | ||
339 | while ($dao->fetch()) { | |
a43adbad | 340 | // Enable/ disable profile. |
6a488035 TO |
341 | CRM_Core_BAO_UFField::setUFField($dao->custom_field_id, $is_active); |
342 | } | |
343 | } | |
344 | ||
345 | /** | |
fe482240 | 346 | * Check the status of custom field used in uf fields. |
6a488035 | 347 | * |
c490a46a | 348 | * @param int $UFFieldId |
f3c39657 | 349 | * |
608e6658 | 350 | * @return bool |
a6c01b45 | 351 | * false if custom field are disabled else true |
6a488035 | 352 | */ |
00be9182 | 353 | public static function checkUFStatus($UFFieldId) { |
6a488035 TO |
354 | $fieldName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFField', $UFFieldId, 'field_name'); |
355 | // return if field is not a custom field | |
356 | if (!$customFieldId = CRM_Core_BAO_CustomField::getKeyID($fieldName)) { | |
357 | return TRUE; | |
358 | } | |
359 | ||
360 | $customField = new CRM_Core_DAO_CustomField(); | |
361 | $customField->id = $customFieldId; | |
362 | // if uf field is custom field | |
363 | if ($customField->find(TRUE)) { | |
364 | if (!$customField->is_active) { | |
365 | return FALSE; | |
366 | } | |
367 | else { | |
368 | return TRUE; | |
369 | } | |
370 | } | |
371 | } | |
372 | ||
44cc86bd | 373 | /** |
100fef9d | 374 | * Find out whether given profile group using Activity |
6a488035 | 375 | * Profile fields with contact fields |
ea3ddccf | 376 | * |
377 | * @param int $ufGroupId | |
378 | * | |
379 | * @return bool | |
6a488035 | 380 | */ |
00be9182 | 381 | public static function checkContactActivityProfileType($ufGroupId) { |
6a488035 TO |
382 | $ufGroup = new CRM_Core_DAO_UFGroup(); |
383 | $ufGroup->id = $ufGroupId; | |
384 | $ufGroup->find(TRUE); | |
385 | ||
386 | return self::checkContactActivityProfileTypeByGroupType($ufGroup->group_type); | |
387 | } | |
388 | ||
389 | /** | |
390 | * FIXME say 10 ha | |
391 | * @param $ufGroupType | |
392 | * @return bool | |
393 | */ | |
394 | public static function checkContactActivityProfileTypeByGroupType($ufGroupType) { | |
be2fb01f | 395 | $profileTypes = []; |
6a488035 TO |
396 | if ($ufGroupType) { |
397 | $typeParts = explode(CRM_Core_DAO::VALUE_SEPARATOR, $ufGroupType); | |
398 | $profileTypes = explode(',', $typeParts[0]); | |
399 | } | |
400 | ||
401 | if (empty($profileTypes)) { | |
402 | return FALSE; | |
403 | } | |
be2fb01f | 404 | $components = ['Contribution', 'Participant', 'Membership']; |
6a488035 TO |
405 | if (!in_array('Activity', $profileTypes)) { |
406 | return FALSE; | |
407 | } | |
408 | elseif (count($profileTypes) == 1) { | |
409 | return FALSE; | |
410 | } | |
411 | ||
412 | if ($index = array_search('Contact', $profileTypes)) { | |
413 | unset($profileTypes[$index]); | |
414 | if (count($profileTypes) == 1) { | |
415 | return TRUE; | |
416 | } | |
417 | } | |
418 | ||
be2fb01f | 419 | $contactTypes = ['Individual', 'Household', 'Organization']; |
6a488035 TO |
420 | $subTypes = CRM_Contact_BAO_ContactType::subTypes(); |
421 | ||
422 | $profileTypeComponent = array_intersect($components, $profileTypes); | |
423 | if (!empty($profileTypeComponent) || | |
424 | count(array_intersect($contactTypes, $profileTypes)) > 1 || | |
425 | count(array_intersect($subTypes, $profileTypes)) > 1 | |
426 | ) { | |
427 | return FALSE; | |
428 | } | |
429 | ||
430 | return TRUE; | |
431 | } | |
432 | ||
f3c39657 | 433 | /** |
100fef9d | 434 | * Find out whether given profile group uses $required |
44cc86bd | 435 | * and/or $optional profile types |
cbb7c7e0 | 436 | * |
6a0b768e TO |
437 | * @param int $ufGroupId |
438 | * Profile id. | |
439 | * @param array $required | |
440 | * Array of types those are required. | |
441 | * @param array $optional | |
442 | * Array of types those are optional. | |
6a488035 | 443 | * |
608e6658 | 444 | * @return bool |
6a488035 | 445 | */ |
00be9182 | 446 | public static function checkValidProfileType($ufGroupId, $required, $optional = NULL) { |
6a488035 | 447 | if (!is_array($required) || empty($required)) { |
608e6658 | 448 | return FALSE; |
6a488035 TO |
449 | } |
450 | ||
451 | $ufGroup = new CRM_Core_DAO_UFGroup(); | |
452 | $ufGroup->id = $ufGroupId; | |
453 | $ufGroup->find(TRUE); | |
454 | ||
be2fb01f | 455 | $profileTypes = []; |
6a488035 TO |
456 | if ($ufGroup->group_type) { |
457 | $typeParts = explode(CRM_Core_DAO::VALUE_SEPARATOR, $ufGroup->group_type); | |
458 | $profileTypes = explode(',', $typeParts[0]); | |
459 | } | |
460 | ||
461 | if (empty($profileTypes)) { | |
462 | return FALSE; | |
463 | } | |
464 | ||
465 | $valid = TRUE; | |
466 | foreach ($required as $key => $val) { | |
467 | if (!in_array($val, $profileTypes)) { | |
468 | $valid = FALSE; | |
469 | break; | |
470 | } | |
471 | } | |
472 | ||
473 | if ($valid && is_array($optional)) { | |
474 | foreach ($optional as $key => $val) { | |
475 | if (in_array($val, $profileTypes)) { | |
476 | $valid = TRUE; | |
477 | break; | |
478 | } | |
479 | } | |
480 | } | |
481 | ||
482 | return $valid; | |
483 | } | |
484 | ||
485 | /** | |
c490a46a | 486 | * Check for mix profile fields (eg: individual + other contact types) |
6a488035 | 487 | * |
c490a46a | 488 | * @param int $ufGroupId |
f3c39657 | 489 | * |
72b3a70c CW |
490 | * @return bool |
491 | * true for mix profile else false | |
6a488035 | 492 | */ |
00be9182 | 493 | public static function checkProfileType($ufGroupId) { |
6a488035 TO |
494 | $ufGroup = new CRM_Core_DAO_UFGroup(); |
495 | $ufGroup->id = $ufGroupId; | |
496 | $ufGroup->find(TRUE); | |
497 | ||
be2fb01f | 498 | $profileTypes = []; |
6a488035 TO |
499 | if ($ufGroup->group_type) { |
500 | $typeParts = explode(CRM_Core_DAO::VALUE_SEPARATOR, $ufGroup->group_type); | |
501 | $profileTypes = explode(',', $typeParts[0]); | |
502 | } | |
503 | ||
504 | //early return if new profile. | |
505 | if (empty($profileTypes)) { | |
506 | return FALSE; | |
507 | } | |
508 | ||
509 | //we need to unset Contact | |
510 | if (count($profileTypes) > 1) { | |
511 | $index = array_search('Contact', $profileTypes); | |
512 | if ($index !== FALSE) { | |
513 | unset($profileTypes[$index]); | |
514 | } | |
515 | } | |
516 | ||
517 | // suppress any subtypes if present | |
518 | CRM_Contact_BAO_ContactType::suppressSubTypes($profileTypes); | |
519 | ||
be2fb01f CW |
520 | $contactTypes = ['Contact', 'Individual', 'Household', 'Organization']; |
521 | $components = ['Contribution', 'Participant', 'Membership', 'Activity']; | |
522 | $fields = []; | |
6a488035 TO |
523 | |
524 | // check for mix profile condition | |
525 | if (count($profileTypes) > 1) { | |
526 | //check the there are any components include in profile | |
527 | foreach ($components as $value) { | |
528 | if (in_array($value, $profileTypes)) { | |
529 | return TRUE; | |
530 | } | |
531 | } | |
532 | //check if there are more than one contact types included in profile | |
533 | if (count($profileTypes) > 1) { | |
534 | return TRUE; | |
535 | } | |
536 | } | |
537 | elseif (count($profileTypes) == 1) { | |
538 | // note for subtype case count would be zero | |
539 | $profileTypes = array_values($profileTypes); | |
540 | if (!in_array($profileTypes[0], $contactTypes)) { | |
541 | return TRUE; | |
542 | } | |
543 | } | |
544 | ||
545 | return FALSE; | |
546 | } | |
547 | ||
548 | /** | |
100fef9d | 549 | * Get the profile type (eg: individual/organization/household) |
6a488035 | 550 | * |
6a0b768e TO |
551 | * @param int $ufGroupId |
552 | * Uf group id. | |
553 | * @param bool $returnMixType | |
554 | * This is true, then field type of mix profile field is returned. | |
555 | * @param bool $onlyPure | |
556 | * True if only pure profiles are required. | |
f3c39657 | 557 | * |
558 | * @param bool $skipComponentType | |
6a488035 | 559 | * |
a6c01b45 CW |
560 | * @return string |
561 | * profile group_type | |
6a488035 | 562 | * |
6a488035 | 563 | */ |
00be9182 | 564 | public static function getProfileType($ufGroupId, $returnMixType = TRUE, $onlyPure = FALSE, $skipComponentType = FALSE) { |
6a488035 TO |
565 | $ufGroup = new CRM_Core_DAO_UFGroup(); |
566 | $ufGroup->id = $ufGroupId; | |
567 | $ufGroup->is_active = 1; | |
568 | ||
569 | $ufGroup->find(TRUE); | |
570 | return self::calculateProfileType($ufGroup->group_type, $returnMixType, $onlyPure, $skipComponentType); | |
571 | } | |
572 | ||
573 | /** | |
100fef9d | 574 | * Get the profile type (eg: individual/organization/household) |
6a488035 | 575 | * |
c490a46a | 576 | * @param string $ufGroupType |
6a0b768e TO |
577 | * @param bool $returnMixType |
578 | * This is true, then field type of mix profile field is returned. | |
579 | * @param bool $onlyPure | |
580 | * True if only pure profiles are required. | |
f3c39657 | 581 | * @param bool $skipComponentType |
6a488035 | 582 | * |
608e6658 | 583 | * @return string profile group_type |
6a488035 | 584 | * |
6a488035 | 585 | */ |
2aa397bc | 586 | public static function calculateProfileType($ufGroupType, $returnMixType = TRUE, $onlyPure = FALSE, $skipComponentType = FALSE) { |
6a488035 | 587 | // profile types |
be2fb01f | 588 | $contactTypes = ['Contact', 'Individual', 'Household', 'Organization']; |
6a488035 | 589 | $subTypes = CRM_Contact_BAO_ContactType::subTypes(); |
be2fb01f | 590 | $components = ['Contribution', 'Participant', 'Membership', 'Activity']; |
6a488035 | 591 | |
be2fb01f | 592 | $profileTypes = []; |
6a488035 TO |
593 | if ($ufGroupType) { |
594 | $typeParts = explode(CRM_Core_DAO::VALUE_SEPARATOR, $ufGroupType); | |
595 | $profileTypes = explode(',', $typeParts[0]); | |
596 | } | |
597 | ||
598 | if ($onlyPure) { | |
599 | if (count($profileTypes) == 1) { | |
600 | return $profileTypes[0]; | |
601 | } | |
602 | else { | |
603 | return NULL; | |
604 | } | |
605 | } | |
606 | ||
607 | //we need to unset Contact | |
608 | if (count($profileTypes) > 1) { | |
609 | $index = array_search('Contact', $profileTypes); | |
610 | if ($index !== FALSE) { | |
611 | unset($profileTypes[$index]); | |
612 | } | |
613 | } | |
614 | ||
615 | $profileType = $mixProfileType = NULL; | |
616 | ||
617 | // this case handles pure profile | |
618 | if (count($profileTypes) == 1) { | |
619 | $profileType = array_pop($profileTypes); | |
620 | } | |
621 | else { | |
622 | //check the there are any components include in profile | |
be2fb01f | 623 | $componentCount = []; |
6a488035 TO |
624 | foreach ($components as $value) { |
625 | if (in_array($value, $profileTypes)) { | |
626 | $componentCount[] = $value; | |
627 | } | |
628 | } | |
629 | ||
630 | //check contact type included in profile | |
be2fb01f | 631 | $contactTypeCount = []; |
6a488035 TO |
632 | foreach ($contactTypes as $value) { |
633 | if (in_array($value, $profileTypes)) { | |
634 | $contactTypeCount[] = $value; | |
635 | } | |
636 | } | |
637 | // subtype counter | |
be2fb01f | 638 | $subTypeCount = []; |
6a488035 TO |
639 | foreach ($subTypes as $value) { |
640 | if (in_array($value, $profileTypes)) { | |
641 | $subTypeCount[] = $value; | |
642 | } | |
643 | } | |
644 | if (!$skipComponentType && count($componentCount) == 1) { | |
645 | $profileType = $componentCount[0]; | |
646 | } | |
647 | elseif (count($componentCount) > 1) { | |
648 | $mixProfileType = $componentCount[1]; | |
649 | } | |
650 | elseif (count($subTypeCount) == 1) { | |
651 | $profileType = $subTypeCount[0]; | |
652 | } | |
653 | elseif (count($contactTypeCount) == 1) { | |
654 | $profileType = $contactTypeCount[0]; | |
655 | } | |
656 | elseif (count($subTypeCount) > 1) { | |
657 | // this is mix subtype profiles | |
658 | $mixProfileType = $subTypeCount[1]; | |
659 | } | |
660 | elseif (count($contactTypeCount) > 1) { | |
661 | // this is mix contact profiles | |
662 | $mixProfileType = $contactTypeCount[1]; | |
663 | } | |
664 | } | |
665 | ||
666 | if ($mixProfileType) { | |
667 | if ($returnMixType) { | |
668 | return $mixProfileType; | |
669 | } | |
670 | else { | |
671 | return 'Mixed'; | |
672 | } | |
673 | } | |
674 | else { | |
675 | return $profileType; | |
676 | } | |
677 | } | |
678 | ||
679 | /** | |
100fef9d | 680 | * Check for mix profiles groups (eg: individual + other contact types) |
6a488035 | 681 | * |
f3c39657 | 682 | * @param $ctype |
683 | * | |
72b3a70c CW |
684 | * @return bool |
685 | * true for mix profile group else false | |
6a488035 | 686 | */ |
00be9182 | 687 | public static function checkProfileGroupType($ctype) { |
6a488035 TO |
688 | $ufGroup = new CRM_Core_DAO_UFGroup(); |
689 | ||
690 | $query = " | |
691 | SELECT ufg.id as id | |
692 | FROM civicrm_uf_group as ufg, civicrm_uf_join as ufj | |
693 | WHERE ufg.id = ufj.uf_group_id | |
694 | AND ufj.module = 'User Registration' | |
695 | AND ufg.is_active = 1 "; | |
696 | ||
697 | $ufGroup = CRM_Core_DAO::executeQuery($query); | |
698 | ||
be2fb01f CW |
699 | $fields = []; |
700 | $validProfiles = ['Individual', 'Organization', 'Household', 'Contribution']; | |
6a488035 TO |
701 | while ($ufGroup->fetch()) { |
702 | $profileType = self::getProfileType($ufGroup->id); | |
703 | if (in_array($profileType, $validProfiles)) { | |
704 | continue; | |
705 | } | |
706 | elseif ($profileType) { | |
707 | return FALSE; | |
708 | } | |
709 | } | |
710 | ||
711 | return TRUE; | |
712 | } | |
713 | ||
714 | /** | |
c490a46a | 715 | * Check for searchable or in selector field for given profile. |
6a488035 | 716 | * |
c490a46a | 717 | * @param int $profileID |
f3c39657 | 718 | * |
608e6658 | 719 | * @return bool |
6a488035 | 720 | */ |
00be9182 | 721 | public static function checkSearchableORInSelector($profileID) { |
6a488035 TO |
722 | $result = FALSE; |
723 | if (!$profileID) { | |
724 | return $result; | |
725 | } | |
726 | ||
727 | $query = " | |
cbb7c7e0 | 728 | SELECT id |
729 | From civicrm_uf_field | |
6a488035 TO |
730 | WHERE (in_selector = 1 OR is_searchable = 1) |
731 | AND uf_group_id = {$profileID}"; | |
732 | ||
733 | $ufFields = CRM_Core_DAO::executeQuery($query); | |
734 | while ($ufFields->fetch()) { | |
735 | $result = TRUE; | |
736 | break; | |
737 | } | |
738 | ||
739 | return $result; | |
740 | } | |
741 | ||
742 | /** | |
c490a46a | 743 | * Reset In selector and is searchable values for given $profileID. |
6a488035 | 744 | * |
c490a46a | 745 | * @param int $profileID |
6a488035 | 746 | */ |
00be9182 | 747 | public function resetInSelectorANDSearchable($profileID) { |
6a488035 TO |
748 | if (!$profileID) { |
749 | return; | |
750 | } | |
751 | $query = "UPDATE civicrm_uf_field SET in_selector = 0, is_searchable = 0 WHERE uf_group_id = {$profileID}"; | |
752 | CRM_Core_DAO::executeQuery($query); | |
753 | } | |
754 | ||
755 | /** | |
756 | * Add fields to $profileAddressFields as appropriate. | |
757 | * profileAddressFields is assigned to the template to tell it | |
758 | * what fields are in the profile address | |
759 | * that potentially should be copied to the Billing fields | |
760 | * we want to give precedence to | |
761 | * 1) Billing & | |
762 | * 2) then Primary designated as 'Primary | |
763 | * 3) location_type is primary | |
764 | * 4) if none of these apply then it just uses the first one | |
765 | * | |
766 | * as this will be used to | |
767 | * transfer profile address data to billing fields | |
768 | * http://issues.civicrm.org/jira/browse/CRM-5869 | |
f3c39657 | 769 | * |
6a0b768e TO |
770 | * @param string $key |
771 | * Field key - e.g. street_address-Primary, first_name. | |
772 | * @param array $profileAddressFields | |
773 | * Array of profile fields that relate to address fields. | |
774 | * @param array $profileFilter | |
775 | * Filter to apply to profile fields - expected usage is to only fill based on. | |
16b10e64 | 776 | * the bottom profile per CRM-13726 |
1ad7255f | 777 | * |
a6c01b45 CW |
778 | * @return bool |
779 | * Can the address block be hidden safe in the knowledge all fields are elsewhere collected (see CRM-15118) | |
6a488035 | 780 | */ |
00be9182 | 781 | public static function assignAddressField($key, &$profileAddressFields, $profileFilter) { |
6a488035 TO |
782 | $billing_id = CRM_Core_BAO_LocationType::getBilling(); |
783 | list($prefixName, $index) = CRM_Utils_System::explode('-', $key, 2); | |
784 | ||
fddfc93f | 785 | $profileFields = civicrm_api3('uf_field', 'get', array_merge($profileFilter, |
be2fb01f | 786 | [ |
353ffa53 TO |
787 | 'is_active' => 1, |
788 | 'return' => 'field_name, is_required', | |
be2fb01f | 789 | 'options' => [ |
353ffa53 | 790 | 'limit' => 0, |
be2fb01f CW |
791 | ], |
792 | ] | |
fddfc93f | 793 | )); |
6a488035 | 794 | //check for valid fields ( fields that are present in billing block ) |
be2fb01f | 795 | $validBillingFields = [ |
6a488035 TO |
796 | 'first_name', |
797 | 'middle_name', | |
798 | 'last_name', | |
799 | 'street_address', | |
800 | 'supplemental_address_1', | |
801 | 'city', | |
802 | 'state_province', | |
803 | 'postal_code', | |
21dfd5f5 | 804 | 'country', |
be2fb01f CW |
805 | ]; |
806 | $requiredBillingFields = array_diff($validBillingFields, ['middle_name', 'supplemental_address_1']); | |
807 | $validProfileFields = []; | |
808 | $requiredProfileFields = []; | |
6a488035 | 809 | |
7d613bb7 | 810 | foreach ($profileFields['values'] as $field) { |
22e263ad | 811 | if (in_array($field['field_name'], $validBillingFields)) { |
7d613bb7 DG |
812 | $validProfileFields[] = $field['field_name']; |
813 | } | |
de6c59ca | 814 | if (!empty($field['is_required'])) { |
1ad7255f DG |
815 | $requiredProfileFields[] = $field['field_name']; |
816 | } | |
7d613bb7 DG |
817 | } |
818 | ||
481a74f4 | 819 | if (!in_array($prefixName, $validProfileFields)) { |
608e6658 | 820 | return FALSE; |
6a488035 TO |
821 | } |
822 | ||
823 | if (!empty($index) && ( | |
353ffa53 | 824 | // it's empty so we set it OR |
1817b503 | 825 | !CRM_Utils_Array::value($prefixName, $profileAddressFields) |
6a488035 TO |
826 | //we are dealing with billing id (precedence) |
827 | || $index == $billing_id | |
828 | // we are dealing with primary & billing not set | |
829 | || ($index == 'Primary' && $profileAddressFields[$prefixName] != $billing_id) | |
830 | || ($index == CRM_Core_BAO_LocationType::getDefault()->id | |
353ffa53 TO |
831 | && $profileAddressFields[$prefixName] != $billing_id |
832 | && $profileAddressFields[$prefixName] != 'Primary' | |
833 | ) | |
6a488035 | 834 | ) |
6a488035 TO |
835 | ) { |
836 | $profileAddressFields[$prefixName] = $index; | |
837 | } | |
d75f2f47 EM |
838 | |
839 | $potentiallyMissingRequiredFields = array_diff($requiredBillingFields, $requiredProfileFields); | |
353ffa53 | 840 | CRM_Core_Resources::singleton() |
be2fb01f | 841 | ->addSetting(['billing' => ['billingProfileIsHideable' => empty($potentiallyMissingRequiredFields)]]); |
6a488035 TO |
842 | } |
843 | ||
844 | /** | |
fe482240 | 845 | * Get a list of fields which can be added to profiles. |
6a488035 | 846 | * |
353ffa53 TO |
847 | * @param int $gid : UF group ID |
848 | * @param array $defaults : Form defaults | |
6a488035 | 849 | * @return array, multidimensional; e.g. $result['FieldGroup']['field_name']['label'] |
6a488035 | 850 | */ |
be2fb01f CW |
851 | public static function getAvailableFields($gid = NULL, $defaults = []) { |
852 | $fields = [ | |
853 | 'Contact' => [], | |
6a488035 TO |
854 | 'Individual' => CRM_Contact_BAO_Contact::importableFields('Individual', FALSE, FALSE, TRUE, TRUE, TRUE), |
855 | 'Household' => CRM_Contact_BAO_Contact::importableFields('Household', FALSE, FALSE, TRUE, TRUE, TRUE), | |
856 | 'Organization' => CRM_Contact_BAO_Contact::importableFields('Organization', FALSE, FALSE, TRUE, TRUE, TRUE), | |
be2fb01f | 857 | ]; |
6a488035 | 858 | |
61750d67 DS |
859 | // include hook injected fields |
860 | $fields['Contact'] = array_merge($fields['Contact'], CRM_Contact_BAO_Query_Hook::singleton()->getFields()); | |
861 | ||
6a488035 | 862 | // add current employer for individuals |
be2fb01f | 863 | $fields['Individual']['current_employer'] = [ |
6a488035 TO |
864 | 'name' => 'organization_name', |
865 | 'title' => ts('Current Employer'), | |
be2fb01f | 866 | ]; |
6a488035 TO |
867 | |
868 | $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
869 | 'address_options', TRUE, NULL, TRUE | |
870 | ); | |
871 | ||
dcd27695 | 872 | if (empty($addressOptions['county'])) { |
6a488035 TO |
873 | unset($fields['Individual']['county'], $fields['Household']['county'], $fields['Organization']['county']); |
874 | } | |
875 | ||
876 | // break out common contact fields array CRM-3037. | |
877 | // from a UI perspective this makes very little sense | |
878 | foreach ($fields['Individual'] as $key => $value) { | |
879 | if (!empty($fields['Household'][$key]) && !empty($fields['Organization'][$key])) { | |
880 | $fields['Contact'][$key] = $value; | |
881 | unset($fields['Individual'][$key], $fields['Household'][$key], $fields['Organization'][$key]); | |
882 | } | |
883 | } | |
884 | ||
885 | // Internal field not exposed to forms | |
886 | unset($fields['Contact']['contact_type']); | |
deb17853 | 887 | unset($fields['Contact']['master_id']); |
6a488035 TO |
888 | |
889 | // convert phone extension in to psedo-field phone + phone extension | |
890 | //unset extension | |
891 | unset($fields['Contact']['phone_ext']); | |
892 | //add psedo field | |
be2fb01f | 893 | $fields['Contact']['phone_and_ext'] = [ |
6a488035 TO |
894 | 'name' => 'phone_and_ext', |
895 | 'title' => ts('Phone and Extension'), | |
896 | 'hasLocationType' => 1, | |
be2fb01f | 897 | ]; |
6a488035 TO |
898 | |
899 | // include Subtypes For Profile | |
900 | $subTypes = CRM_Contact_BAO_ContactType::subTypeInfo(); | |
901 | foreach ($subTypes as $name => $val) { | |
902 | //custom fields for sub type | |
3550c9f6 | 903 | $subTypeFields = CRM_Core_BAO_CustomField::getFieldsForImport($name, FALSE, FALSE, FALSE, TRUE, TRUE); |
6a488035 TO |
904 | if (array_key_exists($val['parent'], $fields)) { |
905 | $fields[$name] = $fields[$val['parent']] + $subTypeFields; | |
906 | } | |
907 | else { | |
908 | $fields[$name] = $subTypeFields; | |
909 | } | |
910 | } | |
911 | ||
912 | if (CRM_Core_Permission::access('CiviContribute')) { | |
913 | $contribFields = CRM_Contribute_BAO_Contribution::getContributionFields(FALSE); | |
914 | if (!empty($contribFields)) { | |
915 | unset($contribFields['is_test']); | |
916 | unset($contribFields['is_pay_later']); | |
917 | unset($contribFields['contribution_id']); | |
be2fb01f | 918 | $contribFields['contribution_note'] = [ |
6a488035 TO |
919 | 'name' => 'contribution_note', |
920 | 'title' => ts('Contribution Note'), | |
be2fb01f | 921 | ]; |
752ddf35 | 922 | $fields['Contribution'] = array_merge($contribFields, self::getContribBatchEntryFields()); |
6a488035 TO |
923 | } |
924 | } | |
925 | ||
926 | if (CRM_Core_Permission::access('CiviEvent')) { | |
927 | $participantFields = CRM_Event_BAO_Query::getParticipantFields(); | |
928 | if ($participantFields) { | |
929 | // Remove fields not supported by profiles | |
930 | CRM_Utils_Array::remove($participantFields, | |
931 | 'external_identifier', | |
932 | 'event_id', | |
933 | 'participant_contact_id', | |
934 | 'participant_role_id', | |
935 | 'participant_status_id', | |
936 | 'participant_is_test', | |
937 | 'participant_fee_level', | |
938 | 'participant_id', | |
939 | 'participant_is_pay_later', | |
940 | 'participant_campaign' | |
941 | ); | |
942 | if (isset($participantFields['participant_campaign_id'])) { | |
943 | $participantFields['participant_campaign_id']['title'] = ts('Campaign'); | |
944 | } | |
945 | $fields['Participant'] = $participantFields; | |
946 | } | |
947 | } | |
948 | ||
949 | if (CRM_Core_Permission::access('CiviMember')) { | |
950 | $membershipFields = CRM_Member_BAO_Membership::getMembershipFields(); | |
951 | // Remove fields not supported by profiles | |
952 | CRM_Utils_Array::remove($membershipFields, | |
953 | 'membership_id', | |
954 | 'membership_type_id', | |
955 | 'member_is_test', | |
956 | 'is_override', | |
8f67d99a | 957 | 'member_is_override', |
e136f704 | 958 | 'status_override_end_date', |
6a488035 TO |
959 | 'status_id', |
960 | 'member_is_pay_later' | |
961 | ); | |
962 | if ($gid && CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $gid, 'name') == 'membership_batch_entry') { | |
963 | $fields['Membership'] = array_merge($membershipFields, self::getMemberBatchEntryFields()); | |
964 | } | |
965 | else { | |
966 | $fields['Membership'] = $membershipFields; | |
967 | } | |
968 | } | |
969 | ||
1cb28d5d | 970 | if (CRM_Core_Permission::access('CiviCase')) { |
971 | $caseFields = CRM_Case_BAO_Query::getFields(TRUE); | |
972 | $caseFields = array_merge($caseFields, CRM_Core_BAO_CustomField::getFieldsForImport('Case')); | |
973 | if ($caseFields) { | |
974 | // Remove fields not supported by profiles | |
975 | CRM_Utils_Array::remove($caseFields, | |
976 | 'case_id', | |
977 | 'case_type', | |
1cb28d5d | 978 | 'case_role', |
1cb28d5d | 979 | 'case_deleted' |
980 | ); | |
981 | } | |
982 | $fields['Case'] = $caseFields; | |
983 | } | |
984 | ||
6a488035 TO |
985 | $activityFields = CRM_Activity_BAO_Activity::getProfileFields(); |
986 | if ($activityFields) { | |
987 | // campaign related fields. | |
988 | if (isset($activityFields['activity_campaign_id'])) { | |
989 | $activityFields['activity_campaign_id']['title'] = ts('Campaign'); | |
990 | } | |
991 | $fields['Activity'] = $activityFields; | |
992 | } | |
993 | ||
be2fb01f | 994 | $fields['Formatting']['format_free_html_' . rand(1000, 9999)] = [ |
6a488035 TO |
995 | 'name' => 'free_html', |
996 | 'import' => FALSE, | |
997 | 'export' => FALSE, | |
998 | 'title' => 'Free HTML', | |
be2fb01f | 999 | ]; |
6a488035 TO |
1000 | |
1001 | // Sort by title | |
1002 | foreach ($fields as &$values) { | |
1003 | $values = CRM_Utils_Array::crmArraySortByField($values, 'title'); | |
1004 | } | |
1005 | ||
1006 | //group selected and unwanted fields list | |
be2fb01f CW |
1007 | $ufFields = $gid ? CRM_Core_BAO_UFGroup::getFields($gid, FALSE, NULL, NULL, NULL, TRUE, NULL, TRUE) : []; |
1008 | $groupFieldList = array_merge($ufFields, [ | |
6a488035 TO |
1009 | 'note', |
1010 | 'email_greeting_custom', | |
1011 | 'postal_greeting_custom', | |
1012 | 'addressee_custom', | |
1013 | 'id', | |
be2fb01f | 1014 | ]); |
6a488035 TO |
1015 | //unset selected fields |
1016 | foreach ($groupFieldList as $key => $value) { | |
608e6658 | 1017 | if (is_int($key)) { |
6a488035 TO |
1018 | unset($fields['Individual'][$value], $fields['Household'][$value], $fields['Organization'][$value]); |
1019 | continue; | |
1020 | } | |
1021 | if (!empty($defaults['field_name']) | |
1022 | && $defaults['field_name']['0'] == $value['field_type'] | |
1023 | && $defaults['field_name']['1'] == $key | |
1024 | ) { | |
1025 | continue; | |
1026 | } | |
1027 | unset($fields[$value['field_type']][$key]); | |
1028 | } | |
1029 | ||
1030 | return $fields; | |
1031 | } | |
1032 | ||
1033 | /** | |
fe482240 | 1034 | * Get a list of fields which can be added to profiles. |
6a488035 | 1035 | * |
f3c39657 | 1036 | * @param bool $force |
1037 | * | |
ffb9cdec CW |
1038 | * @return array |
1039 | * e.g. $result['field_name']['label'] | |
6a488035 TO |
1040 | */ |
1041 | public static function getAvailableFieldsFlat($force = FALSE) { | |
ffb9cdec CW |
1042 | if (!isset(Civi::$statics['UFFieldsFlat']) || $force) { |
1043 | Civi::$statics['UFFieldsFlat'] = []; | |
1044 | foreach (self::getAvailableFields() as $fieldType => $fields) { | |
1045 | foreach ($fields as $fieldName => $field) { | |
1046 | if (!isset(Civi::$statics['UFFieldsFlat'][$fieldName])) { | |
1047 | $field['field_type'] = $fieldType; | |
1048 | Civi::$statics['UFFieldsFlat'][$fieldName] = $field; | |
6a488035 TO |
1049 | } |
1050 | } | |
1051 | } | |
1052 | } | |
ffb9cdec CW |
1053 | return Civi::$statics['UFFieldsFlat']; |
1054 | } | |
1055 | ||
1056 | /** | |
1057 | * Get a list of fields which can be added to profiles in the format [name => title] | |
1058 | * | |
1059 | * @return array | |
1060 | */ | |
1061 | public static function getAvailableFieldTitles() { | |
1062 | $fields = self::getAvailableFieldsFlat(); | |
75c4fcec | 1063 | $fields['formatting'] = ['title' => ts('Formatting')]; |
ffb9cdec | 1064 | return CRM_Utils_Array::collect('title', $fields); |
6a488035 TO |
1065 | } |
1066 | ||
1067 | /** | |
fe482240 | 1068 | * Determine whether the given field_name is valid. |
6a488035 TO |
1069 | * |
1070 | * @param string $fieldName | |
1071 | * @return bool | |
1072 | */ | |
00be9182 | 1073 | public static function isValidFieldName($fieldName) { |
6a488035 TO |
1074 | $availableFields = CRM_Core_BAO_UFField::getAvailableFieldsFlat(); |
1075 | return isset($availableFields[$fieldName]); | |
1076 | } | |
1077 | ||
b5c2afd0 EM |
1078 | /** |
1079 | * @return array|null | |
1080 | */ | |
00be9182 | 1081 | public static function getContribBatchEntryFields() { |
6a488035 | 1082 | if (self::$_contriBatchEntryFields === NULL) { |
be2fb01f CW |
1083 | self::$_contriBatchEntryFields = [ |
1084 | 'send_receipt' => [ | |
6a488035 TO |
1085 | 'name' => 'send_receipt', |
1086 | 'title' => ts('Send Receipt'), | |
be2fb01f CW |
1087 | ], |
1088 | 'soft_credit' => [ | |
6a488035 TO |
1089 | 'name' => 'soft_credit', |
1090 | 'title' => ts('Soft Credit'), | |
be2fb01f CW |
1091 | ], |
1092 | 'soft_credit_type' => [ | |
752ddf35 N |
1093 | 'name' => 'soft_credit_type', |
1094 | 'title' => ts('Soft Credit Type'), | |
be2fb01f CW |
1095 | ], |
1096 | 'product_name' => [ | |
6a488035 TO |
1097 | 'name' => 'product_name', |
1098 | 'title' => ts('Premiums'), | |
be2fb01f CW |
1099 | ], |
1100 | 'contribution_note' => [ | |
6a488035 TO |
1101 | 'name' => 'contribution_note', |
1102 | 'title' => ts('Contribution Note'), | |
be2fb01f CW |
1103 | ], |
1104 | 'contribution_soft_credit_pcp_id' => [ | |
03ad81ae AH |
1105 | 'name' => 'contribution_soft_credit_pcp_id', |
1106 | 'title' => ts('Personal Campaign Page'), | |
be2fb01f CW |
1107 | ], |
1108 | ]; | |
6a488035 TO |
1109 | } |
1110 | return self::$_contriBatchEntryFields; | |
1111 | } | |
1112 | ||
b5c2afd0 EM |
1113 | /** |
1114 | * @return array|null | |
1115 | */ | |
6a488035 TO |
1116 | public static function getMemberBatchEntryFields() { |
1117 | if (self::$_memberBatchEntryFields === NULL) { | |
be2fb01f CW |
1118 | self::$_memberBatchEntryFields = [ |
1119 | 'send_receipt' => [ | |
6a488035 TO |
1120 | 'name' => 'send_receipt', |
1121 | 'title' => ts('Send Receipt'), | |
be2fb01f CW |
1122 | ], |
1123 | 'soft_credit' => [ | |
6a488035 TO |
1124 | 'name' => 'soft_credit', |
1125 | 'title' => ts('Soft Credit'), | |
be2fb01f CW |
1126 | ], |
1127 | 'product_name' => [ | |
6a488035 TO |
1128 | 'name' => 'product_name', |
1129 | 'title' => ts('Premiums'), | |
be2fb01f CW |
1130 | ], |
1131 | 'financial_type' => [ | |
6a488035 TO |
1132 | 'name' => 'financial_type', |
1133 | 'title' => ts('Financial Type'), | |
be2fb01f CW |
1134 | ], |
1135 | 'total_amount' => [ | |
6a488035 TO |
1136 | 'name' => 'total_amount', |
1137 | 'title' => ts('Total Amount'), | |
be2fb01f CW |
1138 | ], |
1139 | 'receive_date' => [ | |
6a488035 | 1140 | 'name' => 'receive_date', |
7bc6b5bb | 1141 | 'title' => ts('Date Received'), |
be2fb01f CW |
1142 | ], |
1143 | 'payment_instrument' => [ | |
6a488035 | 1144 | 'name' => 'payment_instrument', |
536f0e02 | 1145 | 'title' => ts('Payment Method'), |
be2fb01f CW |
1146 | ], |
1147 | 'contribution_status_id' => [ | |
6a488035 TO |
1148 | 'name' => 'contribution_status_id', |
1149 | 'title' => ts('Contribution Status'), | |
be2fb01f CW |
1150 | ], |
1151 | 'trxn_id' => [ | |
47087bdc | 1152 | 'name' => 'contribution_trxn_id', |
1153 | 'title' => ts('Contribution Transaction ID'), | |
be2fb01f CW |
1154 | ], |
1155 | ]; | |
6a488035 TO |
1156 | } |
1157 | return self::$_memberBatchEntryFields; | |
1158 | } | |
96025800 | 1159 | |
6a488035 | 1160 | } |