more comment fixes
[civicrm-core.git] / CRM / Case / Form / Activity / ChangeCaseStartDate.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 */
33
34 /**
35 * This class generates form components for OpenCase Activity.
36 */
37 class CRM_Case_Form_Activity_ChangeCaseStartDate {
38
39 /**
40 * @param CRM_Core_Form $form
41 *
42 * @throws Exception
43 */
44 public static function preProcess(&$form) {
45 if (!isset($form->_caseId)) {
46 CRM_Core_Error::fatal(ts('Case Id not found.'));
47 }
48 if (count($form->_caseId) != 1) {
49 CRM_Core_Resources::fatal(ts('Expected one case-type'));
50 }
51 }
52
53 /**
54 * Set default values for the form.
55 *
56 * For edit/view mode the default values are retrieved from the database.
57 *
58 * @param CRM_Core_Form $form
59 *
60 * @return array
61 */
62 public static function setDefaultValues(&$form) {
63 $defaults = array();
64
65 $openCaseActivityType = CRM_Core_OptionGroup::getValue('activity_type',
66 'Open Case',
67 'name'
68 );
69 $caseId = CRM_Utils_Array::first($form->_caseId);
70 $openCaseParams = array('activity_type_id' => $openCaseActivityType);
71 $openCaseInfo = CRM_Case_BAO_Case::getCaseActivityDates($caseId, $openCaseParams, TRUE);
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
82 list($defaults['start_date'], $defaults['start_date_time']) = CRM_Utils_Date::setDateDefaults($openCaseInfo['activity_date'], 'activityDateTime');
83 }
84 return $defaults;
85 }
86
87 /**
88 * @param CRM_Core_Form $form
89 */
90 public static function buildQuickForm(&$form) {
91 $form->removeElement('status_id');
92 $form->removeElement('priority_id');
93 $caseId = CRM_Utils_Array::first($form->_caseId);
94
95 $currentStartDate = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', $caseId, 'start_date');
96 $form->assign('current_start_date', $currentStartDate);
97 $form->addDate('start_date', ts('New Start Date'), FALSE, array('formatType' => 'activityDateTime'));
98 }
99
100 /**
101 * Global validation rules for the form.
102 *
103 * @param array $values
104 * Posted values of the form.
105 *
106 * @param $files
107 * @param CRM_Core_Form $form
108 *
109 * @return array
110 * list of errors to be posted back to the form
111 */
112 public static function formRule($values, $files, $form) {
113 return TRUE;
114 }
115
116 /**
117 * Process the form submission.
118 *
119 *
120 * @param CRM_Core_Form $form
121 * @param array $params
122 */
123 public static function beginPostProcess(&$form, &$params) {
124 if ($form->_context == 'case') {
125 $params['id'] = $form->_id;
126 }
127 }
128
129 /**
130 * Process the form submission.
131 *
132 *
133 * @param CRM_Core_Form $form
134 * @param array $params
135 * @param $activity
136 */
137 public static function endPostProcess(&$form, &$params, $activity) {
138 if (!empty($params['start_date'])) {
139 $params['start_date'] = CRM_Utils_Date::processDate($params['start_date'], $params['start_date_time']);
140 }
141
142 $caseType = CRM_Utils_Array::first($form->_caseType);
143 $caseId = CRM_Utils_Array::first($form->_caseId);
144
145 if (!$caseType && $caseId) {
146 $caseType = CRM_Case_BAO_Case::getCaseType($caseId, 'title');
147 }
148
149 if (!$form->_currentlyViewedContactId ||
150 !$form->_currentUserId ||
151 !$caseId ||
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',
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;
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'],
179 'caseID' => $caseId,
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
192 $abao = new CRM_Activity_BAO_Activity();
193 $oldParams = array('id' => $form->openCaseActivityId);
194 $oldActivityDefaults = array();
195 $oldActivity = $abao->retrieve($oldParams, $oldActivityDefaults);
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,
231 'case_id' => $caseId,
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 }
247
248 }