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