Merge pull request #15878 from civicrm/5.20
[civicrm-core.git] / CRM / Case / Form / Activity / ChangeCaseStartDate.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_ChangeCaseStartDate {
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 if (count($form->_caseId) != 1) {
33 CRM_Core_Resources::fatal(ts('Expected one case-type'));
34 }
35 }
36
37 /**
38 * Set default values for the form.
39 *
40 * For edit/view mode the default values are retrieved from the database.
41 *
42 * @param CRM_Core_Form $form
43 *
44 * @return array
45 */
46 public static function setDefaultValues(&$form) {
47 $defaults = [];
48
49 $openCaseActivityType = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Open Case');
50 $caseId = CRM_Utils_Array::first($form->_caseId);
51 $openCaseParams = ['activity_type_id' => $openCaseActivityType];
52 $openCaseInfo = CRM_Case_BAO_Case::getCaseActivityDates($caseId, $openCaseParams, TRUE);
53 if (empty($openCaseInfo)) {
54 $defaults['start_date'] = date('Y-m-d H:i:s');
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
63 $defaults['start_date'] = $openCaseInfo['activity_date'];
64 }
65 return $defaults;
66 }
67
68 /**
69 * @param CRM_Core_Form $form
70 */
71 public static function buildQuickForm(&$form) {
72 $form->removeElement('status_id');
73 $form->removeElement('priority_id');
74 $caseId = CRM_Utils_Array::first($form->_caseId);
75
76 $currentStartDate = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', $caseId, 'start_date');
77 $form->assign('current_start_date', $currentStartDate);
78 $form->add('datepicker', 'start_date', ts('New Start Date'), [], TRUE);
79 }
80
81 /**
82 * Global validation rules for the form.
83 *
84 * @param array $values
85 * Posted values of the form.
86 *
87 * @param $files
88 * @param CRM_Core_Form $form
89 *
90 * @return array
91 * list of errors to be posted back to the form
92 */
93 public static function formRule($values, $files, $form) {
94 return TRUE;
95 }
96
97 /**
98 * Process the form submission.
99 *
100 *
101 * @param CRM_Core_Form $form
102 * @param array $params
103 */
104 public static function beginPostProcess(&$form, &$params) {
105 if ($form->_context == 'case') {
106 $params['id'] = $form->_id;
107 }
108 }
109
110 /**
111 * Process the form submission.
112 *
113 *
114 * @param CRM_Core_Form $form
115 * @param array $params
116 * @param $activity
117 */
118 public static function endPostProcess(&$form, &$params, $activity) {
119 $caseType = CRM_Utils_Array::first($form->_caseType);
120 $caseId = CRM_Utils_Array::first($form->_caseId);
121
122 if (!$caseType && $caseId) {
123 $caseType = CRM_Case_BAO_Case::getCaseType($caseId, 'title');
124 }
125
126 if (!$form->_currentlyViewedContactId ||
127 !$form->_currentUserId ||
128 !$caseId ||
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
136 $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed');
137 $activity->status_id = $params['status_id'];
138 $params['priority_id'] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'priority_id', 'Normal');
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',
143 $caseId, 'start_date'
144 ), $config->dateformatFull);
145 $newStartDate = CRM_Utils_Date::customFormat($params['start_date'], $config->dateformatFull);
146 $subject = 'Change Case Start Date from ' . $currentStartDate . ' to ' . $newStartDate;
147 $activity->subject = $subject;
148 $activity->save();
149 // 2. initiate xml processor
150 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
151 $xmlProcessorParams = [
152 'clientID' => $form->_currentlyViewedContactId,
153 'creatorID' => $form->_currentUserId,
154 'standardTimeline' => 0,
155 'activity_date_time' => $params['start_date'],
156 'caseID' => $caseId,
157 'caseType' => $caseType,
158 'activityTypeName' => 'Change Case Start Date',
159 'activitySetName' => 'standard_timeline',
160 'resetTimeline' => 1,
161 ];
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
169 $abao = new CRM_Activity_BAO_Activity();
170 $oldParams = ['id' => $form->openCaseActivityId];
171 $oldActivityDefaults = [];
172 $oldActivity = $abao->retrieve($oldParams, $oldActivityDefaults);
173
174 // save the old values
175 require_once 'api/v3/utils.php';
176 $openCaseParams = [];
177 //@todo calling api functions directly is not supported
178 _civicrm_api3_object_to_array($oldActivity, $openCaseParams);
179
180 // update existing revision
181 $oldParams = [
182 'id' => $form->openCaseActivityId,
183 'is_current_revision' => 0,
184 ];
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
206 $caseActivityParams = [
207 'activity_id' => $newActivity->id,
208 'case_id' => $caseId,
209 ];
210
211 CRM_Case_BAO_Case::processCaseActivity($caseActivityParams);
212
213 $caseActivityParams = [
214 'activityID' => $form->openCaseActivityId,
215 'mainActivityId' => $newActivity->id,
216 ];
217 CRM_Activity_BAO_Activity::copyExtendedActivityData($caseActivityParams);
218 }
219 }
220
221 // 3.status msg
222 $params['statusMsg'] = ts('Case Start Date changed successfully.');
223 }
224
225 }