3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates form components for OpenCase Activity.
21 class CRM_Case_Form_Activity_ChangeCaseStartDate
{
24 * @param CRM_Core_Form $form
28 public static function preProcess(&$form) {
29 if (!isset($form->_caseId
)) {
30 CRM_Core_Error
::fatal(ts('Case Id not found.'));
32 if (count($form->_caseId
) != 1) {
33 CRM_Core_Resources
::fatal(ts('Expected one case-type'));
38 * Set default values for the form.
40 * For edit/view mode the default values are retrieved from the database.
42 * @param CRM_Core_Form $form
46 public static function setDefaultValues(&$form) {
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');
57 // We know there can only be one result
58 $openCaseInfo = current($openCaseInfo);
60 // store activity id for updating it later
61 $form->openCaseActivityId
= $openCaseInfo['id'];
63 $defaults['start_date'] = $openCaseInfo['activity_date'];
69 * @param CRM_Core_Form $form
71 public static function buildQuickForm(&$form) {
72 $form->removeElement('status_id');
73 $form->removeElement('priority_id');
74 $caseId = CRM_Utils_Array
::first($form->_caseId
);
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);
82 * Global validation rules for the form.
84 * @param array $values
85 * Posted values of the form.
88 * @param CRM_Core_Form $form
91 * list of errors to be posted back to the form
93 public static function formRule($values, $files, $form) {
98 * Process the form submission.
101 * @param CRM_Core_Form $form
102 * @param array $params
104 public static function beginPostProcess(&$form, &$params) {
105 if ($form->_context
== 'case') {
106 $params['id'] = $form->_id
;
111 * Process the form submission.
114 * @param CRM_Core_Form $form
115 * @param array $params
118 public static function endPostProcess(&$form, &$params, $activity) {
119 $caseType = CRM_Utils_Array
::first($form->_caseType
);
120 $caseId = CRM_Utils_Array
::first($form->_caseId
);
122 if (!$caseType && $caseId) {
123 $caseType = CRM_Case_BAO_Case
::getCaseType($caseId, 'title');
126 if (!$form->_currentlyViewedContactId ||
127 !$form->_currentUserId ||
131 CRM_Core_Error
::fatal('Required parameter missing for ChangeCaseType - end post processing');
134 $config = CRM_Core_Config
::singleton();
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'];
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;
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'],
157 'caseType' => $caseType,
158 'activityTypeName' => 'Change Case Start Date',
159 'activitySetName' => 'standard_timeline',
160 'resetTimeline' => 1,
163 $xmlProcessor->run($caseType, $xmlProcessorParams);
165 // 2.5 Update open case activity date
166 // Multiple steps since revisioned
167 if ($form->openCaseActivityId
) {
169 $abao = new CRM_Activity_BAO_Activity();
170 $oldParams = ['id' => $form->openCaseActivityId
];
171 $oldActivityDefaults = [];
172 $oldActivity = $abao->retrieve($oldParams, $oldActivityDefaults);
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);
180 // update existing revision
182 'id' => $form->openCaseActivityId
,
183 'is_current_revision' => 0,
185 $oldActivity = new CRM_Activity_DAO_Activity();
186 $oldActivity->copyValues($oldParams);
187 $oldActivity->save();
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');
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
;
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');
205 // Create linkage to case
206 $caseActivityParams = [
207 'activity_id' => $newActivity->id
,
208 'case_id' => $caseId,
211 CRM_Case_BAO_Case
::processCaseActivity($caseActivityParams);
213 $caseActivityParams = [
214 'activityID' => $form->openCaseActivityId
,
215 'mainActivityId' => $newActivity->id
,
217 CRM_Activity_BAO_Activity
::copyExtendedActivityData($caseActivityParams);
222 $params['statusMsg'] = ts('Case Start Date changed successfully.');