3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This class generates form components for OpenCase Activity
40 class CRM_Case_Form_Activity_ChangeCaseStartDate
{
43 * @param CRM_Core_Form $form
47 public static function preProcess(&$form) {
48 if (!isset($form->_caseId
)) {
49 CRM_Core_Error
::fatal(ts('Case Id not found.'));
51 if (count($form->_caseId
) != 1) {
52 CRM_Core_Resources
::fatal(ts('Expected one case-type'));
57 * Set default values for the form. For edit/view mode
58 * the default values are retrieved from the database
61 * @param CRM_Core_Form $form
65 public static function setDefaultValues(&$form) {
68 $openCaseActivityType = CRM_Core_OptionGroup
::getValue('activity_type',
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();
79 // We know there can only be one result
80 $openCaseInfo = current($openCaseInfo);
82 // store activity id for updating it later
83 $form->openCaseActivityId
= $openCaseInfo['id'];
85 list($defaults['start_date'], $defaults['start_date_time']) = CRM_Utils_Date
::setDateDefaults($openCaseInfo['activity_date'], 'activityDateTime');
91 * @param CRM_Core_Form $form
93 public static function buildQuickForm(&$form) {
94 $form->removeElement('status_id');
95 $form->removeElement('priority_id');
96 $caseId = CRM_Utils_Array
::first($form->_caseId
);
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'));
104 * Global validation rules for the form
106 * @param array $values
107 * Posted values of the form.
110 * @param CRM_Core_Form $form
112 * @return array list of errors to be posted back to the form
115 public static function formRule($values, $files, $form) {
120 * Process the form submission
123 * @param CRM_Core_Form $form
124 * @param array $params
128 public static function beginPostProcess(&$form, &$params) {
129 if ($form->_context
== 'case') {
130 $params['id'] = $form->_id
;
135 * Process the form submission
138 * @param CRM_Core_Form $form
139 * @param array $params
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']);
149 $caseType = CRM_Utils_Array
::first($form->_caseType
);
150 $caseId = CRM_Utils_Array
::first($form->_caseId
);
152 if (!$caseType && $caseId) {
153 $caseType = CRM_Case_BAO_Case
::getCaseType($caseId, 'title');
156 if (!$form->_currentlyViewedContactId ||
157 !$form->_currentUserId ||
161 CRM_Core_Error
::fatal('Required parameter missing for ChangeCaseType - end post processing');
164 $config = CRM_Core_Config
::singleton();
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'];
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;
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'],
187 'caseType' => $caseType,
188 'activityTypeName' => 'Change Case Start Date',
189 'activitySetName' => 'standard_timeline',
190 'resetTimeline' => 1,
193 $xmlProcessor->run($caseType, $xmlProcessorParams);
195 // 2.5 Update open case activity date
196 // Multiple steps since revisioned
197 if ($form->openCaseActivityId
) {
199 $abao = new CRM_Activity_BAO_Activity();
200 $oldParams = array('id' => $form->openCaseActivityId
);
201 $oldActivityDefaults = array();
202 $oldActivity = $abao->retrieve($oldParams, $oldActivityDefaults);
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);
210 // update existing revision
212 'id' => $form->openCaseActivityId
,
213 'is_current_revision' => 0,
215 $oldActivity = new CRM_Activity_DAO_Activity();
216 $oldActivity->copyValues($oldParams);
217 $oldActivity->save();
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');
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
;
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');
235 // Create linkage to case
236 $caseActivityParams = array(
237 'activity_id' => $newActivity->id
,
238 'case_id' => $caseId,
241 CRM_Case_BAO_Case
::processCaseActivity($caseActivityParams);
243 $caseActivityParams = array(
244 'activityID' => $form->openCaseActivityId
,
245 'mainActivityId' => $newActivity->id
,
247 CRM_Activity_BAO_Activity
::copyExtendedActivityData($caseActivityParams);
252 $params['statusMsg'] = ts('Case Start Date changed successfully.');