Merge pull request #17770 from civicrm/5.28
[civicrm-core.git] / CRM / Utils / Check / Component / OptionGroups.php
CommitLineData
24877581
SL
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
24877581 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 |
24877581
SL
9 +--------------------------------------------------------------------+
10 */
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
24877581
SL
16 */
17class CRM_Utils_Check_Component_OptionGroups extends CRM_Utils_Check_Component {
18
19 /**
20 * @return array
21 */
22 public function checkOptionGroupValues() {
be2fb01f
CW
23 $messages = [];
24 $problemValues = [];
25 $optionGroups = civicrm_api3('OptionGroup', 'get', [
24877581 26 'sequential' => 1,
be2fb01f
CW
27 'data_type' => ['IS NOT NULL' => 1],
28 'options' => ['limit' => 0],
29 ]);
24877581
SL
30 if ($optionGroups['count'] > 0) {
31 foreach ($optionGroups['values'] as $optionGroup) {
89c05afc
SL
32 $values = CRM_Core_BAO_OptionValue::getOptionValuesArray($optionGroup['id']);
33 if (count($values) > 0) {
34 foreach ($values as $value) {
8f352cb6 35 try {
8a6d5abd 36 CRM_Utils_Type::validate($value['value'], $optionGroup['data_type']);
8f352cb6 37 }
8a6d5abd 38 catch (CRM_Core_Exception $e) {
be2fb01f 39 $problemValues[] = [
c62c37c7 40 'group_name' => $optionGroup['title'],
24877581 41 'value_name' => $value['label'],
be2fb01f 42 ];
24877581
SL
43 }
44 }
45 }
46 }
47 }
48 if (!empty($problemValues)) {
a37e4e6c 49 $strings = '';
24877581 50 foreach ($problemValues as $problemValue) {
be2fb01f 51 $strings .= ts('<tr><td> "%1" </td><td> "%2" </td></tr>', [
24877581
SL
52 1 => $problemValue['group_name'],
53 2 => $problemValue['value_name'],
be2fb01f 54 ]);
24877581
SL
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>') .
a37e4e6c 61 $strings . ts('</tbody></table></p>'),
24877581
SL
62 ts('Option Values with problematic Values'),
63 \Psr\Log\LogLevel::NOTICE,
64 'fa-server'
65 );
66 }
67
68 return $messages;
69 }
70
71}