3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
37 * This class generates form components for OpenCase Activity
40 class CRM_Case_Form_Activity_ChangeCaseStartDate
{
42 static function preProcess(&$form) {
43 if (!isset($form->_caseId
)) {
44 CRM_Core_Error
::fatal(ts('Case Id not found.'));
49 * This function sets the default values for the form. For edit/view mode
50 * the default values are retrieved from the database
56 static function setDefaultValues(&$form) {
59 $openCaseActivityType = CRM_Core_OptionGroup
::getValue('activity_type',
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();
69 // We know there can only be one result
70 $openCaseInfo = current($openCaseInfo);
72 // store activity id for updating it later
73 $form->openCaseActivityId
= $openCaseInfo['id'];
75 list($defaults['start_date'], $defaults['start_date_time']) = CRM_Utils_Date
::setDateDefaults($openCaseInfo['activity_date'], 'activityDateTime');
80 static function buildQuickForm(&$form) {
81 $form->removeElement('status_id');
82 $form->removeElement('priority_id');
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'));
90 * global validation rules for the form
92 * @param array $values posted values of the form
94 * @return array list of errors to be posted back to the form
98 static function formRule($values, $files, $form) {
103 * Function to process the form
109 static function beginPostProcess(&$form, &$params) {
110 if ($form->_context
== 'case') {
111 $params['id'] = $form->_id
;
116 * Function to process the form
122 static function endPostProcess(&$form, &$params, $activity) {
123 if (CRM_Utils_Array
::value('start_date', $params)) {
124 $params['start_date'] = CRM_Utils_Date
::processDate($params['start_date'], $params['start_date_time']);
127 $caseType = $form->_caseType
;
129 if (!$caseType && $form->_caseId
) {
132 SELECT cov_type.label as case_type FROM civicrm_case
133 LEFT JOIN civicrm_option_group cog_type ON cog_type.name = 'case_type'
134 LEFT JOIN civicrm_option_value cov_type ON
135 ( civicrm_case.case_type_id = cov_type.value AND cog_type.id = cov_type.option_group_id )
136 WHERE civicrm_case.id= %1";
138 $queryParams = array(1 => array($form->_caseId
, 'Integer'));
139 $caseType = CRM_Core_DAO
::singleValueQuery($query, $queryParams);
142 if (!$form->_currentlyViewedContactId ||
143 !$form->_currentUserId ||
147 CRM_Core_Error
::fatal('Required parameter missing for ChangeCaseType - end post processing');
150 $config = CRM_Core_Config
::singleton();
152 $params['status_id'] = CRM_Core_OptionGroup
::getValue('activity_status', 'Completed', 'name');
153 $activity->status_id
= $params['status_id'];
154 $params['priority_id'] = CRM_Core_OptionGroup
::getValue('priority', 'Normal', 'name');
155 $activity->priority_id
= $params['priority_id'];
157 // 1. save activity subject with new start date
158 $currentStartDate = CRM_Utils_Date
::customFormat(CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_Case',
159 $form->_caseId
, 'start_date'
160 ), $config->dateformatFull
);
161 $newStartDate = CRM_Utils_Date
::customFormat(CRM_Utils_Date
::mysqlToIso($params['start_date']), $config->dateformatFull
);
162 $subject = 'Change Case Start Date from ' . $currentStartDate . ' to ' . $newStartDate;
163 $activity->subject
= $subject;
165 // 2. initiate xml processor
166 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
167 $xmlProcessorParams = array(
168 'clientID' => $form->_currentlyViewedContactId
,
169 'creatorID' => $form->_currentUserId
,
170 'standardTimeline' => 0,
171 'activity_date_time' => $params['start_date'],
172 'caseID' => $form->_caseId
,
173 'caseType' => $caseType,
174 'activityTypeName' => 'Change Case Start Date',
175 'activitySetName' => 'standard_timeline',
176 'resetTimeline' => 1,
179 $xmlProcessor->run($caseType, $xmlProcessorParams);
181 // 2.5 Update open case activity date
182 // Multiple steps since revisioned
183 if ($form->openCaseActivityId
) {
185 $abao = new CRM_Activity_BAO_Activity();
186 $oldParams = array('id' => $form->openCaseActivityId
);
187 $oldActivityDefaults = array();
188 $oldActivity = $abao->retrieve($oldParams, $oldActivityDefaults);
190 // save the old values
191 require_once 'api/v3/utils.php';
192 $openCaseParams = array();
193 //@todo calling api functions directly is not supported
194 _civicrm_api3_object_to_array($oldActivity, $openCaseParams);
196 // update existing revision
198 'id' => $form->openCaseActivityId
,
199 'is_current_revision' => 0,
201 $oldActivity = new CRM_Activity_DAO_Activity();
202 $oldActivity->copyValues($oldParams);
203 $oldActivity->save();
205 // change some params for the new one
206 unset($openCaseParams['id']);
207 $openCaseParams['activity_date_time'] = $params['start_date'];
208 $openCaseParams['target_contact_id'] = $oldActivityDefaults['target_contact'];
209 $openCaseParams['assignee_contact_id'] = $oldActivityDefaults['assignee_contact'];
210 $session = CRM_Core_Session
::singleton();
211 $openCaseParams['source_contact_id'] = $session->get('userID');
213 // original_id always refers to the first activity, so only update if null (i.e. this is the second revision)
214 $openCaseParams['original_id'] = $openCaseParams['original_id'] ?
$openCaseParams['original_id'] : $form->openCaseActivityId
;
216 $newActivity = CRM_Activity_BAO_Activity
::create($openCaseParams);
217 if (is_a($newActivity, 'CRM_Core_Error')) {
218 CRM_Core_Error
::fatal('Unable to update Open Case activity');
221 // Create linkage to case
222 $caseActivityParams = array(
223 'activity_id' => $newActivity->id
,
224 'case_id' => $form->_caseId
,
227 CRM_Case_BAO_Case
::processCaseActivity($caseActivityParams);
229 $caseActivityParams = array(
230 'activityID' => $form->openCaseActivityId
,
231 'mainActivityId' => $newActivity->id
,
233 CRM_Activity_BAO_Activity
::copyExtendedActivityData($caseActivityParams);
238 $params['statusMsg'] = ts('Case Start Date changed successfully.');