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