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