Merge pull request #17953 from civicrm/5.28
[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 $messages = [];
24 $problemValues = [];
25 $optionGroups = civicrm_api3('OptionGroup', 'get', [
26 'sequential' => 1,
27 'data_type' => ['IS NOT NULL' => 1],
28 'options' => ['limit' => 0],
29 ]);
30 if ($optionGroups['count'] > 0) {
31 foreach ($optionGroups['values'] as $optionGroup) {
32 $values = CRM_Core_BAO_OptionValue::getOptionValuesArray($optionGroup['id']);
33 if (count($values) > 0) {
34 foreach ($values as $value) {
35 try {
36 CRM_Utils_Type::validate($value['value'], $optionGroup['data_type']);
37 }
38 catch (CRM_Core_Exception $e) {
39 $problemValues[] = [
40 'group_name' => $optionGroup['title'],
41 'value_name' => $value['label'],
42 ];
43 }
44 }
45 }
46 }
47 }
48 if (!empty($problemValues)) {
49 $strings = '';
50 foreach ($problemValues as $problemValue) {
51 $strings .= ts('<tr><td> "%1" </td><td> "%2" </td></tr>', [
52 1 => $problemValue['group_name'],
53 2 => $problemValue['value_name'],
54 ]);
55 }
56
57 $messages[] = new CRM_Utils_Check_Message(
58 __FUNCTION__,
59 ts('The Following Option Values contain value fields that do not match the Data Type of the Option Group</p>
60 <p><table><tbody><th>Option Group</th><th>Option Value</th></tbody><tbody>') .
61 $strings . ts('</tbody></table></p>'),
62 ts('Option Values with problematic Values'),
63 \Psr\Log\LogLevel::NOTICE,
64 'fa-server'
65 );
66 }
67
68 return $messages;
69 }
70
71 }