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