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