Commit | Line | Data |
---|---|---|
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 | */ |
21 | class CRM_Case_Form_Activity_ChangeCaseStartDate { | |
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 | } | |
73f72709 JP |
32 | if (count($form->_caseId) != 1) { |
33 | CRM_Core_Resources::fatal(ts('Expected one case-type')); | |
34 | } | |
6a488035 TO |
35 | } |
36 | ||
37 | /** | |
3819f101 | 38 | * Set default values for the form. |
6a488035 | 39 | * |
3819f101 | 40 | * For edit/view mode the default values are retrieved from the database. |
6a488035 | 41 | * |
c490a46a | 42 | * @param CRM_Core_Form $form |
77b97be7 | 43 | * |
3819f101 | 44 | * @return array |
6a488035 | 45 | */ |
00be9182 | 46 | public static function setDefaultValues(&$form) { |
be2fb01f | 47 | $defaults = []; |
6a488035 | 48 | |
9c248a42 | 49 | $openCaseActivityType = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Open Case'); |
73f72709 | 50 | $caseId = CRM_Utils_Array::first($form->_caseId); |
be2fb01f | 51 | $openCaseParams = ['activity_type_id' => $openCaseActivityType]; |
73f72709 | 52 | $openCaseInfo = CRM_Case_BAO_Case::getCaseActivityDates($caseId, $openCaseParams, TRUE); |
6a488035 | 53 | if (empty($openCaseInfo)) { |
06f51047 | 54 | $defaults['start_date'] = date('Y-m-d H:i:s'); |
6a488035 TO |
55 | } |
56 | else { | |
57 | // We know there can only be one result | |
58 | $openCaseInfo = current($openCaseInfo); | |
59 | ||
60 | // store activity id for updating it later | |
61 | $form->openCaseActivityId = $openCaseInfo['id']; | |
62 | ||
06f51047 | 63 | $defaults['start_date'] = $openCaseInfo['activity_date']; |
6a488035 TO |
64 | } |
65 | return $defaults; | |
66 | } | |
67 | ||
4c6ce474 | 68 | /** |
c490a46a | 69 | * @param CRM_Core_Form $form |
4c6ce474 | 70 | */ |
00be9182 | 71 | public static function buildQuickForm(&$form) { |
6a488035 TO |
72 | $form->removeElement('status_id'); |
73 | $form->removeElement('priority_id'); | |
73f72709 | 74 | $caseId = CRM_Utils_Array::first($form->_caseId); |
6a488035 | 75 | |
73f72709 | 76 | $currentStartDate = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', $caseId, 'start_date'); |
6a488035 | 77 | $form->assign('current_start_date', $currentStartDate); |
06f51047 | 78 | $form->add('datepicker', 'start_date', ts('New Start Date'), [], TRUE); |
6a488035 TO |
79 | } |
80 | ||
81 | /** | |
d2e5d2ce | 82 | * Global validation rules for the form. |
6a488035 | 83 | * |
64bd5a0e TO |
84 | * @param array $values |
85 | * Posted values of the form. | |
6a488035 | 86 | * |
dd244018 | 87 | * @param $files |
c490a46a | 88 | * @param CRM_Core_Form $form |
dd244018 | 89 | * |
a6c01b45 CW |
90 | * @return array |
91 | * list of errors to be posted back to the form | |
6a488035 | 92 | */ |
00be9182 | 93 | public static function formRule($values, $files, $form) { |
6a488035 TO |
94 | return TRUE; |
95 | } | |
96 | ||
97 | /** | |
d2e5d2ce | 98 | * Process the form submission. |
6a488035 | 99 | * |
6a488035 | 100 | * |
c490a46a CW |
101 | * @param CRM_Core_Form $form |
102 | * @param array $params | |
6a488035 | 103 | */ |
00be9182 | 104 | public static function beginPostProcess(&$form, &$params) { |
6a488035 TO |
105 | if ($form->_context == 'case') { |
106 | $params['id'] = $form->_id; | |
107 | } | |
108 | } | |
109 | ||
110 | /** | |
d2e5d2ce | 111 | * Process the form submission. |
6a488035 | 112 | * |
6a488035 | 113 | * |
c490a46a CW |
114 | * @param CRM_Core_Form $form |
115 | * @param array $params | |
77b97be7 | 116 | * @param $activity |
6a488035 | 117 | */ |
00be9182 | 118 | public static function endPostProcess(&$form, &$params, $activity) { |
bcbfb933 | 119 | $caseType = CRM_Utils_Array::first($form->_caseType); |
73f72709 | 120 | $caseId = CRM_Utils_Array::first($form->_caseId); |
6a488035 | 121 | |
73f72709 JP |
122 | if (!$caseType && $caseId) { |
123 | $caseType = CRM_Case_BAO_Case::getCaseType($caseId, 'title'); | |
6a488035 TO |
124 | } |
125 | ||
126 | if (!$form->_currentlyViewedContactId || | |
127 | !$form->_currentUserId || | |
73f72709 | 128 | !$caseId || |
6a488035 TO |
129 | !$caseType |
130 | ) { | |
131 | CRM_Core_Error::fatal('Required parameter missing for ChangeCaseType - end post processing'); | |
132 | } | |
133 | ||
134 | $config = CRM_Core_Config::singleton(); | |
135 | ||
9c248a42 | 136 | $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed'); |
6a488035 | 137 | $activity->status_id = $params['status_id']; |
0571e734 | 138 | $params['priority_id'] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'priority_id', 'Normal'); |
6a488035 TO |
139 | $activity->priority_id = $params['priority_id']; |
140 | ||
141 | // 1. save activity subject with new start date | |
142 | $currentStartDate = CRM_Utils_Date::customFormat(CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', | |
353ffa53 TO |
143 | $caseId, 'start_date' |
144 | ), $config->dateformatFull); | |
06f51047 | 145 | $newStartDate = CRM_Utils_Date::customFormat($params['start_date'], $config->dateformatFull); |
353ffa53 | 146 | $subject = 'Change Case Start Date from ' . $currentStartDate . ' to ' . $newStartDate; |
6a488035 TO |
147 | $activity->subject = $subject; |
148 | $activity->save(); | |
149 | // 2. initiate xml processor | |
150 | $xmlProcessor = new CRM_Case_XMLProcessor_Process(); | |
be2fb01f | 151 | $xmlProcessorParams = [ |
6a488035 TO |
152 | 'clientID' => $form->_currentlyViewedContactId, |
153 | 'creatorID' => $form->_currentUserId, | |
154 | 'standardTimeline' => 0, | |
155 | 'activity_date_time' => $params['start_date'], | |
73f72709 | 156 | 'caseID' => $caseId, |
6a488035 TO |
157 | 'caseType' => $caseType, |
158 | 'activityTypeName' => 'Change Case Start Date', | |
159 | 'activitySetName' => 'standard_timeline', | |
160 | 'resetTimeline' => 1, | |
be2fb01f | 161 | ]; |
6a488035 TO |
162 | |
163 | $xmlProcessor->run($caseType, $xmlProcessorParams); | |
164 | ||
165 | // 2.5 Update open case activity date | |
166 | // Multiple steps since revisioned | |
167 | if ($form->openCaseActivityId) { | |
168 | ||
353ffa53 | 169 | $abao = new CRM_Activity_BAO_Activity(); |
be2fb01f CW |
170 | $oldParams = ['id' => $form->openCaseActivityId]; |
171 | $oldActivityDefaults = []; | |
353ffa53 | 172 | $oldActivity = $abao->retrieve($oldParams, $oldActivityDefaults); |
6a488035 TO |
173 | |
174 | // save the old values | |
175 | require_once 'api/v3/utils.php'; | |
be2fb01f | 176 | $openCaseParams = []; |
6a488035 TO |
177 | //@todo calling api functions directly is not supported |
178 | _civicrm_api3_object_to_array($oldActivity, $openCaseParams); | |
179 | ||
180 | // update existing revision | |
be2fb01f | 181 | $oldParams = [ |
6a488035 TO |
182 | 'id' => $form->openCaseActivityId, |
183 | 'is_current_revision' => 0, | |
be2fb01f | 184 | ]; |
6a488035 TO |
185 | $oldActivity = new CRM_Activity_DAO_Activity(); |
186 | $oldActivity->copyValues($oldParams); | |
187 | $oldActivity->save(); | |
188 | ||
189 | // change some params for the new one | |
190 | unset($openCaseParams['id']); | |
191 | $openCaseParams['activity_date_time'] = $params['start_date']; | |
192 | $openCaseParams['target_contact_id'] = $oldActivityDefaults['target_contact']; | |
193 | $openCaseParams['assignee_contact_id'] = $oldActivityDefaults['assignee_contact']; | |
194 | $session = CRM_Core_Session::singleton(); | |
195 | $openCaseParams['source_contact_id'] = $session->get('userID'); | |
196 | ||
197 | // original_id always refers to the first activity, so only update if null (i.e. this is the second revision) | |
198 | $openCaseParams['original_id'] = $openCaseParams['original_id'] ? $openCaseParams['original_id'] : $form->openCaseActivityId; | |
199 | ||
200 | $newActivity = CRM_Activity_BAO_Activity::create($openCaseParams); | |
201 | if (is_a($newActivity, 'CRM_Core_Error')) { | |
202 | CRM_Core_Error::fatal('Unable to update Open Case activity'); | |
203 | } | |
204 | else { | |
205 | // Create linkage to case | |
be2fb01f | 206 | $caseActivityParams = [ |
6a488035 | 207 | 'activity_id' => $newActivity->id, |
73f72709 | 208 | 'case_id' => $caseId, |
be2fb01f | 209 | ]; |
6a488035 TO |
210 | |
211 | CRM_Case_BAO_Case::processCaseActivity($caseActivityParams); | |
212 | ||
be2fb01f | 213 | $caseActivityParams = [ |
6a488035 TO |
214 | 'activityID' => $form->openCaseActivityId, |
215 | 'mainActivityId' => $newActivity->id, | |
be2fb01f | 216 | ]; |
6a488035 TO |
217 | CRM_Activity_BAO_Activity::copyExtendedActivityData($caseActivityParams); |
218 | } | |
219 | } | |
220 | ||
221 | // 3.status msg | |
222 | $params['statusMsg'] = ts('Case Start Date changed successfully.'); | |
223 | } | |
96025800 | 224 | |
6a488035 | 225 | } |