3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates form components for OpenCase Activity.
21 class CRM_Case_Form_Activity_ChangeCaseStatus
{
24 * @param CRM_Core_Form $form
28 public static function preProcess(&$form) {
29 if (!isset($form->_caseId
)) {
30 CRM_Core_Error
::fatal(ts('Case Id not found.'));
33 $form->addElement('checkbox', 'updateLinkedCases', NULL, NULL, ['class' => 'select-row']);
35 $caseID = CRM_Utils_Array
::first($form->_caseId
);
36 $cases = CRM_Case_BAO_Case
::getRelatedCases($caseID);
37 $form->assign('linkedCases', $cases);
41 * Set default values for the form.
43 * For edit/view mode the default values are retrieved from the database.
46 * @param CRM_Core_Form $form
50 public static function setDefaultValues(&$form) {
52 // Retrieve current case status
53 $defaults['case_status_id'] = $form->_defaultCaseStatus
;
59 * @param CRM_Core_Form $form
61 public static function buildQuickForm(&$form) {
62 $form->removeElement('status_id');
63 $form->removeElement('priority_id');
67 $form->_caseStatus
= CRM_Case_PseudoConstant
::caseStatus();
68 $statusNames = CRM_Case_PseudoConstant
::caseStatus('name');
70 // Limit case statuses to allowed types for these case(s)
71 $allCases = civicrm_api3('Case', 'get', ['return' => 'case_type_id', 'id' => ['IN' => (array) $form->_caseId
]]);
72 foreach ($allCases['values'] as $case) {
73 $caseTypes[$case['case_type_id']] = $case['case_type_id'];
75 $caseTypes = civicrm_api3('CaseType', 'get', ['id' => ['IN' => $caseTypes]]);
76 foreach ($caseTypes['values'] as $ct) {
77 if (!empty($ct['definition']['statuses'])) {
78 foreach ($form->_caseStatus
as $id => $label) {
79 if (!in_array($statusNames[$id], $ct['definition']['statuses'])) {
80 unset($form->_caseStatus
[$id]);
86 foreach ($form->_caseId
as $key => $val) {
87 $form->_oldCaseStatus
[] = $form->_defaultCaseStatus
[] = CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_Case', $val, 'status_id');
90 foreach ($form->_defaultCaseStatus
as $keydefault => $valdefault) {
91 if (!array_key_exists($valdefault, $form->_caseStatus
)) {
92 $form->_caseStatus
[$valdefault] = CRM_Core_PseudoConstant
::getLabel('CRM_Case_BAO_Case', 'status_id', $valdefault);
95 $element = $form->add('select', 'case_status_id', ts('Case Status'),
96 $form->_caseStatus
, TRUE
98 // check if the case status id passed in url is a valid one, set as default and freeze
99 if (CRM_Utils_Request
::retrieve('case_status_id', 'Positive', $form)) {
100 $caseStatusId = CRM_Utils_Request
::retrieve('case_status_id', 'Positive', $form);
101 $caseStatus = CRM_Case_PseudoConstant
::caseStatus();
102 $form->_defaultCaseStatus
= array_key_exists($caseStatusId, $caseStatus) ?
$caseStatusId : NULL;
108 * Global validation rules for the form.
110 * @param array $values
111 * Posted values of the form.
114 * @param CRM_Core_Form $form
117 * list of errors to be posted back to the form
119 public static function formRule($values, $files, $form) {
124 * Process the form submission.
126 * @param CRM_Core_Form $form
127 * @param array $params
129 public static function beginPostProcess(&$form, &$params) {
130 $params['id'] = CRM_Utils_Array
::value('case_id', $params);
132 if (CRM_Utils_Array
::value('updateLinkedCases', $params) === '1') {
133 $caseID = CRM_Utils_Array
::first($form->_caseId
);
134 $cases = CRM_Case_BAO_Case
::getRelatedCases($caseID);
136 foreach ($cases as $currentCase) {
137 if ($currentCase['status_id'] != $params['case_status_id']) {
138 $form->_caseId
[] = $currentCase['case_id'];
145 * Process the form submission.
147 * @param CRM_Core_Form $form
148 * @param array $params
149 * @param CRM_Activity_BAO_Activity $activity
151 public static function endPostProcess(&$form, &$params, $activity) {
152 $groupingValues = CRM_Core_OptionGroup
::values('case_status', FALSE, TRUE, FALSE, NULL, 'value');
154 // Set case end_date if we're closing the case. Clear end_date if we're (re)opening it.
155 if (CRM_Utils_Array
::value($params['case_status_id'], $groupingValues) == 'Closed' && !empty($params['activity_date_time'])) {
156 $params['end_date'] = CRM_Utils_Date
::isoToMysql($params['activity_date_time']);
158 // End case-specific relationships (roles)
159 foreach ($params['target_contact_id'] as $cid) {
160 $rels = CRM_Case_BAO_Case
::getCaseRoles($cid, $params['case_id']);
161 foreach ($rels as $relId => $relData) {
162 $relationshipParams = [
164 'end_date' => $params['end_date'],
166 // @todo we can't switch directly to api because there is too much business logic and it breaks closing cases with organisations as client relationships
167 //civicrm_api3('Relationship', 'create', $relationshipParams);
168 CRM_Contact_BAO_Relationship
::add($relationshipParams);
172 elseif (CRM_Utils_Array
::value($params['case_status_id'], $groupingValues) == 'Opened') {
173 $params['end_date'] = "null";
175 // Reopen case-specific relationships (roles)
176 foreach ($params['target_contact_id'] as $cid) {
177 $rels = CRM_Case_BAO_Case
::getCaseRoles($cid, $params['case_id'], NULL, FALSE);
178 foreach ($rels as $relId => $relData) {
179 $relationshipParams = [
181 'end_date' => 'null',
183 // @todo we can't switch directly to api because there is too much business logic and it breaks closing cases with organisations as client relationships
184 //civicrm_api3('Relationship', 'create', $relationshipParams);
185 CRM_Contact_BAO_Relationship
::add($relationshipParams);
189 $params['status_id'] = CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed');
190 $activity->status_id
= $params['status_id'];
191 $params['priority_id'] = CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'priority_id', 'Normal');
192 $activity->priority_id
= $params['priority_id'];
194 foreach ($form->_oldCaseStatus
as $statuskey => $statusval) {
195 if ($activity->subject
== 'null') {
196 $activity->subject
= ts('Case status changed from %1 to %2', [
197 1 => CRM_Utils_Array
::value($statusval, $form->_caseStatus
),
198 2 => CRM_Utils_Array
::value($params['case_status_id'], $form->_caseStatus
),