3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
35 * This class generates form components for OpenCase Activity.
37 class CRM_Case_Form_Activity_ChangeCaseStartDate
{
40 * @param CRM_Core_Form $form
44 public static function preProcess(&$form) {
45 if (!isset($form->_caseId
)) {
46 CRM_Core_Error
::fatal(ts('Case Id not found.'));
48 if (count($form->_caseId
) != 1) {
49 CRM_Core_Resources
::fatal(ts('Expected one case-type'));
54 * Set default values for the form.
56 * For edit/view mode the default values are retrieved from the database.
58 * @param CRM_Core_Form $form
62 public static function setDefaultValues(&$form) {
65 $openCaseActivityType = CRM_Core_OptionGroup
::getValue('activity_type',
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();
76 // We know there can only be one result
77 $openCaseInfo = current($openCaseInfo);
79 // store activity id for updating it later
80 $form->openCaseActivityId
= $openCaseInfo['id'];
82 list($defaults['start_date'], $defaults['start_date_time']) = CRM_Utils_Date
::setDateDefaults($openCaseInfo['activity_date'], 'activityDateTime');
88 * @param CRM_Core_Form $form
90 public static function buildQuickForm(&$form) {
91 $form->removeElement('status_id');
92 $form->removeElement('priority_id');
93 $caseId = CRM_Utils_Array
::first($form->_caseId
);
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'));
101 * Global validation rules for the form.
103 * @param array $values
104 * Posted values of the form.
107 * @param CRM_Core_Form $form
110 * list of errors to be posted back to the form
112 public static function formRule($values, $files, $form) {
117 * Process the form submission.
120 * @param CRM_Core_Form $form
121 * @param array $params
123 public static function beginPostProcess(&$form, &$params) {
124 if ($form->_context
== 'case') {
125 $params['id'] = $form->_id
;
130 * Process the form submission.
133 * @param CRM_Core_Form $form
134 * @param array $params
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']);
142 $caseType = CRM_Utils_Array
::first($form->_caseType
);
143 $caseId = CRM_Utils_Array
::first($form->_caseId
);
145 if (!$caseType && $caseId) {
146 $caseType = CRM_Case_BAO_Case
::getCaseType($caseId, 'title');
149 if (!$form->_currentlyViewedContactId ||
150 !$form->_currentUserId ||
154 CRM_Core_Error
::fatal('Required parameter missing for ChangeCaseType - end post processing');
157 $config = CRM_Core_Config
::singleton();
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'];
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;
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'],
180 'caseType' => $caseType,
181 'activityTypeName' => 'Change Case Start Date',
182 'activitySetName' => 'standard_timeline',
183 'resetTimeline' => 1,
186 $xmlProcessor->run($caseType, $xmlProcessorParams);
188 // 2.5 Update open case activity date
189 // Multiple steps since revisioned
190 if ($form->openCaseActivityId
) {
192 $abao = new CRM_Activity_BAO_Activity();
193 $oldParams = array('id' => $form->openCaseActivityId
);
194 $oldActivityDefaults = array();
195 $oldActivity = $abao->retrieve($oldParams, $oldActivityDefaults);
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);
203 // update existing revision
205 'id' => $form->openCaseActivityId
,
206 'is_current_revision' => 0,
208 $oldActivity = new CRM_Activity_DAO_Activity();
209 $oldActivity->copyValues($oldParams);
210 $oldActivity->save();
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');
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
;
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');
228 // Create linkage to case
229 $caseActivityParams = array(
230 'activity_id' => $newActivity->id
,
231 'case_id' => $caseId,
234 CRM_Case_BAO_Case
::processCaseActivity($caseActivityParams);
236 $caseActivityParams = array(
237 'activityID' => $form->openCaseActivityId
,
238 'mainActivityId' => $newActivity->id
,
240 CRM_Activity_BAO_Activity
::copyExtendedActivityData($caseActivityParams);
245 $params['statusMsg'] = ts('Case Start Date changed successfully.');