Use ?? operator instead of CRM_Utils_Array::value() in array assignments
[civicrm-core.git] / CRM / Case / Form / Activity / ChangeCaseType.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
3819f101 19 * This class generates form components for OpenCase Activity.
6a488035
TO
20 */
21class CRM_Case_Form_Activity_ChangeCaseType {
22
4c6ce474 23 /**
c490a46a 24 * @param CRM_Core_Form $form
4c6ce474
EM
25 *
26 * @throws Exception
27 */
00be9182 28 public static function preProcess(&$form) {
6a488035
TO
29 if (!isset($form->_caseId)) {
30 CRM_Core_Error::fatal(ts('Case Id not found.'));
31 }
32 }
33
34 /**
3819f101 35 * Set default values for the form.
6a488035 36 *
3819f101 37 * For edit/view mode the default values are retrieved from the database.
6a488035 38 *
c490a46a 39 * @param CRM_Core_Form $form
77b97be7 40 *
3819f101 41 * @return array
6a488035 42 */
00be9182 43 public static function setDefaultValues(&$form) {
be2fb01f 44 $defaults = [];
6a488035
TO
45
46 $defaults['is_reset_timeline'] = 1;
47
c70de000 48 $defaults['reset_date_time'] = date('Y-m-d H:i:s');
6a488035
TO
49 $defaults['case_type_id'] = $form->_caseTypeId;
50
51 return $defaults;
52 }
53
4c6ce474 54 /**
c490a46a 55 * @param CRM_Core_Form $form
4c6ce474 56 */
00be9182 57 public static function buildQuickForm(&$form) {
6a488035
TO
58 $form->removeElement('status_id');
59 $form->removeElement('priority_id');
60
c22c1324 61 $caseId = CRM_Utils_Array::first($form->_caseId);
7a5c0c6c 62 $form->_caseType = CRM_Case_BAO_Case::buildOptions('case_type_id', 'create');
8ffdec17 63 $form->_caseTypeId = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case',
c22c1324 64 $caseId,
8ffdec17
ARW
65 'case_type_id'
66 );
6a488035 67 if (!in_array($form->_caseTypeId, $form->_caseType)) {
d14b35a4 68 $form->_caseType[$form->_caseTypeId] = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseType', $form->_caseTypeId, 'title');
6a488035
TO
69 }
70
be2fb01f 71 $form->addField('case_type_id', ['context' => 'create', 'entity' => 'Case']);
6a488035
TO
72
73 // timeline
c70de000
CW
74 $form->addYesNo('is_reset_timeline', ts('Reset Case Timeline?'), NULL, TRUE);
75 $form->add('datepicker', 'reset_date_time', ts('Reset Start Date'), NULL, FALSE, ['allowClear' => FALSE]);
6a488035
TO
76 }
77
78 /**
d2e5d2ce 79 * Global validation rules for the form.
6a488035 80 *
64bd5a0e
TO
81 * @param array $values
82 * Posted values of the form.
6a488035 83 *
77b97be7 84 * @param $files
c490a46a 85 * @param CRM_Core_Form $form
77b97be7 86 *
a6c01b45
CW
87 * @return array
88 * list of errors to be posted back to the form
6a488035 89 */
00be9182 90 public static function formRule($values, $files, $form) {
6a488035
TO
91 return TRUE;
92 }
93
94 /**
d2e5d2ce 95 * Process the form submission.
6a488035 96 *
6a488035 97 *
c490a46a
CW
98 * @param CRM_Core_Form $form
99 * @param array $params
6a488035 100 */
00be9182 101 public static function beginPostProcess(&$form, &$params) {
6a488035
TO
102 if ($form->_context == 'case') {
103 $params['id'] = $form->_id;
104 }
105
de6c59ca 106 if (empty($params['is_reset_timeline'])) {
6a488035
TO
107 unset($params['reset_date_time']);
108 }
6a488035
TO
109 }
110
111 /**
d2e5d2ce 112 * Process the form submission.
6a488035 113 *
6a488035 114 *
c490a46a
CW
115 * @param CRM_Core_Form $form
116 * @param array $params
fd31fa4c 117 * @param $activity
6a488035 118 */
00be9182 119 public static function endPostProcess(&$form, &$params, $activity) {
6a488035
TO
120 if (!$form->_caseId) {
121 // always expecting a change, so case-id is a must.
122 return;
123 }
124
125 $caseTypes = CRM_Case_PseudoConstant::caseType('name');
42047ce4 126 $allCaseTypes = CRM_Case_PseudoConstant::caseType('title', FALSE);
6a488035 127
a7488080 128 if (!empty($caseTypes[$params['case_type_id']])) {
6a488035
TO
129 $caseType = $caseTypes[$params['case_type_id']];
130 }
131
132 if (!$form->_currentlyViewedContactId ||
133 !$form->_currentUserId ||
134 !$params['case_type_id'] ||
135 !$caseType
136 ) {
137 CRM_Core_Error::fatal('Required parameter missing for ChangeCaseType - end post processing');
138 }
139
9c248a42 140 $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed');
6a488035 141 $activity->status_id = $params['status_id'];
0571e734 142 $params['priority_id'] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'priority_id', 'Normal');
6a488035
TO
143 $activity->priority_id = $params['priority_id'];
144
145 if ($activity->subject == 'null') {
146 $activity->subject = ts('Case type changed from %1 to %2',
be2fb01f 147 [
6b409353
CW
148 1 => $allCaseTypes[$form->_defaults['case_type_id']] ?? NULL,
149 2 => $allCaseTypes[$params['case_type_id']] ?? NULL,
be2fb01f 150 ]
6a488035
TO
151 );
152 $activity->save();
153 }
154
155 // 1. initiate xml processor
156 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
c22c1324 157 $caseId = CRM_Utils_Array::first($form->_caseId);
be2fb01f 158 $xmlProcessorParams = [
6a488035
TO
159 'clientID' => $form->_currentlyViewedContactId,
160 'creatorID' => $form->_currentUserId,
161 'standardTimeline' => 1,
162 'activityTypeName' => 'Change Case Type',
6b409353 163 'activity_date_time' => $params['reset_date_time'] ?? NULL,
c22c1324 164 'caseID' => $caseId,
6b409353 165 'resetTimeline' => $params['is_reset_timeline'] ?? NULL,
be2fb01f 166 ];
6a488035
TO
167
168 $xmlProcessor->run($caseType, $xmlProcessorParams);
169 // status msg
170 $params['statusMsg'] = ts('Case Type changed successfully.');
171 }
96025800 172
6a488035 173}