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