Merge pull request #2326 from eileenmcnaughton/CRM-14069
[civicrm-core.git] / CRM / Case / Form / Activity / ChangeCaseStatus.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class generates form components for OpenCase Activity
38 *
39 */
40 class CRM_Case_Form_Activity_ChangeCaseStatus {
41
42 /**
43 * @param $form
44 *
45 * @throws Exception
46 */
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 *
59 * @param $form
60 *
61 * @return void
62 */
63 static function setDefaultValues(&$form) {
64 $defaults = array();
65 // Retrieve current case status
66 $defaults['case_status_id'] = $form->_defaultCaseStatus;
67
68 return $defaults;
69 }
70
71 /**
72 * @param $form
73 */
74 static function buildQuickForm(&$form) {
75 $form->removeElement('status_id');
76 $form->removeElement('priority_id');
77
78 $form->_caseStatus = CRM_Case_PseudoConstant::caseStatus();
79
80 foreach ($form->_caseId as $key => $val) {
81 $form->_oldCaseStatus[] = $form->_defaultCaseStatus[] = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', $val, 'status_id');
82 }
83
84 foreach ($form->_defaultCaseStatus as $keydefault => $valdefault) {
85 if (!array_key_exists($valdefault, $form->_caseStatus)) {
86 $form->_caseStatus[$valdefault] = CRM_Core_OptionGroup::getLabel('case_status',
87 $valdefault,
88 FALSE
89 );
90 }
91 }
92 $element = $form->add('select', 'case_status_id', ts('Case Status'),
93 $form->_caseStatus, TRUE
94 );
95 // check if the case status id passed in url is a valid one, set as default and freeze
96 if (CRM_Utils_Request::retrieve('case_status_id', 'Positive', $form)) {
97 $caseStatusId = CRM_Utils_Request::retrieve('case_status_id', 'Positive', $form);
98 $caseStatus = CRM_Case_PseudoConstant::caseStatus();
99 $form->_defaultCaseStatus = array_key_exists($caseStatusId, $caseStatus) ? $caseStatusId : NULL;
100 $element->freeze();
101 }
102 }
103
104 /**
105 * global validation rules for the form
106 *
107 * @param array $values posted values of the form
108 *
109 * @param $files
110 * @param $form
111 *
112 * @return array list of errors to be posted back to the form
113 * @static
114 * @access public
115 */
116 static function formRule($values, $files, $form) {
117 return TRUE;
118 }
119
120 /**
121 * Function to process the form
122 *
123 * @access public
124 *
125 * @param $form
126 * @param $params
127 *
128 * @return void
129 */
130 static function beginPostProcess(&$form, &$params) {
131 $params['id'] = CRM_Utils_Array::value('case_id', $params);
132 }
133
134 /**
135 * Function to process the form
136 *
137 * @access public
138 *
139 * @param $form
140 * @param $params
141 * @param $activity
142 *
143 * @return void
144 */
145 static function endPostProcess(&$form, &$params, $activity) {
146 $groupingValues = CRM_Core_OptionGroup::values('case_status', FALSE, TRUE, FALSE, NULL, 'value');
147
148 // Set case end_date if we're closing the case. Clear end_date if we're (re)opening it.
149 if (CRM_Utils_Array::value($params['case_status_id'], $groupingValues) == 'Closed' && !empty($params['activity_date_time'])) {
150 $params['end_date'] = $params['activity_date_time'];
151
152 // End case-specific relationships (roles)
153 foreach ($params['target_contact_id'] as $cid) {
154 $rels = CRM_Case_BAO_Case::getCaseRoles($cid, $params['case_id']);
155 // FIXME: Is there an existing function to close a relationship?
156 $query = 'UPDATE civicrm_relationship SET end_date=%2 WHERE id=%1';
157 foreach ($rels as $relId => $relData) {
158 $relParams = array(1 => array($relId, 'Integer'),
159 2 => array($params['end_date'], 'Timestamp'),
160 );
161 CRM_Core_DAO::executeQuery($query, $relParams);
162 }
163 }
164 }
165 elseif (CRM_Utils_Array::value($params['case_status_id'], $groupingValues) == 'Opened') {
166 $params['end_date'] = "null";
167
168 // Reopen case-specific relationships (roles)
169 foreach ($params['target_contact_id'] as $cid) {
170 $rels = CRM_Case_BAO_Case::getCaseRoles($cid, $params['case_id']);
171 // FIXME: Is there an existing function?
172 $query = 'UPDATE civicrm_relationship SET end_date=NULL WHERE id=%1';
173 foreach ($rels as $relId => $relData) {
174 $relParams = array(1 => array($relId, 'Integer'));
175 CRM_Core_DAO::executeQuery($query, $relParams);
176 }
177 }
178 }
179 $params['status_id'] = CRM_Core_OptionGroup::getValue('activity_status', 'Completed', 'name');
180 $activity->status_id = $params['status_id'];
181 $params['priority_id'] = CRM_Core_OptionGroup::getValue('priority', 'Normal', 'name');
182 $activity->priority_id = $params['priority_id'];
183
184 foreach ($form->_oldCaseStatus as $statuskey => $statusval ) {
185 if ($activity->subject == 'null') {
186 $activity->subject = ts('Case status changed from %1 to %2', array(
187 1 => CRM_Utils_Array::value($statusval, $form->_caseStatus),
188 2 => CRM_Utils_Array::value($params['case_status_id'], $form->_caseStatus)
189 )
190 );
191 $activity->save();
192 }
193 }
194
195 // FIXME: does this do anything ?
196 $params['statusMsg'] = ts('Case Status changed successfully.');
197 }
198 }
199