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