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