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