Merge pull request #11393 from jitendrapurohit/CRM-21533
[civicrm-core.git] / CRM / Case / Form / Activity / ChangeCaseType.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
6a488035
TO
32 */
33
34/**
3819f101 35 * This class generates form components for OpenCase Activity.
6a488035
TO
36 */
37class CRM_Case_Form_Activity_ChangeCaseType {
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 }
48 }
49
50 /**
3819f101 51 * Set default values for the form.
6a488035 52 *
3819f101 53 * For edit/view mode the default values are retrieved from the database.
6a488035 54 *
c490a46a 55 * @param CRM_Core_Form $form
77b97be7 56 *
3819f101 57 * @return array
6a488035 58 */
00be9182 59 public static function setDefaultValues(&$form) {
6a488035
TO
60 $defaults = array();
61
62 $defaults['is_reset_timeline'] = 1;
63
64 $defaults['reset_date_time'] = array();
65 list($defaults['reset_date_time'], $defaults['reset_date_time_time']) = CRM_Utils_Date::setDateDefaults(NULL, 'activityDateTime');
66 $defaults['case_type_id'] = $form->_caseTypeId;
67
68 return $defaults;
69 }
70
4c6ce474 71 /**
c490a46a 72 * @param CRM_Core_Form $form
4c6ce474 73 */
00be9182 74 public static function buildQuickForm(&$form) {
6a488035
TO
75 $form->removeElement('status_id');
76 $form->removeElement('priority_id');
77
c22c1324 78 $caseId = CRM_Utils_Array::first($form->_caseId);
7a5c0c6c 79 $form->_caseType = CRM_Case_BAO_Case::buildOptions('case_type_id', 'create');
8ffdec17 80 $form->_caseTypeId = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case',
c22c1324 81 $caseId,
8ffdec17
ARW
82 'case_type_id'
83 );
6a488035 84 if (!in_array($form->_caseTypeId, $form->_caseType)) {
d14b35a4 85 $form->_caseType[$form->_caseTypeId] = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseType', $form->_caseTypeId, 'title');
6a488035
TO
86 }
87
7a5c0c6c 88 $form->addField('case_type_id', array('context' => 'create', 'entity' => 'Case'));
6a488035
TO
89
90 // timeline
91 $form->addYesNo('is_reset_timeline', ts('Reset Case Timeline?'), NULL, TRUE, array('onclick' => "return showHideByValue('is_reset_timeline','','resetTimeline','table-row','radio',false);"));
92 $form->addDateTime('reset_date_time', ts('Reset Start Date'), FALSE, array('formatType' => 'activityDateTime'));
93 }
94
95 /**
d2e5d2ce 96 * Global validation rules for the form.
6a488035 97 *
64bd5a0e
TO
98 * @param array $values
99 * Posted values of the form.
6a488035 100 *
77b97be7 101 * @param $files
c490a46a 102 * @param CRM_Core_Form $form
77b97be7 103 *
a6c01b45
CW
104 * @return array
105 * list of errors to be posted back to the form
6a488035 106 */
00be9182 107 public static function formRule($values, $files, $form) {
6a488035
TO
108 return TRUE;
109 }
110
111 /**
d2e5d2ce 112 * Process the form submission.
6a488035 113 *
6a488035 114 *
c490a46a
CW
115 * @param CRM_Core_Form $form
116 * @param array $params
6a488035 117 */
00be9182 118 public static function beginPostProcess(&$form, &$params) {
6a488035
TO
119 if ($form->_context == 'case') {
120 $params['id'] = $form->_id;
121 }
122
123 if (CRM_Utils_Array::value('is_reset_timeline', $params) == 0) {
124 unset($params['reset_date_time']);
125 }
126 else {
127 // store the date with proper format
128 $params['reset_date_time'] = CRM_Utils_Date::processDate($params['reset_date_time'], $params['reset_date_time_time']);
129 }
130 }
131
132 /**
d2e5d2ce 133 * Process the form submission.
6a488035 134 *
6a488035 135 *
c490a46a
CW
136 * @param CRM_Core_Form $form
137 * @param array $params
fd31fa4c 138 * @param $activity
6a488035 139 */
00be9182 140 public static function endPostProcess(&$form, &$params, $activity) {
6a488035
TO
141 if (!$form->_caseId) {
142 // always expecting a change, so case-id is a must.
143 return;
144 }
145
146 $caseTypes = CRM_Case_PseudoConstant::caseType('name');
42047ce4 147 $allCaseTypes = CRM_Case_PseudoConstant::caseType('title', FALSE);
6a488035 148
a7488080 149 if (!empty($caseTypes[$params['case_type_id']])) {
6a488035
TO
150 $caseType = $caseTypes[$params['case_type_id']];
151 }
152
153 if (!$form->_currentlyViewedContactId ||
154 !$form->_currentUserId ||
155 !$params['case_type_id'] ||
156 !$caseType
157 ) {
158 CRM_Core_Error::fatal('Required parameter missing for ChangeCaseType - end post processing');
159 }
160
9c248a42 161 $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed');
6a488035
TO
162 $activity->status_id = $params['status_id'];
163 $params['priority_id'] = CRM_Core_OptionGroup::getValue('priority', 'Normal', 'name');
164 $activity->priority_id = $params['priority_id'];
165
166 if ($activity->subject == 'null') {
167 $activity->subject = ts('Case type changed from %1 to %2',
e547f744 168 array(
353ffa53 169 1 => CRM_Utils_Array::value($form->_defaults['case_type_id'], $allCaseTypes),
6a488035
TO
170 2 => CRM_Utils_Array::value($params['case_type_id'], $allCaseTypes),
171 )
172 );
173 $activity->save();
174 }
175
176 // 1. initiate xml processor
177 $xmlProcessor = new CRM_Case_XMLProcessor_Process();
c22c1324 178 $caseId = CRM_Utils_Array::first($form->_caseId);
6a488035
TO
179 $xmlProcessorParams = array(
180 'clientID' => $form->_currentlyViewedContactId,
181 'creatorID' => $form->_currentUserId,
182 'standardTimeline' => 1,
183 'activityTypeName' => 'Change Case Type',
184 'activity_date_time' => CRM_Utils_Array::value('reset_date_time', $params),
c22c1324 185 'caseID' => $caseId,
6a488035
TO
186 'resetTimeline' => CRM_Utils_Array::value('is_reset_timeline', $params),
187 );
188
189 $xmlProcessor->run($caseType, $xmlProcessorParams);
190 // status msg
191 $params['statusMsg'] = ts('Case Type changed successfully.');
192 }
96025800 193
6a488035 194}