Merge pull request #19321 from colemanw/profileGetFieldsFix
[civicrm-core.git] / CRM / Utils / Check / Component / OptionGroups.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Utils_Check_Component_OptionGroups extends CRM_Utils_Check_Component {
18
19 /**
20 * @return CRM_Utils_Check_Message[]
21 */
22 public function checkOptionGroupValues() {
23 if (CRM_Utils_System::version() !== CRM_Core_BAO_Domain::version()) {
24 return [];
25 }
26
27 $messages = [];
28 $problemValues = [];
29 $optionGroups = civicrm_api3('OptionGroup', 'get', [
30 'sequential' => 1,
31 'data_type' => ['IS NOT NULL' => 1],
32 'options' => ['limit' => 0],
33 ]);
34 if ($optionGroups['count'] > 0) {
35 foreach ($optionGroups['values'] as $optionGroup) {
36 $values = CRM_Core_BAO_OptionValue::getOptionValuesArray($optionGroup['id']);
37 if (count($values) > 0) {
38 foreach ($values as $value) {
39 try {
40 CRM_Utils_Type::validate($value['value'], $optionGroup['data_type']);
41 }
42 catch (CRM_Core_Exception $e) {
43 $problemValues[] = [
44 'group_name' => $optionGroup['title'],
45 'value_name' => $value['label'],
46 ];
47 }
48 }
49 }
50 }
51 }
52 if (!empty($problemValues)) {
53 $strings = '';
54 foreach ($problemValues as $problemValue) {
55 $strings .= ts('<tr><td> "%1" </td><td> "%2" </td></tr>', [
56 1 => $problemValue['group_name'],
57 2 => $problemValue['value_name'],
58 ]);
59 }
60
61 $messages[] = new CRM_Utils_Check_Message(
62 __FUNCTION__,
63 ts('The Following Option Values contain value fields that do not match the Data Type of the Option Group</p>
64 <p><table><tbody><th>Option Group</th><th>Option Value</th></tbody><tbody>') .
65 $strings . ts('</tbody></table></p>'),
66 ts('Option Values with problematic Values'),
67 \Psr\Log\LogLevel::NOTICE,
68 'fa-server'
69 );
70 }
71
72 return $messages;
73 }
74
75 }