Merge pull request #11986 from eileenmcnaughton/test
[civicrm-core.git] / CRM / Utils / Check / Component / Case.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2018
32 */
33 class CRM_Utils_Check_Component_Case extends CRM_Utils_Check_Component {
34
35 const DOCTOR_WHEN = 'https://github.com/civicrm/org.civicrm.doctorwhen';
36
37 /**
38 * @var CRM_Case_XMLRepository
39 */
40 protected $xmlRepo;
41
42 /**
43 * @var array<string>
44 */
45 protected $caseTypeNames;
46
47 /**
48 * Class constructor.
49 */
50 public function __construct() {
51 $this->caseTypeNames = CRM_Case_PseudoConstant::caseType('name');
52 $this->xmlRepo = CRM_Case_XMLRepository::singleton();
53 }
54
55 /**
56 * @inheritDoc
57 */
58 public function isEnabled() {
59 return CRM_Case_BAO_Case::enabled();
60 }
61
62 /**
63 * Check that the case-type names don't rely on double-munging.
64 *
65 * @return array<CRM_Utils_Check_Message>
66 * An empty array, or a list of warnings
67 */
68 public function checkCaseTypeNameConsistency() {
69 $messages = array();
70
71 foreach ($this->caseTypeNames as $caseTypeName) {
72 $normalFile = $this->xmlRepo->findXmlFile($caseTypeName);
73 $mungedFile = $this->xmlRepo->findXmlFile(CRM_Case_XMLProcessor::mungeCaseType($caseTypeName));
74
75 if ($normalFile && $mungedFile && $normalFile == $mungedFile) {
76 // ok
77 }
78 elseif ($normalFile && $mungedFile) {
79 $messages[] = new CRM_Utils_Check_Message(
80 __FUNCTION__ . $caseTypeName,
81 ts('Case type "%1" has duplicate XML files ("%2" and "%3")', array(
82 1 => $caseTypeName,
83 2 => $normalFile,
84 3 => $mungedFile,
85 )) .
86 '<br /><a href="' . CRM_Utils_System::getWikiBaseURL() . __FUNCTION__ . '">' .
87 ts('Read more about this warning') .
88 '</a>',
89 ts('CiviCase'),
90 \Psr\Log\LogLevel::WARNING,
91 'fa-puzzle-piece'
92 );
93 }
94 elseif ($normalFile && !$mungedFile) {
95 // ok
96 }
97 elseif (!$normalFile && $mungedFile) {
98 $messages[] = new CRM_Utils_Check_Message(
99 __FUNCTION__ . $caseTypeName,
100 ts('Case type "%1" corresponds to XML file ("%2") The XML file should be named "%3".', array(
101 1 => $caseTypeName,
102 2 => $mungedFile,
103 3 => "{$caseTypeName}.xml",
104 )) .
105 '<br /><a href="' . CRM_Utils_System::getWikiBaseURL() . __FUNCTION__ . '">' .
106 ts('Read more about this warning') .
107 '</a>',
108 ts('CiviCase'),
109 \Psr\Log\LogLevel::WARNING,
110 'fa-puzzle-piece'
111 );
112 }
113 elseif (!$normalFile && !$mungedFile) {
114 // ok -- probably a new or DB-based CaseType
115 }
116 }
117
118 return $messages;
119 }
120
121 /**
122 * Check that the timestamp columns are populated. (CRM-20958)
123 *
124 * @return array<CRM_Utils_Check_Message>
125 * An empty array, or a list of warnings
126 */
127 public function checkNullTimestamps() {
128 $messages = array();
129
130 $nullCount = 0;
131 $nullCount += CRM_Utils_SQL_Select::from('civicrm_activity')
132 ->where('created_date IS NULL OR modified_date IS NULL')
133 ->select('COUNT(*)')
134 ->execute()
135 ->fetchValue();
136 $nullCount += CRM_Utils_SQL_Select::from('civicrm_case')
137 ->where('created_date IS NULL OR modified_date IS NULL')
138 ->select('COUNT(*)')
139 ->execute()
140 ->fetchValue();
141
142 if ($nullCount > 0) {
143 $messages[] = new CRM_Utils_Check_Message(
144 __FUNCTION__,
145 '<p>' .
146 ts('The tables "<em>civicrm_activity</em>" and "<em>civicrm_case</em>" were updated to support two new fields, "<em>created_date</em>" and "<em>modified_date</em>". For historical data, these fields may appear blank. (%1 records have NULL timestamps.)', array(
147 1 => $nullCount,
148 )) .
149 '</p><p>' .
150 ts('At time of writing, this is not a problem. However, future extensions and improvements could rely on these fields, so it may be useful to back-fill them.') .
151 '</p><p>' .
152 ts('For further discussion, please visit %1', array(
153 1 => sprintf('<a href="%s" target="_blank">%s</a>', self::DOCTOR_WHEN, self::DOCTOR_WHEN),
154 )) .
155 '</p>',
156 ts('Timestamps for Activities and Cases'),
157 \Psr\Log\LogLevel::NOTICE,
158 'fa-clock-o'
159 );
160 }
161
162 return $messages;
163 }
164
165 }