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