Merge pull request #16004 from civicrm/5.20
[civicrm-core.git] / CRM / Case / Form / Activity / ChangeCaseType.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class generates form components for OpenCase Activity.
20 */
21 class CRM_Case_Form_Activity_ChangeCaseType {
22
23 /**
24 * @param CRM_Core_Form $form
25 *
26 * @throws Exception
27 */
28 public static function preProcess(&$form) {
29 if (!isset($form->_caseId)) {
30 CRM_Core_Error::fatal(ts('Case Id not found.'));
31 }
32 }
33
34 /**
35 * Set default values for the form.
36 *
37 * For edit/view mode the default values are retrieved from the database.
38 *
39 * @param CRM_Core_Form $form
40 *
41 * @return array
42 */
43 public static function setDefaultValues(&$form) {
44 $defaults = [];
45
46 $defaults['is_reset_timeline'] = 1;
47
48 $defaults['reset_date_time'] = date('Y-m-d H:i:s');
49 $defaults['case_type_id'] = $form->_caseTypeId;
50
51 return $defaults;
52 }
53
54 /**
55 * @param CRM_Core_Form $form
56 */
57 public static function buildQuickForm(&$form) {
58 $form->removeElement('status_id');
59 $form->removeElement('priority_id');
60
61 $caseId = CRM_Utils_Array::first($form->_caseId);
62 $form->_caseType = CRM_Case_BAO_Case::buildOptions('case_type_id', 'create');
63 $form->_caseTypeId = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case',
64 $caseId,
65 'case_type_id'
66 );
67 if (!in_array($form->_caseTypeId, $form->_caseType)) {
68 $form->_caseType[$form->_caseTypeId] = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseType', $form->_caseTypeId, 'title');
69 }
70
71 $form->addField('case_type_id', ['context' => 'create', 'entity' => 'Case']);
72
73 // timeline
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]);
76 }
77
78 /**
79 * Global validation rules for the form.
80 *
81 * @param array $values
82 * Posted values of the form.
83 *
84 * @param $files
85 * @param CRM_Core_Form $form
86 *
87 * @return array
88 * list of errors to be posted back to the form
89 */
90 public static function formRule($values, $files, $form) {
91 return TRUE;
92 }
93
94 /**
95 * Process the form submission.
96 *
97 *
98 * @param CRM_Core_Form $form
99 * @param array $params
100 */
101 public static function beginPostProcess(&$form, &$params) {
102 if ($form->_context == 'case') {
103 $params['id'] = $form->_id;
104 }
105
106 if (empty($params['is_reset_timeline'])) {
107 unset($params['reset_date_time']);
108 }
109 }
110
111 /**
112 * Process the form submission.
113 *
114 *
115 * @param CRM_Core_Form $form
116 * @param array $params
117 * @param $activity
118 */
119 public static function endPostProcess(&$form, &$params, $activity) {
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');
126 $allCaseTypes = CRM_Case_PseudoConstant::caseType('title', FALSE);
127
128 if (!empty($caseTypes[$params['case_type_id']])) {
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
140 $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed');
141 $activity->status_id = $params['status_id'];
142 $params['priority_id'] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'priority_id', 'Normal');
143 $activity->priority_id = $params['priority_id'];
144
145 if ($activity->subject == 'null') {
146 $activity->subject = ts('Case type changed from %1 to %2',
147 [
148 1 => CRM_Utils_Array::value($form->_defaults['case_type_id'], $allCaseTypes),
149 2 => CRM_Utils_Array::value($params['case_type_id'], $allCaseTypes),
150 ]
151 );
152 $activity->save();
153 }
154
155 // 1. initiate xml processor
156 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
157 $caseId = CRM_Utils_Array::first($form->_caseId);
158 $xmlProcessorParams = [
159 'clientID' => $form->_currentlyViewedContactId,
160 'creatorID' => $form->_currentUserId,
161 'standardTimeline' => 1,
162 'activityTypeName' => 'Change Case Type',
163 'activity_date_time' => CRM_Utils_Array::value('reset_date_time', $params),
164 'caseID' => $caseId,
165 'resetTimeline' => CRM_Utils_Array::value('is_reset_timeline', $params),
166 ];
167
168 $xmlProcessor->run($caseType, $xmlProcessorParams);
169 // status msg
170 $params['statusMsg'] = ts('Case Type changed successfully.');
171 }
172
173 }