3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * This class generates form components for OpenCase Activity.
37 class CRM_Case_Form_Activity_ChangeCaseStatus
{
40 * @param CRM_Core_Form $form
44 public static function preProcess(&$form) {
45 if (!isset($form->_caseId
)) {
46 CRM_Core_Error
::fatal(ts('Case Id not found.'));
49 $form->addElement('checkbox', 'updateLinkedCases', NULL, NULL, ['class' => 'select-row']);
51 $caseID = CRM_Utils_Array
::first($form->_caseId
);
52 $cases = CRM_Case_BAO_Case
::getRelatedCases($caseID);
53 $form->assign('linkedCases', $cases);
57 * Set default values for the form.
59 * For edit/view mode the default values are retrieved from the database.
62 * @param CRM_Core_Form $form
66 public static function setDefaultValues(&$form) {
68 // Retrieve current case status
69 $defaults['case_status_id'] = $form->_defaultCaseStatus
;
75 * @param CRM_Core_Form $form
77 public static function buildQuickForm(&$form) {
78 $form->removeElement('status_id');
79 $form->removeElement('priority_id');
83 $form->_caseStatus
= CRM_Case_PseudoConstant
::caseStatus();
84 $statusNames = CRM_Case_PseudoConstant
::caseStatus('name');
86 // Limit case statuses to allowed types for these case(s)
87 $allCases = civicrm_api3('Case', 'get', ['return' => 'case_type_id', 'id' => ['IN' => (array) $form->_caseId
]]);
88 foreach ($allCases['values'] as $case) {
89 $caseTypes[$case['case_type_id']] = $case['case_type_id'];
91 $caseTypes = civicrm_api3('CaseType', 'get', ['id' => ['IN' => $caseTypes]]);
92 foreach ($caseTypes['values'] as $ct) {
93 if (!empty($ct['definition']['statuses'])) {
94 foreach ($form->_caseStatus
as $id => $label) {
95 if (!in_array($statusNames[$id], $ct['definition']['statuses'])) {
96 unset($form->_caseStatus
[$id]);
102 foreach ($form->_caseId
as $key => $val) {
103 $form->_oldCaseStatus
[] = $form->_defaultCaseStatus
[] = CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_Case', $val, 'status_id');
106 foreach ($form->_defaultCaseStatus
as $keydefault => $valdefault) {
107 if (!array_key_exists($valdefault, $form->_caseStatus
)) {
108 $form->_caseStatus
[$valdefault] = CRM_Core_PseudoConstant
::getLabel('CRM_Case_BAO_Case', 'status_id', $valdefault);
111 $element = $form->add('select', 'case_status_id', ts('Case Status'),
112 $form->_caseStatus
, TRUE
114 // check if the case status id passed in url is a valid one, set as default and freeze
115 if (CRM_Utils_Request
::retrieve('case_status_id', 'Positive', $form)) {
116 $caseStatusId = CRM_Utils_Request
::retrieve('case_status_id', 'Positive', $form);
117 $caseStatus = CRM_Case_PseudoConstant
::caseStatus();
118 $form->_defaultCaseStatus
= array_key_exists($caseStatusId, $caseStatus) ?
$caseStatusId : NULL;
124 * Global validation rules for the form.
126 * @param array $values
127 * Posted values of the form.
130 * @param CRM_Core_Form $form
133 * list of errors to be posted back to the form
135 public static function formRule($values, $files, $form) {
140 * Process the form submission.
142 * @param CRM_Core_Form $form
143 * @param array $params
145 public static function beginPostProcess(&$form, &$params) {
146 $params['id'] = CRM_Utils_Array
::value('case_id', $params);
148 if ($params['updateLinkedCases'] === '1') {
149 $caseID = CRM_Utils_Array
::first($form->_caseId
);
150 $cases = CRM_Case_BAO_Case
::getRelatedCases($caseID);
152 foreach ($cases as $currentCase) {
153 if ($currentCase['status_id'] != $params['case_status_id']) {
154 $form->_caseId
[] = $currentCase['case_id'];
161 * Process the form submission.
163 * @param CRM_Core_Form $form
164 * @param array $params
165 * @param CRM_Activity_BAO_Activity $activity
167 public static function endPostProcess(&$form, &$params, $activity) {
169 $groupingValues = CRM_Core_OptionGroup
::values('case_status', FALSE, TRUE, FALSE, NULL, 'value');
171 // Set case end_date if we're closing the case. Clear end_date if we're (re)opening it.
172 if (CRM_Utils_Array
::value($params['case_status_id'], $groupingValues) == 'Closed' && !empty($params['activity_date_time'])) {
173 $params['end_date'] = CRM_Utils_Date
::isoToMysql($params['activity_date_time']);
175 // End case-specific relationships (roles)
176 foreach ($params['target_contact_id'] as $cid) {
177 $rels = CRM_Case_BAO_Case
::getCaseRoles($cid, $params['case_id']);
178 // FIXME: Is there an existing function to close a relationship?
179 $query = 'UPDATE civicrm_relationship SET end_date=%2 WHERE id=%1';
180 foreach ($rels as $relId => $relData) {
182 1 => [$relId, 'Integer'],
183 2 => [$params['end_date'], 'Timestamp'],
185 CRM_Core_DAO
::executeQuery($query, $relParams);
189 elseif (CRM_Utils_Array
::value($params['case_status_id'], $groupingValues) == 'Opened') {
190 $params['end_date'] = "null";
192 // Reopen case-specific relationships (roles)
193 foreach ($params['target_contact_id'] as $cid) {
194 $rels = CRM_Case_BAO_Case
::getCaseRoles($cid, $params['case_id'], NULL, FALSE);
195 // FIXME: Is there an existing function?
196 $query = 'UPDATE civicrm_relationship SET end_date=NULL WHERE id=%1';
197 foreach ($rels as $relId => $relData) {
198 $relParams = [1 => [$relId, 'Integer']];
199 CRM_Core_DAO
::executeQuery($query, $relParams);
203 $params['status_id'] = CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed');
204 $activity->status_id
= $params['status_id'];
205 $params['priority_id'] = CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'priority_id', 'Normal');
206 $activity->priority_id
= $params['priority_id'];
208 foreach ($form->_oldCaseStatus
as $statuskey => $statusval) {
209 if ($activity->subject
== 'null') {
210 $activity->subject
= ts('Case status changed from %1 to %2', [
211 1 => CRM_Utils_Array
::value($statusval, $form->_caseStatus
),
212 2 => CRM_Utils_Array
::value($params['case_status_id'], $form->_caseStatus
),