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